summaryrefslogtreecommitdiff
path: root/ooxml/source/framework/JavaOOXMLParser/src/org/apache/openoffice/ooxml/parser/StateMachine.java
blob: 39ff4dfd8137d14ef8801ed7b35c4e49f904d6c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*
*************************************************************/

package org.apache.openoffice.ooxml.parser;

import java.io.File;
import java.util.Stack;
import java.util.Vector;

import javax.xml.stream.Location;

import org.apache.openoffice.ooxml.parser.action.ActionManager;
import org.apache.openoffice.ooxml.parser.action.ActionTrigger;
import org.apache.openoffice.ooxml.parser.action.IAction;
import org.apache.openoffice.ooxml.parser.attribute.AttributeManager;
import org.apache.openoffice.ooxml.parser.attribute.AttributeProvider;
import org.apache.openoffice.ooxml.parser.attribute.AttributeValues;
import org.apache.openoffice.ooxml.parser.type.SimpleTypeManager;

/** The state machine is initialized at creation from the data tables
 *  created previously by a stack automaton.
 */
public class StateMachine
{
    public StateMachine (
        final File aParseTableFile,
        final Vector<String> aErrorsAndWarnings)
    {
        if (Log.Dbg != null)
            Log.Dbg.printf("reading parse tables from %s\n", aParseTableFile.toString());

        final ParseTableReader aReader = new ParseTableReader(aParseTableFile);
        maNamespaceMap = new NamespaceMap(aReader.GetSection("namespace"));
        maNameMap = new NameMap(aReader.GetSection("name"));
        maStateNameMap = new NameMap(aReader.GetSection("state-name"));
        maTransitions = new TransitionTable(aReader.GetSection("transition"));
        maSkipStates = new SkipStateTable(aReader.GetSection("skip"));
        maAttributeValueMap = new NameMap(aReader.GetSection("attribute-value"));
        maAcceptingStates = new AcceptingStateTable(aReader.GetSection("accepting-state"));
        maSimpleTypeManager = new SimpleTypeManager(
            aReader.GetSection("simple-type"),
            maAttributeValueMap);
        maAttributeManager = new AttributeManager(
            aReader.GetSection("attribute"),
            maNamespaceMap,
            maNameMap,
            maStateNameMap,
            maSimpleTypeManager,
            aErrorsAndWarnings);
        mnStartStateId = Integer.parseInt(aReader.GetSection("start-state").firstElement()[1]);
        mnEndStateId = Integer.parseInt(aReader.GetSection("end-state").firstElement()[1]);

        mnCurrentStateId = mnStartStateId;
        maStateStack = new Stack<>();
        maElementContextStack = new Stack<>();
        maActionManager = new ActionManager(maStateNameMap);
        maErrorsAndWarnings  = aErrorsAndWarnings;

        if (Log.Dbg != null)
        {
            Log.Dbg.printf("read %d namespace, %d names, %d states (%d skip, %d accept), %d transitions and %d attributes\n",
                maNamespaceMap.GetNamespaceCount(),
                maNameMap.GetNameCount(),
                maStateNameMap.GetNameCount(),
                maSkipStates.GetSkipStateCount(),
                maAcceptingStates.GetAcceptingStateCount(),
                maTransitions.GetTransitionCount(),
                maAttributeManager.GetAttributeCount());
            Log.Dbg.printf("starting in state _start_ (%d)\n", mnCurrentStateId);
        }
    }




    public boolean ProcessStartElement (
        final String sNamespaceURI,
        final String sElementName,
        final Location aStartLocation,
        final Location aEndLocation,
        final AttributeProvider aAttributes)
    {
        boolean bResult = false;

        try
        {
            final NamespaceMap.NamespaceDescriptor aNamespaceDescriptor = maNamespaceMap.GetDescriptorForURI(sNamespaceURI);
            final int nElementNameId = maNameMap.GetIdForName(sElementName);
            if (Log.Dbg != null)
                Log.Dbg.printf("%s:%s(%d:%d) L%dC%d\n",
                    aNamespaceDescriptor.Prefix,
                    sElementName,
                    aNamespaceDescriptor.Id,
                    nElementNameId,
                    aStartLocation.getLineNumber(),
                    aStartLocation.getColumnNumber());

            final Transition aTransition = maTransitions.GetTransition(
                mnCurrentStateId,
                aNamespaceDescriptor.Id,
                nElementNameId);
            if (aTransition == null)
            {
                final String sText = String.format(
                    "can not find transition for state %s(%d) and element %s:%s(%d:%d) at L%dC%d\n",
                    maStateNameMap.GetNameForId(mnCurrentStateId),
                    mnCurrentStateId,
                    aNamespaceDescriptor.Prefix,
                    maNameMap.GetNameForId(nElementNameId),
                    aNamespaceDescriptor.Id,
                    nElementNameId,
                    aStartLocation.getLineNumber(),
                    aStartLocation.getColumnNumber());
                Log.Err.printf(sText);
                if (Log.Dbg != null)
                    Log.Dbg.printf(sText);
            }
            else
            {
                if (Log.Dbg != null)
                {
                    Log.Dbg.printf(" %s(%d) -> %s(%d) via %s(%d)",
                        maStateNameMap.GetNameForId(mnCurrentStateId),
                        mnCurrentStateId,
                        maStateNameMap.GetNameForId(aTransition.GetEndStateId()),
                        aTransition.GetEndStateId(),
                        maStateNameMap.GetNameForId(aTransition.GetActionId()),
                        aTransition.GetActionId());
                    Log.Dbg.printf("\n");
                }

                // Follow the transition to its end state but first process its
                // content.  We do that by

                if (Log.Dbg != null)
                    Log.Dbg.IncreaseIndentation();

                // a) pushing the end state to the state stack so that on the
                // end tag that corresponds to the current start tag it will become the current state.
                maStateStack.push(aTransition.GetEndStateId());

                // b) entering the state that corresponds to start tag that
                // we are currently processing.
                mnCurrentStateId = aTransition.GetActionId();

                // c) Prepare the attributes and store them in the new element context.
                final AttributeValues aAttributeValues = maAttributeManager.ParseAttributes(
                    mnCurrentStateId,
                    aAttributes);

                // d) creating a new ElementContext for the element that just starts.
                maElementContextStack.push(maCurrentElementContext);
                final ElementContext aPreviousElementContext = maCurrentElementContext;
                maCurrentElementContext = new ElementContext(
                    sElementName,
                    maStateNameMap.GetNameForId(aTransition.GetActionId()),
                    false,
                    aAttributeValues,
                    aPreviousElementContext);

                // e) and run all actions that are bound to the the current start tag.
                ExecuteActions(
                    mnCurrentStateId,
                    maCurrentElementContext,
                    ActionTrigger.ElementStart,
                    null,
                    aStartLocation,
                    aEndLocation);

                bResult = true;
            }
        }
        catch (RuntimeException aException)
        {
            Log.Err.printf("error at line %d and column %d\n",
                aStartLocation.getLineNumber(),
                aStartLocation.getColumnNumber());
            throw aException;
        }
        return bResult;
    }




    public void ProcessEndElement (
        final String sNamespaceURI,
        final String sElementName,
        final Location aStartLocation,
        final Location aEndLocation)
    {
        if ( ! maAcceptingStates.Contains(mnCurrentStateId)
            && mnCurrentStateId!=-1)
        {
            if (Log.Dbg != null)
                Log.Dbg.printf("current state %s(%d) is not an accepting state\n",
                    maStateNameMap.GetNameForId(mnCurrentStateId),
                    mnCurrentStateId);
            throw new RuntimeException("not expecting end element "+sElementName);
        }

        final NamespaceMap.NamespaceDescriptor aDescriptor = maNamespaceMap.GetDescriptorForURI(sNamespaceURI);

        // Leave the current element.

        final int nPreviousStateId = mnCurrentStateId;
        mnCurrentStateId = maStateStack.pop();
        if (mnCurrentStateId == mnEndStateId)
            mnCurrentStateId = mnStartStateId;

        final ElementContext aPreviousElementContext = maCurrentElementContext;
        maCurrentElementContext = maElementContextStack.pop();

        ExecuteActions(
            nPreviousStateId,
            aPreviousElementContext,
            ActionTrigger.ElementEnd,
            null,
            aStartLocation,
            aEndLocation);

        if (Log.Dbg != null)
        {
            Log.Dbg.DecreaseIndentation();
            Log.Dbg.printf("/%s:%s L%d%d\n",
                aDescriptor.Prefix,
                sElementName,
                aStartLocation.getLineNumber(),
                aStartLocation.getColumnNumber());
            Log.Dbg.printf(" %s(%d) <- %s(%d)\n",
                maStateNameMap.GetNameForId(nPreviousStateId),
                nPreviousStateId,
                maStateNameMap.GetNameForId(mnCurrentStateId),
                mnCurrentStateId);
        }
    }




    public void ProcessCharacters (
        final String sText,
        final Location aStartLocation,
        final Location aEndLocation)
    {
        if (Log.Dbg != null)
            Log.Dbg.printf("text [%s]\n", sText.replace("\n", "\\n"));

        ExecuteActions(
            mnCurrentStateId,
            maCurrentElementContext,
            ActionTrigger.Text,
            sText,
            aStartLocation,
            aEndLocation);

    }




    public boolean IsInSkipState ()
    {
        return maSkipStates.Contains(mnCurrentStateId);
    }




    public ActionManager GetActionManager ()
    {
        return maActionManager;
    }




    private void ExecuteActions (
        final int nStateId,
        final ElementContext aElementContext,
        final ActionTrigger eTrigger,
        final String sText,
        final Location aStartLocation,
        final Location aEndLocation)
    {
        final Iterable<IAction> aActions = maActionManager.GetActions(nStateId, eTrigger);
        if (aActions != null)
            for (final IAction aAction : aActions)
                aAction.Run(eTrigger, aElementContext, sText, aStartLocation, aEndLocation);
    }




    private final NamespaceMap maNamespaceMap;
    private final NameMap maNameMap;
    private final NameMap maStateNameMap;
    private final TransitionTable maTransitions;
    private final SimpleTypeManager maSimpleTypeManager;
    private final AttributeManager maAttributeManager;
    private final NameMap maAttributeValueMap;
    private int mnCurrentStateId;
    private Stack<Integer> maStateStack;
    private ElementContext maCurrentElementContext;
    private Stack<ElementContext> maElementContextStack;
    private final int mnStartStateId;
    private final int mnEndStateId;
    private SkipStateTable maSkipStates;
    private AcceptingStateTable maAcceptingStates;
    private final ActionManager maActionManager;
    private final Vector<String> maErrorsAndWarnings;
}