1 package org.simantics.xml.sax;
3 import java.util.ArrayDeque;
4 import java.util.ArrayList;
5 import java.util.Deque;
6 import java.util.HashMap;
7 import java.util.HashSet;
12 import javax.xml.bind.JAXBElement;
13 import javax.xml.namespace.QName;
15 import org.simantics.utils.datastructures.BijectionMap;
16 import org.simantics.xml.sax.SchemaElement.ElementType;
17 import org.simantics.xml.sax.SchemaObject.ObjectType;
18 import org.simantics.xml.sax.configuration.AttributeComposition;
19 import org.simantics.xml.sax.configuration.Configuration;
20 import org.simantics.xml.sax.configuration.IDProvider;
21 import org.simantics.xml.sax.configuration.IDReference;
22 import org.simantics.xml.sax.configuration.OrderedChild;
23 import org.simantics.xml.sax.configuration.Rename;
24 import org.simantics.xml.sax.configuration.UnrecognizedChildElement;
25 import org.w3._2001.xmlschema.All;
26 import org.w3._2001.xmlschema.Annotated;
27 import org.w3._2001.xmlschema.Any;
28 import org.w3._2001.xmlschema.Attribute;
29 import org.w3._2001.xmlschema.AttributeGroup;
30 import org.w3._2001.xmlschema.ComplexContent;
31 import org.w3._2001.xmlschema.ComplexType;
32 import org.w3._2001.xmlschema.Element;
33 import org.w3._2001.xmlschema.ExplicitGroup;
34 import org.w3._2001.xmlschema.ExtensionType;
35 import org.w3._2001.xmlschema.GroupRef;
36 import org.w3._2001.xmlschema.LocalElement;
37 import org.w3._2001.xmlschema.NamedAttributeGroup;
38 import org.w3._2001.xmlschema.NamedGroup;
39 import org.w3._2001.xmlschema.OpenAttrs;
40 import org.w3._2001.xmlschema.RealGroup;
41 import org.w3._2001.xmlschema.Restriction;
42 import org.w3._2001.xmlschema.Schema;
43 import org.w3._2001.xmlschema.SimpleContent;
44 import org.w3._2001.xmlschema.SimpleType;
45 import org.w3._2001.xmlschema.TopLevelAttribute;
46 import org.w3._2001.xmlschema.TopLevelComplexType;
47 import org.w3._2001.xmlschema.TopLevelElement;
48 import org.w3._2001.xmlschema.TopLevelSimpleType;
49 import org.w3._2001.xmlschema.Union;
51 public final class SchemaConversionBase {
53 protected Schema schema;
54 protected SchemaConverter converter;
55 protected SchemaConversionComponent component;
56 protected Configuration configuration;
58 public static final String SCHEMA_NS = "http://www.w3.org/2001/XMLSchema";
59 public static final String CONVERSION_NS = "http://www.simantics.org/Layer0";
61 protected Map<String,Map<String,TypeEntry>> typeMap;
63 protected String ontologyURI;
64 protected String className;
66 public SchemaConversionBase(SchemaConverter converter, String ontologyUri, String className) {
67 this.converter = converter;
68 this.configuration = converter.getConfiguration();
69 this.ontologyURI = ontologyUri;
70 this.className = className;
75 protected void initTypes() {
76 typeMap = new HashMap<String, Map<String,TypeEntry>>();
77 Map<String,TypeEntry> schemaTypes = new HashMap<String, SchemaConversionBase.TypeEntry>();
78 typeMap.put(SCHEMA_NS, schemaTypes);
79 Map<String,TypeEntry> l0Types = new HashMap<String, SchemaConversionBase.TypeEntry>();
80 typeMap.put(CONVERSION_NS, l0Types);
82 schemaTypes.put("string", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","",""));
83 schemaTypes.put("NMTOKEN", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","",""));
84 schemaTypes.put("token", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","",""));
85 schemaTypes.put("ID", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","",true));
86 schemaTypes.put("IDREF", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","",""));
87 schemaTypes.put("Name", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","",""));
88 schemaTypes.put("NCName", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","",""));
89 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()"));
90 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()"));
91 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()"));
92 schemaTypes.put("gYearMonth", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","",""));
93 schemaTypes.put("gYear", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","",""));
94 schemaTypes.put("gMonth", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","",""));
95 schemaTypes.put("gMonthDay", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","",""));
96 schemaTypes.put("anyURI", new TypeEntry("L0.URI", "Bindings.STRING", "java.lang.String", "","","","",""));
97 schemaTypes.put("double", new TypeEntry("L0.Double", "Bindings.DOUBLE", "double", "java.lang.Double.NaN","java.lang.Double.parseDouble(",")","java.lang.Double.toString(",")"));
98 schemaTypes.put("float", new TypeEntry("L0.Float", "Bindings.FLOAT", "float", "java.lang.Float.NaN","java.lang.Float.parseFloat(",")","java.lang.Float.toString(",")"));
99 schemaTypes.put("decimal", new TypeEntry("L0.Double", "Bindings.DOUBLE", "double", "java.lang.Double.NaN","java.lang.Double.parseDouble(",")","java.lang.Double.toString(",")"));
100 schemaTypes.put("boolean", new TypeEntry("L0.Boolean", "Bindings.BOOLEAN", "boolean", "false","java.lang.Boolean.parseBoolean(",")","java.lang.Boolean.toString(",")"));
101 schemaTypes.put("integer", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
102 schemaTypes.put("positiveInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
103 schemaTypes.put("nonPositiveInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
104 schemaTypes.put("nonNegativeInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
105 schemaTypes.put("negativeInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
106 schemaTypes.put("unsignedInt", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
107 schemaTypes.put("int", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
108 schemaTypes.put("short", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
109 schemaTypes.put("unsignedShort",new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));
110 schemaTypes.put("byte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","java.lang.Byte.parseByte(",")","java.lang.Byte.toString(",")"));
111 schemaTypes.put("unsignedByte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","java.lang.Byte.parseByte(",")","java.lang.Byte.toString(",")"));
112 schemaTypes.put("long", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")","java.lang.Long.toString(",")"));
113 schemaTypes.put("unsignedLong", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")","java.lang.Long.toString(",")"));
114 schemaTypes.put("base64Binary", new TypeEntry("L0.ByteArray", "Bindings.BYTE_ARRAY", "byte[]", "new byte[0]","java.util.Base64.getDecoder().decode(",".replaceAll(\"\\n\", \"\").getBytes(java.nio.charset.StandardCharsets.UTF_8))","java.util.Base64.getEncoder().encodeToString(",")"));
116 l0Types.put("doubleArray", new TypeEntry("L0.DoubleArray", "Bindings.DOUBLE_ARRAY", "double[]", null,null,null,"java.lang.Double.toString(",")"));
117 l0Types.put("stringArray", new TypeEntry("L0.StringArray", "Bindings.STRING_ARRAY", "string[]", null,null,null,"",""));
120 protected TypeEntry getTypeEntry(QName type) {
121 Map<String,TypeEntry> types = typeMap.get(type.getNamespaceURI());
124 TypeEntry entry = types.get(type.getLocalPart());
127 protected TypeEntry getTypeEntry(String type) {
128 for (Map<String,TypeEntry> types : typeMap.values()) {
129 TypeEntry entry = types.get(type);
136 protected String getL0TypeFromPrimitiveType(QName primitiveType) {
137 TypeEntry entry = getTypeEntry(primitiveType);
143 protected String getL0Type(QName primitiveType) {
144 String type = getL0TypeFromPrimitiveType(primitiveType);
147 SchemaObject simpleTypeObj = simpleTypeName.get(primitiveType.getLocalPart());
148 if (simpleTypeObj == null)
150 SimpleType simpleType = simpleTypeObj.getSimpleType();
151 while (simpleType != null) {
152 QName base = simpleType.getRestriction().getBase();
154 return getL0Type(base);
155 simpleType = simpleType.getRestriction().getSimpleType();
160 protected String getBindingFromPrimitiveType(QName primitiveType) {
161 TypeEntry entry = getTypeEntry(primitiveType);
164 return entry.binding;
167 protected String getJavaTypeFromPrimitiveType(QName primitiveType) {
168 TypeEntry entry = getTypeEntry(primitiveType);
171 return entry.javaType;
175 public void init(Schema schema) {
176 this.schema = schema;
181 public void handle(SchemaConversionComponent component) {
182 this.component = component;
183 for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) {
184 if (attrs instanceof TopLevelAttribute) {
185 handle((TopLevelAttribute)attrs);
187 } else if (attrs instanceof TopLevelComplexType) {
188 handleComplexType(complexTypes.get(attrs));
189 } else if (attrs instanceof TopLevelElement) {
190 handleElement(elements.get(attrs));
191 } else if (attrs instanceof TopLevelSimpleType) {
192 handleSimpleType(simpleTypes.get(attrs));
193 } else if (attrs instanceof NamedAttributeGroup) {
194 handle((NamedAttributeGroup)attrs);
195 } else if (attrs instanceof NamedGroup) {
196 handle((NamedGroup)attrs);
198 System.out.println(attrs.getClass().getName());
203 private Map<String,SchemaObject> elementName = new HashMap<>();
204 private Map<String,SchemaObject> complexTypeName = new HashMap<>();
205 private Map<String,SchemaObject> simpleTypeName = new HashMap<>();
206 private Map<String,SchemaObject> modelGroupName = new HashMap<>();
207 private Map<String,SchemaObject> attrName = new HashMap<>();
208 private Map<Element,SchemaObject> elements = new HashMap<>();
209 private Map<ComplexType,SchemaObject> complexTypes = new HashMap<>();
210 private Map<SimpleType,SchemaObject> simpleTypes = new HashMap<>();
211 private Map<NamedGroup,SchemaObject> modelGroups = new HashMap<>();
212 private Map<Attribute,SchemaObject> attributes = new HashMap<>();
214 private SchemaObject _getWithName(QName name) {
215 SchemaObject obj = elementName.get(name.getLocalPart());
217 obj = complexTypeName.get(name.getLocalPart());
219 obj = simpleTypeName.get(name.getLocalPart());
221 obj = attrName.get(name.getLocalPart());
225 protected SchemaObject getWithName(QName name) {
226 SchemaObject obj = _getWithName(name);
229 if (name.getNamespaceURI() != null) {
230 for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) {
231 if (sc.base != null) {
232 obj = sc.base._getWithName(name);
242 private NamedAttributeGroup _getAttributeGroup(QName name) {
243 for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) {
244 if (attrs instanceof NamedAttributeGroup) {
245 NamedAttributeGroup group = (NamedAttributeGroup)attrs;
246 if (group.getName().equals(name.getLocalPart()))
253 public NamedAttributeGroup getAttributeGroup(QName name) {
254 NamedAttributeGroup group = _getAttributeGroup(name);
257 if (name.getNamespaceURI() != null) {
258 for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) {
259 if (sc.base != null) {
260 group = sc.base._getAttributeGroup(name);
270 private SchemaObject _getElement(QName name) {
271 return elementName.get(name.getLocalPart());
274 protected SchemaObject getElement(QName name) {
275 SchemaObject obj = _getElement(name);
278 if (name.getNamespaceURI() != null) {
279 for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) {
280 if (sc.base != null) {
281 obj = sc.base._getElement(name);
291 protected SchemaObject getElement(Element element) {
292 return elements.get(element);
296 private SchemaObject _getComplexType(QName name) {
297 return complexTypeName.get(name.getLocalPart());
300 protected SchemaObject getComplexType(QName name) {
301 SchemaObject obj = _getComplexType(name);
304 if (name.getNamespaceURI() != null) {
305 for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) {
306 if (sc.base != null) {
307 obj = sc.base._getComplexType(name);
317 protected SchemaObject getComplexType(ComplexType complexType) {
318 return complexTypes.get(complexType);
321 private SchemaObject _getSimpleType(QName name) {
322 return simpleTypeName.get(name.getLocalPart());
325 protected SchemaObject getSimpleType(QName name) {
326 SchemaObject obj = _getSimpleType(name);
329 if (name.getNamespaceURI() != null) {
330 for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) {
331 if (sc.base != null) {
332 obj = sc.base._getSimpleType(name);
342 protected SchemaObject getSimpleType(SimpleType simpleType) {
343 return simpleTypes.get(simpleType);
346 protected SchemaObject getWithObj(SchemaObject referrer, OpenAttrs attrs) {
347 // FIXME : this method cannot handle references to other schemas.
348 SchemaObject obj = null;
349 if (attrs instanceof Element)
350 obj = elements.get(attrs);
351 else if (attrs instanceof ComplexType)
352 obj = complexTypes.get(attrs);
353 else if (attrs instanceof SimpleType)
354 obj = simpleTypes.get(attrs);
356 throw new RuntimeException("Cannot locate referred object " + attrs + " when handling " + referrer.getName());
361 private void preload() {
362 Deque<SchemaObject> stack = new ArrayDeque<SchemaObject>();
363 //stack.addAll(schema.getSimpleTypeOrComplexTypeOrGroup());
364 for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) {
365 if (attrs instanceof Element) {
366 Element element = (Element)attrs;
367 SchemaObject obj = new SchemaObject(this,element);
368 obj.setRename(getRename(element));
370 } else if (attrs instanceof ComplexType) {
371 ComplexType complexType = (ComplexType)attrs;
372 SchemaObject obj = new SchemaObject(this,complexType);
373 obj.setRename(getRename(complexType));
375 } else if (attrs instanceof SimpleType) {
376 SimpleType simpleType = (SimpleType)attrs;
377 SchemaObject obj = new SchemaObject(this,simpleType);
379 } else if (attrs instanceof Attribute) {
380 Attribute attribute = (Attribute)attrs;
381 SchemaObject obj = new SchemaObject(this, attribute);
383 } else if (attrs instanceof AttributeGroup) {
384 // Attribute groups are not cached
385 } else if (attrs instanceof NamedGroup) {
386 NamedGroup group = (NamedGroup)attrs;
387 SchemaObject obj = new SchemaObject(this,group);
390 System.out.println(attrs.getClass().getName());
394 while (!stack.isEmpty()) {
395 SchemaObject object = stack.pop();
396 switch (object.getType()) {
398 ComplexType ct = object.getComplexType();
399 if (ct.getName() != null && ct.getName().length() > 0 && ct instanceof TopLevelComplexType)
400 complexTypeName.put(ct.getName(), object);
401 complexTypes.put(ct, object);
402 if (ct.getChoice() != null) {
403 preload(object,ct.getChoice(), stack);
405 if (ct.getSequence() != null) {
406 preload(object,ct.getSequence(), stack);
408 if (ct.getAll() != null) {
409 preload(object,ct.getAll(), stack);
411 if (ct.getGroup() != null)
412 throw new RuntimeException("Groups not supported");
413 if (ct.getComplexContent() != null) {
414 ComplexContent cc = ct.getComplexContent();
415 ExtensionType extensionType = cc.getExtension();
416 if (extensionType != null) {
417 if (extensionType.getChoice() != null) {
418 preload(object,extensionType.getChoice(), stack);
420 if (extensionType.getSequence()!= null) {
421 preload(object,extensionType.getSequence(), stack);
423 if (extensionType.getAll()!= null) {
424 preload(object,extensionType.getAll(), stack);
426 if (extensionType.getGroup() != null) {
427 throw new RuntimeException("Groups not supported");
428 //preload(object,extensionType.getGroup(), stack);
432 if (ct.getSimpleContent() != null) {
433 SimpleContent cc = ct.getSimpleContent();
434 ExtensionType extensionType = cc.getExtension();
435 if (extensionType != null) {
436 if (extensionType.getChoice() != null) {
437 preload(object,extensionType.getChoice(), stack);
439 if (extensionType.getSequence()!= null) {
440 preload(object,extensionType.getSequence(), stack);
442 if (extensionType.getAll()!= null) {
443 preload(object,extensionType.getAll(), stack);
445 if (extensionType.getGroup() != null)
446 throw new RuntimeException("Groups not supported");
452 Element e = object.getElement();
453 if (e instanceof TopLevelElement)
454 elementName.put(e.getName(), object);
455 elements.put(e, object);
456 if (e.getComplexType() != null)
457 stack.push(new SchemaObject(this,object,e.getComplexType()));
458 if (e.getSimpleType() != null)
459 stack.push(new SchemaObject(this,object,e.getSimpleType()));
463 SimpleType e = object.getSimpleType();
464 if (e instanceof TopLevelSimpleType)
465 simpleTypeName.put(e.getName(), object);
466 simpleTypes.put(e, object);
470 NamedGroup e = object.getModelGroup();
471 modelGroupName.put(e.getName(), object);
472 modelGroups.put(e, object);
476 Attribute e = object.getAttribute();
477 attrName.put(e.getName(), object);
478 attributes.put(e, object);
485 private void preload(SchemaObject parent,ExplicitGroup eg, Deque<SchemaObject> stack) {
486 for (Object o : eg.getParticle()) {
487 if (o instanceof JAXBElement<?>) {
488 JAXBElement<?> element = (JAXBElement<?>)o;
489 Object elemValue = element.getValue();
490 if (elemValue instanceof Element) {
491 SchemaObject obj = new SchemaObject(this,parent,(Element)elemValue);
492 obj.setRename(getRename((Element)elemValue));
494 } else if (elemValue instanceof ExplicitGroup) {
495 preload(parent,(ExplicitGroup)elemValue, stack);
496 } else if (elemValue instanceof RealGroup) {
497 preload(parent,(RealGroup)elemValue, stack);
499 throw new RuntimeException("Unknown ExplicitGroup element " + elemValue.getClass().getName());
501 } else if (o instanceof Any){
504 throw new RuntimeException("Unknown ExplicitGroup reference " + o.getClass().getName());
509 private void preload(SchemaObject parent, RealGroup eg, Deque<SchemaObject> stack) {
510 System.out.println(eg);
511 if (eg instanceof NamedGroup) {
512 SchemaObject obj = new SchemaObject(this,parent,(NamedGroup)eg);
518 protected void handle(TopLevelAttribute topLevelAttribute) {
519 handle(null, topLevelAttribute);
522 protected void handleSimpleType(SchemaObject topLevelSimpleType) {
523 handleSimpleType(null,topLevelSimpleType);
526 protected void handle(NamedAttributeGroup namedAttributeGroup){
527 handle(null, namedAttributeGroup);
530 protected void handle(NamedGroup namedAttributeGroup){
531 handle(null, namedAttributeGroup);
534 protected QName getComplexTypeBase(ComplexType complexType) {
535 if (complexType == null)
537 ComplexContent complexContent = complexType.getComplexContent();
538 if (complexContent != null) {
539 ExtensionType extensionType = complexContent.getExtension();
540 if (extensionType != null) {
541 QName type = extensionType.getBase();
548 protected QName getSimpleTypeBase(SimpleType simpleType) {
549 // if (simpleType == null)
551 // return simpleType.getRestriction().getBase();
553 Restriction restriction = simpleType.getRestriction();
554 if (restriction != null) {
555 QName base = restriction.getBase();
557 } else if (simpleType.getId() != null) {
558 throw new RuntimeException(simpleType.getName() + " restriction error");
559 } else if (simpleType.getUnion() != null) {
560 Union union = simpleType.getUnion();
561 if (union.getMemberTypes().size() > 0) {
563 for (QName type : union.getMemberTypes()) {
565 TypeEntry entry = getTypeEntry(type);
567 //SchemaObject obj = simpleTypeName.get(type.getLocalPart());
568 SchemaObject obj = getSimpleType(type);
570 throw new RuntimeException(simpleType.getName() + " union has unresolved reference " + type.getLocalPart());
571 sType = getSimpleTypeBase(obj.getSimpleType());
577 else if (!base.equals(sType)) {
578 //FIXME : throw new RuntimeException(simpleType.getName() + " union has incompatible member types");
579 // fall back to string.
580 base = new QName(SCHEMA_NS, "string");
586 if (union.getSimpleType().size() == 0)
587 throw new RuntimeException(simpleType.getName() + " union error");
588 for (SimpleType s : union.getSimpleType()) {
589 if (restriction == null)
590 restriction = s.getRestriction();
592 Restriction r = s.getRestriction();
593 if (!r.getBase().equals(restriction.getBase())) {
594 Inheritance rI = new Inheritance("");
595 getAtomicTypeInheritance(r.getBase(), rI);
596 Inheritance restI = new Inheritance("");
597 getAtomicTypeInheritance(restriction.getBase(), restI);
598 if (!rI.atomicType.l0Type.equals(restI.atomicType.l0Type))
599 throw new RuntimeException(simpleType.getName() + " union has incompatible restriction bases");
603 QName base = restriction.getBase();
606 } else if (simpleType.getList() != null) {
607 // FIXME: callers cannot get the information that we have a list.
608 org.w3._2001.xmlschema.List list = simpleType.getList();
609 return list.getItemType();
611 throw new RuntimeException(simpleType.getName() + " restriction error");
615 protected QName getElementBase(Element element) {
616 ComplexType complexType = element.getComplexType();
617 SimpleType simpleType = element.getSimpleType();
618 if (complexType != null)
619 return getComplexTypeBase(complexType);
620 if (simpleType != null) {
621 return getSimpleTypeBase(simpleType);
626 private void handleAttributes(SchemaObject complexType, List<Annotated> attributeOrAttributeGroup) {
627 //name = getComplexTypePrefix()+complexType.getName()
629 Set<Annotated> handled = handleAttributeCompositions(complexType,attributeOrAttributeGroup);
630 for (Annotated annotated : attributeOrAttributeGroup) {
631 if (handled.contains(annotated))
633 if (annotated instanceof Attribute) {
634 handle(complexType,(Attribute)annotated);
635 } else if (annotated instanceof AttributeGroup){
636 handle(complexType,(AttributeGroup)annotated);
637 //comment("AttributeGroup " + ((AttributeGroup)annotated).getRef().getLocalPart());
639 throw new RuntimeException();
644 protected void handleAttributes(SchemaObject simpleTypeObj) {
645 component.handleAttributes(simpleTypeObj);
648 protected void handleExtensionAttributes(SchemaObject complexType) {
649 ComplexContent complexContent = complexType.getComplexType().getComplexContent();
650 if (complexContent != null) {
651 ExtensionType extensionType = complexContent.getExtension();
652 if (extensionType != null) {
653 handleAttributes(complexType, extensionType.getAttributeOrAttributeGroup());
656 SimpleContent simpleContent = complexType.getComplexType().getSimpleContent();
657 if (simpleContent != null) {
658 ExtensionType extensionType = simpleContent.getExtension();
659 if (extensionType != null) {
660 handleAttributes(complexType, extensionType.getAttributeOrAttributeGroup());
667 protected void handleComplexTypeAttributes(SchemaObject complexType) {
668 handleAttributes(complexType,complexType.getComplexType().getAttributeOrAttributeGroup());
671 protected void handleElementComplexTypeAttributes(SchemaObject complexType) {
672 if (complexType != null) {
673 handleComplexTypeAttributes(complexType);
674 handleExtensionAttributes(complexType);
678 protected void handleElementSimpleTypeAttributes(SchemaObject simpleType) {
679 if (simpleType != null) {
680 handleAttributes(simpleType);
684 protected Set<Annotated> handleAttributeCompositions(SchemaObject obj, List<Annotated> attributeOrAttributeGroup) {
686 Set<Annotated> handled = new HashSet<Annotated>();
687 for (JAXBElement<?> e : configuration.getConversionRule()) {
688 if (e.getValue() instanceof AttributeComposition) {
689 AttributeComposition composition = (AttributeComposition)e.getValue();
690 if (composition.getAttribute().size() < 2)
691 throw new RuntimeException("Attribute Composition is not valid");
692 BijectionMap<org.simantics.xml.sax.configuration.Attribute, Annotated> map = new BijectionMap<org.simantics.xml.sax.configuration.Attribute, Annotated>();
693 for (org.simantics.xml.sax.configuration.Attribute a : composition.getAttribute()) {
694 for (Annotated annotated : attributeOrAttributeGroup) {
695 if (annotated instanceof Attribute) {
696 Attribute attribute = (Attribute)annotated;
697 QName type = getBaseType(attribute);
698 if (a.getName().equals(attribute.getName()) && type != null && a.getType().equals(type.getLocalPart())) {
699 map.map(a, attribute);
704 if (composition.getAttribute().size() == map.size()) {
705 handled.addAll(map.getRightSet());
706 handleAttributeComposition(obj, composition, map);
713 protected QName getBaseType(Attribute attribute) {
714 if (attribute.getType() != null)
715 return attribute.getType();
716 if (attribute.getRef() != null)
717 return attribute.getRef();
718 SimpleType simpleType = attribute.getSimpleType();
719 if (simpleType != null) {
720 Restriction restriction = simpleType.getRestriction();
721 if (restriction != null)
722 if (restriction.getBase() != null)
723 return restriction.getBase();
728 protected QName getPrimitiveType(Attribute attribute) {
729 QName type = getBaseType(attribute);
730 String b = getBindingFromPrimitiveType(type);
731 while (b==null && type != null) {
732 SchemaObject baseType = simpleTypeName.get(type.getLocalPart());
733 if (baseType != null) {
734 Restriction restriction = baseType.getSimpleType().getRestriction();
735 if (restriction != null)
736 if (restriction.getBase() != null) {
737 type = restriction.getBase();
738 b = getBindingFromPrimitiveType(type);
745 private SchemaObject _getAttribute(QName name) {
746 return attrName.get(name.getLocalPart());
749 protected Attribute getRefAttribute(QName ref) {
750 // for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) {
751 // if (attrs instanceof TopLevelAttribute) {
752 // TopLevelAttribute attribute = (TopLevelAttribute)attrs;
753 // if (attribute.getName().equals(ref.getLocalPart()))
757 SchemaObject obj = _getAttribute(ref);
759 return obj.getAttribute();
760 if (ref.getNamespaceURI() != null) {
761 for (SchemaConverter sc : converter.getConverter(ref.getNamespaceURI())) {
762 if (sc.base != null) {
763 obj = sc.base._getAttribute(ref);
765 return obj.getAttribute();
773 //protected abstract void handleAttributeComposition(SchemaObject obj, AttributeComposition composition, BijectionMap<org.simantics.xml.sax.configuration.Attribute, Annotated> attributes);
774 protected void handleAttributeComposition(SchemaObject obj, AttributeComposition composition, BijectionMap<org.simantics.xml.sax.configuration.Attribute, Annotated> attributes) {
775 component.handleAttributeComposition(obj, composition, attributes);
781 protected void handleComplexType(SchemaObject complexType) {
782 // handleComplexTypeAttributes(complexType);
783 // handleComplexTypeExtension(complexType);
784 // handleExtensionAttributes(complexType);
785 component.handleComplexType(complexType);
788 protected void handleElement(SchemaObject topLevelElement) {
789 // LocalComplexType complexType = topLevelElement.getElement().getComplexType();
791 // if (complexType != null) {
792 // SchemaObject complextTypeObj = complexTypes.get(complexType);
793 // handleElementComplexTypeAttributes(complextTypeObj);
794 // handleComplexTypeExtension(complextTypeObj);
796 component.handleElement(topLevelElement);
799 protected enum RefType{Element,Reference,Type};
801 protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType, String baseRelationName) {
802 component.handleIndicator(parent, indicator, element, refName, refType, baseRelationName);
804 protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any) {
805 component.handleIndicator(parent, indicator, any);
807 protected void handle(SchemaObject parent, SchemaElement indicator, List<SchemaElement> elements) {
808 //component.handle(parent, indicator, elements);
809 // Generate combined relation
810 String baseRelationName = null;
811 if (indicator.getType() == SchemaElement.ElementType.CHOICE) {
812 String name = getChoiceName(elements);
814 baseRelationName = component.handleChoice(parent, indicator, elements, name);
817 if (indicator.getType() == SchemaElement.ElementType.SEQUENCE || indicator.getType() == SchemaElement.ElementType.CHOICE) {
818 for (SchemaElement e : elements) {
819 handle(parent, indicator, e, baseRelationName);
824 protected void handle(SchemaObject parent, ExplicitGroup eg, SchemaElement.ElementType indicator) {
825 handle(parent, new SchemaElement(eg, indicator));
828 protected void handle(SchemaObject parent, GroupRef eg, SchemaElement.ElementType indicator) {
829 handle(parent, new SchemaElement(eg, indicator));
832 protected void handle(SchemaObject parent, SchemaElement indicator) {
835 List<SchemaElement> elements = new ArrayList<SchemaElement>();
836 List<SchemaElement> choices = new ArrayList<SchemaElement>();
837 List<SchemaElement> sequences = new ArrayList<SchemaElement>();
838 List<SchemaElement> alls = new ArrayList<SchemaElement>();
839 List<SchemaElement> anys = new ArrayList<SchemaElement>();
840 List<SchemaElement> groups = new ArrayList<SchemaElement>();
842 for (Object o : indicator.getGroup().getParticle()) {
843 if (o instanceof JAXBElement<?>) {
844 JAXBElement<?> element = (JAXBElement<?>)o;
845 Object elemValue = element.getValue();
846 if (elemValue instanceof LocalElement) {
847 LocalElement localElement = (LocalElement)elemValue;
848 elements.add(new SchemaElement(indicator,localElement, ElementType.ELEMENT));
849 } else if (elemValue instanceof All) {
850 alls.add(new SchemaElement(indicator,(All)elemValue, ElementType.ALL));
851 } else if (elemValue instanceof ExplicitGroup) {
852 QName qname = element.getName();
853 if ("choice".equals(qname.getLocalPart())) {
854 choices.add(new SchemaElement(indicator,(ExplicitGroup)elemValue, ElementType.CHOICE));
855 } else if ("sequence".equals(qname.getLocalPart())) {
856 sequences.add(new SchemaElement(indicator,(ExplicitGroup)elemValue, ElementType.SEQUENCE));
858 } else if (elemValue instanceof RealGroup) {
859 if (elemValue instanceof GroupRef) {
860 groups.add(new SchemaElement(indicator,(GroupRef)elemValue, ElementType.GROUP_REF));
861 } else if (elemValue instanceof NamedGroup) {
862 groups.add(new SchemaElement(indicator,(NamedGroup)elemValue, ElementType.NAMED_GROUP));
864 throw new RuntimeException("Unknown ExplicitGroup element " + elemValue.getClass().getName());
867 throw new RuntimeException("Unknown ExplicitGroup element " + elemValue.getClass().getName());
869 } else if (o instanceof Any){
870 anys.add(new SchemaElement(indicator,(Any)o, ElementType.ANY));
872 throw new RuntimeException("Unknown ExplicitGroup reference " + o.getClass().getName());
876 if (elements.size() == 0 && choices.size() == 0 && sequences.size() == 0 && alls.size() == 0 && anys.size() == 0 && groups.size() == 0) {
880 if (indicator.getType() == SchemaElement.ElementType.SEQUENCE) {
881 if (indicator.getRestriction().single()) {
882 if (elements.size() > 0) {
883 for (SchemaElement e : sequences) {
886 for (SchemaElement c : choices) {
890 for (SchemaElement c : alls) {
894 for (SchemaElement c : groups) {
897 handle(parent, indicator, elements);
898 for (SchemaElement a : anys) {
899 handleIndicator(parent, indicator, a);
902 if (sequences.size() > 0) {
903 throw new RuntimeException("Cannot handle Sequence with inner Sequences");
905 for (SchemaElement c : choices) {
908 for (SchemaElement a : anys) {
909 handleIndicator(parent, indicator, a);
911 for (SchemaElement c : groups) {
916 if (choices.size() == 1 && sequences.size() == 0 && alls.size() == 0 && groups.size() == 0) {
917 // special case: handle lone choice inside sequence with maxOccurs > 1
918 SchemaElement choice = choices.get(0);
919 // move multiplicity restrictions to choice
920 if (indicator.getRestriction().max == -1 || (choice.getRestriction().max > 0 && indicator.getRestriction().max > choice.getRestriction().max))
921 choice.getRestriction().max = indicator.getRestriction().max;
922 if (indicator.getRestriction().min == 0 || choice.getRestriction().min > indicator.getRestriction().min)
923 choice.getRestriction().min = indicator.getRestriction().min;
924 handle(parent, choice, elements);
927 if (sequences.size() > 0 || choices.size() > 0 || alls.size() > 0 || groups.size() > 0) {
928 throw new RuntimeException("Cannot handle Sequence with inner ExplicitGroups");
930 handle(parent, indicator, elements);
931 for (SchemaElement a : anys) {
932 handleIndicator(parent, indicator, a);
936 } else if (indicator.getType() == SchemaElement.ElementType.CHOICE){
937 if (indicator.getRestriction().single()) {
938 if (sequences.size()> 0 || choices.size() > 0 || alls.size() > 0 || anys.size() > 0 || groups.size() > 0) {
939 throw new RuntimeException("Cannot handle Choice that contains something else than Elements");
940 //System.out.println("Cannot handle Choice that contains something else than Elements");
943 handle(parent, indicator, elements);
946 if (sequences.size() > 0 || choices.size() > 0 || alls.size() > 0 || groups.size() > 0) {
947 throw new RuntimeException("Cannot handle Choice with inner ExplicitGroups");
948 //System.out.println("Cannot handle Choice with inner ExplicitGroups");
951 handle(parent, indicator, elements);
952 for (SchemaElement a : anys) {
953 handleIndicator(parent, indicator, a);
956 } else if (indicator.getType() == ElementType.ALL) {
957 if (sequences.size()> 0 || choices.size() > 0 || alls.size() > 0 || anys.size() > 0 || groups.size() > 0) {
958 throw new RuntimeException("Cannot handle All that contains something else than Elements");
960 if (!indicator.getRestriction().single()) {
961 throw new RuntimeException("All indicator must have maxOccurs=1");
963 handle(parent, indicator, elements);
968 protected void handle(SchemaObject parent, SchemaElement indicator, SchemaElement element, String baseRelationName) {
969 Element localElement = element.getElement();
970 if (localElement.getName() != null) {
971 SchemaObject eObj = elements.get(localElement);
972 QName refType = localElement.getType();
974 handleIndicator(parent, indicator, element, null, RefType.Type, baseRelationName);
977 handleIndicator(parent, indicator, element, null, RefType.Element, baseRelationName);
979 } else if (localElement.getRef() != null) {
980 handleIndicator(parent, indicator,element, null, RefType.Reference, baseRelationName);
984 protected String getName(Element localElement) {
985 if (localElement.getName() != null) {
986 String refName = localElement.getName();
987 return refName.replaceAll("\\.", "_");
988 } else if (localElement.getRef() != null) {
989 QName refType = localElement.getRef();
991 return getName(refType);
997 protected String getName(QName ref) {
998 String n = ref.getLocalPart();
999 return n.replaceAll("\\.", "_");
1002 protected String getName(Attribute ref) {
1003 String n = ref.getName();
1005 return n.replaceAll("\\.", "_");
1010 protected String getChoiceName(List<SchemaElement> elements) {
1011 if (elements.size() == 1) {
1014 else if (elements.size() > 0 && elements.size() <= 3) {
1015 List<String> names = new ArrayList<String>();
1016 for (SchemaElement e : elements) {
1017 String name = getName(e.getElement());
1022 for (int i = 0; i < names.size(); i++) {
1024 name = names.get(i);
1026 name += "Or"+names.get(i);
1031 return "SubElement";
1035 protected void handle(SchemaObject parent, Attribute attribute) {
1036 component.handle(parent, attribute);
1038 protected void handle(SchemaObject parent, AttributeGroup attribute) {
1039 component.handle(parent, attribute);
1041 protected void handle(SchemaObject parent, NamedGroup attribute){
1042 component.handle(parent, attribute);
1045 protected void handleSimpleType(SchemaObject parent, SchemaObject simpleType) {
1046 component.handleSimpleType(parent, simpleType);
1051 protected void handleComplexTypeExtension(SchemaObject complexTypeObj) {
1052 ComplexType complexType = complexTypeObj.getComplexType();
1053 if (complexType != null) {
1054 if (complexType.getChoice() != null)
1055 handle(complexTypeObj, complexType.getChoice(), SchemaElement.ElementType.CHOICE);
1056 if (complexType.getSequence() != null)
1057 handle(complexTypeObj, complexType.getSequence(), SchemaElement.ElementType.SEQUENCE);
1058 if (complexType.getAll() != null)
1059 handle(complexTypeObj, complexType.getAll(), SchemaElement.ElementType.ALL);
1060 if (complexType.getGroup() != null)
1061 throw new RuntimeException("Groups not supported");
1062 ComplexContent complexContent = complexType.getComplexContent();
1063 if (complexContent != null) {
1064 ExtensionType extensionType = complexContent.getExtension();
1065 if (extensionType != null) {
1066 if (extensionType.getChoice() != null) {
1067 handle(complexTypeObj, extensionType.getChoice(), SchemaElement.ElementType.CHOICE);
1069 if (extensionType.getSequence()!= null) {
1070 handle(complexTypeObj, extensionType.getSequence(), SchemaElement.ElementType.SEQUENCE);
1072 if (extensionType.getAll()!= null) {
1073 handle(complexTypeObj, extensionType.getAll(), SchemaElement.ElementType.ALL);
1075 if (extensionType.getGroup() != null) {
1076 throw new RuntimeException("Groups not supported");
1077 //handle(complexTypeObj, extensionType.getGroup(), SchemaElement.ElementType.GROUP_REF);
1081 // SimpleContent simpleContent = complexType.getSimpleContent();
1082 // if (simpleContent != null) {
1083 // ExtensionType extensionType = simpleContent.getExtension();
1089 public boolean isElementRef(String ref) {
1090 return elementName.containsKey(ref);
1093 public boolean isComplexTypeRef(String ref) {
1094 return complexTypeName.containsKey(ref);
1097 public boolean isSimpleTypeRef(String ref) {
1098 return simpleTypeName.containsKey(ref);
1101 public IDProvider getIDProvider(Element element) {
1102 List<IDProvider> idProviders = new ArrayList<IDProvider>(2);
1103 for (JAXBElement<?> e : configuration.getConversionRule()) {
1104 if (e.getValue() instanceof IDProvider) {
1105 IDProvider ref = (IDProvider)e.getValue();
1106 org.simantics.xml.sax.configuration.Element element2 = ref.getElement();
1107 if (element2 != null) {
1108 if (element.getName().equals(element2.getName()))
1109 idProviders.add(ref);
1114 if (idProviders.size() == 0)
1116 if (idProviders.size() > 1)
1117 throw new RuntimeException("Element " + element.getName() + " contains " + idProviders.size() + " id provider rules, only one is allowed.");
1118 return idProviders.get(0);
1121 public IDProvider getIDProvider(ComplexType complexType) {
1122 List<IDProvider> idProviders = new ArrayList<IDProvider>(2);
1123 for (JAXBElement<?> e : configuration.getConversionRule()) {
1124 if (e.getValue() instanceof IDProvider) {
1125 IDProvider ref = (IDProvider)e.getValue();
1126 org.simantics.xml.sax.configuration.ComplexType complexType2 = ref.getComplexType();
1127 if (complexType2 != null) {
1128 if (complexType.getName().equals(complexType2.getName()))
1129 idProviders.add(ref);
1134 if (idProviders.size() == 0)
1136 if (idProviders.size() > 1)
1137 throw new RuntimeException("Element " + complexType.getName() + " contains " + idProviders.size() + " id provider rules, only one is allowed.");
1138 return idProviders.get(0);
1141 public List<IDReference> getIDReferences(Element element) {
1142 List<IDReference> idReferences = new ArrayList<IDReference>(2);
1143 for (JAXBElement<?> e : configuration.getConversionRule()) {
1144 if (e.getValue() instanceof IDReference) {
1145 IDReference ref = (IDReference)e.getValue();
1146 org.simantics.xml.sax.configuration.Element element2 = ref.getElement();
1147 if (element2 != null) {
1148 if (element.getName().equals(element2.getName()))
1149 idReferences.add(ref);
1153 return idReferences;
1156 public List<IDReference> getIDReferences(ComplexType complexType) {
1157 List<IDReference> idReferences = new ArrayList<IDReference>(2);
1158 for (JAXBElement<?> e : configuration.getConversionRule()) {
1159 if (e.getValue() instanceof IDReference) {
1160 IDReference ref = (IDReference)e.getValue();
1161 org.simantics.xml.sax.configuration.ComplexType complexType2 = ref.getComplexType();
1162 if (complexType2 != null) {
1163 if (complexType.getName().equals(complexType2.getName()))
1164 idReferences.add(ref);
1168 return idReferences;
1171 public UnrecognizedChildElement getUnknown(ComplexType complexType) {
1172 for (JAXBElement<?> e : configuration.getConversionRule()) {
1173 if (e.getValue() instanceof UnrecognizedChildElement) {
1174 UnrecognizedChildElement rule = (UnrecognizedChildElement)e.getValue();
1175 org.simantics.xml.sax.configuration.ComplexType complexType2 = rule.getComplexType();
1176 if (complexType2 != null) {
1177 if (complexType.getName().equals(complexType2.getName()))
1185 public UnrecognizedChildElement getUnknown(Element element) {
1186 for (JAXBElement<?> e : configuration.getConversionRule()) {
1187 if (e.getValue() instanceof UnrecognizedChildElement) {
1188 UnrecognizedChildElement rule = (UnrecognizedChildElement)e.getValue();
1189 org.simantics.xml.sax.configuration.Element element2 = rule.getElement();
1190 if (element2 != null) {
1191 if (element.getName().equals(element2.getName()))
1199 public Rename getRename(Attribute element) {
1200 for (JAXBElement<?> e : configuration.getConversionRule()) {
1201 if (e.getValue() instanceof Rename) {
1202 Rename rule = (Rename)e.getValue();
1203 Object ref = rule.getElementOrComplexTypeOrAttribute().get(0);
1204 if (!(ref instanceof org.simantics.xml.sax.configuration.Attribute))
1206 org.simantics.xml.sax.configuration.Attribute element2 = (org.simantics.xml.sax.configuration.Attribute)ref;
1207 if (element2.getName().equals(element.getName())) {
1215 public Rename getRename(ComplexType element) {
1216 for (JAXBElement<?> e : configuration.getConversionRule()) {
1217 if (e.getValue() instanceof Rename) {
1218 Rename rule = (Rename)e.getValue();
1219 Object ref = rule.getElementOrComplexTypeOrAttribute().get(0);
1220 if (!(ref instanceof org.simantics.xml.sax.configuration.ComplexType))
1222 org.simantics.xml.sax.configuration.ComplexType element2 = (org.simantics.xml.sax.configuration.ComplexType)ref;
1223 if (element2.getName().equals(element.getName())) {
1231 public Rename getRename(Element element) {
1232 for (JAXBElement<?> e : configuration.getConversionRule()) {
1233 if (e.getValue() instanceof Rename) {
1234 Rename rule = (Rename)e.getValue();
1235 Object ref = rule.getElementOrComplexTypeOrAttribute().get(0);
1236 if (!(ref instanceof org.simantics.xml.sax.configuration.Element))
1238 org.simantics.xml.sax.configuration.Element element2 = (org.simantics.xml.sax.configuration.Element)ref;
1239 if (element2.getName().equals(element.getName())) {
1248 public boolean useOriginalList(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String ref, QName refType) {
1249 if (parent.getName() == null)
1250 parent = parent.getParent();
1251 if (parent.getName().contains("PipingNetworkSegment"))
1252 System.out.println();
1253 for (JAXBElement<?> e : configuration.getConversionRule()) {
1254 if (e.getValue() instanceof OrderedChild) {
1255 OrderedChild oc = (OrderedChild)e.getValue();
1256 org.simantics.xml.sax.configuration.Element element2 = oc.getElement();
1257 org.simantics.xml.sax.configuration.ComplexType complexType = oc.getComplexType();
1258 org.simantics.xml.sax.configuration.Element child = oc.getChild();
1259 if (!oc.getType().equals("original"))
1261 boolean match = false;
1262 if (element2 != null) {
1263 if (parent.getType() == ObjectType.ELEMENT && parent.getName().equals(element2.getName())) {
1266 } else if (complexType != null) {
1267 if (parent.getType() == ObjectType.COMPLEX_TYPE && parent.getName() != null && parent.getName().equals(complexType.getName())) {
1273 if (child != null) {
1274 if (matchChild(child, ref, refType)) {
1275 if (oc.getValue().equals("disable"))
1280 if (oc.getValue().equals("disable"))
1288 return indicator.order();
1291 public boolean useElementList(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String refName, QName refType) {
1292 if (parent.getName() == null)
1293 parent = parent.getParent();
1294 if (parent.getName() == "PipingNetworkSegment")
1295 System.out.println();
1296 for (JAXBElement<?> e : configuration.getConversionRule()) {
1297 if (e.getValue() instanceof OrderedChild) {
1298 OrderedChild oc = (OrderedChild)e.getValue();
1299 org.simantics.xml.sax.configuration.Element element2 = oc.getElement();
1300 org.simantics.xml.sax.configuration.ComplexType complexType = oc.getComplexType();
1301 org.simantics.xml.sax.configuration.Element child = oc.getChild();
1302 if (!oc.getType().equals("child"))
1304 boolean match = false;
1305 if (element2 != null) {
1306 if (parent.getType() == ObjectType.ELEMENT && parent.getName().equals(element2.getName())) {
1309 } else if (complexType != null) {
1310 if (parent.getType() == ObjectType.COMPLEX_TYPE && parent.getName() != null && parent.getName().equals(complexType.getName())) {
1316 if (child != null) {
1317 if (matchChild(child, refName, refType)) {
1318 if (oc.getValue().equals("disable"))
1323 if (oc.getValue().equals("disable"))
1331 return element.many() && element.order();
1334 private boolean matchChild(org.simantics.xml.sax.configuration.Element child, String refName, QName refType) {
1335 if (refType != null && refType.getLocalPart().equals(child.getName()))
1337 if (refName != null && refName.equals(child.getName()))
1342 public static class TypeEntry {
1346 String defaultValue;
1348 String getterPrefix;
1349 String getterPostfix;
1350 String stringPrefix;
1351 String stringPostfix;
1352 public TypeEntry(String l0Type, String binding, String javaType, String defaultValue, String getterPrefix, String getterPostfix, String stringPrefix, String stringPostfix) {
1354 this.l0Type = l0Type;
1355 this.binding = binding;
1356 this.javaType = javaType;
1357 this.defaultValue = defaultValue;
1359 this.getterPrefix = getterPrefix;
1360 this.getterPostfix = getterPostfix;
1361 this.stringPrefix = stringPrefix;
1362 this.stringPostfix = stringPostfix;
1365 public TypeEntry(String l0Type, String binding, String javaType, String defaultValue, String getterPrefix, String getterPostfix, String stringPrefix, String stringPostfix, boolean id) {
1367 this.l0Type = l0Type;
1368 this.binding = binding;
1369 this.javaType = javaType;
1370 this.defaultValue = defaultValue;
1372 this.getterPrefix = getterPrefix;
1373 this.getterPostfix = getterPostfix;
1374 this.stringPrefix = stringPrefix;
1375 this.stringPostfix = stringPostfix;
1378 public String getValueGetterMethod(String name) {
1379 return getterPrefix + name + ".getValue()"+getterPostfix;
1381 public String getValueGetter(String name) {
1382 return getterPrefix + name+getterPostfix;
1384 public String getValueGetter()
1386 return getValueGetter("value");
1389 public String getToString(String name) {
1390 return stringPrefix +"("+javaType+")"+name+stringPostfix;
1393 public String getElementToString(String name) {
1394 return stringPrefix + name+stringPostfix;
1399 public enum InheritanceType{ComplexType,AtomicType,None};
1401 public static class Inheritance {
1402 public String baseClass;
1403 public String additionalClass;
1404 public InheritanceType type;
1405 public TypeEntry atomicType;
1407 public Inheritance(String baseClass) {
1408 this.baseClass = baseClass;
1409 this.type = InheritanceType.None;
1413 public String getComplexTypePrefix() {
1414 return component.getComplexTypePrefix();
1416 public String getAttributeGroupPrefix() {
1417 return component.getAttributeGroupPrefix();
1419 public String getName(SchemaObject obj) {
1420 return component.getName(obj);
1422 public String getBaseClass(ObjectType type) {
1423 return component.getBaseClass(type);
1428 public Inheritance getInheritance(SchemaObject topLevelObj) {
1429 Inheritance inheritance = null;
1430 if (topLevelObj.getType() == ObjectType.ELEMENT) {
1431 Element topLevelElement = topLevelObj.getElement();
1432 inheritance = new Inheritance(getBaseClass(ObjectType.ELEMENT));
1433 if (topLevelElement.getType() != null) {
1434 QName type = topLevelElement.getType();
1435 if (!type.getNamespaceURI().equals(SCHEMA_NS)) {
1436 SchemaObject obj = complexTypeName.get(type.getLocalPart());
1438 inheritance.baseClass = getName(obj);
1439 inheritance.type = InheritanceType.ComplexType;
1442 obj = simpleTypeName.get(type.getLocalPart());
1444 getAtomicTypeInheritance(type, inheritance);
1447 TypeEntry entry = getTypeEntry(type);
1448 if (entry != null) {
1449 inheritance.type = InheritanceType.AtomicType;
1450 inheritance.atomicType = entry;
1454 if (inheritance.type == InheritanceType.None) {
1455 QName type = getElementBase(topLevelElement);
1457 if (!type.getNamespaceURI().equals(SCHEMA_NS)) {
1458 SchemaObject obj = getWithName(type);
1459 inheritance.baseClass = getName(obj);
1460 inheritance.type = InheritanceType.ComplexType;
1462 TypeEntry entry = getTypeEntry(type);
1463 if (entry != null) {
1464 inheritance.type = InheritanceType.AtomicType;
1465 inheritance.atomicType = entry;
1470 if (inheritance.type == InheritanceType.None) {
1471 QName type = topLevelElement.getSubstitutionGroup();
1473 if (!type.getNamespaceURI().equals(SCHEMA_NS)) {
1474 SchemaObject obj = getWithName(type);
1475 inheritance.baseClass = getName(obj);
1476 inheritance.type = InheritanceType.ComplexType;
1478 TypeEntry entry = getTypeEntry(type);
1479 if (entry != null) {
1480 inheritance.type = InheritanceType.AtomicType;
1481 inheritance.atomicType = entry;
1486 } else if (topLevelObj.getType() == ObjectType.COMPLEX_TYPE) {
1487 ComplexType complexType = topLevelObj.getComplexType();
1488 QName type = getComplexTypeBase(complexType);
1489 inheritance = new Inheritance(getBaseClass(ObjectType.COMPLEX_TYPE));
1490 if (type != null && !type.getNamespaceURI().equals("http://www.w3.org/2001/XMLSchema")) {
1491 SchemaObject obj = complexTypeName.get(type.getLocalPart());
1493 inheritance.baseClass = getName(obj);
1494 inheritance.type = InheritanceType.ComplexType;
1497 SimpleContent simpleContent = complexType.getSimpleContent();
1498 if (simpleContent != null) {
1499 ExtensionType extensionType = simpleContent.getExtension();
1500 if (extensionType != null) {
1501 type = extensionType.getBase();
1502 SchemaObject simpleType = getSimpleType(type);
1503 if (simpleType != null)
1504 inheritance.additionalClass = getName(simpleType);
1505 getAtomicTypeInheritance(type, inheritance);
1513 * Goes through chain of SimpleTypes until locates Atomic Type (type defined in XML schema).
1515 * @param topLevelObj
1516 * @param inheritance
1518 public void getAtomicTypeInheritance(QName type, Inheritance inheritance) {
1519 if (!type.getNamespaceURI().equals(SCHEMA_NS)) {
1520 SchemaObject obj = getSimpleType(type);
1522 throw new RuntimeException("Cannot locate SimpleType " + type.getLocalPart());
1523 SimpleType simpleType = obj.getSimpleType();
1524 type = getSimpleTypeBase(simpleType);
1525 getAtomicTypeInheritance(type, inheritance);
1527 TypeEntry entry = getTypeEntry(type);
1528 if (entry != null) {
1529 inheritance.type = InheritanceType.AtomicType;
1530 inheritance.atomicType = entry;
1535 public String getDefaultValue(QName atype) {
1536 Map<String,TypeEntry> types = typeMap.get(atype.getNamespaceURI());
1539 TypeEntry entry = types.get(atype.getLocalPart());
1542 return entry.defaultValue;