1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements.  See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership.  The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License.  You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied.  See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
22 package org.apache.openoffice.ooxml.viewer.xmltokenview;
23 
24 import java.util.Vector;
25 
26 import javax.xml.stream.Location;
27 
28 import org.apache.openoffice.ooxml.parser.ElementContext;
29 import org.apache.openoffice.ooxml.parser.ParseException;
30 import org.apache.openoffice.ooxml.parser.Parser;
31 import org.apache.openoffice.ooxml.parser.action.ActionTrigger;
32 import org.apache.openoffice.ooxml.parser.action.IAction;
33 import org.apache.openoffice.ooxml.viewer.tokenview.Run;
34 import org.apache.openoffice.ooxml.viewer.tokenview.RunRange;
35 import org.apache.openoffice.ooxml.viewer.tokenview.TokenView;
36 import org.apache.openoffice.ooxml.viewer.xml.TokenType;
37 
38 public class XMLViewFactory
39 {
AddSemanticInformation( final TokenView<TokenType> aView, final Parser aParser, final Vector<String> aErrorsAndWarnings)40     public static void AddSemanticInformation(
41         final TokenView<TokenType> aView,
42         final Parser aParser,
43         final Vector<String> aErrorsAndWarnings)
44     {
45         aParser.GetActionManager().AddElementStartAction(
46             "*",
47             new IAction()
48             {
49                 public void Run (
50                     final ActionTrigger eTrigger,
51                     final ElementContext aContext,
52                     final String sText,
53                     final Location aStartLocation,
54                     final Location aEndLocation)
55                 {
56                     final RunRange<TokenType> aRuns = aView.GetRuns(
57                         aStartLocation.getCharacterOffset(),
58                         aEndLocation.getCharacterOffset());
59                     if (aRuns.IsEmpty())
60                         aView.GetRuns(
61                             aStartLocation.getCharacterOffset(),
62                             aEndLocation.getCharacterOffset());
63 
64                     // Search for the name (including namespace prefix) of the element.
65                     int nIndex = aRuns.FindTokens(
66                         TokenType.TAG_START,
67                         TokenType.IDENTIFIER,
68                         TokenType.COLON,
69                         TokenType.IDENTIFIER);
70                     if (nIndex < 0)
71                         return;
72 
73                     aRuns.Get(nIndex+1).SetToolTipText(aContext.GetTypeName());
74                     aRuns.Get(nIndex+2).SetToolTipText(aContext.GetTypeName());
75                     aRuns.Get(nIndex+3).SetToolTipText(aContext.GetTypeName());
76                     nIndex += 4;
77 
78                     // Process the attributes.
79                     while (true)
80                     {
81                         final int nStartIndex = nIndex;
82                         nIndex = aRuns.FindTokens(
83                             nStartIndex,
84                             TokenType.IDENTIFIER,
85                             TokenType.COLON,
86                             TokenType.IDENTIFIER,
87                             TokenType.ATTRIBUTE_DEFINE);
88                         if (nIndex >= 0)
89                         {
90                             final String sAttributeName = aRuns.Get(nIndex+2).GetText();
91                             aRuns.Get(nIndex+0).SetToolTipText("attribute define of "+sAttributeName);
92                             aRuns.Get(nIndex+1).SetToolTipText("attribute define of "+sAttributeName);
93                             aRuns.Get(nIndex+2).SetToolTipText("attribute define of "+sAttributeName);
94                             nIndex += 5;
95                         }
96                         else
97                         {
98                             // Try the variant without namespace.
99                             nIndex = aRuns.FindTokens(
100                                 nStartIndex,
101                                 TokenType.IDENTIFIER,
102                                 TokenType.ATTRIBUTE_DEFINE);
103                             if (nIndex >= 0)
104                             {
105                                 final String sAttributeName = aRuns.Get(nIndex).GetText();
106                                 aRuns.Get(nIndex).SetToolTipText("attribute define of "+sAttributeName);
107                                 nIndex += 3;
108                             }
109                             else
110                             {
111                                 // No more attributes.
112                                 break;
113                             }
114                         }
115                     }
116                 }
117             });
118         try
119         {
120             aParser.Parse();
121         }
122         catch (final ParseException aException)
123         {
124             System.err.printf("caught exception when parsing %d,%d/%d\n",
125                 aException.Location.getLineNumber(),
126                 aException.Location.getColumnNumber(),
127                 aException.Location.getCharacterOffset());
128 
129             final Run<TokenType> aRun = aView.GetRun(aException.Location.getCharacterOffset());
130             if (aRun != null)
131             {
132                 aView.MarkError(aRun);
133                 aRun.SetToolTipText(
134                     String.format(
135                         "parse error at %d,%d/%d\n",
136                         aException.Location.getLineNumber(),
137                         aException.Location.getColumnNumber(),
138                         aException.Location.getCharacterOffset()));
139                 aView.ShowRun(aRun);
140             }
141         }
142     }
143 }
144