1 package org.simantics.xml.sax;
\r
3 import java.util.ArrayDeque;
\r
4 import java.util.ArrayList;
\r
5 import java.util.Deque;
\r
6 import java.util.HashMap;
\r
7 import java.util.HashSet;
\r
8 import java.util.List;
\r
9 import java.util.Map;
\r
10 import java.util.Set;
\r
12 import javax.xml.bind.JAXBElement;
\r
13 import javax.xml.namespace.QName;
\r
15 import org.simantics.utils.datastructures.BijectionMap;
\r
16 import org.simantics.xml.sax.SchemaElement.ElementType;
\r
17 import org.simantics.xml.sax.SchemaObject.ObjectType;
\r
18 import org.simantics.xml.sax.configuration.AttributeComposition;
\r
19 import org.simantics.xml.sax.configuration.Configuration;
\r
20 import org.simantics.xml.sax.configuration.IDProvider;
\r
21 import org.simantics.xml.sax.configuration.IDReference;
\r
22 import org.simantics.xml.sax.configuration.OrderedChild;
\r
23 import org.simantics.xml.sax.configuration.Rename;
\r
24 import org.simantics.xml.sax.configuration.UnrecognizedChildElement;
\r
25 import org.w3._2001.xmlschema.All;
\r
26 import org.w3._2001.xmlschema.Annotated;
\r
27 import org.w3._2001.xmlschema.Any;
\r
28 import org.w3._2001.xmlschema.Attribute;
\r
29 import org.w3._2001.xmlschema.AttributeGroup;
\r
30 import org.w3._2001.xmlschema.ComplexContent;
\r
31 import org.w3._2001.xmlschema.ComplexType;
\r
32 import org.w3._2001.xmlschema.Element;
\r
33 import org.w3._2001.xmlschema.ExplicitGroup;
\r
34 import org.w3._2001.xmlschema.ExtensionType;
\r
35 import org.w3._2001.xmlschema.LocalComplexType;
\r
36 import org.w3._2001.xmlschema.LocalElement;
\r
37 import org.w3._2001.xmlschema.NamedAttributeGroup;
\r
38 import org.w3._2001.xmlschema.OpenAttrs;
\r
39 import org.w3._2001.xmlschema.Restriction;
\r
40 import org.w3._2001.xmlschema.Schema;
\r
41 import org.w3._2001.xmlschema.SimpleContent;
\r
42 import org.w3._2001.xmlschema.SimpleType;
\r
43 import org.w3._2001.xmlschema.TopLevelAttribute;
\r
44 import org.w3._2001.xmlschema.TopLevelComplexType;
\r
45 import org.w3._2001.xmlschema.TopLevelElement;
\r
46 import org.w3._2001.xmlschema.TopLevelSimpleType;
\r
48 public abstract class SchemaConversionBase {
\r
50 protected Schema schema;
\r
51 protected Configuration configuration;
\r
53 protected static final String SCHEMA_NS = "http://www.w3.org/2001/XMLSchema";
\r
54 protected static final String CONVERSION_NS = "http://www.simantics.org/Layer0";
\r
56 protected Map<String,Map<String,TypeEntry>> typeMap;
\r
58 public SchemaConversionBase(Configuration configuration) {
\r
59 this.configuration = configuration;
\r
60 typeMap = new HashMap<String, Map<String,TypeEntry>>();
\r
62 Map<String,TypeEntry> schemaTypes = new HashMap<String, SchemaConversionBase.TypeEntry>();
\r
63 typeMap.put(SCHEMA_NS, schemaTypes);
\r
64 Map<String,TypeEntry> l0Types = new HashMap<String, SchemaConversionBase.TypeEntry>();
\r
65 typeMap.put(CONVERSION_NS, l0Types);
\r
67 schemaTypes.put("string", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","",""));
\r
68 schemaTypes.put("NMTOKEN", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","",""));
\r
69 schemaTypes.put("token", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","",""));
\r
70 schemaTypes.put("ID", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","",true));
\r
71 schemaTypes.put("IDREF", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","",""));
\r
72 schemaTypes.put("date", new TypeEntry("XML.Date", "org.simantics.xml.sax.base.datatypes.literal.Date.BINDING", "org.simantics.xml.sax.base.datatypes.literal.Date", "","org.simantics.xml.sax.base.datatypes.literal.Date.parseDate(",")","(",").toString()"));
\r
73 schemaTypes.put("time", new TypeEntry("XML.Time", "org.simantics.xml.sax.base.datatypes.literal.Time.BINDING", "org.simantics.xml.sax.base.datatypes.literal.Time", "","org.simantics.xml.sax.base.datatypes.literal.Time.parseTime(",")","(",").toString()"));
\r
74 schemaTypes.put("dateTime", new TypeEntry("XML.DateTime", "org.simantics.xml.sax.base.datatypes.literal.DateTime.BINDING", "org.simantics.xml.sax.base.datatypes.literal.DateTime", "","org.simantics.xml.sax.base.datatypes.literal.DateTime.parseDateTime(",")","(",").toString()"));
\r
75 schemaTypes.put("anyURI", new TypeEntry("L0.URI", "Bindings.STRING", "java.lang.String", "","","","",""));
\r
76 schemaTypes.put("double", new TypeEntry("L0.Double", "Bindings.DOUBLE", "double", "java.lang.Double.NaN","java.lang.Double.parseDouble(",")","java.lang.Double.toString(",")"));
\r
77 schemaTypes.put("float", new TypeEntry("L0.Float", "Bindings.FLOAT", "float", "java.lang.Float.NaN","java.lang.Float.parseFloat(",")","java.lang.Float.toString(",")"));
\r
78 schemaTypes.put("decimal", new TypeEntry("L0.Double", "Bindings.DOUBLE", "double", "java.lang.Double.NaN","java.lang.Double.parseDouble(",")","java.lang.Double.toString(",")"));
\r
79 schemaTypes.put("boolean", new TypeEntry("L0.Boolean", "Bindings.BOOLEAN", "boolean", "false","java.lang.Boolean.parseBoolean(",")","java.lang.Boolean.toString(",")"));
\r
80 schemaTypes.put("integer", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
\r
81 schemaTypes.put("positiveInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
\r
82 schemaTypes.put("nonPositiveInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
\r
83 schemaTypes.put("nonNegativeInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
\r
84 schemaTypes.put("negativeInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
\r
85 schemaTypes.put("unsignedInt", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
\r
86 schemaTypes.put("int", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
\r
87 schemaTypes.put("short", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
\r
88 schemaTypes.put("unsignedShort",new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
\r
89 schemaTypes.put("byte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","java.lang.Byte.parseByte(",")","java.lang.Byte.toString(",")"));
\r
90 schemaTypes.put("unsignedByte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","java.lang.Byte.parseByte(",")","java.lang.Byte.toString(",")"));
\r
91 schemaTypes.put("long", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")","java.lang.Long.toString(",")"));
\r
92 schemaTypes.put("unsignedLong", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")","java.lang.Long.toString(",")"));
\r
93 schemaTypes.put("base64Binary", new TypeEntry("L0.ByteArray", "Bindings.BYTE_ARRAY", "byte[]", "new byte[0]","",".getBytes(org.simantics.databoard.util.binary.UTF8.CHARSET)","new java.lang.String(",", org.simantics.databoard.util.binary.UTF8.CHARSET)"));
\r
97 l0Types.put("doubleArray", new TypeEntry("L0.DoubleArray", "Bindings.DOUBLE_ARRAY", "double[]", null,null,null,"java.lang.Double.toString(",")"));
\r
98 l0Types.put("stringArray", new TypeEntry("L0.StringArray", "Bindings.STRING_ARRAY", "string[]", null,null,null,"",""));
\r
102 protected TypeEntry getTypeEntry(QName type) {
\r
103 Map<String,TypeEntry> types = typeMap.get(type.getNamespaceURI());
\r
106 TypeEntry entry = types.get(type.getLocalPart());
\r
109 protected TypeEntry getTypeEntry(String type) {
\r
110 for (Map<String,TypeEntry> types : typeMap.values()) {
\r
111 TypeEntry entry = types.get(type);
\r
118 protected String getL0TypeFromPrimitiveType(QName primitiveType) {
\r
119 TypeEntry entry = getTypeEntry(primitiveType);
\r
122 return entry.l0Type;
\r
125 protected String getL0Type(QName primitiveType) {
\r
126 String type = getL0TypeFromPrimitiveType(primitiveType);
\r
129 SchemaObject simpleTypeObj = simpleTypeName.get(primitiveType.getLocalPart());
\r
130 if (simpleTypeObj == null)
\r
132 SimpleType simpleType = simpleTypeObj.getSimpleType();
\r
133 while (simpleType != null) {
\r
134 QName base = simpleType.getRestriction().getBase();
\r
136 return getL0Type(base);
\r
137 simpleType = simpleType.getRestriction().getSimpleType();
\r
142 protected String getBindingFromPrimitiveType(QName primitiveType) {
\r
143 TypeEntry entry = getTypeEntry(primitiveType);
\r
146 return entry.binding;
\r
149 protected String getJavaTypeFromPrimitiveType(QName primitiveType) {
\r
150 TypeEntry entry = getTypeEntry(primitiveType);
\r
153 return entry.javaType;
\r
157 protected void handle(Schema schema) {
\r
158 this.schema = schema;
\r
161 for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) {
\r
162 if (attrs instanceof TopLevelAttribute) {
\r
163 handle((TopLevelAttribute)attrs);
\r
165 } else if (attrs instanceof TopLevelComplexType) {
\r
166 handleComplexType(complexTypes.get(attrs));
\r
167 } else if (attrs instanceof TopLevelElement) {
\r
168 handleElement(elements.get(attrs));
\r
169 } else if (attrs instanceof TopLevelSimpleType) {
\r
170 handleSimpleType(simpleTypes.get(attrs));
\r
171 } else if (attrs instanceof NamedAttributeGroup) {
\r
172 handle((NamedAttributeGroup)attrs);
\r
174 System.out.println(attrs.getClass().getName());
\r
179 protected Map<String,SchemaObject> elementName = new HashMap<String, SchemaObject>();
\r
180 protected Map<String,SchemaObject> complexTypeName = new HashMap<String, SchemaObject>();
\r
181 protected Map<String,SchemaObject> simpleTypeName = new HashMap<String, SchemaObject>();
\r
182 protected Map<Element,SchemaObject> elements = new HashMap<Element, SchemaObject>();
\r
183 protected Map<ComplexType,SchemaObject> complexTypes = new HashMap<ComplexType, SchemaObject>();
\r
184 protected Map<SimpleType,SchemaObject> simpleTypes = new HashMap<SimpleType, SchemaObject>();
\r
187 protected SchemaObject getWithName(SchemaObject referrer, String name) {
\r
188 SchemaObject obj = elementName.get(name);
\r
190 obj = complexTypeName.get(name);
\r
192 obj = simpleTypeName.get(name);
\r
194 throw new RuntimeException("Cannot locate referred type " + name + " when handling " + referrer.getName());
\r
199 protected SchemaObject getWithObj(SchemaObject referrer, OpenAttrs attrs) {
\r
200 SchemaObject obj = null;
\r
201 if (attrs instanceof Element)
\r
202 obj = elements.get(attrs);
\r
203 else if (attrs instanceof ComplexType)
\r
204 obj = complexTypes.get(attrs);
\r
205 else if (attrs instanceof SimpleType)
\r
206 obj = simpleTypes.get(attrs);
\r
208 throw new RuntimeException("Cannot locate referred object " + attrs + " when handling " + referrer.getName());
\r
213 private void preload() {
\r
214 Deque<SchemaObject> stack = new ArrayDeque<SchemaObject>();
\r
215 //stack.addAll(schema.getSimpleTypeOrComplexTypeOrGroup());
\r
216 for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) {
\r
217 if (attrs instanceof Element) {
\r
218 Element element = (Element)attrs;
\r
219 SchemaObject obj = new SchemaObject(element);
\r
220 obj.setRename(getRename(element));
\r
222 } else if (attrs instanceof ComplexType) {
\r
223 ComplexType complexType = (ComplexType)attrs;
\r
224 SchemaObject obj = new SchemaObject(complexType);
\r
225 obj.setRename(getRename(complexType));
\r
227 } else if (attrs instanceof SimpleType) {
\r
228 SimpleType simpleType = (SimpleType)attrs;
\r
229 SchemaObject obj = new SchemaObject(simpleType);
\r
234 while (!stack.isEmpty()) {
\r
235 SchemaObject object = stack.pop();
\r
236 if (object.getType() == ObjectType.COMPLEX_TYPE) {
\r
237 ComplexType ct = object.getComplexType();
\r
238 if (ct.getName() != null && ct.getName().length() > 0 && ct instanceof TopLevelComplexType)
\r
239 complexTypeName.put(ct.getName(), object);
\r
240 complexTypes.put(ct, object);
\r
241 if (ct.getChoice() != null) {
\r
242 preload(object,ct.getChoice(), stack);
\r
244 if (ct.getSequence() != null) {
\r
245 preload(object,ct.getSequence(), stack);
\r
247 if (ct.getAll() != null) {
\r
248 preload(object,ct.getAll(), stack);
\r
250 if (ct.getGroup() != null)
\r
251 throw new RuntimeException("Groups not supported");
\r
252 if (ct.getComplexContent() != null) {
\r
253 ComplexContent cc = ct.getComplexContent();
\r
254 ExtensionType extensionType = cc.getExtension();
\r
255 if (extensionType != null) {
\r
256 if (extensionType.getChoice() != null) {
\r
257 preload(object,extensionType.getChoice(), stack);
\r
259 if (extensionType.getSequence()!= null) {
\r
260 preload(object,extensionType.getSequence(), stack);
\r
262 if (extensionType.getAll()!= null) {
\r
263 preload(object,extensionType.getAll(), stack);
\r
265 if (extensionType.getGroup() != null)
\r
266 throw new RuntimeException("Groups not supported");
\r
269 if (ct.getSimpleContent() != null) {
\r
270 SimpleContent cc = ct.getSimpleContent();
\r
271 ExtensionType extensionType = cc.getExtension();
\r
272 if (extensionType != null) {
\r
273 if (extensionType.getChoice() != null) {
\r
274 preload(object,extensionType.getChoice(), stack);
\r
276 if (extensionType.getSequence()!= null) {
\r
277 preload(object,extensionType.getSequence(), stack);
\r
279 if (extensionType.getAll()!= null) {
\r
280 preload(object,extensionType.getAll(), stack);
\r
282 if (extensionType.getGroup() != null)
\r
283 throw new RuntimeException("Groups not supported");
\r
286 } else if (object.getType() == ObjectType.ELEMENT) {
\r
287 Element e = object.getElement();
\r
288 if (e instanceof TopLevelElement)
\r
289 elementName.put(e.getName(), object);
\r
290 elements.put(e, object);
\r
291 if (e.getComplexType() != null)
\r
292 stack.push(new SchemaObject(object,e.getComplexType()));
\r
293 if (e.getSimpleType() != null)
\r
294 stack.push(new SchemaObject(object,e.getSimpleType()));
\r
295 } else if (object.getType() == ObjectType.SIMPLE_TYPE) {
\r
296 SimpleType e = object.getSimpleType();
\r
297 if (e instanceof TopLevelSimpleType)
\r
298 simpleTypeName.put(e.getName(), object);
\r
299 simpleTypes.put(e, object);
\r
304 private void preload(SchemaObject parent,ExplicitGroup eg, Deque<SchemaObject> stack) {
\r
305 for (Object o : eg.getParticle()) {
\r
306 if (o instanceof JAXBElement<?>) {
\r
307 JAXBElement<?> element = (JAXBElement<?>)o;
\r
308 Object elemValue = element.getValue();
\r
309 if (elemValue instanceof Element) {
\r
310 SchemaObject obj = new SchemaObject(parent,(Element)elemValue);
\r
311 obj.setRename(getRename((Element)elemValue));
\r
313 } else if (elemValue instanceof All) {
\r
314 preload(parent,(All)elemValue, stack);
\r
315 } else if (elemValue instanceof ExplicitGroup) {
\r
316 preload(parent,(ExplicitGroup)elemValue, stack);
\r
318 throw new RuntimeException("Unknown ExplicitGroup element " + elemValue.getClass().getName());
\r
320 } else if (o instanceof Any){
\r
323 throw new RuntimeException("Unknown ExplicitGroup reference " + o.getClass().getName());
\r
328 protected void handle(TopLevelAttribute topLevelAttribute) {
\r
329 handle(null, topLevelAttribute);
\r
332 protected void handleSimpleType(SchemaObject topLevelSimpleType) {
\r
333 handleSimpleType(null,topLevelSimpleType);
\r
336 protected void handle(NamedAttributeGroup namedAttributeGroup){
\r
337 handle(null, namedAttributeGroup);
\r
340 protected QName getComplexTypeBase(ComplexType complexType) {
\r
341 if (complexType == null)
\r
343 ComplexContent complexContent = complexType.getComplexContent();
\r
344 if (complexContent != null) {
\r
345 ExtensionType extensionType = complexContent.getExtension();
\r
346 if (extensionType != null) {
\r
347 QName type = extensionType.getBase();
\r
351 // SimpleContent simpleContent = complexType.getSimpleContent();
\r
352 // if (simpleContent != null) {
\r
353 // ExtensionType extensionType = simpleContent.getExtension();
\r
354 // if (extensionType != null) {
\r
355 // QName type = extensionType.getBase();
\r
362 protected QName getSimpleTypeBase(SimpleType simpleType) {
\r
363 if (simpleType == null)
\r
365 return simpleType.getRestriction().getBase();
\r
368 protected QName getElementBase(Element element) {
\r
369 ComplexType complexType = element.getComplexType();
\r
370 SimpleType simpleType = element.getSimpleType();
\r
371 if (complexType != null)
\r
372 return getComplexTypeBase(complexType);
\r
373 if (simpleType != null) {
\r
374 return getSimpleTypeBase(simpleType);
\r
386 private void handleAttributes(SchemaObject complexType, List<Annotated> attributeOrAttributeGroup) {
\r
387 //name = getComplexTypePrefix()+complexType.getName()
\r
389 Set<Annotated> handled = handleAttributeCompositions(complexType,attributeOrAttributeGroup);
\r
390 for (Annotated annotated : attributeOrAttributeGroup) {
\r
391 if (handled.contains(annotated))
\r
393 if (annotated instanceof Attribute) {
\r
394 handle(complexType,(Attribute)annotated);
\r
395 } else if (annotated instanceof AttributeGroup){
\r
396 handle(complexType,(AttributeGroup)annotated);
\r
397 //comment("AttributeGroup " + ((AttributeGroup)annotated).getRef().getLocalPart());
\r
399 throw new RuntimeException();
\r
404 protected abstract void handleAttributes(SchemaObject simpleTypeObj);
\r
406 protected void handleExtensionAttributes(SchemaObject complexType) {
\r
407 ComplexContent complexContent = complexType.getComplexType().getComplexContent();
\r
408 if (complexContent != null) {
\r
409 ExtensionType extensionType = complexContent.getExtension();
\r
410 if (extensionType != null) {
\r
411 handleAttributes(complexType, extensionType.getAttributeOrAttributeGroup());
\r
414 SimpleContent simpleContent = complexType.getComplexType().getSimpleContent();
\r
415 if (simpleContent != null) {
\r
416 ExtensionType extensionType = simpleContent.getExtension();
\r
417 if (extensionType != null) {
\r
418 handleAttributes(complexType, extensionType.getAttributeOrAttributeGroup());
\r
425 protected void handleComplexTypeAttributes(SchemaObject complexType) {
\r
426 handleAttributes(complexType,complexType.getComplexType().getAttributeOrAttributeGroup());
\r
429 protected void handleElementComplexTypeAttributes(SchemaObject complexType) {
\r
430 if (complexType != null) {
\r
431 handleComplexTypeAttributes(complexType);
\r
432 handleExtensionAttributes(complexType);
\r
436 protected void handleElementSimpleTypeAttributes(SchemaObject simpleType) {
\r
437 if (simpleType != null) {
\r
438 handleAttributes(simpleType);
\r
442 protected Set<Annotated> handleAttributeCompositions(SchemaObject obj, List<Annotated> attributeOrAttributeGroup) {
\r
444 Set<Annotated> handled = new HashSet<Annotated>();
\r
445 for (JAXBElement<?> e : configuration.getConversionRule()) {
\r
446 if (e.getValue() instanceof AttributeComposition) {
\r
447 AttributeComposition composition = (AttributeComposition)e.getValue();
\r
448 if (composition.getAttribute().size() < 2)
\r
449 throw new RuntimeException("Attribute Composition is not valid");
\r
450 BijectionMap<org.simantics.xml.sax.configuration.Attribute, Annotated> map = new BijectionMap<org.simantics.xml.sax.configuration.Attribute, Annotated>();
\r
451 for (org.simantics.xml.sax.configuration.Attribute a : composition.getAttribute()) {
\r
452 for (Annotated annotated : attributeOrAttributeGroup) {
\r
453 if (annotated instanceof Attribute) {
\r
454 Attribute attribute = (Attribute)annotated;
\r
455 QName type = getBaseType(attribute);
\r
456 if (a.getName().equals(attribute.getName()) && type != null && a.getType().equals(type.getLocalPart())) {
\r
457 map.map(a, attribute);
\r
462 if (composition.getAttribute().size() == map.size()) {
\r
463 handled.addAll(map.getRightSet());
\r
464 handleAttributeComposition(obj, composition, map);
\r
471 protected QName getBaseType(Attribute attribute) {
\r
472 if (attribute.getType() != null)
\r
473 return attribute.getType();
\r
474 if (attribute.getRef() != null)
\r
475 return attribute.getRef();
\r
476 SimpleType simpleType = attribute.getSimpleType();
\r
477 if (simpleType != null) {
\r
478 Restriction restriction = simpleType.getRestriction();
\r
479 if (restriction != null)
\r
480 if (restriction.getBase() != null)
\r
481 return restriction.getBase();
\r
486 protected QName getPrimitiveType(Attribute attribute) {
\r
487 QName type = getBaseType(attribute);
\r
488 String b = getBindingFromPrimitiveType(type);
\r
489 while (b==null && type != null) {
\r
490 SchemaObject baseType = simpleTypeName.get(type.getLocalPart());
\r
491 if (baseType != null) {
\r
492 Restriction restriction = baseType.getSimpleType().getRestriction();
\r
493 if (restriction != null)
\r
494 if (restriction.getBase() != null) {
\r
495 type = restriction.getBase();
\r
496 b = getBindingFromPrimitiveType(type);
\r
503 protected Attribute getRefAttribute(QName ref) {
\r
504 for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) {
\r
505 if (attrs instanceof TopLevelAttribute) {
\r
506 TopLevelAttribute attribute = (TopLevelAttribute)attrs;
\r
507 if (attribute.getName().equals(ref.getLocalPart()))
\r
514 protected abstract void handleAttributeComposition(SchemaObject obj, AttributeComposition composition, BijectionMap<org.simantics.xml.sax.configuration.Attribute, Annotated> attributes);
\r
519 protected void handleComplexType(SchemaObject complexType) {
\r
520 handleComplexTypeAttributes(complexType);
\r
521 handleComplexTypeExtension(complexType);
\r
522 handleExtensionAttributes(complexType);
\r
525 protected enum RefType{Element,Reference,Type};
\r
527 protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType);
\r
528 protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any);
\r
529 protected abstract void handle(SchemaObject parent, SchemaElement indicator, List<SchemaElement> elements);
\r
531 protected void handle(SchemaObject parent, ExplicitGroup eg, SchemaElement.ElementType indicator) {
\r
532 handle(parent, new SchemaElement(eg, indicator));
\r
535 protected void handle(SchemaObject parent, SchemaElement indicator) {
\r
538 List<SchemaElement> elements = new ArrayList<SchemaElement>();
\r
539 List<SchemaElement> choices = new ArrayList<SchemaElement>();
\r
540 List<SchemaElement> sequences = new ArrayList<SchemaElement>();
\r
541 List<SchemaElement> alls = new ArrayList<SchemaElement>();
\r
542 List<SchemaElement> anys = new ArrayList<SchemaElement>();
\r
544 for (Object o : indicator.getGroup().getParticle()) {
\r
545 if (o instanceof JAXBElement<?>) {
\r
546 JAXBElement<?> element = (JAXBElement<?>)o;
\r
547 Object elemValue = element.getValue();
\r
548 if (elemValue instanceof LocalElement) {
\r
549 LocalElement localElement = (LocalElement)elemValue;
\r
550 elements.add(new SchemaElement(indicator,localElement, ElementType.ELEMENT));
\r
551 } else if (elemValue instanceof All) {
\r
552 alls.add(new SchemaElement(indicator,(All)elemValue, ElementType.ALL));
\r
553 } else if (elemValue instanceof ExplicitGroup) {
\r
554 QName qname = element.getName();
\r
555 if ("choice".equals(qname.getLocalPart())) {
\r
556 choices.add(new SchemaElement(indicator,(ExplicitGroup)elemValue, ElementType.CHOICE));
\r
557 } else if ("sequence".equals(qname.getLocalPart())) {
\r
558 sequences.add(new SchemaElement(indicator,(ExplicitGroup)elemValue, ElementType.SEQUENCE));
\r
561 throw new RuntimeException("Unknown ExplicitGroup element " + elemValue.getClass().getName());
\r
563 } else if (o instanceof Any){
\r
564 anys.add(new SchemaElement(indicator,(Any)o, ElementType.ANY));
\r
566 throw new RuntimeException("Unknown ExplicitGroup reference " + o.getClass().getName());
\r
570 if (elements.size() == 0 && choices.size() == 0 && sequences.size() == 0 && alls.size() == 0 && anys.size() == 0) {
\r
574 if (indicator.getType() == SchemaElement.ElementType.SEQUENCE) {
\r
575 if (indicator.getRestriction().single()) {
\r
576 if (elements.size() > 0) {
\r
577 for (SchemaElement e : sequences) {
\r
580 for (SchemaElement c : choices) {
\r
584 for (SchemaElement c : alls) {
\r
587 handle(parent, indicator, elements);
\r
588 for (SchemaElement a : anys) {
\r
589 handleIndicator(parent, indicator, a);
\r
592 if (sequences.size() > 0) {
\r
593 throw new RuntimeException("Cannot handle Sequence with inner Sequences");
\r
595 for (SchemaElement c : choices) {
\r
598 for (SchemaElement a : anys) {
\r
599 handleIndicator(parent, indicator, a);
\r
603 if (sequences.size() > 0 || choices.size() > 0 || alls.size() > 0) {
\r
604 throw new RuntimeException("Cannot handle Sequence with inner ExplicitGroups");
\r
606 handle(parent, indicator, elements);
\r
607 for (SchemaElement a : anys) {
\r
608 handleIndicator(parent, indicator, a);
\r
612 } else if (indicator.getType() == SchemaElement.ElementType.CHOICE){
\r
613 if (indicator.getRestriction().single()) {
\r
614 if (sequences.size()> 0 || choices.size() > 0 || alls.size() > 0 || anys.size() > 0) {
\r
615 throw new RuntimeException("Cannot handle Choice that contains something else than Elements");
\r
617 handle(parent, indicator, elements);
\r
620 if (sequences.size() > 0 || choices.size() > 0) {
\r
621 throw new RuntimeException("Cannot handle Choice with inner ExplicitGroups");
\r
623 handle(parent, indicator, elements);
\r
624 for (SchemaElement a : anys) {
\r
625 handleIndicator(parent, indicator, a);
\r
628 } else if (indicator.getType() == ElementType.ALL) {
\r
629 if (sequences.size()> 0 || choices.size() > 0 || alls.size() > 0 || anys.size() > 0) {
\r
630 throw new RuntimeException("Cannot handle All that contains something else than Elements");
\r
632 if (!indicator.getRestriction().single()) {
\r
633 throw new RuntimeException("All indicator must have maxOccurs=1");
\r
635 handle(parent, indicator, elements);
\r
640 protected void handle(SchemaObject parent, SchemaElement indicator, SchemaElement element) {
\r
641 Element localElement = element.getElement();
\r
642 if (localElement.getName() != null) {
\r
643 SchemaObject eObj = elements.get(localElement); // FIXME: handleIndicator should be refactored, not this methdof must be overridden in JavaGenerator to handle renaming of Elements properly
\r
644 //String refName = eObj.getName();//localElement.getName();
\r
645 QName refType = localElement.getType();
\r
646 if (refType != null)
\r
647 //handleIndicator(parent, indicator, element, false, refName, refType);
\r
648 handleIndicator(parent, indicator, element, null, RefType.Type);
\r
650 handleElement(eObj);
\r
651 //handleIndicator(parent, indicator, element, false, refName, localElement);
\r
652 handleIndicator(parent, indicator, element, null, RefType.Element);
\r
654 } else if (localElement.getRef() != null) {
\r
655 //QName refType = localElement.getRef();
\r
656 //handleIndicator(parent, indicator,element, true, refType.getLocalPart(), refType);
\r
657 handleIndicator(parent, indicator,element, null, RefType.Reference);
\r
661 protected String getElementName(Element localElement) {
\r
662 if (localElement.getName() != null) {
\r
663 String refName = localElement.getName();
\r
664 QName refType = localElement.getType();
\r
665 if (refType != null)
\r
667 } else if (localElement.getRef() != null) {
\r
668 QName refType = localElement.getRef();
\r
669 if (refType != null)
\r
670 return refType.getLocalPart();
\r
675 protected String getChoiceName(List<SchemaElement> elements) {
\r
676 if (elements.size() == 1) {
\r
677 return getElementName(elements.get(0).getElement());
\r
679 List<String> names = new ArrayList<String>();
\r
680 for (SchemaElement e : elements) {
\r
681 String name = getElementName(e.getElement());
\r
686 for (int i = 0; i < names.size(); i++) {
\r
688 name = names.get(i);
\r
690 name += "Or"+names.get(i);
\r
696 protected abstract void handle(SchemaObject parent, Attribute attribute) ;
\r
697 protected abstract void handle(SchemaObject parent, AttributeGroup attribute) ;
\r
699 protected abstract void handleSimpleType(SchemaObject parent, SchemaObject simpleType);
\r
703 protected void handleComplexTypeExtension(SchemaObject complexTypeObj) {
\r
704 ComplexType complexType = complexTypeObj.getComplexType();
\r
705 if (complexType != null) {
\r
706 if (complexType.getChoice() != null)
\r
707 handle(complexTypeObj, complexType.getChoice(), SchemaElement.ElementType.CHOICE);
\r
708 if (complexType.getSequence() != null)
\r
709 handle(complexTypeObj, complexType.getSequence(), SchemaElement.ElementType.SEQUENCE);
\r
710 if (complexType.getAll() != null)
\r
711 handle(complexTypeObj, complexType.getAll(), SchemaElement.ElementType.ALL);
\r
712 if (complexType.getGroup() != null)
\r
713 throw new RuntimeException("Groups not supported");
\r
714 ComplexContent complexContent = complexType.getComplexContent();
\r
715 if (complexContent != null) {
\r
716 ExtensionType extensionType = complexContent.getExtension();
\r
717 if (extensionType != null) {
\r
718 if (extensionType.getChoice() != null) {
\r
719 handle(complexTypeObj, extensionType.getChoice(), SchemaElement.ElementType.CHOICE);
\r
721 if (extensionType.getSequence()!= null) {
\r
722 handle(complexTypeObj, extensionType.getSequence(), SchemaElement.ElementType.SEQUENCE);
\r
724 if (extensionType.getAll()!= null) {
\r
725 handle(complexTypeObj, extensionType.getAll(), SchemaElement.ElementType.ALL);
\r
727 if (extensionType.getGroup() != null) {
\r
728 throw new RuntimeException("Groups not supported");
\r
732 // SimpleContent simpleContent = complexType.getSimpleContent();
\r
733 // if (simpleContent != null) {
\r
734 // ExtensionType extensionType = simpleContent.getExtension();
\r
739 protected void handleElement(SchemaObject topLevelElement) {
\r
740 LocalComplexType complexType = topLevelElement.getElement().getComplexType();
\r
742 if (complexType != null) {
\r
743 SchemaObject complextTypeObj = complexTypes.get(complexType);
\r
744 handleElementComplexTypeAttributes(complextTypeObj);
\r
745 handleComplexTypeExtension(complextTypeObj);
\r
752 protected boolean isElementRef(String ref) {
\r
753 return elementName.containsKey(ref);
\r
756 protected boolean isComplexTypeRef(String ref) {
\r
757 return complexTypeName.containsKey(ref);
\r
760 protected boolean isSimpleTypeRef(String ref) {
\r
761 return simpleTypeName.containsKey(ref);
\r
764 protected NamedAttributeGroup getAttributeGroup(String name) {
\r
765 for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) {
\r
766 if (attrs instanceof NamedAttributeGroup) {
\r
767 NamedAttributeGroup group = (NamedAttributeGroup)attrs;
\r
768 if (group.getName().equals(name))
\r
775 protected IDProvider getIDProvider(Element element) {
\r
776 List<IDProvider> idProviders = new ArrayList<IDProvider>(2);
\r
777 for (JAXBElement<?> e : configuration.getConversionRule()) {
\r
778 if (e.getValue() instanceof IDProvider) {
\r
779 IDProvider ref = (IDProvider)e.getValue();
\r
780 org.simantics.xml.sax.configuration.Element element2 = ref.getElement();
\r
781 if (element2 != null) {
\r
782 if (element.getName().equals(element2.getName()))
\r
783 idProviders.add(ref);
\r
788 if (idProviders.size() == 0)
\r
790 if (idProviders.size() > 1)
\r
791 throw new RuntimeException("Element " + element.getName() + " contains " + idProviders.size() + " id provider rules, only one is allowed.");
\r
792 return idProviders.get(0);
\r
795 protected IDProvider getIDProvider(ComplexType complexType) {
\r
796 List<IDProvider> idProviders = new ArrayList<IDProvider>(2);
\r
797 for (JAXBElement<?> e : configuration.getConversionRule()) {
\r
798 if (e.getValue() instanceof IDProvider) {
\r
799 IDProvider ref = (IDProvider)e.getValue();
\r
800 org.simantics.xml.sax.configuration.ComplexType complexType2 = ref.getComplexType();
\r
801 if (complexType2 != null) {
\r
802 if (complexType.getName().equals(complexType2.getName()))
\r
803 idProviders.add(ref);
\r
808 if (idProviders.size() == 0)
\r
810 if (idProviders.size() > 1)
\r
811 throw new RuntimeException("Element " + complexType.getName() + " contains " + idProviders.size() + " id provider rules, only one is allowed.");
\r
812 return idProviders.get(0);
\r
815 protected List<IDReference> getIDReferences(Element element) {
\r
816 List<IDReference> idReferences = new ArrayList<IDReference>(2);
\r
817 for (JAXBElement<?> e : configuration.getConversionRule()) {
\r
818 if (e.getValue() instanceof IDReference) {
\r
819 IDReference ref = (IDReference)e.getValue();
\r
820 org.simantics.xml.sax.configuration.Element element2 = ref.getElement();
\r
821 if (element2 != null) {
\r
822 if (element.getName().equals(element2.getName()))
\r
823 idReferences.add(ref);
\r
827 return idReferences;
\r
830 protected List<IDReference> getIDReferences(ComplexType complexType) {
\r
831 List<IDReference> idReferences = new ArrayList<IDReference>(2);
\r
832 for (JAXBElement<?> e : configuration.getConversionRule()) {
\r
833 if (e.getValue() instanceof IDReference) {
\r
834 IDReference ref = (IDReference)e.getValue();
\r
835 org.simantics.xml.sax.configuration.ComplexType complexType2 = ref.getComplexType();
\r
836 if (complexType2 != null) {
\r
837 if (complexType.getName().equals(complexType2.getName()))
\r
838 idReferences.add(ref);
\r
842 return idReferences;
\r
845 public UnrecognizedChildElement getUnknown(ComplexType complexType) {
\r
846 for (JAXBElement<?> e : configuration.getConversionRule()) {
\r
847 if (e.getValue() instanceof UnrecognizedChildElement) {
\r
848 UnrecognizedChildElement rule = (UnrecognizedChildElement)e.getValue();
\r
849 org.simantics.xml.sax.configuration.ComplexType complexType2 = rule.getComplexType();
\r
850 if (complexType2 != null) {
\r
851 if (complexType.getName().equals(complexType2.getName()))
\r
859 public UnrecognizedChildElement getUnknown(Element element) {
\r
860 for (JAXBElement<?> e : configuration.getConversionRule()) {
\r
861 if (e.getValue() instanceof UnrecognizedChildElement) {
\r
862 UnrecognizedChildElement rule = (UnrecognizedChildElement)e.getValue();
\r
863 org.simantics.xml.sax.configuration.Element element2 = rule.getElement();
\r
864 if (element2 != null) {
\r
865 if (element.getName().equals(element2.getName()))
\r
873 public Rename getRename(Attribute element) {
\r
874 for (JAXBElement<?> e : configuration.getConversionRule()) {
\r
875 if (e.getValue() instanceof Rename) {
\r
876 Rename rule = (Rename)e.getValue();
\r
877 Object ref = rule.getElementOrComplexTypeOrAttribute().get(0);
\r
878 if (!(ref instanceof org.simantics.xml.sax.configuration.Attribute))
\r
880 org.simantics.xml.sax.configuration.Attribute element2 = (org.simantics.xml.sax.configuration.Attribute)ref;
\r
881 if (element2.getName().equals(element.getName())) {
\r
889 public Rename getRename(ComplexType element) {
\r
890 for (JAXBElement<?> e : configuration.getConversionRule()) {
\r
891 if (e.getValue() instanceof Rename) {
\r
892 Rename rule = (Rename)e.getValue();
\r
893 Object ref = rule.getElementOrComplexTypeOrAttribute().get(0);
\r
894 if (!(ref instanceof org.simantics.xml.sax.configuration.ComplexType))
\r
896 org.simantics.xml.sax.configuration.ComplexType element2 = (org.simantics.xml.sax.configuration.ComplexType)ref;
\r
897 if (element2.getName().equals(element.getName())) {
\r
905 public Rename getRename(Element element) {
\r
906 for (JAXBElement<?> e : configuration.getConversionRule()) {
\r
907 if (e.getValue() instanceof Rename) {
\r
908 Rename rule = (Rename)e.getValue();
\r
909 Object ref = rule.getElementOrComplexTypeOrAttribute().get(0);
\r
910 if (!(ref instanceof org.simantics.xml.sax.configuration.Element))
\r
912 org.simantics.xml.sax.configuration.Element element2 = (org.simantics.xml.sax.configuration.Element)ref;
\r
913 if (element2.getName().equals(element.getName())) {
\r
922 public boolean useOriginalList(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String ref, QName refType) {
\r
923 if (parent.getName() == null)
\r
924 parent = parent.getParent();
\r
925 if (parent.getName().contains("PipingNetworkSegment"))
\r
926 System.out.println();
\r
927 for (JAXBElement<?> e : configuration.getConversionRule()) {
\r
928 if (e.getValue() instanceof OrderedChild) {
\r
929 OrderedChild oc = (OrderedChild)e.getValue();
\r
930 org.simantics.xml.sax.configuration.Element element2 = oc.getElement();
\r
931 org.simantics.xml.sax.configuration.ComplexType complexType = oc.getComplexType();
\r
932 org.simantics.xml.sax.configuration.Element child = oc.getChild();
\r
933 if (!oc.getType().equals("original"))
\r
935 boolean match = false;
\r
936 if (element2 != null) {
\r
937 if (parent.getType() == ObjectType.ELEMENT && parent.getName().equals(element2.getName())) {
\r
940 } else if (complexType != null) {
\r
941 if (parent.getType() == ObjectType.COMPLEX_TYPE && parent.getName() != null && parent.getName().equals(complexType.getName())) {
\r
947 if (child != null) {
\r
948 if (matchChild(child, ref, refType)) {
\r
949 if (oc.getValue().equals("disable"))
\r
954 if (oc.getValue().equals("disable"))
\r
962 return indicator.order();
\r
965 public boolean useElementList(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String refName, QName refType) {
\r
966 if (parent.getName() == null)
\r
967 parent = parent.getParent();
\r
968 if (parent.getName() == "PipingNetworkSegment")
\r
969 System.out.println();
\r
970 for (JAXBElement<?> e : configuration.getConversionRule()) {
\r
971 if (e.getValue() instanceof OrderedChild) {
\r
972 OrderedChild oc = (OrderedChild)e.getValue();
\r
973 org.simantics.xml.sax.configuration.Element element2 = oc.getElement();
\r
974 org.simantics.xml.sax.configuration.ComplexType complexType = oc.getComplexType();
\r
975 org.simantics.xml.sax.configuration.Element child = oc.getChild();
\r
976 if (!oc.getType().equals("child"))
\r
978 boolean match = false;
\r
979 if (element2 != null) {
\r
980 if (parent.getType() == ObjectType.ELEMENT && parent.getName().equals(element2.getName())) {
\r
983 } else if (complexType != null) {
\r
984 if (parent.getType() == ObjectType.COMPLEX_TYPE && parent.getName() != null && parent.getName().equals(complexType.getName())) {
\r
990 if (child != null) {
\r
991 if (matchChild(child, refName, refType)) {
\r
992 if (oc.getValue().equals("disable"))
\r
997 if (oc.getValue().equals("disable"))
\r
1005 return element.many() && element.order();
\r
1008 private boolean matchChild(org.simantics.xml.sax.configuration.Element child, String refName, QName refType) {
\r
1009 if (refType != null && refType.getLocalPart().equals(child.getName()))
\r
1011 if (refName != null && refName.equals(child.getName()))
\r
1016 public static class TypeEntry {
\r
1020 String defaultValue;
\r
1022 String getterPrefix;
\r
1023 String getterPostfix;
\r
1024 String stringPrefix;
\r
1025 String stringPostfix;
\r
1026 public TypeEntry(String l0Type, String binding, String javaType, String defaultValue, String getterPrefix, String getterPostfix, String stringPrefix, String stringPostfix) {
\r
1028 this.l0Type = l0Type;
\r
1029 this.binding = binding;
\r
1030 this.javaType = javaType;
\r
1031 this.defaultValue = defaultValue;
\r
1033 this.getterPrefix = getterPrefix;
\r
1034 this.getterPostfix = getterPostfix;
\r
1035 this.stringPrefix = stringPrefix;
\r
1036 this.stringPostfix = stringPostfix;
\r
1039 public TypeEntry(String l0Type, String binding, String javaType, String defaultValue, String getterPrefix, String getterPostfix, String stringPrefix, String stringPostfix, boolean id) {
\r
1041 this.l0Type = l0Type;
\r
1042 this.binding = binding;
\r
1043 this.javaType = javaType;
\r
1044 this.defaultValue = defaultValue;
\r
1046 this.getterPrefix = getterPrefix;
\r
1047 this.getterPostfix = getterPostfix;
\r
1048 this.stringPrefix = stringPrefix;
\r
1049 this.stringPostfix = stringPostfix;
\r
1052 public String getValueGetterMethod(String name) {
\r
1053 return getterPrefix + name + ".getValue()"+getterPostfix;
\r
1055 public String getValueGetter(String name) {
\r
1056 return getterPrefix + name+getterPostfix;
\r
1058 public String getValueGetter()
\r
1060 return getValueGetter("value");
\r
1063 public String getToString(String name) {
\r
1064 return stringPrefix +"("+javaType+")"+name+stringPostfix;
\r
1067 public String getElementToString(String name) {
\r
1068 return stringPrefix + name+stringPostfix;
\r