2
* Copyright © 2008-2012 NetAllied Systems GmbH, Ravensburg, Germany.
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
8
package de.netallied.xsd2cppsax;
10
import java.util.HashSet;
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;
27
* Traverses Xerces XS data structure.
30
public abstract class AbstractXSTraverser {
32
/** Code generator configuration. */
33
protected Config config;
36
* To avoid traversing of sub-trees multiple times when <xs:element ref="">
39
private Set<XSElementDeclaration> handledElements = new HashSet<XSElementDeclaration>();
41
/** Model being traversed. */
42
private XSModel model;
47
public AbstractXSTraverser(Config config) {
52
* Searches given model for a top level element with given name.
56
* @param rootElementName
58
* @return Root element.
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;
74
* @return Code generator configuration.
76
protected Config getConfig() {
81
* @return Model being traversed.
83
public XSModel getModel() {
92
* @return True if traversing shall continue, false for abort.
94
public boolean handleElement(XSElementDeclaration element) {
95
if (!handledElements.contains(element)) {
96
handledElements.add(element);
98
XSTypeDefinition typeDefinition = element.getTypeDefinition();
99
return handleTypeDefinition(typeDefinition);
105
* Handles a model group.
108
* Model group to handle.
109
* @return True if traversing shall continue, false for abort.
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);
124
* Handles a particle.
127
* Particle to handle.
128
* @return True if traversing shall continue, false for abort.
130
public boolean handleParticle(XSParticle particle) {
131
if (particle == null) {
135
XSTerm term = particle.getTerm();
136
if (term instanceof XSModelGroup) {
137
XSModelGroup modelGroup = (XSModelGroup) term;
139
if (!handleModelGroup(modelGroup)) {
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))) {
151
} else if (term instanceof XSElementDeclaration) {
152
return handleElement((XSElementDeclaration) term);
154
} else if (term instanceof XSWildcard) {
155
return handleWildcard((XSWildcard) term);
163
* @param typeDefinition
165
* @return True if traversing shall continue, false for abort.
167
protected boolean handleTypeDefinition(XSTypeDefinition typeDefinition) {
168
if (isTypeComplex(typeDefinition)) {
169
XSComplexTypeDefinition complexTypeDefi = (XSComplexTypeDefinition) typeDefinition;
170
boolean success = handleParticle(complexTypeDefi.getParticle());
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);
186
* Handles a wildcard.
189
* Wildcard to handle.
190
* @return True if traversing shall continue, false for abort.
192
public boolean handleWildcard(XSWildcard wildcard) {
196
public boolean isTypeComplex(XSTypeDefinition type) {
197
return type.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE;
202
* Model being traversed.
204
protected void setModel(XSModel model) {
214
public void start(XSModel 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);
226
* Starts traversing with given root element.
230
* @param rootElementName
231
* Name of element to start with.
233
public void startWithRootElement(XSModel model, String rootElementName) {
235
XSElementDeclaration rootElement = findRootElement(model, rootElementName);
236
if (handleElement(rootElement)) {