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