X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FSchemaConversionBase.java;fp=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FSchemaConversionBase.java;h=f35a8845acc74e7a0e081ffdf76498c67b7cf5b9;hb=ada38ab0a1a98dcb413bef3273064da36ce2d273;hp=0000000000000000000000000000000000000000;hpb=db6def39515fd442543064502a9ae7ae07ffc160;p=simantics%2Finterop.git diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java new file mode 100644 index 0000000..f35a884 --- /dev/null +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java @@ -0,0 +1,936 @@ +package org.simantics.xml.sax; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Deque; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.xml.bind.JAXBElement; +import javax.xml.namespace.QName; + +import org.simantics.utils.datastructures.BijectionMap; +import org.simantics.xml.sax.SchemaElement.ElementType; +import org.simantics.xml.sax.SchemaObject.ObjectType; +import org.simantics.xml.sax.configuration.AttributeComposition; +import org.simantics.xml.sax.configuration.Configuration; +import org.simantics.xml.sax.configuration.IDProvider; +import org.simantics.xml.sax.configuration.IDReference; +import org.simantics.xml.sax.configuration.OrderedChild; +import org.simantics.xml.sax.configuration.UnrecognizedChildElement; +import org.w3._2001.xmlschema.All; +import org.w3._2001.xmlschema.Annotated; +import org.w3._2001.xmlschema.Any; +import org.w3._2001.xmlschema.Attribute; +import org.w3._2001.xmlschema.AttributeGroup; +import org.w3._2001.xmlschema.ComplexContent; +import org.w3._2001.xmlschema.ComplexType; +import org.w3._2001.xmlschema.Element; +import org.w3._2001.xmlschema.ExplicitGroup; +import org.w3._2001.xmlschema.ExtensionType; +import org.w3._2001.xmlschema.LocalComplexType; +import org.w3._2001.xmlschema.LocalElement; +import org.w3._2001.xmlschema.NamedAttributeGroup; +import org.w3._2001.xmlschema.OpenAttrs; +import org.w3._2001.xmlschema.Restriction; +import org.w3._2001.xmlschema.Schema; +import org.w3._2001.xmlschema.SimpleType; +import org.w3._2001.xmlschema.TopLevelAttribute; +import org.w3._2001.xmlschema.TopLevelComplexType; +import org.w3._2001.xmlschema.TopLevelElement; +import org.w3._2001.xmlschema.TopLevelSimpleType; + +public abstract class SchemaConversionBase { + + protected Schema schema; + protected Configuration configuration; + + protected static final String SCHEMA_NS = "http://www.w3.org/2001/XMLSchema"; + protected static final String CONVERSION_NS = "http://www.simantics.org/Layer0"; + + protected Map> typeMap; + + public SchemaConversionBase(Configuration configuration) { + this.configuration = configuration; + typeMap = new HashMap>(); + + Map schemaTypes = new HashMap(); + typeMap.put(SCHEMA_NS, schemaTypes); + Map l0Types = new HashMap(); + typeMap.put(CONVERSION_NS, l0Types); + + schemaTypes.put("string", new TypeEntry("L0.String", "STRING", "String", "")); + schemaTypes.put("NMTOKEN", new TypeEntry("L0.String", "STRING", "String", "")); + schemaTypes.put("token", new TypeEntry("L0.String", "STRING", "String", "")); + schemaTypes.put("ID", new TypeEntry("L0.String", "STRING", "String", "",true)); + schemaTypes.put("IDREF", new TypeEntry("L0.String", "STRING", "String", "")); + schemaTypes.put("date", new TypeEntry("L0.String", "STRING", "String", "")); + schemaTypes.put("time", new TypeEntry("L0.String", "STRING", "String", "")); + schemaTypes.put("anyURI", new TypeEntry("L0.URI", "STRING", "String", "")); + schemaTypes.put("double", new TypeEntry("L0.Double", "DOUBLE", "double", "Double.NaN")); + schemaTypes.put("float", new TypeEntry("L0.Float", "FLOAT", "float", "Float.NaN")); + schemaTypes.put("decimal", new TypeEntry("L0.Double", "DOUBLE", "double", "Double.NaN")); + schemaTypes.put("boolean", new TypeEntry("L0.Boolean", "BOOLEAN", "boolean", "false")); + schemaTypes.put("integer", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); + schemaTypes.put("positiveInteger", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); + schemaTypes.put("nonPositiveInteger", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); + schemaTypes.put("nonNegativeInteger", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); + schemaTypes.put("negativeInteger", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); + schemaTypes.put("unsignedInt", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); + schemaTypes.put("int", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); + schemaTypes.put("short", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); + schemaTypes.put("unsignedShort",new TypeEntry("L0.Integer", "INTEGER", "int", "0")); + schemaTypes.put("byte", new TypeEntry("L0.Byte", "BYTE", "byte", "0")); + schemaTypes.put("unsignedByte", new TypeEntry("L0.Byte", "BYTE", "byte", "0")); + schemaTypes.put("long", new TypeEntry("L0.Long", "LONG", "long", "0")); + schemaTypes.put("unsignedLong", new TypeEntry("L0.Long", "LONG", "long", "0")); + + + + l0Types.put("doubleArray", new TypeEntry("L0.DoubleArray", "DOUBLE_ARRAY", "double[]", null)); + l0Types.put("stringArray", new TypeEntry("L0.StringArray", "STRING_ARRAY", "string[]", null)); + } + + + protected TypeEntry getTypeEntry(QName type) { + Map types = typeMap.get(type.getNamespaceURI()); + if (types == null) + return null; + TypeEntry entry = types.get(type.getLocalPart()); + return entry; + + } + + protected String getL0TypeFromPrimitiveType(QName primitiveType) { + TypeEntry entry = getTypeEntry(primitiveType); + if (entry == null) + return null; + return entry.l0Type; + } + + protected String getL0Type(QName primitiveType) { + String type = getL0TypeFromPrimitiveType(primitiveType); + if (type != null) + return type; + SchemaObject simpleTypeObj = simpleTypeName.get(primitiveType.getLocalPart()); + if (simpleTypeObj == null) + return null; + SimpleType simpleType = simpleTypeObj.getSimpleType(); + while (simpleType != null) { + QName base = simpleType.getRestriction().getBase(); + if (base != null) + return getL0Type(base); + simpleType = simpleType.getRestriction().getSimpleType(); + } + return null; + } + + protected String getBindingFromPrimitiveType(QName primitiveType) { + TypeEntry entry = getTypeEntry(primitiveType); + if (entry == null) + return null; + return entry.binding; + } + + protected String getJavaTypeFromPrimitiveType(QName primitiveType) { + TypeEntry entry = getTypeEntry(primitiveType); + if (entry == null) + return null; + return entry.javaType; + } + + + protected void handle(Schema schema) { + this.schema = schema; + preload(); + + for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { + if (attrs instanceof TopLevelAttribute) { + handle((TopLevelAttribute)attrs); + + } else if (attrs instanceof TopLevelComplexType) { + handleComplexType(complexTypes.get(attrs)); + } else if (attrs instanceof TopLevelElement) { + handleElement(elements.get(attrs)); + } else if (attrs instanceof TopLevelSimpleType) { + handleSimpleType(simpleTypes.get(attrs)); + } else if (attrs instanceof NamedAttributeGroup) { + handle((NamedAttributeGroup)attrs); + } else { + System.out.println(attrs.getClass().getName()); + } + } + } + + protected Map elementName = new HashMap(); + protected Map complexTypeName = new HashMap(); + protected Map simpleTypeName = new HashMap(); + protected Map elements = new HashMap(); + protected Map complexTypes = new HashMap(); + protected Map simpleTypes = new HashMap(); + + + protected SchemaObject getWithName(SchemaObject referrer, String name) { + SchemaObject obj = elementName.get(name); + if (obj == null) + obj = complexTypeName.get(name); + if (obj == null) + obj = simpleTypeName.get(name); + if (obj == null) { + throw new RuntimeException("Cannot locate referred type " + name + " when handling " + referrer.getName()); + } + return obj; + } + + protected SchemaObject getWithObj(SchemaObject referrer, OpenAttrs attrs) { + SchemaObject obj = null; + if (attrs instanceof Element) + obj = elements.get(attrs); + else if (attrs instanceof ComplexType) + obj = complexTypes.get(attrs); + else if (attrs instanceof SimpleType) + obj = simpleTypes.get(attrs); + if (obj == null){ + throw new RuntimeException("Cannot locate referred object " + attrs + " when handling " + referrer.getName()); + } + return obj; + } + + private void preload() { + Deque stack = new ArrayDeque(); + //stack.addAll(schema.getSimpleTypeOrComplexTypeOrGroup()); + for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { + if (attrs instanceof Element) { + Element element = (Element)attrs; + SchemaObject obj = new SchemaObject(element); + stack.push(obj); + } else if (attrs instanceof ComplexType) { + ComplexType complexType = (ComplexType)attrs; + SchemaObject obj = new SchemaObject(complexType); + stack.push(obj); + } else if (attrs instanceof SimpleType) { + SimpleType simpleType = (SimpleType)attrs; + SchemaObject obj = new SchemaObject(simpleType); + stack.push(obj); + } + } + + while (!stack.isEmpty()) { + SchemaObject object = stack.pop(); + if (object.getType() == ObjectType.COMPLEX_TYPE) { + ComplexType ct = object.getComplexType(); + if (ct.getName() != null && ct.getName().length() > 0 && ct instanceof TopLevelComplexType) + complexTypeName.put(ct.getName(), object); + complexTypes.put(ct, object); + if (ct.getChoice() != null) { + preload(object,ct.getChoice(), stack); + } + if (ct.getSequence() != null) { + preload(object,ct.getSequence(), stack); + } + if (ct.getAll() != null) { + preload(object,ct.getAll(), stack); + } + if (ct.getGroup() != null) + throw new RuntimeException("Groups not supported"); + if (ct.getComplexContent() != null) { + ComplexContent cc = ct.getComplexContent(); + ExtensionType extensionType = cc.getExtension(); + if (extensionType != null) { + if (extensionType.getChoice() != null) { + preload(object,extensionType.getChoice(), stack); + } + if (extensionType.getSequence()!= null) { + preload(object,extensionType.getSequence(), stack); + } + if (extensionType.getAll()!= null) { + preload(object,extensionType.getAll(), stack); + } + if (extensionType.getGroup() != null) + throw new RuntimeException("Groups not supported"); + } + } + } else if (object.getType() == ObjectType.ELEMENT) { + Element e = object.getElement(); + if (e instanceof TopLevelElement) + elementName.put(e.getName(), object); + elements.put(e, object); + if (e.getComplexType() != null) + stack.push(new SchemaObject(object,e.getComplexType())); + if (e.getSimpleType() != null) + stack.push(new SchemaObject(object,e.getSimpleType())); + } else if (object.getType() == ObjectType.SIMPLE_TYPE) { + SimpleType e = object.getSimpleType(); + if (e instanceof TopLevelSimpleType) + simpleTypeName.put(e.getName(), object); + simpleTypes.put(e, object); + } + } + } + + private void preload(SchemaObject parent,ExplicitGroup eg, Deque stack) { + for (Object o : eg.getParticle()) { + if (o instanceof JAXBElement) { + JAXBElement element = (JAXBElement)o; + Object elemValue = element.getValue(); + if (elemValue instanceof Element) { + stack.add(new SchemaObject(parent,(Element)elemValue)); + } else if (elemValue instanceof All) { + preload(parent,(All)elemValue, stack); + } else if (elemValue instanceof ExplicitGroup) { + preload(parent,(ExplicitGroup)elemValue, stack); + } else { + throw new RuntimeException("Unknown ExplicitGroup element " + elemValue.getClass().getName()); + } + } else if (o instanceof Any){ + + } else { + throw new RuntimeException("Unknown ExplicitGroup reference " + o.getClass().getName()); + } + } + } + + protected void handle(TopLevelAttribute topLevelAttribute) { + handle(null, topLevelAttribute); + } + + protected void handleSimpleType(SchemaObject topLevelSimpleType) { + handleSimpleType(null,topLevelSimpleType); + } + + protected void handle(NamedAttributeGroup namedAttributeGroup){ + handle(null, namedAttributeGroup); + } + + protected QName getComplexTypeBase(ComplexType complexType) { + if (complexType == null) + return null; + ComplexContent complexContent = complexType.getComplexContent(); + if (complexContent != null) { + ExtensionType extensionType = complexContent.getExtension(); + if (extensionType != null) { + QName type = extensionType.getBase(); + return type; + } + } + return null; + } + + protected QName getSimpleTypeBase(SimpleType simpleType) { + if (simpleType == null) + return null; + return simpleType.getRestriction().getBase(); + } + + protected QName getElementBase(Element element) { + ComplexType complexType = element.getComplexType(); + SimpleType simpleType = element.getSimpleType(); + if (complexType != null) + return getComplexTypeBase(complexType); + if (simpleType != null) { + return getSimpleTypeBase(simpleType); + } + return null; + } + + + + + + + + + private void handleAttributes(SchemaObject complexType, List attributeOrAttributeGroup) { + //name = getComplexTypePrefix()+complexType.getName() + + Set handled = handleAttributeCompositions(complexType,attributeOrAttributeGroup); + for (Annotated annotated : attributeOrAttributeGroup) { + if (handled.contains(annotated)) + continue; + if (annotated instanceof Attribute) { + handle(complexType,(Attribute)annotated); + } else if (annotated instanceof AttributeGroup){ + handle(complexType,(AttributeGroup)annotated); + //comment("AttributeGroup " + ((AttributeGroup)annotated).getRef().getLocalPart()); + } else { + throw new RuntimeException(); + } + } + } + + protected abstract void handleAttributes(SchemaObject simpleTypeObj); + + protected void handleExtensionAttributes(SchemaObject complexType) { + ComplexContent complexContent = complexType.getComplexType().getComplexContent(); + if (complexContent != null) { + ExtensionType extensionType = complexContent.getExtension(); + if (extensionType != null) { + handleAttributes(complexType, extensionType.getAttributeOrAttributeGroup()); + } + } + } + + + + protected void handleComplexTypeAttributes(SchemaObject complexType) { + handleAttributes(complexType,complexType.getComplexType().getAttributeOrAttributeGroup()); + } + + protected void handleElementComplexTypeAttributes(SchemaObject complexType) { + if (complexType != null) { + handleComplexTypeAttributes(complexType); + handleExtensionAttributes(complexType); + } + } + + protected void handleElementSimpleTypeAttributes(SchemaObject simpleType) { + if (simpleType != null) { + handleAttributes(simpleType); + } + } + + protected Set handleAttributeCompositions(SchemaObject obj, List attributeOrAttributeGroup) { + + Set handled = new HashSet(); + for (JAXBElement e : configuration.getConversionRule()) { + if (e.getValue() instanceof AttributeComposition) { + AttributeComposition composition = (AttributeComposition)e.getValue(); + if (composition.getAttribute().size() < 2) + throw new RuntimeException("Attribute Composition is not valid"); + BijectionMap map = new BijectionMap(); + for (org.simantics.xml.sax.configuration.Attribute a : composition.getAttribute()) { + for (Annotated annotated : attributeOrAttributeGroup) { + if (annotated instanceof Attribute) { + Attribute attribute = (Attribute)annotated; + QName type = getBaseType(attribute); + if (a.getName().equals(attribute.getName()) && type != null && a.getType().equals(type.getLocalPart())) { + map.map(a, attribute); + } + } + } + } + if (composition.getAttribute().size() == map.size()) { + handled.addAll(map.getRightSet()); + handleAttributeComposition(obj, composition, map); + } + } + } + return handled; + } + + protected QName getBaseType(Attribute attribute) { + if (attribute.getType() != null) + return attribute.getType(); + if (attribute.getRef() != null) + return attribute.getRef(); + SimpleType simpleType = attribute.getSimpleType(); + if (simpleType != null) { + Restriction restriction = simpleType.getRestriction(); + if (restriction != null) + if (restriction.getBase() != null) + return restriction.getBase(); + } + return null; + } + + protected Attribute getRefAttribute(QName ref) { + for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { + if (attrs instanceof TopLevelAttribute) { + TopLevelAttribute attribute = (TopLevelAttribute)attrs; + if (attribute.getName().equals(ref.getLocalPart())) + return attribute; + } + } + return null; + } + + protected abstract void handleAttributeComposition(SchemaObject obj, AttributeComposition composition, BijectionMap attributes); + + + + + protected void handleComplexType(SchemaObject complexType) { + handleComplexTypeAttributes(complexType); + handleComplexTypeExtension(complexType); + handleExtensionAttributes(complexType); + } + + protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String refName, QName refType); + protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String refName, OpenAttrs ref); + protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any); + protected abstract void handle(SchemaObject parent, SchemaElement indicator, List elements); + + protected void handle(SchemaObject parent, ExplicitGroup eg, SchemaElement.ElementType indicator) { + handle(parent, new SchemaElement(eg, indicator)); + } + + protected void handle(SchemaObject parent, SchemaElement indicator) { + + + List elements = new ArrayList(); + List choices = new ArrayList(); + List sequences = new ArrayList(); + List alls = new ArrayList(); + List anys = new ArrayList(); + + for (Object o : indicator.getGroup().getParticle()) { + if (o instanceof JAXBElement) { + JAXBElement element = (JAXBElement)o; + Object elemValue = element.getValue(); + if (elemValue instanceof LocalElement) { + LocalElement localElement = (LocalElement)elemValue; + + elements.add(new SchemaElement(indicator,localElement, ElementType.ELEMENT)); + } else if (elemValue instanceof All) { + alls.add(new SchemaElement(indicator,(All)elemValue, ElementType.ALL)); + } else if (elemValue instanceof ExplicitGroup) { + QName qname = element.getName(); + if ("choice".equals(qname.getLocalPart())) { + choices.add(new SchemaElement(indicator,(ExplicitGroup)elemValue, ElementType.CHOICE)); + } else if ("sequence".equals(qname.getLocalPart())) { + sequences.add(new SchemaElement(indicator,(ExplicitGroup)elemValue, ElementType.SEQUENCE)); + } + } else { + throw new RuntimeException("Unknown ExplicitGroup element " + elemValue.getClass().getName()); + } + } else if (o instanceof Any){ + anys.add(new SchemaElement(indicator,(Any)o, ElementType.ANY)); + } else { + throw new RuntimeException("Unknown ExplicitGroup reference " + o.getClass().getName()); + } + } + + if (elements.size() == 0 && choices.size() == 0 && sequences.size() == 0 && alls.size() == 0 && anys.size() == 0) { + return; + } + + if (indicator.getType() == SchemaElement.ElementType.SEQUENCE) { + if (indicator.getRestriction().single()) { + if (elements.size() > 0) { + for (SchemaElement e : sequences) { + handle(parent, e); + } + for (SchemaElement c : choices) { + handle(parent, c); + } + + for (SchemaElement c : alls) { + handle(parent, c); + } + handle(parent, indicator, elements); + for (SchemaElement a : anys) { + handleIndicator(parent, indicator, a); + } + } else { + if (sequences.size() > 0) { + throw new RuntimeException("Cannot handle Sequence with inner Sequences"); + } + for (SchemaElement c : choices) { + handle(parent, c); + } + for (SchemaElement a : anys) { + handleIndicator(parent, indicator, a); + } + } + } else { + if (sequences.size() > 0 || choices.size() > 0 || alls.size() > 0) { + throw new RuntimeException("Cannot handle Sequence with inner ExplicitGroups"); + } + handle(parent, indicator, elements); + for (SchemaElement a : anys) { + handleIndicator(parent, indicator, a); + } + } + + } else if (indicator.getType() == SchemaElement.ElementType.CHOICE){ + if (indicator.getRestriction().single()) { + if (sequences.size()> 0 || choices.size() > 0 || alls.size() > 0 || anys.size() > 0) { + throw new RuntimeException("Cannot handle Choice that contains something else than Elements"); + } + handle(parent, indicator, elements); + + } else { + if (sequences.size() > 0 || choices.size() > 0) { + throw new RuntimeException("Cannot handle Choice with inner ExplicitGroups"); + } + handle(parent, indicator, elements); + for (SchemaElement a : anys) { + handleIndicator(parent, indicator, a); + } + } + } else if (indicator.getType() == ElementType.ALL) { + if (sequences.size()> 0 || choices.size() > 0 || alls.size() > 0 || anys.size() > 0) { + throw new RuntimeException("Cannot handle All that contains something else than Elements"); + } + if (!indicator.getRestriction().single()) { + throw new RuntimeException("All indicator must have maxOccurs=1"); + } + handle(parent, indicator, elements); + } + } + + + protected void handle(SchemaObject parent, SchemaElement indicator, SchemaElement element) { + Element localElement = element.getElement(); + if (localElement.getName() != null) { + String refName = localElement.getName(); + QName refType = localElement.getType(); + if (refType != null) + handleIndicator(parent, indicator, element, false, refName, refType); + else { + handleElement(elements.get(localElement)); + handleIndicator(parent, indicator, element, false, refName, localElement); + //FIXME: + } + } else if (localElement.getRef() != null) { + QName refType = localElement.getRef(); + handleIndicator(parent, indicator,element, true, refType.getLocalPart(), refType); + } + } + + protected String getElementName(Element localElement) { + if (localElement.getName() != null) { + String refName = localElement.getName(); + QName refType = localElement.getType(); + if (refType != null) + return refName; + } else if (localElement.getRef() != null) { + QName refType = localElement.getRef(); + if (refType != null) + return refType.getLocalPart(); + } + return null; + } + + protected String getChoiceName(List elements) { + if (elements.size() == 1) { + return getElementName(elements.get(0).getElement()); + } + List names = new ArrayList(); + for (SchemaElement e : elements) { + String name = getElementName(e.getElement()); + if (name != null) + names.add(name); + } + String name = ""; + for (int i = 0; i < names.size(); i++) { + if (i == 0) + name = names.get(i); + else + name += "Or"+names.get(i); + } + return name; + } + + + protected abstract void handle(SchemaObject parent, Attribute attribute) ; + protected abstract void handle(SchemaObject parent, AttributeGroup attribute) ; + + protected abstract void handleSimpleType(SchemaObject parent, SchemaObject simpleType); + + + + protected void handleComplexTypeExtension(SchemaObject complexTypeObj) { + ComplexType complexType = complexTypeObj.getComplexType(); + if (complexType != null) { + if (complexType.getChoice() != null) + handle(complexTypeObj, complexType.getChoice(), SchemaElement.ElementType.CHOICE); + if (complexType.getSequence() != null) + handle(complexTypeObj, complexType.getSequence(), SchemaElement.ElementType.SEQUENCE); + if (complexType.getAll() != null) + handle(complexTypeObj, complexType.getAll(), SchemaElement.ElementType.ALL); + if (complexType.getGroup() != null) + throw new RuntimeException("Groups not supported"); + ComplexContent complexContent = complexType.getComplexContent(); + if (complexContent != null) { + ExtensionType extensionType = complexContent.getExtension(); + if (extensionType != null) { + if (extensionType.getChoice() != null) { + handle(complexTypeObj, extensionType.getChoice(), SchemaElement.ElementType.CHOICE); + } + if (extensionType.getSequence()!= null) { + handle(complexTypeObj, extensionType.getSequence(), SchemaElement.ElementType.SEQUENCE); + } + if (extensionType.getAll()!= null) { + handle(complexTypeObj, extensionType.getAll(), SchemaElement.ElementType.ALL); + } + if (extensionType.getGroup() != null) { + throw new RuntimeException("Groups not supported"); + } + } + } + } + } + + protected void handleElement(SchemaObject topLevelElement) { + LocalComplexType complexType = topLevelElement.getElement().getComplexType(); + + if (complexType != null) { + SchemaObject complextTypeObj = complexTypes.get(complexType); + handleElementComplexTypeAttributes(complextTypeObj); + handleComplexTypeExtension(complextTypeObj); + } + + + } + + + protected boolean isElementRef(String ref) { +// for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { +// if (attrs instanceof TopLevelElement) { +// TopLevelElement element = (TopLevelElement)attrs; +// if (ref.equals(element.getName())) +// return true; +// } +// } +// return false; + return elementName.containsKey(ref); + } + + protected boolean isComplexTypeRef(String ref) { +// for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { +// if (attrs instanceof TopLevelComplexType) { +// TopLevelComplexType element = (TopLevelComplexType)attrs; +// if (ref.equals(element.getName())) +// return true; +// } +// } +// return false; + return complexTypeName.containsKey(ref); + + } + + protected NamedAttributeGroup getAttributeGroup(String name) { + for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { + if (attrs instanceof NamedAttributeGroup) { + NamedAttributeGroup group = (NamedAttributeGroup)attrs; + if (group.getName().equals(name)) + return group; + } + } + return null; + } + + protected IDProvider getIDProvider(Element element) { + List idProviders = new ArrayList(2); + for (JAXBElement e : configuration.getConversionRule()) { + if (e.getValue() instanceof IDProvider) { + IDProvider ref = (IDProvider)e.getValue(); + org.simantics.xml.sax.configuration.Element element2 = ref.getElement(); + if (element2 != null) { + if (element.getName().equals(element2.getName())) + idProviders.add(ref); + } + + } + } + if (idProviders.size() == 0) + return null; + if (idProviders.size() > 1) + throw new RuntimeException("Element " + element.getName() + " contains " + idProviders.size() + " id provider rules, only one is allowed."); + return idProviders.get(0); + } + + protected IDProvider getIDProvider(ComplexType complexType) { + List idProviders = new ArrayList(2); + for (JAXBElement e : configuration.getConversionRule()) { + if (e.getValue() instanceof IDProvider) { + IDProvider ref = (IDProvider)e.getValue(); + org.simantics.xml.sax.configuration.ComplexType complexType2 = ref.getComplexType(); + if (complexType2 != null) { + if (complexType.getName().equals(complexType2.getName())) + idProviders.add(ref); + } + + } + } + if (idProviders.size() == 0) + return null; + if (idProviders.size() > 1) + throw new RuntimeException("Element " + complexType.getName() + " contains " + idProviders.size() + " id provider rules, only one is allowed."); + return idProviders.get(0); + } + + protected List getIDReferences(Element element) { + List idReferences = new ArrayList(2); + for (JAXBElement e : configuration.getConversionRule()) { + if (e.getValue() instanceof IDReference) { + IDReference ref = (IDReference)e.getValue(); + org.simantics.xml.sax.configuration.Element element2 = ref.getElement(); + if (element2 != null) { + if (element.getName().equals(element2.getName())) + idReferences.add(ref); + } + } + } + return idReferences; + } + + protected List getIDReferences(ComplexType complexType) { + List idReferences = new ArrayList(2); + for (JAXBElement e : configuration.getConversionRule()) { + if (e.getValue() instanceof IDReference) { + IDReference ref = (IDReference)e.getValue(); + org.simantics.xml.sax.configuration.ComplexType complexType2 = ref.getComplexType(); + if (complexType2 != null) { + if (complexType.getName().equals(complexType2.getName())) + idReferences.add(ref); + } + } + } + return idReferences; + } + + public UnrecognizedChildElement getUnknown(ComplexType complexType) { + for (JAXBElement e : configuration.getConversionRule()) { + if (e.getValue() instanceof UnrecognizedChildElement) { + UnrecognizedChildElement ref = (UnrecognizedChildElement)e.getValue(); + org.simantics.xml.sax.configuration.ComplexType complexType2 = ref.getComplexType(); + if (complexType2 != null) { + if (complexType.getName().equals(complexType2.getName())) + return ref; + } + } + } + return null; + } + + public UnrecognizedChildElement getUnknown(Element element) { + for (JAXBElement e : configuration.getConversionRule()) { + if (e.getValue() instanceof UnrecognizedChildElement) { + UnrecognizedChildElement ref = (UnrecognizedChildElement)e.getValue(); + org.simantics.xml.sax.configuration.Element element2 = ref.getElement(); + if (element2 != null) { + if (element.getName().equals(element2.getName())) + return ref; + } + } + } + return null; + } + + + public boolean useOriginalList(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String ref, QName refType) { + if (parent.getName() == null) + parent = parent.getParent(); + if (parent.getName().contains("PipingNetworkSegment")) + System.out.println(); + for (JAXBElement e : configuration.getConversionRule()) { + if (e.getValue() instanceof OrderedChild) { + OrderedChild oc = (OrderedChild)e.getValue(); + org.simantics.xml.sax.configuration.Element element2 = oc.getElement(); + org.simantics.xml.sax.configuration.ComplexType complexType = oc.getComplexType(); + org.simantics.xml.sax.configuration.Element child = oc.getChild(); + if (!oc.getType().equals("original")) + continue; + boolean match = false; + if (element2 != null) { + if (parent.getType() == ObjectType.ELEMENT && parent.getName().equals(element2.getName())) { + match = true; + } + } else if (complexType != null) { + if (parent.getType() == ObjectType.COMPLEX_TYPE && parent.getName() != null && parent.getName().equals(complexType.getName())) { + match = true; + } + + } + if (match) { + if (child != null) { + if (matchChild(child, ref, refType)) { + if (oc.getValue().equals("disable")) + return false; + else return true; + } + } else { + if (oc.getValue().equals("disable")) + return false; + return true; + } + + } + } + } + return indicator.order(); + } + + public boolean useElementList(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String refName, QName refType) { + if (parent.getName() == null) + parent = parent.getParent(); + if (parent.getName() == "PipingNetworkSegment") + System.out.println(); + for (JAXBElement e : configuration.getConversionRule()) { + if (e.getValue() instanceof OrderedChild) { + OrderedChild oc = (OrderedChild)e.getValue(); + org.simantics.xml.sax.configuration.Element element2 = oc.getElement(); + org.simantics.xml.sax.configuration.ComplexType complexType = oc.getComplexType(); + org.simantics.xml.sax.configuration.Element child = oc.getChild(); + if (!oc.getType().equals("child")) + continue; + boolean match = false; + if (element2 != null) { + if (parent.getType() == ObjectType.ELEMENT && parent.getName().equals(element2.getName())) { + match = true; + } + } else if (complexType != null) { + if (parent.getType() == ObjectType.COMPLEX_TYPE && parent.getName() != null && parent.getName().equals(complexType.getName())) { + match = true; + } + + } + if (match) { + if (child != null) { + if (matchChild(child, refName, refType)) { + if (oc.getValue().equals("disable")) + return false; + else return true; + } + } else { + if (oc.getValue().equals("disable")) + return false; + return true; + } + + } + } + } + return element.many() && element.order(); + } + + private boolean matchChild(org.simantics.xml.sax.configuration.Element child, String refName, QName refType) { + if (refType != null && refType.getLocalPart().equals(child.getName())) + return true; + if (refName != null && refName.equals(child.getName())) + return true; + return false; + } + + public static class TypeEntry { + String l0Type; + String binding; + String javaType; + String defaultValue; + boolean id; + public TypeEntry(String l0Type, String binding, String javaType, String defaultValue) { + super(); + this.l0Type = l0Type; + this.binding = binding; + this.javaType = javaType; + this.defaultValue = defaultValue; + this.id = false; + } + + public TypeEntry(String l0Type, String binding, String javaType, String defaultValue, boolean id) { + super(); + this.l0Type = l0Type; + this.binding = binding; + this.javaType = javaType; + this.defaultValue = defaultValue; + this.id = id; + } + + } + +}