summaryrefslogtreecommitdiff
path: root/ooxml/source/framework/SchemaParser/src/org/apache/openoffice/ooxml/schema/automaton/State.java
blob: 7d9982c0e5c289f85ea5fd8e3d41be4ca05a3e5e (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
/**************************************************************
*
* 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.schema.automaton;

import java.util.Vector;

import org.apache.openoffice.ooxml.schema.model.base.INode;
import org.apache.openoffice.ooxml.schema.model.base.QualifiedName;

/** Each complex type is represented by a State object (primary state).
 *  For a validating parser additional states are created for sequences, choices, etc. (secondary states).
 *  Secondary states have the same basename as primary states and suffixes to make their names unique.
 *  Full names of states contain both the basename and the suffix.
 */
public class State
    implements Comparable<State>
{
    /** Create a new state from a basename and an optional suffix.
     *
     *  Don't call this constructor directly.  Use methods in StateContext instead.
     *  They ensure that states are unique per context.
     */
    State (
        final QualifiedName aBasename,
        final String sSuffix)
    {
        maBasename = aBasename;
        msSuffix = sSuffix;
        msFullname = GetStateName(aBasename, msSuffix);
        maTransitions = new Vector<>();
        maEpsilonTransitions = new Vector<>();
        maSkipData = new Vector<>();
        mbIsAccepting = false;
        maTextType = null;
    }




    State Clone (final StateContext aContext)
    {
        return aContext.GetOrCreateState(maBasename, msSuffix);
    }




    static String GetStateName (
        final QualifiedName aBasename,
        final String sSuffix)
    {
        if (sSuffix == null)
            return aBasename.GetStateName();
        else
            return aBasename.GetStateName()+"_"+sSuffix;
    }




    public String GetFullname ()
    {
        return msFullname;
    }




    public QualifiedName GetBasename ()
    {
        return maBasename;
    }




    /** Return a qualified name that contains the suffix.
     *  This is typically only used for sorting type names.
     */
    public QualifiedName GetQualifiedName ()
    {
        return new QualifiedName(
            maBasename.GetNamespacePrefix(),
            maBasename.GetNamespaceURI(),
            msSuffix != null
                ? maBasename.GetLocalPart() + "_" + msSuffix
                : maBasename.GetLocalPart());
    }




    public String GetSuffix ()
    {
        return msSuffix;
    }




    public void AddTransition (final Transition aTransition)
    {
        assert(this == aTransition.GetStartState());
        maTransitions.add(aTransition);
    }




    public Iterable<Transition> GetTransitions()
    {
        return maTransitions;
    }




    public int GetTransitionCount ()
    {
        return maTransitions.size();
    }




    public void AddEpsilonTransition (final EpsilonTransition aTransition)
    {
        assert(this == aTransition.GetStartState());
        maEpsilonTransitions.add(aTransition);
    }




    public Iterable<EpsilonTransition> GetEpsilonTransitions()
    {
        return maEpsilonTransitions;
    }




    public void AddSkipData (final SkipData aSkipData)
    {
        maSkipData.add(aSkipData);
    }




    public Iterable<SkipData> GetSkipData ()
    {
        return maSkipData;
    }




    public void SetIsAccepting ()
    {
        mbIsAccepting = true;
    }




    public boolean IsAccepting ()
    {
        return mbIsAccepting;
    }




    /** The basename is the primary sort key.  The suffix is the secondary key.
     */
    @Override
    public int compareTo (final State aOther)
    {
        int nResult = maBasename.compareTo(aOther.maBasename);
        if (nResult == 0)
        {
            if (msSuffix==null && aOther.msSuffix==null)
                nResult = 0;
            else if (msSuffix!=null && aOther.msSuffix!=null)
                nResult = msSuffix.compareTo(aOther.msSuffix);
            else if (msSuffix==null)
                nResult = -1;
            else
                nResult = +1;
        }
        return nResult;
    }




    public void SetTextType (final INode aTextType)
    {
        assert(maTextType==null);
        maTextType = aTextType;
    }




    public INode GetTextType ()
    {
        return maTextType;
    }




    public void CopyFrom (final State aOther)
    {
        if (aOther.IsAccepting())
            SetIsAccepting();
        for (final SkipData aSkipData : aOther.GetSkipData())
            AddSkipData(aSkipData.Clone(this));
        SetTextType(aOther.GetTextType());
    }




    @Override
    public String toString ()
    {
        return msFullname;
    }




    private final QualifiedName maBasename;
    private final String msSuffix;
    private final String msFullname;
    private final Vector<Transition> maTransitions;
    private final Vector<EpsilonTransition> maEpsilonTransitions;
    private final Vector<SkipData> maSkipData;
    private boolean mbIsAccepting;
    private INode maTextType;
}