X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=inline;f=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FSchemaConversionBase.java;h=6621cc716f17853f3549644bf2f6b42cf8cc6d44;hb=894fd479af4bd23016e38165b8ba3d6235d27125;hp=1e13951da7d8f522c47558e872534da51a474fb4;hpb=7f59891d2747280b09e77bc833bc70e05eece706;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 1e13951..6621cc7 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 @@ -28,6 +28,7 @@ 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.ComplexRestrictionType; import org.w3._2001.xmlschema.ComplexType; import org.w3._2001.xmlschema.Element; import org.w3._2001.xmlschema.ExplicitGroup; @@ -111,7 +112,7 @@ public final class SchemaConversionBase { 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)")); + schemaTypes.put("base64Binary", new TypeEntry("L0.ByteArray", "Bindings.BYTE_ARRAY", "byte[]", "new byte[0]","java.util.Base64.getDecoder().decode(",".replaceAll(\"\\n\", \"\").getBytes(java.nio.charset.StandardCharsets.UTF_8))","java.util.Base64.getEncoder().encodeToString(",")")); 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,"","")); @@ -204,10 +205,12 @@ public final class SchemaConversionBase { private Map complexTypeName = new HashMap<>(); private Map simpleTypeName = new HashMap<>(); private Map modelGroupName = new HashMap<>(); + private Map attrName = new HashMap<>(); private Map elements = new HashMap<>(); private Map complexTypes = new HashMap<>(); private Map simpleTypes = new HashMap<>(); private Map modelGroups = new HashMap<>(); + private Map attributes = new HashMap<>(); private SchemaObject _getWithName(QName name) { SchemaObject obj = elementName.get(name.getLocalPart()); @@ -215,6 +218,8 @@ public final class SchemaConversionBase { obj = complexTypeName.get(name.getLocalPart()); if (obj == null) obj = simpleTypeName.get(name.getLocalPart()); + if (obj == null) + obj = attrName.get(name.getLocalPart()); return obj; } @@ -360,25 +365,27 @@ public final class SchemaConversionBase { for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { if (attrs instanceof Element) { Element element = (Element)attrs; - SchemaObject obj = new SchemaObject(element); + SchemaObject obj = new SchemaObject(this,element); obj.setRename(getRename(element)); stack.push(obj); } else if (attrs instanceof ComplexType) { ComplexType complexType = (ComplexType)attrs; - SchemaObject obj = new SchemaObject(complexType); + SchemaObject obj = new SchemaObject(this,complexType); obj.setRename(getRename(complexType)); stack.push(obj); } else if (attrs instanceof SimpleType) { SimpleType simpleType = (SimpleType)attrs; - SchemaObject obj = new SchemaObject(simpleType); + SchemaObject obj = new SchemaObject(this,simpleType); stack.push(obj); } else if (attrs instanceof Attribute) { - // Attributes are not cached + Attribute attribute = (Attribute)attrs; + SchemaObject obj = new SchemaObject(this, attribute); + stack.push(obj); } else if (attrs instanceof AttributeGroup) { // Attribute groups are not cached } else if (attrs instanceof NamedGroup) { NamedGroup group = (NamedGroup)attrs; - SchemaObject obj = new SchemaObject(group); + SchemaObject obj = new SchemaObject(this,group); stack.push(obj); } else { System.out.println(attrs.getClass().getName()); @@ -448,9 +455,9 @@ public final class SchemaConversionBase { elementName.put(e.getName(), object); elements.put(e, object); if (e.getComplexType() != null) - stack.push(new SchemaObject(object,e.getComplexType())); + stack.push(new SchemaObject(this,object,e.getComplexType())); if (e.getSimpleType() != null) - stack.push(new SchemaObject(object,e.getSimpleType())); + stack.push(new SchemaObject(this,object,e.getSimpleType())); break; } case SIMPLE_TYPE:{ @@ -466,6 +473,12 @@ public final class SchemaConversionBase { modelGroups.put(e, object); break; } + case ATTRIBUTE: { + Attribute e = object.getAttribute(); + attrName.put(e.getName(), object); + attributes.put(e, object); + break; + } } } // while } @@ -476,7 +489,7 @@ public final class SchemaConversionBase { JAXBElement element = (JAXBElement)o; Object elemValue = element.getValue(); if (elemValue instanceof Element) { - SchemaObject obj = new SchemaObject(parent,(Element)elemValue); + SchemaObject obj = new SchemaObject(this,parent,(Element)elemValue); obj.setRename(getRename((Element)elemValue)); stack.add(obj); } else if (elemValue instanceof ExplicitGroup) { @@ -497,7 +510,7 @@ public final class SchemaConversionBase { private void preload(SchemaObject parent, RealGroup eg, Deque stack) { System.out.println(eg); if (eg instanceof NamedGroup) { - SchemaObject obj = new SchemaObject(parent,(NamedGroup)eg); + SchemaObject obj = new SchemaObject(this,parent,(NamedGroup)eg); stack.add(obj); } } @@ -529,6 +542,13 @@ public final class SchemaConversionBase { QName type = extensionType.getBase(); return type; } + ComplexRestrictionType restriction = complexContent.getRestriction(); + if (restriction != null) + return restriction.getBase(); + } + if (complexType.isMixed()) { + // Handle characters data of mixed ComplexType as String. + return new QName(SCHEMA_NS, "string"); } return null; } @@ -730,13 +750,30 @@ public final class SchemaConversionBase { return type; } + private SchemaObject _getAttribute(QName name) { + return attrName.get(name.getLocalPart()); + } + 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; - } +// for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { +// if (attrs instanceof TopLevelAttribute) { +// TopLevelAttribute attribute = (TopLevelAttribute)attrs; +// if (attribute.getName().equals(ref.getLocalPart())) +// return attribute; +// } +// } + SchemaObject obj = _getAttribute(ref); + if (obj != null) + return obj.getAttribute(); + if (ref.getNamespaceURI() != null) { + for (SchemaConverter sc : converter.getConverter(ref.getNamespaceURI())) { + if (sc.base != null) { + obj = sc.base._getAttribute(ref); + if (obj != null) { + return obj.getAttribute(); + } + } + } } return null; } @@ -769,21 +806,26 @@ public final class SchemaConversionBase { 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 element, String refName, RefType refType, String baseRelationName) { + component.handleIndicator(parent, indicator, element, refName, refType, baseRelationName); } 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())) { + // Generate combined relation + String baseRelationName = null; + if (indicator.getType() == SchemaElement.ElementType.CHOICE) { + String name = getChoiceName(elements); + if (name != null) + baseRelationName = component.handleChoice(parent, indicator, elements, name); + } + + if (indicator.getType() == SchemaElement.ElementType.SEQUENCE || indicator.getType() == SchemaElement.ElementType.CHOICE) { for (SchemaElement e : elements) { - handle(parent, indicator, e); + handle(parent, indicator, e, baseRelationName); } - } else if (indicator.getType() == SchemaElement.ElementType.CHOICE) { - String name = getChoiceName(elements); - component.handleChoice(parent, indicator, elements, name); } } @@ -931,54 +973,71 @@ public final class SchemaConversionBase { } - protected void handle(SchemaObject parent, SchemaElement indicator, SchemaElement element) { + protected void handle(SchemaObject parent, SchemaElement indicator, SchemaElement element, String baseRelationName) { Element localElement = element.getElement(); if (localElement.getName() != null) { - SchemaObject eObj = elements.get(localElement); + SchemaObject eObj = elements.get(localElement); QName refType = localElement.getType(); if (refType != null) - handleIndicator(parent, indicator, element, null, RefType.Type); + handleIndicator(parent, indicator, element, null, RefType.Type, baseRelationName); else { handleElement(eObj); - handleIndicator(parent, indicator, element, null, RefType.Element); - } + handleIndicator(parent, indicator, element, null, RefType.Element, baseRelationName); + } } else if (localElement.getRef() != null) { - handleIndicator(parent, indicator,element, null, RefType.Reference); + handleIndicator(parent, indicator,element, null, RefType.Reference, baseRelationName); } } - protected String getElementName(Element localElement) { + protected String getName(Element localElement) { if (localElement.getName() != null) { String refName = localElement.getName(); - QName refType = localElement.getType(); - if (refType != null) - return refName; + return refName.replaceAll("\\.", "_"); } else if (localElement.getRef() != null) { QName refType = localElement.getRef(); if (refType != null) - return refType.getLocalPart(); + return getName(refType); + } return null; } + + protected String getName(QName ref) { + String n = ref.getLocalPart(); + return n.replaceAll("\\.", "_"); + } + + protected String getName(Attribute ref) { + String n = ref.getName(); + if (n != null) + return n.replaceAll("\\.", "_"); + else + return null; + } protected String getChoiceName(List elements) { if (elements.size() == 1) { - return getElementName(elements.get(0).getElement()); + return null; } - List names = new ArrayList(); - for (SchemaElement e : elements) { - String name = getElementName(e.getElement()); - if (name != null) - names.add(name); + else if (elements.size() > 0 && elements.size() <= 3) { + List names = new ArrayList(); + for (SchemaElement e : elements) { + String name = getName(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; } - String name = ""; - for (int i = 0; i < names.size(); i++) { - if (i == 0) - name = names.get(i); - else - name += "Or"+names.get(i); + else { + return "SubElement"; } - return name; } protected void handle(SchemaObject parent, Attribute attribute) { @@ -1349,6 +1408,7 @@ public final class SchemaConversionBase { public static class Inheritance { public String baseClass; + public String additionalClass; public InheritanceType type; public TypeEntry atomicType; @@ -1382,12 +1442,15 @@ public final class SchemaConversionBase { 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 { + obj = simpleTypeName.get(type.getLocalPart()); + if (obj != null) + getAtomicTypeInheritance(type, inheritance); + } } else { TypeEntry entry = getTypeEntry(type); if (entry != null) { @@ -1444,6 +1507,9 @@ public final class SchemaConversionBase { ExtensionType extensionType = simpleContent.getExtension(); if (extensionType != null) { type = extensionType.getBase(); + SchemaObject simpleType = getSimpleType(type); + if (simpleType != null) + inheritance.additionalClass = getName(simpleType); getAtomicTypeInheritance(type, inheritance); } } @@ -1459,9 +1525,9 @@ public final class SchemaConversionBase { */ 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"); + SchemaObject obj = getSimpleType(type); + if (obj == null) + throw new RuntimeException("Cannot locate SimpleType " + type.getLocalPart()); SimpleType simpleType = obj.getSimpleType(); type = getSimpleTypeBase(simpleType); getAtomicTypeInheritance(type, inheritance);