X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FImporterGenerator.java;fp=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FImporterGenerator.java;h=d192ad77000aa3f60e4fe25bdf9209364b93b1c0;hb=057ca542a11f7d0eb83860e3e65fd6eff79813ea;hp=f3279e25ad4ffea031c61057cd38be0b10c00d1e;hpb=53437c09b34c51c21408f4332eb8b6d7f85dd8d4;p=simantics%2Finterop.git 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) {