]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaObject.java
53f9da9fb0b4a749041f8704440aa5b34627f36f
[simantics/interop.git] / org.simantics.xml.sax / src / org / simantics / xml / sax / SchemaObject.java
1 package org.simantics.xml.sax;
2
3 import org.simantics.xml.sax.configuration.Rename;
4 import org.w3._2001.xmlschema.AttributeGroup;
5 import org.w3._2001.xmlschema.AttributeGroupRef;
6 import org.w3._2001.xmlschema.ComplexType;
7 import org.w3._2001.xmlschema.Element;
8 import org.w3._2001.xmlschema.NamedAttributeGroup;
9 import org.w3._2001.xmlschema.NamedGroup;
10 import org.w3._2001.xmlschema.OpenAttrs;
11 import org.w3._2001.xmlschema.SimpleType;
12
13 public class SchemaObject {
14         enum ObjectType{ELEMENT,COMPLEX_TYPE,SIMPLE_TYPE,ATTRIBUTE_GROUP,MODEL_GROUP};
15         
16         private SchemaConversionBase sc;
17         private String name;
18         private SchemaObject parent;
19         private ObjectType type;
20         private OpenAttrs obj;
21         private Rename rename;
22         
23         public SchemaObject(SchemaConversionBase sc, Element element) {
24                 this(sc,null,element);
25         }
26         
27         public void setRename(Rename rename) {
28                 this.rename = rename;
29                 _init();
30         }
31         
32         public SchemaObject(SchemaConversionBase sc,ComplexType complexType) {
33                 this(sc,null, complexType);
34         }
35         
36         public SchemaObject(SchemaConversionBase sc,SimpleType simpleType) {
37                 this(sc,null, simpleType);
38         }
39         
40         public SchemaObject(SchemaConversionBase sc,NamedGroup namedGroup) {
41                 this(sc,null, namedGroup);
42         }
43         
44         public SchemaObject(SchemaConversionBase sc,SchemaObject parent, Element element) {
45                 this.parent = parent;
46                 this.obj = element;
47                 this.type = ObjectType.ELEMENT;
48                 this.sc = sc;
49                 _init();
50         }
51         
52         public SchemaObject(SchemaConversionBase sc,SchemaObject parent, ComplexType complexType) {
53                 this.parent = parent;
54                 this.obj = complexType;
55                 this.type = ObjectType.COMPLEX_TYPE;
56                 this.sc = sc;
57                 _init();
58         }
59         
60         public SchemaObject(SchemaConversionBase sc,SchemaObject parent, AttributeGroup attributeGroup) {
61                 this.parent = parent;
62                 this.obj = attributeGroup;
63                 this.type = ObjectType.ATTRIBUTE_GROUP;
64                 this.sc = sc;
65                 _init();
66         }
67         
68         public SchemaObject(SchemaConversionBase sc,SchemaObject parent, NamedGroup namedGroup) {
69                 this.parent = parent;
70                 this.obj = namedGroup;
71                 this.type = ObjectType.MODEL_GROUP;
72                 this.sc = sc;
73                 _init();
74         }
75         
76
77         public SchemaObject(SchemaConversionBase sc,SchemaObject parent, SimpleType simpleType) {
78                 this.parent = parent;
79                 this.obj = simpleType;
80                 this.type = ObjectType.SIMPLE_TYPE;
81                 this.sc = sc;
82                 _init();
83         }
84         
85         private void _init() {
86                 name = _getName();
87                 if (name != null)
88                         name = name.replaceAll("\\.", "_");
89         }
90         
91         public Element getElement() {
92                 if (type != ObjectType.ELEMENT)
93                         return null;
94                 return (Element)obj;
95         }
96         
97         public ComplexType getComplexType() {
98                 if (type != ObjectType.COMPLEX_TYPE)
99                         return null;
100                 return (ComplexType)obj;
101         }
102         
103         public SimpleType getSimpleType() {
104                 if (type != ObjectType.SIMPLE_TYPE)
105                         return null;
106                 return (SimpleType)obj;
107         }
108         
109         public AttributeGroup getAttributeGroup() {
110                 if (type != ObjectType.ATTRIBUTE_GROUP)
111                         return null;
112                 return (AttributeGroup)obj;
113         }
114         
115         public NamedGroup getModelGroup() {
116                 if (type != ObjectType.MODEL_GROUP)
117                         return null;
118                 return (NamedGroup)obj;
119         }
120         
121         public SchemaObject getParent() {
122                 return parent;
123         }
124         
125         public OpenAttrs getObj() {
126                 return obj;
127         }
128         
129         
130         public String getName() {
131                 return name;
132         }
133         
134         public String getLibShortName() {
135                 return sc.converter.shortName;
136         }
137         
138         
139         private String _getName() {
140                 switch (type) {
141                 case ATTRIBUTE_GROUP:
142                         if (obj instanceof NamedAttributeGroup)
143                                 return ((NamedAttributeGroup)obj).getName();
144                         else
145                                 return ((AttributeGroupRef)obj).getRef().getLocalPart();
146                 case COMPLEX_TYPE:
147                         if (rename != null)
148                                 return rename.getName();
149                         return ((ComplexType)obj).getName();
150                 case ELEMENT:
151                         if (rename != null)
152                                 return rename.getName();
153                         return ((Element)obj).getName();
154                 case SIMPLE_TYPE:
155                         return ((SimpleType)obj).getName();
156                 }
157                 throw new RuntimeException("Unknown object type " + type);              
158         }
159         
160         public ObjectType getType() {
161                 return type;
162         }
163         
164         public SchemaConversionBase getSc() {
165                 return sc;
166         }
167
168 }