X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;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=1e13951da7d8f522c47558e872534da51a474fb4;hb=7f59891d2747280b09e77bc833bc70e05eece706;hp=2bca88d64f5dfd666fee38f5d5cdf6a956fc4e84;hpb=5a290a430279a97270c968f18fd667766672e7ba;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 index 2bca88d..1e13951 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java @@ -1,1466 +1,1487 @@ -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.Rename; -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.GroupRef; -import org.w3._2001.xmlschema.LocalElement; -import org.w3._2001.xmlschema.NamedAttributeGroup; -import org.w3._2001.xmlschema.NamedGroup; -import org.w3._2001.xmlschema.OpenAttrs; -import org.w3._2001.xmlschema.RealGroup; -import org.w3._2001.xmlschema.Restriction; -import org.w3._2001.xmlschema.Schema; -import org.w3._2001.xmlschema.SimpleContent; -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; -import org.w3._2001.xmlschema.Union; - -public final class SchemaConversionBase { - - protected Schema schema; - protected SchemaConverter converter; - protected SchemaConversionComponent component; - protected Configuration configuration; - - public static final String SCHEMA_NS = "http://www.w3.org/2001/XMLSchema"; - public static final String CONVERSION_NS = "http://www.simantics.org/Layer0"; - - protected Map> typeMap; - - protected String ontologyURI; - protected String className; - - public SchemaConversionBase(SchemaConverter converter, String ontologyUri, String className) { - this.converter = converter; - this.configuration = converter.getConfiguration(); - this.ontologyURI = ontologyUri; - this.className = className; - - initTypes(); - } - - protected void initTypes() { - 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", "Bindings.STRING", "java.lang.String", "","","","","")); - schemaTypes.put("NMTOKEN", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); - schemaTypes.put("token", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); - schemaTypes.put("ID", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","",true)); - schemaTypes.put("IDREF", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); - schemaTypes.put("Name", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); - schemaTypes.put("NCName", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); - 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()")); - 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()")); - 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()")); - schemaTypes.put("anyURI", new TypeEntry("L0.URI", "Bindings.STRING", "java.lang.String", "","","","","")); - schemaTypes.put("double", new TypeEntry("L0.Double", "Bindings.DOUBLE", "double", "java.lang.Double.NaN","java.lang.Double.parseDouble(",")","java.lang.Double.toString(",")")); - schemaTypes.put("float", new TypeEntry("L0.Float", "Bindings.FLOAT", "float", "java.lang.Float.NaN","java.lang.Float.parseFloat(",")","java.lang.Float.toString(",")")); - schemaTypes.put("decimal", new TypeEntry("L0.Double", "Bindings.DOUBLE", "double", "java.lang.Double.NaN","java.lang.Double.parseDouble(",")","java.lang.Double.toString(",")")); - schemaTypes.put("boolean", new TypeEntry("L0.Boolean", "Bindings.BOOLEAN", "boolean", "false","java.lang.Boolean.parseBoolean(",")","java.lang.Boolean.toString(",")")); - schemaTypes.put("integer", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); - schemaTypes.put("positiveInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); - schemaTypes.put("nonPositiveInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); - schemaTypes.put("nonNegativeInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); - schemaTypes.put("negativeInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); - schemaTypes.put("unsignedInt", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); - schemaTypes.put("int", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); - schemaTypes.put("short", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); - schemaTypes.put("unsignedShort",new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); - schemaTypes.put("byte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","java.lang.Byte.parseByte(",")","java.lang.Byte.toString(",")")); - schemaTypes.put("unsignedByte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","java.lang.Byte.parseByte(",")","java.lang.Byte.toString(",")")); - schemaTypes.put("long", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")","java.lang.Long.toString(",")")); - schemaTypes.put("unsignedLong", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")","java.lang.Long.toString(",")")); - 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)")); - - l0Types.put("doubleArray", new TypeEntry("L0.DoubleArray", "Bindings.DOUBLE_ARRAY", "double[]", null,null,null,"java.lang.Double.toString(",")")); - l0Types.put("stringArray", new TypeEntry("L0.StringArray", "Bindings.STRING_ARRAY", "string[]", null,null,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 TypeEntry getTypeEntry(String type) { - for (Map types : typeMap.values()) { - TypeEntry entry = types.get(type); - if (entry != null) - return entry; - } - return null; - } - - 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; - } - - - public void init(Schema schema) { - this.schema = schema; - - preload(); - } - - public void handle(SchemaConversionComponent component) { - this.component = component; - 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 if (attrs instanceof NamedGroup) { - handle((NamedGroup)attrs); - } else { - System.out.println(attrs.getClass().getName()); - } - } - } - - private Map elementName = new HashMap<>(); - private Map complexTypeName = new HashMap<>(); - private Map simpleTypeName = new HashMap<>(); - private Map modelGroupName = new HashMap<>(); - private Map elements = new HashMap<>(); - private Map complexTypes = new HashMap<>(); - private Map simpleTypes = new HashMap<>(); - private Map modelGroups = new HashMap<>(); - - private SchemaObject _getWithName(QName name) { - SchemaObject obj = elementName.get(name.getLocalPart()); - if (obj == null) - obj = complexTypeName.get(name.getLocalPart()); - if (obj == null) - obj = simpleTypeName.get(name.getLocalPart()); - return obj; - } - - protected SchemaObject getWithName(QName name) { - SchemaObject obj = _getWithName(name); - if (obj != null) - return obj; - if (name.getNamespaceURI() != null) { - for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { - if (sc.base != null) { - obj = sc.base._getWithName(name); - if (obj != null) { - return obj; - } - } - } - } - return null; - } - - private NamedAttributeGroup _getAttributeGroup(QName name) { - for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { - if (attrs instanceof NamedAttributeGroup) { - NamedAttributeGroup group = (NamedAttributeGroup)attrs; - if (group.getName().equals(name.getLocalPart())) - return group; - } - } - return null; - } - - public NamedAttributeGroup getAttributeGroup(QName name) { - NamedAttributeGroup group = _getAttributeGroup(name); - if (group != null) - return group; - if (name.getNamespaceURI() != null) { - for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { - if (sc.base != null) { - group = sc.base._getAttributeGroup(name); - if (group != null) { - return group; - } - } - } - } - return null; - } - - private SchemaObject _getElement(QName name) { - return elementName.get(name.getLocalPart()); - } - - protected SchemaObject getElement(QName name) { - SchemaObject obj = _getElement(name); - if (obj != null) - return obj; - if (name.getNamespaceURI() != null) { - for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { - if (sc.base != null) { - obj = sc.base._getElement(name); - if (obj != null) { - return obj; - } - } - } - } - return null; - } - - protected SchemaObject getElement(Element element) { - return elements.get(element); - } - - - private SchemaObject _getComplexType(QName name) { - return complexTypeName.get(name.getLocalPart()); - } - - protected SchemaObject getComplexType(QName name) { - SchemaObject obj = _getComplexType(name); - if (obj != null) - return obj; - if (name.getNamespaceURI() != null) { - for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { - if (sc.base != null) { - obj = sc.base._getComplexType(name); - if (obj != null) { - return obj; - } - } - } - } - return null; - } - - protected SchemaObject getComplexType(ComplexType complexType) { - return complexTypes.get(complexType); - } - - private SchemaObject _getSimpleType(QName name) { - return simpleTypeName.get(name.getLocalPart()); - } - - protected SchemaObject getSimpleType(QName name) { - SchemaObject obj = _getSimpleType(name); - if (obj != null) - return obj; - if (name.getNamespaceURI() != null) { - for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { - if (sc.base != null) { - obj = sc.base._getSimpleType(name); - if (obj != null) { - return obj; - } - } - } - } - return null; - } - - protected SchemaObject getSimpleType(SimpleType simpleType) { - return simpleTypes.get(simpleType); - } - - protected SchemaObject getWithObj(SchemaObject referrer, OpenAttrs attrs) { - // FIXME : this method cannot handle references to other schemas. - 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); - obj.setRename(getRename(element)); - stack.push(obj); - } else if (attrs instanceof ComplexType) { - ComplexType complexType = (ComplexType)attrs; - SchemaObject obj = new SchemaObject(complexType); - obj.setRename(getRename(complexType)); - stack.push(obj); - } else if (attrs instanceof SimpleType) { - SimpleType simpleType = (SimpleType)attrs; - SchemaObject obj = new SchemaObject(simpleType); - stack.push(obj); - } else if (attrs instanceof Attribute) { - // Attributes are not cached - } else if (attrs instanceof AttributeGroup) { - // Attribute groups are not cached - } else if (attrs instanceof NamedGroup) { - NamedGroup group = (NamedGroup)attrs; - SchemaObject obj = new SchemaObject(group); - stack.push(obj); - } else { - System.out.println(attrs.getClass().getName()); - } - } - - while (!stack.isEmpty()) { - SchemaObject object = stack.pop(); - switch (object.getType()) { - case 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"); - } - } - if (ct.getSimpleContent() != null) { - SimpleContent cc = ct.getSimpleContent(); - 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"); - } - } - break; - } - case 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())); - break; - } - case SIMPLE_TYPE:{ - SimpleType e = object.getSimpleType(); - if (e instanceof TopLevelSimpleType) - simpleTypeName.put(e.getName(), object); - simpleTypes.put(e, object); - break; - } - case MODEL_GROUP:{ - NamedGroup e = object.getModelGroup(); - modelGroupName.put(e.getName(), object); - modelGroups.put(e, object); - break; - } - } - } // while - } - - 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) { - SchemaObject obj = new SchemaObject(parent,(Element)elemValue); - obj.setRename(getRename((Element)elemValue)); - stack.add(obj); - } else if (elemValue instanceof ExplicitGroup) { - preload(parent,(ExplicitGroup)elemValue, stack); - } else if (elemValue instanceof RealGroup) { - preload(parent,(RealGroup)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()); - } - } - } - - private void preload(SchemaObject parent, RealGroup eg, Deque stack) { - System.out.println(eg); - if (eg instanceof NamedGroup) { - SchemaObject obj = new SchemaObject(parent,(NamedGroup)eg); - stack.add(obj); - } - } - - - 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 void handle(NamedGroup 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(); - - Restriction restriction = simpleType.getRestriction(); - if (restriction != null) { - QName base = restriction.getBase(); - return base; - } else if (simpleType.getId() != null) { - throw new RuntimeException(simpleType.getName() + " restriction error"); - } else if (simpleType.getUnion() != null) { - Union union = simpleType.getUnion(); - if (union.getMemberTypes().size() > 0) { - QName base = null; - for (QName type : union.getMemberTypes()) { - QName sType = null; - TypeEntry entry = getTypeEntry(type); - if (entry == null) { - SchemaObject obj = simpleTypeName.get(type.getLocalPart()); - if (obj == null) - throw new RuntimeException(simpleType.getName() + " union has unresolved reference " + type.getLocalPart()); - sType = getSimpleTypeBase(obj.getSimpleType()); - } else { - sType = type; - } - if (base == null) - base = sType; - else if (!base.equals(sType)) { - //FIXME : throw new RuntimeException(simpleType.getName() + " union has incompatible member types"); - // fall back to string. - base = new QName(SCHEMA_NS, "string"); - - } - } - return base; - } else { - if (union.getSimpleType().size() == 0) - throw new RuntimeException(simpleType.getName() + " union error"); - for (SimpleType s : union.getSimpleType()) { - if (restriction == null) - restriction = s.getRestriction(); - else { - Restriction r = s.getRestriction(); - if (!r.getBase().equals(restriction.getBase())) - throw new RuntimeException(simpleType.getName() + " union has incompatible restriction bases"); - } - } - QName base = restriction.getBase(); - return base; - } - } else if (simpleType.getList() != null) { - // FIXME: callers cannot get the information that we have a list. - org.w3._2001.xmlschema.List list = simpleType.getList(); - return list.getItemType(); - } else { - throw new RuntimeException(simpleType.getName() + " restriction error"); - } - } - - 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 void handleAttributes(SchemaObject simpleTypeObj) { - component.handleAttributes(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()); - } - } - SimpleContent simpleContent = complexType.getComplexType().getSimpleContent(); - if (simpleContent != null) { - ExtensionType extensionType = simpleContent.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 QName getPrimitiveType(Attribute attribute) { - QName type = getBaseType(attribute); - String b = getBindingFromPrimitiveType(type); - while (b==null && type != null) { - SchemaObject baseType = simpleTypeName.get(type.getLocalPart()); - if (baseType != null) { - Restriction restriction = baseType.getSimpleType().getRestriction(); - if (restriction != null) - if (restriction.getBase() != null) { - type = restriction.getBase(); - b = getBindingFromPrimitiveType(type); - } - } - } - return type; - } - - 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 handleAttributeComposition(SchemaObject obj, AttributeComposition composition, BijectionMap attributes) { - component.handleAttributeComposition(obj, composition, attributes); - } - - - - - protected void handleComplexType(SchemaObject complexType) { -// handleComplexTypeAttributes(complexType); -// handleComplexTypeExtension(complexType); -// handleExtensionAttributes(complexType); - component.handleComplexType(complexType); - } - - protected void handleElement(SchemaObject topLevelElement) { -// LocalComplexType complexType = topLevelElement.getElement().getComplexType(); -// -// if (complexType != null) { -// SchemaObject complextTypeObj = complexTypes.get(complexType); -// handleElementComplexTypeAttributes(complextTypeObj); -// handleComplexTypeExtension(complextTypeObj); -// } - component.handleElement(topLevelElement); - } - - protected enum RefType{Element,Reference,Type}; - - protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType) { - component.handleIndicator(parent, indicator, element, refName, refType); - } - protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any) { - component.handleIndicator(parent, indicator, any); - } - protected void handle(SchemaObject parent, SchemaElement indicator, List elements) { - //component.handle(parent, indicator, elements); - if (indicator.getType() == SchemaElement.ElementType.SEQUENCE || (indicator.getType() == SchemaElement.ElementType.CHOICE && indicator.getRestriction().many())) { - for (SchemaElement e : elements) { - handle(parent, indicator, e); - } - } else if (indicator.getType() == SchemaElement.ElementType.CHOICE) { - String name = getChoiceName(elements); - component.handleChoice(parent, indicator, elements, name); - } - } - - 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(); - List groups = 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 if (elemValue instanceof RealGroup) { - if (elemValue instanceof GroupRef) { - groups.add(new SchemaElement(indicator,(GroupRef)elemValue, ElementType.GROUP_REF)); - } else if (elemValue instanceof NamedGroup) { - groups.add(new SchemaElement(indicator,(NamedGroup)elemValue, ElementType.NAMED_GROUP)); - } else { - throw new RuntimeException("Unknown ExplicitGroup element " + elemValue.getClass().getName()); - } - } 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 && groups.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); - } - - for (SchemaElement c : groups) { - 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); - } - for (SchemaElement c : groups) { - handle(parent, c); - } - } - } else { - if (choices.size() == 1 && sequences.size() == 0 && alls.size() == 0 && groups.size() == 0) { - // special case: handle lone choice inside sequence with maxOccurs > 1 - SchemaElement choice = choices.get(0); - // move multiplicity restrictions to choice - if (indicator.getRestriction().max == -1 || (choice.getRestriction().max > 0 && indicator.getRestriction().max > choice.getRestriction().max)) - choice.getRestriction().max = indicator.getRestriction().max; - if (indicator.getRestriction().min == 0 || choice.getRestriction().min > indicator.getRestriction().min) - choice.getRestriction().min = indicator.getRestriction().min; - handle(parent, choice, elements); - return; - } - if (sequences.size() > 0 || choices.size() > 0 || alls.size() > 0 || groups.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 || groups.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 || alls.size() > 0 || groups.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 || groups.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) { - SchemaObject eObj = elements.get(localElement); - QName refType = localElement.getType(); - if (refType != null) - handleIndicator(parent, indicator, element, null, RefType.Type); - else { - handleElement(eObj); - handleIndicator(parent, indicator, element, null, RefType.Element); - } - } else if (localElement.getRef() != null) { - handleIndicator(parent, indicator,element, null, RefType.Reference); - } - } - - 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 void handle(SchemaObject parent, Attribute attribute) { - component.handle(parent, attribute); - } - protected void handle(SchemaObject parent, AttributeGroup attribute) { - component.handle(parent, attribute); - } - protected void handle(SchemaObject parent, NamedGroup attribute){ - component.handle(parent, attribute); - }; - - protected void handleSimpleType(SchemaObject parent, SchemaObject simpleType) { - component.handleSimpleType(parent, 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"); - } - } - } -// SimpleContent simpleContent = complexType.getSimpleContent(); -// if (simpleContent != null) { -// ExtensionType extensionType = simpleContent.getExtension(); -// } - } - } - - - public boolean isElementRef(String ref) { - return elementName.containsKey(ref); - } - - public boolean isComplexTypeRef(String ref) { - return complexTypeName.containsKey(ref); - } - - public boolean isSimpleTypeRef(String ref) { - return simpleTypeName.containsKey(ref); - } - - public 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); - } - - public 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); - } - - public 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; - } - - public 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 rule = (UnrecognizedChildElement)e.getValue(); - org.simantics.xml.sax.configuration.ComplexType complexType2 = rule.getComplexType(); - if (complexType2 != null) { - if (complexType.getName().equals(complexType2.getName())) - return rule; - } - } - } - return null; - } - - public UnrecognizedChildElement getUnknown(Element element) { - for (JAXBElement e : configuration.getConversionRule()) { - if (e.getValue() instanceof UnrecognizedChildElement) { - UnrecognizedChildElement rule = (UnrecognizedChildElement)e.getValue(); - org.simantics.xml.sax.configuration.Element element2 = rule.getElement(); - if (element2 != null) { - if (element.getName().equals(element2.getName())) - return rule; - } - } - } - return null; - } - - public Rename getRename(Attribute element) { - for (JAXBElement e : configuration.getConversionRule()) { - if (e.getValue() instanceof Rename) { - Rename rule = (Rename)e.getValue(); - Object ref = rule.getElementOrComplexTypeOrAttribute().get(0); - if (!(ref instanceof org.simantics.xml.sax.configuration.Attribute)) - continue; - org.simantics.xml.sax.configuration.Attribute element2 = (org.simantics.xml.sax.configuration.Attribute)ref; - if (element2.getName().equals(element.getName())) { - return rule; - } - } - } - return null; - } - - public Rename getRename(ComplexType element) { - for (JAXBElement e : configuration.getConversionRule()) { - if (e.getValue() instanceof Rename) { - Rename rule = (Rename)e.getValue(); - Object ref = rule.getElementOrComplexTypeOrAttribute().get(0); - if (!(ref instanceof org.simantics.xml.sax.configuration.ComplexType)) - continue; - org.simantics.xml.sax.configuration.ComplexType element2 = (org.simantics.xml.sax.configuration.ComplexType)ref; - if (element2.getName().equals(element.getName())) { - return rule; - } - } - } - return null; - } - - public Rename getRename(Element element) { - for (JAXBElement e : configuration.getConversionRule()) { - if (e.getValue() instanceof Rename) { - Rename rule = (Rename)e.getValue(); - Object ref = rule.getElementOrComplexTypeOrAttribute().get(0); - if (!(ref instanceof org.simantics.xml.sax.configuration.Element)) - continue; - org.simantics.xml.sax.configuration.Element element2 = (org.simantics.xml.sax.configuration.Element)ref; - if (element2.getName().equals(element.getName())) { - return rule; - } - } - } - 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; - String getterPrefix; - String getterPostfix; - String stringPrefix; - String stringPostfix; - public TypeEntry(String l0Type, String binding, String javaType, String defaultValue, String getterPrefix, String getterPostfix, String stringPrefix, String stringPostfix) { - super(); - this.l0Type = l0Type; - this.binding = binding; - this.javaType = javaType; - this.defaultValue = defaultValue; - this.id = false; - this.getterPrefix = getterPrefix; - this.getterPostfix = getterPostfix; - this.stringPrefix = stringPrefix; - this.stringPostfix = stringPostfix; - } - - public TypeEntry(String l0Type, String binding, String javaType, String defaultValue, String getterPrefix, String getterPostfix, String stringPrefix, String stringPostfix, boolean id) { - super(); - this.l0Type = l0Type; - this.binding = binding; - this.javaType = javaType; - this.defaultValue = defaultValue; - this.id = id; - this.getterPrefix = getterPrefix; - this.getterPostfix = getterPostfix; - this.stringPrefix = stringPrefix; - this.stringPostfix = stringPostfix; - } - - public String getValueGetterMethod(String name) { - return getterPrefix + name + ".getValue()"+getterPostfix; - } - public String getValueGetter(String name) { - return getterPrefix + name+getterPostfix; - } - public String getValueGetter() - { - return getValueGetter("value"); - } - - public String getToString(String name) { - return stringPrefix +"("+javaType+")"+name+stringPostfix; - } - - public String getElementToString(String name) { - return stringPrefix + name+stringPostfix; - } - - } - - public enum InheritanceType{ComplexType,AtomicType,None}; - - public static class Inheritance { - public String baseClass; - public InheritanceType type; - public TypeEntry atomicType; - - public Inheritance(String baseClass) { - this.baseClass = baseClass; - this.type = InheritanceType.None; - } - } - - public String getComplexTypePrefix() { - return component.getComplexTypePrefix(); - } - public String getAttributeGroupPrefix() { - return component.getAttributeGroupPrefix(); - } - public String getName(SchemaObject obj) { - return component.getName(obj); - } - public String getBaseClass(ObjectType type) { - return component.getBaseClass(type); - } - - - - public Inheritance getInheritance(SchemaObject topLevelObj) { - Inheritance inheritance = null; - if (topLevelObj.getType() == ObjectType.ELEMENT) { - Element topLevelElement = topLevelObj.getElement(); - inheritance = new Inheritance(getBaseClass(ObjectType.ELEMENT)); - if (topLevelElement.getType() != null) { - QName type = topLevelElement.getType(); - if (!type.getNamespaceURI().equals(SCHEMA_NS)) { - SchemaObject obj = complexTypeName.get(type.getLocalPart()); - // if (obj == null) - // obj = simpleTypeName.get(type.getLocalPart()); - if (obj != null) { - inheritance.baseClass = getName(obj); - inheritance.type = InheritanceType.ComplexType; - } - } else { - TypeEntry entry = getTypeEntry(type); - if (entry != null) { - inheritance.type = InheritanceType.AtomicType; - inheritance.atomicType = entry; - } - } - } - if (inheritance.type == InheritanceType.None) { - QName type = getElementBase(topLevelElement); - if (type != null) { - if (!type.getNamespaceURI().equals(SCHEMA_NS)) { - SchemaObject obj = getWithName(type); - inheritance.baseClass = getName(obj); - inheritance.type = InheritanceType.ComplexType; - } else { - TypeEntry entry = getTypeEntry(type); - if (entry != null) { - inheritance.type = InheritanceType.AtomicType; - inheritance.atomicType = entry; - } - } - } - } - if (inheritance.type == InheritanceType.None) { - QName type = topLevelElement.getSubstitutionGroup(); - if (type != null) { - if (!type.getNamespaceURI().equals(SCHEMA_NS)) { - SchemaObject obj = getWithName(type); - inheritance.baseClass = getName(obj); - inheritance.type = InheritanceType.ComplexType; - } else { - TypeEntry entry = getTypeEntry(type); - if (entry != null) { - inheritance.type = InheritanceType.AtomicType; - inheritance.atomicType = entry; - } - } - } - } - } else if (topLevelObj.getType() == ObjectType.COMPLEX_TYPE) { - ComplexType complexType = topLevelObj.getComplexType(); - QName type = getComplexTypeBase(complexType); - inheritance = new Inheritance(getBaseClass(ObjectType.COMPLEX_TYPE)); - if (type != null && !type.getNamespaceURI().equals("http://www.w3.org/2001/XMLSchema")) { - SchemaObject obj = complexTypeName.get(type.getLocalPart()); - if (obj != null) { - inheritance.baseClass = getName(obj); - inheritance.type = InheritanceType.ComplexType; - } - } - SimpleContent simpleContent = complexType.getSimpleContent(); - if (simpleContent != null) { - ExtensionType extensionType = simpleContent.getExtension(); - if (extensionType != null) { - type = extensionType.getBase(); - getAtomicTypeInheritance(type, inheritance); - } - } - } - - return inheritance; - } - /** - * Goes through chain of SimpleTypes until locates Atomic Type (type defined in XML schema). - * @param type - * @param topLevelObj - * @param inheritance - */ - public void getAtomicTypeInheritance(QName type, Inheritance inheritance) { - if (!type.getNamespaceURI().equals(SCHEMA_NS)) { - SchemaObject obj = getWithName(type); - System.out.println(); - if (obj.getType() != ObjectType.SIMPLE_TYPE) - throw new RuntimeException("SimpleContent does not use SimpleType definition"); - SimpleType simpleType = obj.getSimpleType(); - type = getSimpleTypeBase(simpleType); - getAtomicTypeInheritance(type, inheritance); - } else { - TypeEntry entry = getTypeEntry(type); - if (entry != null) { - inheritance.type = InheritanceType.AtomicType; - inheritance.atomicType = entry; - } - } - } - - public String getDefaultValue(QName atype) { - Map types = typeMap.get(atype.getNamespaceURI()); - if (types == null) - return null; - TypeEntry entry = types.get(atype.getLocalPart()); - if (entry == null) - return null; - return entry.defaultValue; - } - -} +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.Rename; +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.GroupRef; +import org.w3._2001.xmlschema.LocalElement; +import org.w3._2001.xmlschema.NamedAttributeGroup; +import org.w3._2001.xmlschema.NamedGroup; +import org.w3._2001.xmlschema.OpenAttrs; +import org.w3._2001.xmlschema.RealGroup; +import org.w3._2001.xmlschema.Restriction; +import org.w3._2001.xmlschema.Schema; +import org.w3._2001.xmlschema.SimpleContent; +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; +import org.w3._2001.xmlschema.Union; + +public final class SchemaConversionBase { + + protected Schema schema; + protected SchemaConverter converter; + protected SchemaConversionComponent component; + protected Configuration configuration; + + public static final String SCHEMA_NS = "http://www.w3.org/2001/XMLSchema"; + public static final String CONVERSION_NS = "http://www.simantics.org/Layer0"; + + protected Map> typeMap; + + protected String ontologyURI; + protected String className; + + public SchemaConversionBase(SchemaConverter converter, String ontologyUri, String className) { + this.converter = converter; + this.configuration = converter.getConfiguration(); + this.ontologyURI = ontologyUri; + this.className = className; + + initTypes(); + } + + protected void initTypes() { + 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", "Bindings.STRING", "java.lang.String", "","","","","")); + schemaTypes.put("NMTOKEN", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); + schemaTypes.put("token", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); + schemaTypes.put("ID", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","",true)); + schemaTypes.put("IDREF", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); + schemaTypes.put("Name", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); + schemaTypes.put("NCName", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); + 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()")); + 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()")); + 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()")); + schemaTypes.put("gYearMonth", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); + schemaTypes.put("gYear", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); + schemaTypes.put("gMonth", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); + schemaTypes.put("gMonthDay", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); + schemaTypes.put("anyURI", new TypeEntry("L0.URI", "Bindings.STRING", "java.lang.String", "","","","","")); + schemaTypes.put("double", new TypeEntry("L0.Double", "Bindings.DOUBLE", "double", "java.lang.Double.NaN","java.lang.Double.parseDouble(",")","java.lang.Double.toString(",")")); + schemaTypes.put("float", new TypeEntry("L0.Float", "Bindings.FLOAT", "float", "java.lang.Float.NaN","java.lang.Float.parseFloat(",")","java.lang.Float.toString(",")")); + schemaTypes.put("decimal", new TypeEntry("L0.Double", "Bindings.DOUBLE", "double", "java.lang.Double.NaN","java.lang.Double.parseDouble(",")","java.lang.Double.toString(",")")); + schemaTypes.put("boolean", new TypeEntry("L0.Boolean", "Bindings.BOOLEAN", "boolean", "false","java.lang.Boolean.parseBoolean(",")","java.lang.Boolean.toString(",")")); + schemaTypes.put("integer", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); + schemaTypes.put("positiveInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); + schemaTypes.put("nonPositiveInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); + schemaTypes.put("nonNegativeInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); + schemaTypes.put("negativeInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); + schemaTypes.put("unsignedInt", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); + schemaTypes.put("int", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); + schemaTypes.put("short", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); + schemaTypes.put("unsignedShort",new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")")); + schemaTypes.put("byte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","java.lang.Byte.parseByte(",")","java.lang.Byte.toString(",")")); + schemaTypes.put("unsignedByte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","java.lang.Byte.parseByte(",")","java.lang.Byte.toString(",")")); + schemaTypes.put("long", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")","java.lang.Long.toString(",")")); + schemaTypes.put("unsignedLong", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")","java.lang.Long.toString(",")")); + 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)")); + + l0Types.put("doubleArray", new TypeEntry("L0.DoubleArray", "Bindings.DOUBLE_ARRAY", "double[]", null,null,null,"java.lang.Double.toString(",")")); + l0Types.put("stringArray", new TypeEntry("L0.StringArray", "Bindings.STRING_ARRAY", "string[]", null,null,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 TypeEntry getTypeEntry(String type) { + for (Map types : typeMap.values()) { + TypeEntry entry = types.get(type); + if (entry != null) + return entry; + } + return null; + } + + 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; + } + + + public void init(Schema schema) { + this.schema = schema; + + preload(); + } + + public void handle(SchemaConversionComponent component) { + this.component = component; + 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 if (attrs instanceof NamedGroup) { + handle((NamedGroup)attrs); + } else { + System.out.println(attrs.getClass().getName()); + } + } + } + + private Map elementName = new HashMap<>(); + private Map complexTypeName = new HashMap<>(); + private Map simpleTypeName = new HashMap<>(); + private Map modelGroupName = new HashMap<>(); + private Map elements = new HashMap<>(); + private Map complexTypes = new HashMap<>(); + private Map simpleTypes = new HashMap<>(); + private Map modelGroups = new HashMap<>(); + + private SchemaObject _getWithName(QName name) { + SchemaObject obj = elementName.get(name.getLocalPart()); + if (obj == null) + obj = complexTypeName.get(name.getLocalPart()); + if (obj == null) + obj = simpleTypeName.get(name.getLocalPart()); + return obj; + } + + protected SchemaObject getWithName(QName name) { + SchemaObject obj = _getWithName(name); + if (obj != null) + return obj; + if (name.getNamespaceURI() != null) { + for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { + if (sc.base != null) { + obj = sc.base._getWithName(name); + if (obj != null) { + return obj; + } + } + } + } + return null; + } + + private NamedAttributeGroup _getAttributeGroup(QName name) { + for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { + if (attrs instanceof NamedAttributeGroup) { + NamedAttributeGroup group = (NamedAttributeGroup)attrs; + if (group.getName().equals(name.getLocalPart())) + return group; + } + } + return null; + } + + public NamedAttributeGroup getAttributeGroup(QName name) { + NamedAttributeGroup group = _getAttributeGroup(name); + if (group != null) + return group; + if (name.getNamespaceURI() != null) { + for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { + if (sc.base != null) { + group = sc.base._getAttributeGroup(name); + if (group != null) { + return group; + } + } + } + } + return null; + } + + private SchemaObject _getElement(QName name) { + return elementName.get(name.getLocalPart()); + } + + protected SchemaObject getElement(QName name) { + SchemaObject obj = _getElement(name); + if (obj != null) + return obj; + if (name.getNamespaceURI() != null) { + for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { + if (sc.base != null) { + obj = sc.base._getElement(name); + if (obj != null) { + return obj; + } + } + } + } + return null; + } + + protected SchemaObject getElement(Element element) { + return elements.get(element); + } + + + private SchemaObject _getComplexType(QName name) { + return complexTypeName.get(name.getLocalPart()); + } + + protected SchemaObject getComplexType(QName name) { + SchemaObject obj = _getComplexType(name); + if (obj != null) + return obj; + if (name.getNamespaceURI() != null) { + for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { + if (sc.base != null) { + obj = sc.base._getComplexType(name); + if (obj != null) { + return obj; + } + } + } + } + return null; + } + + protected SchemaObject getComplexType(ComplexType complexType) { + return complexTypes.get(complexType); + } + + private SchemaObject _getSimpleType(QName name) { + return simpleTypeName.get(name.getLocalPart()); + } + + protected SchemaObject getSimpleType(QName name) { + SchemaObject obj = _getSimpleType(name); + if (obj != null) + return obj; + if (name.getNamespaceURI() != null) { + for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { + if (sc.base != null) { + obj = sc.base._getSimpleType(name); + if (obj != null) { + return obj; + } + } + } + } + return null; + } + + protected SchemaObject getSimpleType(SimpleType simpleType) { + return simpleTypes.get(simpleType); + } + + protected SchemaObject getWithObj(SchemaObject referrer, OpenAttrs attrs) { + // FIXME : this method cannot handle references to other schemas. + 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); + obj.setRename(getRename(element)); + stack.push(obj); + } else if (attrs instanceof ComplexType) { + ComplexType complexType = (ComplexType)attrs; + SchemaObject obj = new SchemaObject(complexType); + obj.setRename(getRename(complexType)); + stack.push(obj); + } else if (attrs instanceof SimpleType) { + SimpleType simpleType = (SimpleType)attrs; + SchemaObject obj = new SchemaObject(simpleType); + stack.push(obj); + } else if (attrs instanceof Attribute) { + // Attributes are not cached + } else if (attrs instanceof AttributeGroup) { + // Attribute groups are not cached + } else if (attrs instanceof NamedGroup) { + NamedGroup group = (NamedGroup)attrs; + SchemaObject obj = new SchemaObject(group); + stack.push(obj); + } else { + System.out.println(attrs.getClass().getName()); + } + } + + while (!stack.isEmpty()) { + SchemaObject object = stack.pop(); + switch (object.getType()) { + case 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"); + //preload(object,extensionType.getGroup(), stack); + } + } + } + if (ct.getSimpleContent() != null) { + SimpleContent cc = ct.getSimpleContent(); + 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"); + } + } + break; + } + case 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())); + break; + } + case SIMPLE_TYPE:{ + SimpleType e = object.getSimpleType(); + if (e instanceof TopLevelSimpleType) + simpleTypeName.put(e.getName(), object); + simpleTypes.put(e, object); + break; + } + case MODEL_GROUP:{ + NamedGroup e = object.getModelGroup(); + modelGroupName.put(e.getName(), object); + modelGroups.put(e, object); + break; + } + } + } // while + } + + 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) { + SchemaObject obj = new SchemaObject(parent,(Element)elemValue); + obj.setRename(getRename((Element)elemValue)); + stack.add(obj); + } else if (elemValue instanceof ExplicitGroup) { + preload(parent,(ExplicitGroup)elemValue, stack); + } else if (elemValue instanceof RealGroup) { + preload(parent,(RealGroup)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()); + } + } + } + + private void preload(SchemaObject parent, RealGroup eg, Deque stack) { + System.out.println(eg); + if (eg instanceof NamedGroup) { + SchemaObject obj = new SchemaObject(parent,(NamedGroup)eg); + stack.add(obj); + } + } + + + 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 void handle(NamedGroup 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(); + + Restriction restriction = simpleType.getRestriction(); + if (restriction != null) { + QName base = restriction.getBase(); + return base; + } else if (simpleType.getId() != null) { + throw new RuntimeException(simpleType.getName() + " restriction error"); + } else if (simpleType.getUnion() != null) { + Union union = simpleType.getUnion(); + if (union.getMemberTypes().size() > 0) { + QName base = null; + for (QName type : union.getMemberTypes()) { + QName sType = null; + TypeEntry entry = getTypeEntry(type); + if (entry == null) { + //SchemaObject obj = simpleTypeName.get(type.getLocalPart()); + SchemaObject obj = getSimpleType(type); + if (obj == null) + throw new RuntimeException(simpleType.getName() + " union has unresolved reference " + type.getLocalPart()); + sType = getSimpleTypeBase(obj.getSimpleType()); + } else { + sType = type; + } + if (base == null) + base = sType; + else if (!base.equals(sType)) { + //FIXME : throw new RuntimeException(simpleType.getName() + " union has incompatible member types"); + // fall back to string. + base = new QName(SCHEMA_NS, "string"); + + } + } + return base; + } else { + if (union.getSimpleType().size() == 0) + throw new RuntimeException(simpleType.getName() + " union error"); + for (SimpleType s : union.getSimpleType()) { + if (restriction == null) + restriction = s.getRestriction(); + else { + Restriction r = s.getRestriction(); + if (!r.getBase().equals(restriction.getBase())) { + Inheritance rI = new Inheritance(""); + getAtomicTypeInheritance(r.getBase(), rI); + Inheritance restI = new Inheritance(""); + getAtomicTypeInheritance(restriction.getBase(), restI); + if (!rI.atomicType.l0Type.equals(restI.atomicType.l0Type)) + throw new RuntimeException(simpleType.getName() + " union has incompatible restriction bases"); + } + } + } + QName base = restriction.getBase(); + return base; + } + } else if (simpleType.getList() != null) { + // FIXME: callers cannot get the information that we have a list. + org.w3._2001.xmlschema.List list = simpleType.getList(); + return list.getItemType(); + } else { + throw new RuntimeException(simpleType.getName() + " restriction error"); + } + } + + 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 void handleAttributes(SchemaObject simpleTypeObj) { + component.handleAttributes(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()); + } + } + SimpleContent simpleContent = complexType.getComplexType().getSimpleContent(); + if (simpleContent != null) { + ExtensionType extensionType = simpleContent.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 QName getPrimitiveType(Attribute attribute) { + QName type = getBaseType(attribute); + String b = getBindingFromPrimitiveType(type); + while (b==null && type != null) { + SchemaObject baseType = simpleTypeName.get(type.getLocalPart()); + if (baseType != null) { + Restriction restriction = baseType.getSimpleType().getRestriction(); + if (restriction != null) + if (restriction.getBase() != null) { + type = restriction.getBase(); + b = getBindingFromPrimitiveType(type); + } + } + } + return type; + } + + 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 handleAttributeComposition(SchemaObject obj, AttributeComposition composition, BijectionMap attributes) { + component.handleAttributeComposition(obj, composition, attributes); + } + + + + + protected void handleComplexType(SchemaObject complexType) { +// handleComplexTypeAttributes(complexType); +// handleComplexTypeExtension(complexType); +// handleExtensionAttributes(complexType); + component.handleComplexType(complexType); + } + + protected void handleElement(SchemaObject topLevelElement) { +// LocalComplexType complexType = topLevelElement.getElement().getComplexType(); +// +// if (complexType != null) { +// SchemaObject complextTypeObj = complexTypes.get(complexType); +// handleElementComplexTypeAttributes(complextTypeObj); +// handleComplexTypeExtension(complextTypeObj); +// } + component.handleElement(topLevelElement); + } + + protected enum RefType{Element,Reference,Type}; + + protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType) { + component.handleIndicator(parent, indicator, element, refName, refType); + } + protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any) { + component.handleIndicator(parent, indicator, any); + } + protected void handle(SchemaObject parent, SchemaElement indicator, List elements) { + //component.handle(parent, indicator, elements); + if (indicator.getType() == SchemaElement.ElementType.SEQUENCE || (indicator.getType() == SchemaElement.ElementType.CHOICE && indicator.getRestriction().many())) { + for (SchemaElement e : elements) { + handle(parent, indicator, e); + } + } else if (indicator.getType() == SchemaElement.ElementType.CHOICE) { + String name = getChoiceName(elements); + component.handleChoice(parent, indicator, elements, name); + } + } + + protected void handle(SchemaObject parent, ExplicitGroup eg, SchemaElement.ElementType indicator) { + handle(parent, new SchemaElement(eg, indicator)); + } + + protected void handle(SchemaObject parent, GroupRef 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(); + List groups = 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 if (elemValue instanceof RealGroup) { + if (elemValue instanceof GroupRef) { + groups.add(new SchemaElement(indicator,(GroupRef)elemValue, ElementType.GROUP_REF)); + } else if (elemValue instanceof NamedGroup) { + groups.add(new SchemaElement(indicator,(NamedGroup)elemValue, ElementType.NAMED_GROUP)); + } else { + throw new RuntimeException("Unknown ExplicitGroup element " + elemValue.getClass().getName()); + } + } 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 && groups.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); + } + + for (SchemaElement c : groups) { + 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); + } + for (SchemaElement c : groups) { + handle(parent, c); + } + } + } else { + if (choices.size() == 1 && sequences.size() == 0 && alls.size() == 0 && groups.size() == 0) { + // special case: handle lone choice inside sequence with maxOccurs > 1 + SchemaElement choice = choices.get(0); + // move multiplicity restrictions to choice + if (indicator.getRestriction().max == -1 || (choice.getRestriction().max > 0 && indicator.getRestriction().max > choice.getRestriction().max)) + choice.getRestriction().max = indicator.getRestriction().max; + if (indicator.getRestriction().min == 0 || choice.getRestriction().min > indicator.getRestriction().min) + choice.getRestriction().min = indicator.getRestriction().min; + handle(parent, choice, elements); + return; + } + if (sequences.size() > 0 || choices.size() > 0 || alls.size() > 0 || groups.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 || groups.size() > 0) { + throw new RuntimeException("Cannot handle Choice that contains something else than Elements"); + //System.out.println("Cannot handle Choice that contains something else than Elements"); + //return; + } + handle(parent, indicator, elements); + + } else { + if (sequences.size() > 0 || choices.size() > 0 || alls.size() > 0 || groups.size() > 0) { + throw new RuntimeException("Cannot handle Choice with inner ExplicitGroups"); + //System.out.println("Cannot handle Choice with inner ExplicitGroups"); + //return; + } + 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 || groups.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) { + SchemaObject eObj = elements.get(localElement); + QName refType = localElement.getType(); + if (refType != null) + handleIndicator(parent, indicator, element, null, RefType.Type); + else { + handleElement(eObj); + handleIndicator(parent, indicator, element, null, RefType.Element); + } + } else if (localElement.getRef() != null) { + handleIndicator(parent, indicator,element, null, RefType.Reference); + } + } + + 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 void handle(SchemaObject parent, Attribute attribute) { + component.handle(parent, attribute); + } + protected void handle(SchemaObject parent, AttributeGroup attribute) { + component.handle(parent, attribute); + } + protected void handle(SchemaObject parent, NamedGroup attribute){ + component.handle(parent, attribute); + }; + + protected void handleSimpleType(SchemaObject parent, SchemaObject simpleType) { + component.handleSimpleType(parent, 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"); + //handle(complexTypeObj, extensionType.getGroup(), SchemaElement.ElementType.GROUP_REF); + } + } + } +// SimpleContent simpleContent = complexType.getSimpleContent(); +// if (simpleContent != null) { +// ExtensionType extensionType = simpleContent.getExtension(); +// } + } + } + + + public boolean isElementRef(String ref) { + return elementName.containsKey(ref); + } + + public boolean isComplexTypeRef(String ref) { + return complexTypeName.containsKey(ref); + } + + public boolean isSimpleTypeRef(String ref) { + return simpleTypeName.containsKey(ref); + } + + public 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); + } + + public 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); + } + + public 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; + } + + public 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 rule = (UnrecognizedChildElement)e.getValue(); + org.simantics.xml.sax.configuration.ComplexType complexType2 = rule.getComplexType(); + if (complexType2 != null) { + if (complexType.getName().equals(complexType2.getName())) + return rule; + } + } + } + return null; + } + + public UnrecognizedChildElement getUnknown(Element element) { + for (JAXBElement e : configuration.getConversionRule()) { + if (e.getValue() instanceof UnrecognizedChildElement) { + UnrecognizedChildElement rule = (UnrecognizedChildElement)e.getValue(); + org.simantics.xml.sax.configuration.Element element2 = rule.getElement(); + if (element2 != null) { + if (element.getName().equals(element2.getName())) + return rule; + } + } + } + return null; + } + + public Rename getRename(Attribute element) { + for (JAXBElement e : configuration.getConversionRule()) { + if (e.getValue() instanceof Rename) { + Rename rule = (Rename)e.getValue(); + Object ref = rule.getElementOrComplexTypeOrAttribute().get(0); + if (!(ref instanceof org.simantics.xml.sax.configuration.Attribute)) + continue; + org.simantics.xml.sax.configuration.Attribute element2 = (org.simantics.xml.sax.configuration.Attribute)ref; + if (element2.getName().equals(element.getName())) { + return rule; + } + } + } + return null; + } + + public Rename getRename(ComplexType element) { + for (JAXBElement e : configuration.getConversionRule()) { + if (e.getValue() instanceof Rename) { + Rename rule = (Rename)e.getValue(); + Object ref = rule.getElementOrComplexTypeOrAttribute().get(0); + if (!(ref instanceof org.simantics.xml.sax.configuration.ComplexType)) + continue; + org.simantics.xml.sax.configuration.ComplexType element2 = (org.simantics.xml.sax.configuration.ComplexType)ref; + if (element2.getName().equals(element.getName())) { + return rule; + } + } + } + return null; + } + + public Rename getRename(Element element) { + for (JAXBElement e : configuration.getConversionRule()) { + if (e.getValue() instanceof Rename) { + Rename rule = (Rename)e.getValue(); + Object ref = rule.getElementOrComplexTypeOrAttribute().get(0); + if (!(ref instanceof org.simantics.xml.sax.configuration.Element)) + continue; + org.simantics.xml.sax.configuration.Element element2 = (org.simantics.xml.sax.configuration.Element)ref; + if (element2.getName().equals(element.getName())) { + return rule; + } + } + } + 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; + String getterPrefix; + String getterPostfix; + String stringPrefix; + String stringPostfix; + public TypeEntry(String l0Type, String binding, String javaType, String defaultValue, String getterPrefix, String getterPostfix, String stringPrefix, String stringPostfix) { + super(); + this.l0Type = l0Type; + this.binding = binding; + this.javaType = javaType; + this.defaultValue = defaultValue; + this.id = false; + this.getterPrefix = getterPrefix; + this.getterPostfix = getterPostfix; + this.stringPrefix = stringPrefix; + this.stringPostfix = stringPostfix; + } + + public TypeEntry(String l0Type, String binding, String javaType, String defaultValue, String getterPrefix, String getterPostfix, String stringPrefix, String stringPostfix, boolean id) { + super(); + this.l0Type = l0Type; + this.binding = binding; + this.javaType = javaType; + this.defaultValue = defaultValue; + this.id = id; + this.getterPrefix = getterPrefix; + this.getterPostfix = getterPostfix; + this.stringPrefix = stringPrefix; + this.stringPostfix = stringPostfix; + } + + public String getValueGetterMethod(String name) { + return getterPrefix + name + ".getValue()"+getterPostfix; + } + public String getValueGetter(String name) { + return getterPrefix + name+getterPostfix; + } + public String getValueGetter() + { + return getValueGetter("value"); + } + + public String getToString(String name) { + return stringPrefix +"("+javaType+")"+name+stringPostfix; + } + + public String getElementToString(String name) { + return stringPrefix + name+stringPostfix; + } + + } + + public enum InheritanceType{ComplexType,AtomicType,None}; + + public static class Inheritance { + public String baseClass; + public InheritanceType type; + public TypeEntry atomicType; + + public Inheritance(String baseClass) { + this.baseClass = baseClass; + this.type = InheritanceType.None; + } + } + + public String getComplexTypePrefix() { + return component.getComplexTypePrefix(); + } + public String getAttributeGroupPrefix() { + return component.getAttributeGroupPrefix(); + } + public String getName(SchemaObject obj) { + return component.getName(obj); + } + public String getBaseClass(ObjectType type) { + return component.getBaseClass(type); + } + + + + public Inheritance getInheritance(SchemaObject topLevelObj) { + Inheritance inheritance = null; + if (topLevelObj.getType() == ObjectType.ELEMENT) { + Element topLevelElement = topLevelObj.getElement(); + inheritance = new Inheritance(getBaseClass(ObjectType.ELEMENT)); + if (topLevelElement.getType() != null) { + QName type = topLevelElement.getType(); + if (!type.getNamespaceURI().equals(SCHEMA_NS)) { + SchemaObject obj = complexTypeName.get(type.getLocalPart()); + // if (obj == null) + // obj = simpleTypeName.get(type.getLocalPart()); + if (obj != null) { + inheritance.baseClass = getName(obj); + inheritance.type = InheritanceType.ComplexType; + } + } else { + TypeEntry entry = getTypeEntry(type); + if (entry != null) { + inheritance.type = InheritanceType.AtomicType; + inheritance.atomicType = entry; + } + } + } + if (inheritance.type == InheritanceType.None) { + QName type = getElementBase(topLevelElement); + if (type != null) { + if (!type.getNamespaceURI().equals(SCHEMA_NS)) { + SchemaObject obj = getWithName(type); + inheritance.baseClass = getName(obj); + inheritance.type = InheritanceType.ComplexType; + } else { + TypeEntry entry = getTypeEntry(type); + if (entry != null) { + inheritance.type = InheritanceType.AtomicType; + inheritance.atomicType = entry; + } + } + } + } + if (inheritance.type == InheritanceType.None) { + QName type = topLevelElement.getSubstitutionGroup(); + if (type != null) { + if (!type.getNamespaceURI().equals(SCHEMA_NS)) { + SchemaObject obj = getWithName(type); + inheritance.baseClass = getName(obj); + inheritance.type = InheritanceType.ComplexType; + } else { + TypeEntry entry = getTypeEntry(type); + if (entry != null) { + inheritance.type = InheritanceType.AtomicType; + inheritance.atomicType = entry; + } + } + } + } + } else if (topLevelObj.getType() == ObjectType.COMPLEX_TYPE) { + ComplexType complexType = topLevelObj.getComplexType(); + QName type = getComplexTypeBase(complexType); + inheritance = new Inheritance(getBaseClass(ObjectType.COMPLEX_TYPE)); + if (type != null && !type.getNamespaceURI().equals("http://www.w3.org/2001/XMLSchema")) { + SchemaObject obj = complexTypeName.get(type.getLocalPart()); + if (obj != null) { + inheritance.baseClass = getName(obj); + inheritance.type = InheritanceType.ComplexType; + } + } + SimpleContent simpleContent = complexType.getSimpleContent(); + if (simpleContent != null) { + ExtensionType extensionType = simpleContent.getExtension(); + if (extensionType != null) { + type = extensionType.getBase(); + getAtomicTypeInheritance(type, inheritance); + } + } + } + + return inheritance; + } + /** + * Goes through chain of SimpleTypes until locates Atomic Type (type defined in XML schema). + * @param type + * @param topLevelObj + * @param inheritance + */ + public void getAtomicTypeInheritance(QName type, Inheritance inheritance) { + if (!type.getNamespaceURI().equals(SCHEMA_NS)) { + SchemaObject obj = getWithName(type); + if (obj.getType() != ObjectType.SIMPLE_TYPE) + throw new RuntimeException("SimpleContent does not use SimpleType definition"); + SimpleType simpleType = obj.getSimpleType(); + type = getSimpleTypeBase(simpleType); + getAtomicTypeInheritance(type, inheritance); + } else { + TypeEntry entry = getTypeEntry(type); + if (entry != null) { + inheritance.type = InheritanceType.AtomicType; + inheritance.atomicType = entry; + } + } + } + + public String getDefaultValue(QName atype) { + Map types = typeMap.get(atype.getNamespaceURI()); + if (types == null) + return null; + TypeEntry entry = types.get(atype.getLocalPart()); + if (entry == null) + return null; + return entry.defaultValue; + } + +}