From 057ca542a11f7d0eb83860e3e65fd6eff79813ea Mon Sep 17 00:00:00 2001 From: luukkainen Date: Thu, 19 Jan 2017 11:11:27 +0000 Subject: [PATCH] Handling elements that inherit AtomicTypes Handling Base64 encoded data as ByteArray refs #6985 git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@33432 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../simantics/xml/sax/ImporterGenerator.java | 125 ++++++++++++------ .../simantics/xml/sax/OntologyGenerator.java | 37 ++++-- .../xml/sax/SchemaConversionBase.java | 58 ++++---- 3 files changed, 145 insertions(+), 75 deletions(-) diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java index f3279e2..d192ad7 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java @@ -12,7 +12,6 @@ import java.util.Map; import javax.xml.namespace.QName; import org.simantics.utils.datastructures.BijectionMap; -import org.simantics.utils.datastructures.Pair; import org.simantics.xml.sax.configuration.AttributeComposition; import org.simantics.xml.sax.configuration.Configuration; import org.simantics.xml.sax.configuration.IDProvider; @@ -143,9 +142,15 @@ public class ImporterGenerator extends SchemaConversionBase{ // return "Float.parseFloat(value)"; // return "value"; // } - protected String getValueGetter(TypeEntry binding,String name) { + protected String getValueGetterMethod(TypeEntry binding,String name) { if (binding == null) return name+".getValue()"; + return binding.getValueGetterMethod(name); + } + + protected String getValueGetter(TypeEntry binding,String name) { + if (binding == null) + return name; return binding.getValueGetter(name); } protected String getValueGetter(TypeEntry binding) { @@ -208,7 +213,7 @@ public class ImporterGenerator extends SchemaConversionBase{ intrerfaces.add("org.simantics.xml.sax.base.UnrecognizedElementParser"); createClassHeader(fw.writer, isList); - writeClass(fw.writer,true, null, className, baseClass, intrerfaces); + writeClass(fw.writer,false, null, className, baseClass, intrerfaces); writeIDProvider(fw.writer); @@ -532,15 +537,21 @@ public class ImporterGenerator extends SchemaConversionBase{ } } if (simpleType != null) { - Restriction restriction = simpleType.getRestriction(); - if (restriction == null) - throw new RuntimeException("Cannot resolve type for Attribute " + attrName + " -> " + primitiveType.getLocalPart()+ ", SimpleType restriction is unset"); - QName base = restriction.getBase(); - - - //String binding = getBindingFromPrimitiveType(base); - TypeEntry binding = getTypeEntry(base); - writeAttribute(fw, attrName, relationName, binding, isReference); + org.w3._2001.xmlschema.List list = simpleType.getList(); + if (list != null) { + TypeEntry binding = getTypeEntry(new QName(SCHEMA_NS, "string")); + writeAttribute(fw, attrName, relationName, binding, isReference); + } else { + Restriction restriction = simpleType.getRestriction(); + if (restriction == null) + throw new RuntimeException("Cannot resolve type for Attribute " + attrName + " -> " + primitiveType.getLocalPart()+ ", SimpleType restriction is unset"); + QName base = restriction.getBase(); + + + //String binding = getBindingFromPrimitiveType(base); + TypeEntry binding = getTypeEntry(base); + writeAttribute(fw, attrName, relationName, binding, isReference); + } } else { // TODO : using default String attribute should be configured with rules. //throw new RuntimeException("Cannot resolve type for Attribute " + attrName + " -> " + primitiveType.getLocalPart()); @@ -555,7 +566,7 @@ public class ImporterGenerator extends SchemaConversionBase{ fw.writer.println(" {"); fw.writer.println(" Attribute a = element.getAttribute(\"" +attrName+"\");"); fw.writer.println(" if (a != null) {"); - fw.writer.println(" graph.claimLiteral(element.getData(),"+relationName+","+getValueGetter(binding,"a")+", "+binding.binding+");"); + fw.writer.println(" graph.claimLiteral(element.getData(),"+relationName+","+getValueGetterMethod(binding,"a")+", "+binding.binding+");"); if (isReference) fw.writer.println(" idProviderValue = a.getValue();"); fw.writer.println(" }"); @@ -642,9 +653,9 @@ public class ImporterGenerator extends SchemaConversionBase{ if (i > 0) fw.writer.print(","); if (defaultValue != null) - fw.writer.print("a"+(i)+"!= null ? "+ getValueGetter(binding,"a"+(i++)) + " : " +defaultValue); + fw.writer.print("a"+(i)+"!= null ? "+ getValueGetterMethod(binding,"a"+(i++)) + " : " +defaultValue); else - fw.writer.print(getValueGetter(binding,"a"+(i++))); + fw.writer.print(getValueGetterMethod(binding,"a"+(i++))); } fw.writer.println("};"); fw.writer.println(" graph.claimLiteral(element.getData(),"+relationName+", value, "+arrayBinding+");"); @@ -687,10 +698,7 @@ public class ImporterGenerator extends SchemaConversionBase{ writers.put(elementObj, fw); boolean isList = false; - Pair inhertiance = getElementInheritance(elementObj); - boolean inherited = inhertiance.second; - String baseClass = inhertiance.first;//"org.simantics.xml.sax.base.XMLElementParserBase"; - + Inheritance inhertiance = getElementInheritance(elementObj); provider = getIDProvider(element); List references = getIDReferences(element); @@ -703,7 +711,7 @@ public class ImporterGenerator extends SchemaConversionBase{ intrerfaces.add("org.simantics.xml.sax.base.UnrecognizedElementParser"); createClassHeader(fw.writer, isList); - writeClass(fw.writer,false, element.getName(), className, baseClass, intrerfaces); + writeClass(fw.writer,false, element.getName(), className, inhertiance.baseClass, intrerfaces); writeIDProvider(fw.writer); fw.writer.println(" @Override"); fw.writer.println(" public Resource create(WriteGraph graph, Element element) throws DatabaseException{"); @@ -729,7 +737,7 @@ public class ImporterGenerator extends SchemaConversionBase{ fw.writer.println(" @Override"); fw.writer.println(" public void configure(WriteGraph graph, Deque parents, Element element) throws DatabaseException {"); - if (inherited) { + if (inhertiance.type == InheritanceType.ComplexType) { fw.writer.println(" super.configure(graph,parents,element);"); } fw.writer.println(" "+getOntologyImport()); @@ -743,6 +751,13 @@ public class ImporterGenerator extends SchemaConversionBase{ } fw.writer.println(" }"); + if (inhertiance.type == InheritanceType.AtomicType) { + fw.writer.println(); + fw.writer.println(" @Override"); + fw.writer.println(" public void configure(WriteGraph graph, Element element, java.lang.String string) throws DatabaseException {"); + fw.writer.println(" graph.claimValue(element.getData(), "+getValueGetter(inhertiance.atomicType,"string")+", "+inhertiance.atomicType.binding+");"); + fw.writer.println(" }"); + } if (simpleType != null) { SchemaObject obj = simpleTypes.get(simpleType); handleElementSimpleTypeAttributes(obj); @@ -773,7 +788,7 @@ public class ImporterGenerator extends SchemaConversionBase{ if (stringWriter.getBuffer().length() > 0) { fw.writer.write(stringWriter.toString()); } - if (inherited) { + if (inhertiance.type == InheritanceType.ComplexType) { fw.writer.println(" return super.connectChild(graph,element,child);"); } else { fw.writer.println(" return false;"); @@ -806,40 +821,76 @@ public class ImporterGenerator extends SchemaConversionBase{ ruleClassNames.add(converter.getPluginName()+"."+elementPackageName+"."+name); } - private Pair getElementInheritance(SchemaObject topLevelElementObj) { + private enum InheritanceType{ComplexType,AtomicType,None}; + + private class Inheritance { + public String baseClass; + public InheritanceType type; + public TypeEntry atomicType; + + public Inheritance() { + baseClass = "org.simantics.xml.sax.base.XMLElementNamedChildParserBase"; + type = InheritanceType.None; + } + } + + private Inheritance getElementInheritance(SchemaObject topLevelElementObj) { + if (topLevelElementObj.getElement() != null && topLevelElementObj.getElement().getName().equals("ByteString")) + System.out.println(); Element topLevelElement = topLevelElementObj.getElement(); - String baseClass = "org.simantics.xml.sax.base.XMLElementNamedChildParserBase"; - boolean inherited = false; + Inheritance inheritance = new Inheritance(); if (topLevelElement.getType() != null) { QName type = topLevelElement.getType(); if (!type.getNamespaceURI().equals(SCHEMA_NS)) { SchemaObject obj = complexTypeName.get(type.getLocalPart()); - baseClass = getName(obj); - inherited = true; - } +// 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 (!inherited) { + if (inheritance.type == InheritanceType.None) { QName type = getElementBase(topLevelElement); if (type != null) { if (!type.getNamespaceURI().equals(SCHEMA_NS)) { SchemaObject obj = getWithName(topLevelElementObj, type.getLocalPart()); - baseClass = getName(obj); - inherited = true; - } + inheritance.baseClass = getName(obj); + inheritance.type = InheritanceType.ComplexType; + } else { + TypeEntry entry = getTypeEntry(type); + if (entry != null) { + inheritance.type = InheritanceType.AtomicType; + inheritance.atomicType = entry; + } + } } } - if (!inherited) { + if (inheritance.type == InheritanceType.None) { QName type = topLevelElement.getSubstitutionGroup(); if (type != null) { if (!type.getNamespaceURI().equals(SCHEMA_NS)) { SchemaObject obj = getWithName(topLevelElementObj, type.getLocalPart()); - baseClass = getName(obj); - inherited = true; - } + inheritance.baseClass = getName(obj); + inheritance.type = InheritanceType.ComplexType; + } else { + TypeEntry entry = getTypeEntry(type); + if (entry != null) { + inheritance.type = InheritanceType.AtomicType; + inheritance.atomicType = entry; + } + } } } - return new Pair(baseClass, inherited); + return inheritance; } private void writeClass(PrintWriter writer,boolean abst, String name, String className, String baseClass, List interfaces) { diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java index 5ecb02e..095de9d 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java @@ -403,19 +403,32 @@ public class OntologyGenerator extends SchemaConversionBase { protected void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { SimpleType simpleType = simpleTypeObj.getSimpleType(); String name = simpleType.getName(); - Restriction restriction = simpleType.getRestriction(); - if (restriction == null || simpleType.getUnion() != null || simpleType.getId() != null) - throw new RuntimeException(); - QName base = restriction.getBase(); - - String relationName = ontRoot+"has"+name; - if (parent != null) - relationName = ontRoot+getName(parent)+".has"+name; - - writer.println(relationName+ " " + ontType); + org.w3._2001.xmlschema.List list = simpleType.getList(); + if (list != null) { + // TODO : process restriction in lists + String relationName = ontRoot+"has"+name; + if (parent != null) + relationName = ontRoot+getName(parent)+".has"+name; + writer.println(relationName+ " " + ontType); + } else { + Restriction restriction = simpleType.getRestriction(); + if (restriction == null || simpleType.getUnion() != null || simpleType.getId() != null) + throw new RuntimeException(simpleType.getName() + " restriction error"); + QName base = restriction.getBase(); + + String relationName = ontRoot+"has"+name; + if (parent != null) + relationName = ontRoot+getName(parent)+".has"+name; + + writer.println(relationName+ " " + ontType); + } } @Override 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 e04b093..38c934a 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 @@ -2,6 +2,7 @@ package org.simantics.xml.sax; import java.util.ArrayDeque; import java.util.ArrayList; +import java.util.Base64; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; @@ -62,32 +63,33 @@ public abstract class SchemaConversionBase { Map l0Types = new HashMap(); typeMap.put(CONVERSION_NS, l0Types); - schemaTypes.put("string", new TypeEntry("L0.String", "Bindings.STRING", "String", "","","")); - schemaTypes.put("NMTOKEN", new TypeEntry("L0.String", "Bindings.STRING", "String", "","","")); - schemaTypes.put("token", new TypeEntry("L0.String", "Bindings.STRING", "String", "","","")); - schemaTypes.put("ID", new TypeEntry("L0.String", "Bindings.STRING", "String", "","","",true)); - schemaTypes.put("IDREF", new TypeEntry("L0.String", "Bindings.STRING", "String", "","","")); + 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(",")")); 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(",")")); 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(",")")); - schemaTypes.put("anyURI", new TypeEntry("L0.URI", "Bindings.STRING", "String", "","","")); - schemaTypes.put("double", new TypeEntry("L0.Double", "Bindings.DOUBLE", "double", "Double.NaN","Double.parseDouble(",")")); - schemaTypes.put("float", new TypeEntry("L0.Float", "Bindings.FLOAT", "float", "Float.NaN","Float.parseFloat(",")")); - schemaTypes.put("decimal", new TypeEntry("L0.Double", "Bindings.DOUBLE", "double", "Double.NaN","Double.parseDouble(",")")); - schemaTypes.put("boolean", new TypeEntry("L0.Boolean", "Bindings.BOOLEAN", "boolean", "false","Boolean.parseBoolean(",")")); - schemaTypes.put("integer", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); - schemaTypes.put("positiveInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); - schemaTypes.put("nonPositiveInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); - schemaTypes.put("nonNegativeInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); - schemaTypes.put("negativeInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); - schemaTypes.put("unsignedInt", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); - schemaTypes.put("int", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); - schemaTypes.put("short", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); - schemaTypes.put("unsignedShort",new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); - schemaTypes.put("byte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","Byte.parseByte(",")")); - schemaTypes.put("unsignedByte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","Byte.parseByte(",")")); - schemaTypes.put("long", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","Long.parseLong(",")")); - schemaTypes.put("unsignedLong", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","Long.parseLong(",")")); + 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(",")")); + schemaTypes.put("float", new TypeEntry("L0.Float", "Bindings.FLOAT", "float", "java.lang.Float.NaN","java.lang.Float.parseFloat(",")")); + schemaTypes.put("decimal", new TypeEntry("L0.Double", "Bindings.DOUBLE", "double", "java.lang.Double.NaN","java.lang.Double.parseDouble(",")")); + schemaTypes.put("boolean", new TypeEntry("L0.Boolean", "Bindings.BOOLEAN", "boolean", "false","java.lang.Boolean.parseBoolean(",")")); + schemaTypes.put("integer", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")")); + schemaTypes.put("positiveInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")")); + schemaTypes.put("nonPositiveInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")")); + schemaTypes.put("nonNegativeInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")")); + schemaTypes.put("negativeInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")")); + schemaTypes.put("unsignedInt", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")")); + schemaTypes.put("int", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")")); + schemaTypes.put("short", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")")); + schemaTypes.put("unsignedShort",new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")")); + schemaTypes.put("byte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","java.lang.Byte.parseByte(",")")); + schemaTypes.put("unsignedByte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","java.lang.Byte.parseByte(",")")); + schemaTypes.put("long", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")")); + schemaTypes.put("unsignedLong", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")")); + schemaTypes.put("base64Binary", new TypeEntry("L0.ByteArray", "Bindings.BYTE_ARRAY", "byte[]", "new byte[0]","java.util.Base64.getDecoder().decode(",")")); @@ -963,11 +965,15 @@ public abstract class SchemaConversionBase { this.getterPostfix = getterPostfix; } - public String getValueGetter(String name) { + public String getValueGetterMethod(String name) { return getterPrefix + name + ".getValue()"+getterPostfix; } - public String getValueGetter() { - return getterPrefix + "value"+getterPostfix; + public String getValueGetter(String name) { + return getterPrefix + name+getterPostfix; + } + public String getValueGetter() + { + return getValueGetter("value"); } } -- 2.47.1