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.schema.automaton;
23 
24 import java.util.Vector;
25 
26 import org.apache.openoffice.ooxml.schema.model.attribute.Attribute;
27 import org.apache.openoffice.ooxml.schema.model.base.Location;
28 import org.apache.openoffice.ooxml.schema.model.base.QualifiedName;
29 
30 
31 /** Represents a DFA (deterministic FA) or a NFA (non-deterministic FA).
32  *  There is one automaton for each complex type and one for the top level elements.
33  *  Transitions correspond to 'element' elements in the schema or a start tag in
34  *  the input file.  During parsing the current automaton is pushed on a stack
35  *  and the automaton that represents the complex type associated with the
36  *  starting element is made the current automaton.  An end tag pops an automaton
37  *  from the stack and replaces the current automaton with it.
38  */
39 public class FiniteAutomaton
40 {
FiniteAutomaton( final StateContext aContext, final Vector<Attribute> aAttributes, final Location aLocation)41     FiniteAutomaton (
42         final StateContext aContext,
43         final Vector<Attribute> aAttributes,
44         final Location aLocation)
45     {
46         maStateContext = aContext;
47         maAttributes = aAttributes!=null
48             ? aAttributes
49             : new Vector<Attribute>();
50         maLocation = aLocation;
51     }
52 
53 
54 
55 
GetStateCount()56     public int GetStateCount ()
57     {
58         return maStateContext.GetStateCount();
59     }
60 
61 
62 
63 
GetStates()64     public Iterable<State> GetStates()
65     {
66         return maStateContext.GetStates();
67     }
68 
69 
70 
71 
GetStatesSorted()72     public Iterable<State> GetStatesSorted ()
73     {
74         return maStateContext.GetStatesSorted();
75     }
76 
77 
78 
79 
GetStartState()80     public State GetStartState ()
81     {
82         return maStateContext.GetStartState();
83     }
84 
85 
86 
87 
GetAcceptingStates()88     public Iterable<State> GetAcceptingStates ()
89     {
90         return maStateContext.GetAcceptingStates();
91     }
92 
93 
94 
95 
CreateDFA( final StateContainer aDFAContainer, final QualifiedName aTypeName)96     public FiniteAutomaton CreateDFA (
97         final StateContainer aDFAContainer,
98         final QualifiedName aTypeName)
99     {
100         return DFACreator.CreateDFAforNFA(
101             aDFAContainer,
102             maStateContext,
103             maAttributes,
104             aTypeName,
105             maLocation);
106     }
107 
108 
109 
110 
GetStateContext()111     public StateContext GetStateContext()
112     {
113         return maStateContext;
114     }
115 
116 
117 
118 
GetTransitions()119     public Iterable<Transition> GetTransitions ()
120     {
121         final Vector<Transition> aTransitions = new Vector<>();
122         for (final State aState : maStateContext.GetStates())
123             for (final Transition aTransition : aState.GetTransitions())
124                 aTransitions.add(aTransition);
125         return aTransitions;
126     }
127 
128 
129 
130 
GetTransitionCount()131     public int GetTransitionCount()
132     {
133         int nTransitionCount = 0;
134         for (final State aState : maStateContext.GetStates())
135             nTransitionCount += aState.GetTransitionCount();
136         return nTransitionCount;
137     }
138 
139 
140 
141 
GetTypeName()142     public String GetTypeName ()
143     {
144         return maStateContext.GetStartState().GetFullname();
145     }
146 
147 
148 
149 
GetLocation()150     public Location GetLocation ()
151     {
152         return maLocation;
153     }
154 
155 
156 
157 
GetAttributes()158     public Vector<Attribute> GetAttributes ()
159     {
160         return maAttributes;
161     }
162 
163 
164 
165 
166     private final StateContext maStateContext;
167     private final Vector<Attribute> maAttributes;
168     private final Location maLocation;
169 }
170