01: package org.geotools.maven.xmlcodegen;
02:
03: import java.util.ArrayList;
04: import java.util.Iterator;
05: import java.util.List;
06:
07: import org.eclipse.xsd.XSDElementDeclaration;
08: import org.eclipse.xsd.XSDParticle;
09: import org.eclipse.xsd.XSDSchema;
10: import org.eclipse.xsd.XSDTypeDefinition;
11: import org.geotools.xml.Schemas;
12:
13: /**
14: * Utility class used for generating.
15: *
16: * @author Justin Deoliveira, The Open Planning Project
17: *
18: */
19: public class GeneratorUtils {
20:
21: /**
22: * Returns all the types defined in <param>schema</param>, including anonymous complex
23: * types.
24: *
25: * @param schema the schema.
26: *
27: * @return A list of types, including anonymous complex types.
28: */
29: public static List allTypes(XSDSchema schema) {
30: //get the named types
31: List types = new ArrayList(schema.getTypeDefinitions());
32:
33: //get anonymous types
34: List anonymous = new ArrayList();
35:
36: //add any anonymous types of global elements
37: for (Iterator e = schema.getElementDeclarations().iterator(); e
38: .hasNext();) {
39: XSDElementDeclaration element = (XSDElementDeclaration) e
40: .next();
41: if (element.getAnonymousTypeDefinition() != null) {
42: element.getAnonymousTypeDefinition().setName(
43: "_" + element.getName());
44: anonymous.add(element.getAnonymousTypeDefinition());
45: }
46: }
47:
48: //add any anonymous types foudn with type definitions
49: for (Iterator t = types.iterator(); t.hasNext();) {
50: XSDTypeDefinition type = (XSDTypeDefinition) t.next();
51: List particles = Schemas.getChildElementParticles(type,
52: false);
53: for (Iterator p = particles.iterator(); p.hasNext();) {
54: XSDParticle particle = (XSDParticle) p.next();
55: XSDElementDeclaration element = (XSDElementDeclaration) particle
56: .getContent();
57:
58: //ignore element references, caught in teh above loop
59: if (element.isElementDeclarationReference())
60: continue;
61:
62: if (element.getAnonymousTypeDefinition() != null) {
63: element.getAnonymousTypeDefinition().setName(
64: type.getName() + "_" + element.getName());
65: anonymous.add(element.getAnonymousTypeDefinition());
66: }
67: }
68: }
69:
70: types.addAll(anonymous);
71: return types;
72: }
73: }
|