X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FSchemaConversionBase.java;h=5df8c4aefcbf4736d477410f4599c65c26286249;hb=404be73748777cdd2d09b2f29308ae6f4a3d730c;hp=f35a8845acc74e7a0e081ffdf76498c67b7cf5b9;hpb=ada38ab0a1a98dcb413bef3273064da36ce2d273;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 f35a884..5df8c4a 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 @@ -20,6 +20,7 @@ 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; @@ -37,6 +38,7 @@ import org.w3._2001.xmlschema.NamedAttributeGroup; import org.w3._2001.xmlschema.OpenAttrs; import org.w3._2001.xmlschema.Restriction; import org.w3._2001.xmlschema.Schema; +import org.w3._2001.xmlschema.SimpleContent; import org.w3._2001.xmlschema.SimpleType; import org.w3._2001.xmlschema.TopLevelAttribute; import org.w3._2001.xmlschema.TopLevelComplexType; @@ -62,36 +64,38 @@ public abstract class SchemaConversionBase { Map l0Types = new HashMap(); typeMap.put(CONVERSION_NS, l0Types); - schemaTypes.put("string", new TypeEntry("L0.String", "STRING", "String", "")); - schemaTypes.put("NMTOKEN", new TypeEntry("L0.String", "STRING", "String", "")); - schemaTypes.put("token", new TypeEntry("L0.String", "STRING", "String", "")); - schemaTypes.put("ID", new TypeEntry("L0.String", "STRING", "String", "",true)); - schemaTypes.put("IDREF", new TypeEntry("L0.String", "STRING", "String", "")); - schemaTypes.put("date", new TypeEntry("L0.String", "STRING", "String", "")); - schemaTypes.put("time", new TypeEntry("L0.String", "STRING", "String", "")); - schemaTypes.put("anyURI", new TypeEntry("L0.URI", "STRING", "String", "")); - schemaTypes.put("double", new TypeEntry("L0.Double", "DOUBLE", "double", "Double.NaN")); - schemaTypes.put("float", new TypeEntry("L0.Float", "FLOAT", "float", "Float.NaN")); - schemaTypes.put("decimal", new TypeEntry("L0.Double", "DOUBLE", "double", "Double.NaN")); - schemaTypes.put("boolean", new TypeEntry("L0.Boolean", "BOOLEAN", "boolean", "false")); - schemaTypes.put("integer", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("positiveInteger", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("nonPositiveInteger", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("nonNegativeInteger", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("negativeInteger", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("unsignedInt", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("int", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("short", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("unsignedShort",new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("byte", new TypeEntry("L0.Byte", "BYTE", "byte", "0")); - schemaTypes.put("unsignedByte", new TypeEntry("L0.Byte", "BYTE", "byte", "0")); - schemaTypes.put("long", new TypeEntry("L0.Long", "LONG", "long", "0")); - schemaTypes.put("unsignedLong", new TypeEntry("L0.Long", "LONG", "long", "0")); + 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("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", "DOUBLE_ARRAY", "double[]", null)); - l0Types.put("stringArray", new TypeEntry("L0.StringArray", "STRING_ARRAY", "string[]", null)); + 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,"","")); } @@ -101,7 +105,14 @@ public abstract class SchemaConversionBase { 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) { @@ -206,10 +217,12 @@ public abstract class SchemaConversionBase { 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; @@ -253,6 +266,23 @@ public abstract class SchemaConversionBase { 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"); + } + } } else if (object.getType() == ObjectType.ELEMENT) { Element e = object.getElement(); if (e instanceof TopLevelElement) @@ -277,7 +307,9 @@ public abstract class SchemaConversionBase { JAXBElement element = (JAXBElement)o; Object elemValue = element.getValue(); if (elemValue instanceof Element) { - stack.add(new SchemaObject(parent,(Element)elemValue)); + SchemaObject obj = new SchemaObject(parent,(Element)elemValue); + obj.setRename(getRename((Element)elemValue)); + stack.add(obj); } else if (elemValue instanceof All) { preload(parent,(All)elemValue, stack); } else if (elemValue instanceof ExplicitGroup) { @@ -316,6 +348,14 @@ public abstract class SchemaConversionBase { return type; } } +// SimpleContent simpleContent = complexType.getSimpleContent(); +// if (simpleContent != null) { +// ExtensionType extensionType = simpleContent.getExtension(); +// if (extensionType != null) { +// QName type = extensionType.getBase(); +// return type; +// } +// } return null; } @@ -371,6 +411,13 @@ public abstract class SchemaConversionBase { handleAttributes(complexType, extensionType.getAttributeOrAttributeGroup()); } } + SimpleContent simpleContent = complexType.getComplexType().getSimpleContent(); + if (simpleContent != null) { + ExtensionType extensionType = simpleContent.getExtension(); + if (extensionType != null) { + handleAttributes(complexType, extensionType.getAttributeOrAttributeGroup()); + } + } } @@ -436,6 +483,23 @@ public abstract class SchemaConversionBase { 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) { @@ -458,8 +522,9 @@ public abstract class SchemaConversionBase { handleExtensionAttributes(complexType); } - protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String refName, QName refType); - protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String refName, OpenAttrs ref); + protected enum RefType{Element,Reference,Type}; + + protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType); protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any); protected abstract void handle(SchemaObject parent, SchemaElement indicator, List elements); @@ -482,7 +547,6 @@ public abstract class SchemaConversionBase { 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)); @@ -576,18 +640,21 @@ public abstract class SchemaConversionBase { protected void handle(SchemaObject parent, SchemaElement indicator, SchemaElement element) { Element localElement = element.getElement(); if (localElement.getName() != null) { - String refName = localElement.getName(); + SchemaObject eObj = elements.get(localElement); // FIXME: handleIndicator should be refactored, not this methdof must be overridden in JavaGenerator to handle renaming of Elements properly + //String refName = eObj.getName();//localElement.getName(); QName refType = localElement.getType(); if (refType != null) - handleIndicator(parent, indicator, element, false, refName, refType); + //handleIndicator(parent, indicator, element, false, refName, refType); + handleIndicator(parent, indicator, element, null, RefType.Type); else { - handleElement(elements.get(localElement)); - handleIndicator(parent, indicator, element, false, refName, localElement); - //FIXME: + handleElement(eObj); + //handleIndicator(parent, indicator, element, false, refName, localElement); + handleIndicator(parent, indicator, element, null, RefType.Element); } } else if (localElement.getRef() != null) { - QName refType = localElement.getRef(); - handleIndicator(parent, indicator,element, true, refType.getLocalPart(), refType); + //QName refType = localElement.getRef(); + //handleIndicator(parent, indicator,element, true, refType.getLocalPart(), refType); + handleIndicator(parent, indicator,element, null, RefType.Reference); } } @@ -662,6 +729,10 @@ public abstract class SchemaConversionBase { } } } +// SimpleContent simpleContent = complexType.getSimpleContent(); +// if (simpleContent != null) { +// ExtensionType extensionType = simpleContent.getExtension(); +// } } } @@ -679,28 +750,15 @@ public abstract class SchemaConversionBase { protected boolean isElementRef(String ref) { -// for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { -// if (attrs instanceof TopLevelElement) { -// TopLevelElement element = (TopLevelElement)attrs; -// if (ref.equals(element.getName())) -// return true; -// } -// } -// return false; return elementName.containsKey(ref); } protected boolean isComplexTypeRef(String ref) { -// for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { -// if (attrs instanceof TopLevelComplexType) { -// TopLevelComplexType element = (TopLevelComplexType)attrs; -// if (ref.equals(element.getName())) -// return true; -// } -// } -// return false; return complexTypeName.containsKey(ref); - + } + + protected boolean isSimpleTypeRef(String ref) { + return simpleTypeName.containsKey(ref); } protected NamedAttributeGroup getAttributeGroup(String name) { @@ -787,11 +845,11 @@ public abstract class SchemaConversionBase { public UnrecognizedChildElement getUnknown(ComplexType complexType) { for (JAXBElement e : configuration.getConversionRule()) { if (e.getValue() instanceof UnrecognizedChildElement) { - UnrecognizedChildElement ref = (UnrecognizedChildElement)e.getValue(); - org.simantics.xml.sax.configuration.ComplexType complexType2 = ref.getComplexType(); + UnrecognizedChildElement rule = (UnrecognizedChildElement)e.getValue(); + org.simantics.xml.sax.configuration.ComplexType complexType2 = rule.getComplexType(); if (complexType2 != null) { if (complexType.getName().equals(complexType2.getName())) - return ref; + return rule; } } } @@ -801,17 +859,65 @@ public abstract class SchemaConversionBase { public UnrecognizedChildElement getUnknown(Element element) { for (JAXBElement e : configuration.getConversionRule()) { if (e.getValue() instanceof UnrecognizedChildElement) { - UnrecognizedChildElement ref = (UnrecognizedChildElement)e.getValue(); - org.simantics.xml.sax.configuration.Element element2 = ref.getElement(); + UnrecognizedChildElement rule = (UnrecognizedChildElement)e.getValue(); + org.simantics.xml.sax.configuration.Element element2 = rule.getElement(); if (element2 != null) { if (element.getName().equals(element2.getName())) - return ref; + 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) @@ -913,22 +1019,53 @@ public abstract class SchemaConversionBase { String javaType; String defaultValue; boolean id; - public TypeEntry(String l0Type, String binding, String javaType, String defaultValue) { + 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, boolean id) { + 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; } }