~ubuntu-branches/ubuntu/wily/opencollada/wily-proposed

« back to all changes in this revision

Viewing changes to xsd2cppsax/src/de/netallied/xsd2cppsax/AbstractXSTraverser.java

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2015-05-14 17:23:27 UTC
  • Revision ID: package-import@ubuntu.com-20150514172327-f862u8envms01fra
Tags: upstream-0.1.0~20140703.ddf8f47+dfsg1
ImportĀ upstreamĀ versionĀ 0.1.0~20140703.ddf8f47+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *   Copyright © 2008-2012 NetAllied Systems GmbH, Ravensburg, Germany. 
 
3
 *       
 
4
 *   Licensed under the MIT Open Source License, 
 
5
 *   for details please see LICENSE file or the website
 
6
 *   http://www.opensource.org/licenses/mit-license.php
 
7
*/
 
8
package de.netallied.xsd2cppsax;
 
9
 
 
10
import java.util.HashSet;
 
11
import java.util.Set;
 
12
 
 
13
import org.apache.xerces.xs.XSComplexTypeDefinition;
 
14
import org.apache.xerces.xs.XSConstants;
 
15
import org.apache.xerces.xs.XSElementDeclaration;
 
16
import org.apache.xerces.xs.XSModel;
 
17
import org.apache.xerces.xs.XSModelGroup;
 
18
import org.apache.xerces.xs.XSNamedMap;
 
19
import org.apache.xerces.xs.XSObject;
 
20
import org.apache.xerces.xs.XSObjectList;
 
21
import org.apache.xerces.xs.XSParticle;
 
22
import org.apache.xerces.xs.XSTerm;
 
23
import org.apache.xerces.xs.XSTypeDefinition;
 
24
import org.apache.xerces.xs.XSWildcard;
 
25
 
 
26
/**
 
27
 * Traverses Xerces XS data structure.
 
28
 * 
 
29
 */
 
30
public abstract class AbstractXSTraverser {
 
31
 
 
32
    /** Code generator configuration. */
 
33
    protected Config config;
 
34
 
 
35
    /**
 
36
     * To avoid traversing of sub-trees multiple times when <xs:element ref="">
 
37
     * is used excessively
 
38
     */
 
39
    private Set<XSElementDeclaration> handledElements = new HashSet<XSElementDeclaration>();
 
40
 
 
41
    /** Model being traversed. */
 
42
    private XSModel model;
 
43
 
 
44
    /**
 
45
     * Constructor.
 
46
     */
 
47
    public AbstractXSTraverser(Config config) {
 
48
        this.config = config;
 
49
    }
 
50
 
 
51
    /**
 
52
     * Searches given model for a top level element with given name.
 
53
     * 
 
54
     * @param model
 
55
     *            Model to search.
 
56
     * @param rootElementName
 
57
     *            Root element name.
 
58
     * @return Root element.
 
59
     */
 
60
    public XSElementDeclaration findRootElement(XSModel model, String rootElementName) {
 
61
        XSNamedMap elementMap = model.getComponents(XSConstants.ELEMENT_DECLARATION);
 
62
        for (int i = 0; i < elementMap.getLength(); i++) {
 
63
            XSObject item = elementMap.item(i);
 
64
            if (item.getType() == XSConstants.ELEMENT_DECLARATION) {
 
65
                if (item.getName().equals(rootElementName)) {
 
66
                    return (XSElementDeclaration) item;
 
67
                }
 
68
            }
 
69
        }
 
70
        return null;
 
71
    }
 
72
 
 
73
    /**
 
74
     * @return Code generator configuration.
 
75
     */
 
76
    protected Config getConfig() {
 
77
        return config;
 
78
    }
 
79
 
 
80
    /**
 
81
     * @return Model being traversed.
 
82
     */
 
83
    public XSModel getModel() {
 
84
        return model;
 
85
    }
 
86
 
 
87
    /**
 
88
     * Handles an element,
 
89
     * 
 
90
     * @param element
 
91
     *            Element to handle.
 
92
     * @return True if traversing shall continue, false for abort.
 
93
     */
 
94
    public boolean handleElement(XSElementDeclaration element) {
 
95
        if (!handledElements.contains(element)) {
 
96
            handledElements.add(element);
 
97
 
 
98
            XSTypeDefinition typeDefinition = element.getTypeDefinition();
 
99
            return handleTypeDefinition(typeDefinition);
 
100
        }
 
101
        return true;
 
102
    }
 
103
 
 
104
    /**
 
105
     * Handles a model group.
 
106
     * 
 
107
     * @param modelGroup
 
108
     *            Model group to handle.
 
109
     * @return True if traversing shall continue, false for abort.
 
110
     */
 
111
    public boolean handleModelGroup(XSModelGroup modelGroup) {
 
112
        XSObjectList particles = modelGroup.getParticles();
 
113
        for (int i = 0; i < particles.getLength(); i++) {
 
114
            XSParticle particle = (XSParticle) particles.item(i);
 
115
            boolean success = handleParticle(particle);
 
116
            if (!success) {
 
117
                return false;
 
118
            }
 
119
        }
 
120
        return true;
 
121
    }
 
122
 
 
123
    /**
 
124
     * Handles a particle.
 
125
     * 
 
126
     * @param particle
 
127
     *            Particle to handle.
 
128
     * @return True if traversing shall continue, false for abort.
 
129
     */
 
130
    public boolean handleParticle(XSParticle particle) {
 
131
        if (particle == null) {
 
132
            return true;
 
133
        }
 
134
 
 
135
        XSTerm term = particle.getTerm();
 
136
        if (term instanceof XSModelGroup) {
 
137
            XSModelGroup modelGroup = (XSModelGroup) term;
 
138
 
 
139
            if (!handleModelGroup(modelGroup)) {
 
140
                return false;
 
141
            }
 
142
 
 
143
            XSObjectList particles = modelGroup.getParticles();
 
144
            for (int i = 0; i < particles.getLength(); i++) {
 
145
                if (particles.item(i) instanceof XSParticle) {
 
146
                    if (!handleParticle((XSParticle) particles.item(i))) {
 
147
                        return false;
 
148
                    }
 
149
                }
 
150
            }
 
151
        } else if (term instanceof XSElementDeclaration) {
 
152
            return handleElement((XSElementDeclaration) term);
 
153
 
 
154
        } else if (term instanceof XSWildcard) {
 
155
            return handleWildcard((XSWildcard) term);
 
156
        }
 
157
        return true;
 
158
    }
 
159
 
 
160
    /**
 
161
     * Handles a XSD type
 
162
     * 
 
163
     * @param typeDefinition
 
164
     *            Type to handle.
 
165
     * @return True if traversing shall continue, false for abort.
 
166
     */
 
167
    protected boolean handleTypeDefinition(XSTypeDefinition typeDefinition) {
 
168
        if (isTypeComplex(typeDefinition)) {
 
169
            XSComplexTypeDefinition complexTypeDefi = (XSComplexTypeDefinition) typeDefinition;
 
170
            boolean success = handleParticle(complexTypeDefi.getParticle());
 
171
            if (!success) {
 
172
                return false;
 
173
            }
 
174
        }
 
175
        XSTypeDefinition baseType = typeDefinition.getBaseType();
 
176
        if (baseType != null) {
 
177
            if (!Constants.XSD_ANYTYPE_NAME.equals(baseType.getName())
 
178
                    && !getConfig().getXSNamespace().equals(baseType.getNamespace())) {
 
179
                return handleTypeDefinition(baseType);
 
180
            }
 
181
        }
 
182
        return true;
 
183
    }
 
184
 
 
185
    /**
 
186
     * Handles a wildcard.
 
187
     * 
 
188
     * @param wildcard
 
189
     *            Wildcard to handle.
 
190
     * @return True if traversing shall continue, false for abort.
 
191
     */
 
192
    public boolean handleWildcard(XSWildcard wildcard) {
 
193
        return true;
 
194
    }
 
195
 
 
196
    public boolean isTypeComplex(XSTypeDefinition type) {
 
197
        return type.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE;
 
198
    }
 
199
 
 
200
    /**
 
201
     * @param model
 
202
     *            Model being traversed.
 
203
     */
 
204
    protected void setModel(XSModel model) {
 
205
        this.model = model;
 
206
    }
 
207
 
 
208
    /**
 
209
     * Starts traversing.
 
210
     * 
 
211
     * @param model
 
212
     *            Model to traverse.
 
213
     */
 
214
    public void start(XSModel model) {
 
215
        this.model = model;
 
216
        XSNamedMap elementMap = model.getComponents(XSConstants.ELEMENT_DECLARATION);
 
217
        for (int i = 0; i < elementMap.getLength(); i++) {
 
218
            XSObject item = elementMap.item(i);
 
219
            if (item.getType() == XSConstants.ELEMENT_DECLARATION) {
 
220
                handleElement((XSElementDeclaration) item);
 
221
            }
 
222
        }
 
223
    }
 
224
 
 
225
    /**
 
226
     * Starts traversing with given root element.
 
227
     * 
 
228
     * @param model
 
229
     *            Model to traverse.
 
230
     * @param rootElementName
 
231
     *            Name of element to start with.
 
232
     */
 
233
    public void startWithRootElement(XSModel model, String rootElementName) {
 
234
        this.model = model;
 
235
        XSElementDeclaration rootElement = findRootElement(model, rootElementName);
 
236
        if (handleElement(rootElement)) {
 
237
            start(model);
 
238
        }
 
239
    }
 
240
}