From 09fb8eafbc9046ead17f0529eebad0b0caf55e2b Mon Sep 17 00:00:00 2001 From: luukkainen Date: Thu, 26 Jan 2017 15:33:15 +0000 Subject: [PATCH] Handling ComplexTypes inheriting AtomicType Fixed element specific relation handling refs #6985 git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@33437 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sax/base/IndentingXMLStreamWriter.java | 35 +++-- .../simantics/xml/sax/ExporterGenerator.java | 103 ++++++++----- .../simantics/xml/sax/ImporterGenerator.java | 90 +++++++----- .../org/simantics/xml/sax/JavaGenerator.java | 78 +--------- .../simantics/xml/sax/OntologyGenerator.java | 127 +++++++++------- .../xml/sax/SchemaConversionBase.java | 139 ++++++++++++++++-- 6 files changed, 347 insertions(+), 225 deletions(-) diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/IndentingXMLStreamWriter.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/IndentingXMLStreamWriter.java index a912564..d3a6cfd 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/IndentingXMLStreamWriter.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/IndentingXMLStreamWriter.java @@ -14,6 +14,7 @@ public class IndentingXMLStreamWriter implements XMLStreamWriter{ XMLStreamWriter delegate; int depth = 0; + boolean indent = true; public IndentingXMLStreamWriter(XMLStreamWriter delegate) { this.delegate = delegate; @@ -55,7 +56,7 @@ public class IndentingXMLStreamWriter implements XMLStreamWriter{ } private String getIdent() { - String s = ""; + String s = "\n"; for (int i = 0; i < depth; i++) s += " "; return s; @@ -63,76 +64,91 @@ public class IndentingXMLStreamWriter implements XMLStreamWriter{ @Override public void writeStartElement(String localName) throws XMLStreamException { - delegate.writeCharacters("\n"+getIdent()); + if(indent) + delegate.writeCharacters(getIdent()); delegate.writeStartElement(localName); depth++; + indent = true; } @Override public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException { - delegate.writeCharacters("\n"+getIdent()); + if (indent) + delegate.writeCharacters(getIdent()); delegate.writeStartElement(namespaceURI, localName); depth++; + indent = true; } @Override public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { - delegate.writeCharacters("\n"+getIdent()); + if (indent) + delegate.writeCharacters(getIdent()); delegate.writeStartElement(prefix, localName, namespaceURI); depth++; + indent = true; } @Override public void writeStartDocument() throws XMLStreamException { delegate.writeStartDocument(); depth++; + indent = true; } @Override public void writeStartDocument(String version) throws XMLStreamException { delegate.writeStartDocument(version); depth++; + indent = true; } @Override public void writeStartDocument(String encoding, String version) throws XMLStreamException { delegate.writeStartDocument(encoding, version); depth++; + indent = true; } @Override public void writeEndElement() throws XMLStreamException { depth--; - delegate.writeCharacters("\n"+getIdent()); + if(indent) + delegate.writeCharacters(getIdent()); delegate.writeEndElement(); + indent = true; } @Override public void writeEndDocument() throws XMLStreamException { depth--; - delegate.writeCharacters("\n"+getIdent()); + if (indent) + delegate.writeCharacters(getIdent()); delegate.writeEndDocument(); + indent = true; } @Override public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException { delegate.writeEmptyElement(namespaceURI, localName); + indent = true; } @Override public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { delegate.writeEmptyElement(prefix, localName, namespaceURI); + indent = true; } @Override public void writeEmptyElement(String localName) throws XMLStreamException { delegate.writeEmptyElement(localName); + indent = true; } @Override public void writeAttribute(String localName, String value) throws XMLStreamException { delegate.writeAttribute( localName, value); - } @Override @@ -158,8 +174,7 @@ public class IndentingXMLStreamWriter implements XMLStreamWriter{ @Override public void writeComment(String data) throws XMLStreamException { - delegate.writeComment(data); - + delegate.writeComment(data); } @Override @@ -192,11 +207,13 @@ public class IndentingXMLStreamWriter implements XMLStreamWriter{ @Override public void writeCharacters(String text) throws XMLStreamException { delegate.writeCharacters(text); + indent=false; } @Override public void writeCharacters(char[] text, int start, int len) throws XMLStreamException { delegate.writeCharacters(text, start, len); + indent=false; } @Override diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java index 1976664..c5c3f5e 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java @@ -11,6 +11,8 @@ import java.util.Map; import javax.xml.namespace.QName; import org.simantics.utils.datastructures.BijectionMap; +import org.simantics.xml.sax.SchemaConversionBase.RefType; +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; @@ -122,7 +124,7 @@ public class ExporterGenerator extends JavaGenerator{ } @Override - protected String getBaseClass() { + protected String getBaseClass(ObjectType type) { return "org.simantics.xml.sax.base.XMLElementNamedChildWriterBase"; } @@ -146,20 +148,21 @@ public class ExporterGenerator extends JavaGenerator{ } writers.put(complexTypeObj, fw); - String baseClass = getBaseClass(); - - boolean inherited = false; - - QName type = getComplexTypeBase(topLevelComplexType); - if (type != null && !type.getNamespaceURI().equals("http://www.w3.org/2001/XMLSchema")) { - SchemaObject obj = complexTypeName.get(type.getLocalPart()); -// if (obj == null) -// obj = simpleTypeName.get(type.getLocalPart()); - if (obj != null) { - baseClass = getName(obj); - inherited = true; - } - } +// String baseClass = getBaseClass(ObjectType.COMPLEX_TYPE); +// +// boolean inherited = false; + +// QName type = getComplexTypeBase(topLevelComplexType); +// if (type != null && !type.getNamespaceURI().equals("http://www.w3.org/2001/XMLSchema")) { +// SchemaObject obj = complexTypeName.get(type.getLocalPart()); +//// if (obj == null) +//// obj = simpleTypeName.get(type.getLocalPart()); +// if (obj != null) { +// baseClass = getName(obj); +// inherited = true; +// } +// } + Inheritance inheritance = getInheritance(complexTypeObj); provider = getIDProvider(topLevelComplexType); // List references = getIDReferences(topLevelComplexType); @@ -172,7 +175,7 @@ public class ExporterGenerator extends JavaGenerator{ // intrerfaces.add("org.simantics.xml.sax.base.UnrecognizedElementParser"); createClassHeader(fw.writer); - writeClass(fw.writer,false, topLevelComplexType.getName(), className, baseClass, Collections.EMPTY_LIST); + writeClass(fw.writer,false, topLevelComplexType.getName(), className, inheritance.baseClass, Collections.EMPTY_LIST); // writeIDProvider(fw.writer); @@ -225,7 +228,8 @@ public class ExporterGenerator extends JavaGenerator{ if (stringWriter.getBuffer().length() > 0) { fw.writer.write(stringWriter.toString()); } - + if (inheritance.type == InheritanceType.ComplexType) + fw.writer.println(" super.children(graph,instance,result);"); fw.writer.println(" }"); fw.writer.println(); @@ -242,7 +246,7 @@ public class ExporterGenerator extends JavaGenerator{ fw.writer.println(" @Override"); fw.writer.println(" public void attributes(ReadGraph graph, WriterElement instance, Collection attributes, XMLStreamWriter writer) throws XMLStreamException, DatabaseException{"); - if (inherited) { + if (inheritance.type == InheritanceType.ComplexType) { fw.writer.println(" super.attributes(graph,instance,attributes,writer);"); } fw.writer.println(" "+getOntologyImport()); @@ -252,6 +256,14 @@ public class ExporterGenerator extends JavaGenerator{ fw.writer.println(" }"); fw.writer.println(" }"); + if (inheritance.type == InheritanceType.AtomicType) { + fw.writer.println(); + fw.writer.println(" @Override"); + fw.writer.println(" public void characters(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException {"); + fw.writer.println(" writer.writeCharacters("+inheritance.atomicType.getToString("graph.getValue(instance.getResource(),"+inheritance.atomicType.binding+")")+");"); + fw.writer.println(" }"); + } + // writeIDReferences(fw.writer,name, references); // writeUnknownChild(fw.writer,name,unknownChildElement); @@ -267,19 +279,21 @@ public class ExporterGenerator extends JavaGenerator{ @Override - //protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String ref, QName refType) { protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType referenceType) { FileWriter fw = getWriter(parent); + String objectName; if (referenceType != RefType.Element) { QName refType; if (referenceType == RefType.Type) { refType = element.getElement().getType(); if (refName == null) refName = element.getElement().getName(); + objectName = element.getElement().getName(); } else { refType = element.getElement().getRef(); if (refName == null) refName = refType.getLocalPart(); + objectName = refType.getLocalPart(); } String binding = getBindingFromPrimitiveType(refType); @@ -287,26 +301,39 @@ public class ExporterGenerator extends JavaGenerator{ SchemaObject refElement = elementName.get(refType.getLocalPart()); SchemaObject refComplexType = complexTypeName.get(refType.getLocalPart()); - String className = null; - String refClassName = null; + // prefer element reference over complex type reference + String primaryClassName = null; + String secondaryClassName = null; if (refElement != null) - className = getName(refElement); + primaryClassName = getName(refElement); else - className = getName(refComplexType); + primaryClassName = getName(refComplexType); if (refComplexType != null) { - refClassName = getName(refComplexType); + secondaryClassName = getName(refComplexType); } else { - refClassName = getName(refElement); + secondaryClassName = getName(refElement); } - if (referenceType != RefType.Reference) - fw.writer.println(" addTypeWriter("+ontShort+ className +", "+className+".class);"); - else - fw.writer.println(" addWriter("+className+".class);"); - if (!className.equals(refClassName)) - fw.writer.println(" addWriter("+refClassName+".class);"); - + if (referenceType == RefType.Type) { + // create internal class for handling the element and child attachment + secondaryClassName = getName(parent) +"_" +objectName; + fw.writer.println(" addRelationWriter("+ontShort+ getName(parent)+"_has"+refName +", "+secondaryClassName+".class);"); + fw.delayedWriter2.println(" public static class " + secondaryClassName +" extends " + primaryClassName +"{"); + fw.delayedWriter2.println(" public "+ secondaryClassName +"(ReadGraph graph){"); + fw.delayedWriter2.println(" super(graph);"); + fw.delayedWriter2.println(" }"); + fw.delayedWriter2.println(" "); + fw.delayedWriter2.println(" @Override"); + fw.delayedWriter2.println(" public java.lang.String getElementId() {"); + fw.delayedWriter2.println(" return \"" + refName +"\";"); + fw.delayedWriter2.println(" }"); + fw.delayedWriter2.println(" }"); + } else { //referenceType == RefType.Reference + fw.writer.println(" addWriter("+primaryClassName+".class);"); + if (!primaryClassName.equals(secondaryClassName)) + fw.writer.println(" addWriter("+secondaryClassName+".class);"); + } // fw.delayedWriter.println(" if (child.getElementParser() instanceof "+refClassName+"){"); // fw.delayedWriter.println(" graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+ref + ", child.getData());"); if (useElementList(parent, indicator,element, referenceType == RefType.Reference, refName, refType)) { @@ -643,7 +670,7 @@ public class ExporterGenerator extends JavaGenerator{ writers.put(elementObj, fw); boolean isList = false; - Inheritance inhertiance = getElementInheritance(elementObj); + Inheritance inheritance = getInheritance(elementObj); provider = getIDProvider(element); List references = getIDReferences(element); @@ -656,7 +683,7 @@ public class ExporterGenerator extends JavaGenerator{ // intrerfaces.add("org.simantics.xml.sax.base.UnrecognizedElementParser"); createClassHeader(fw.writer); - writeClass(fw.writer,false, element.getName(), className, inhertiance.baseClass, Collections.EMPTY_LIST); + writeClass(fw.writer,false, element.getName(), className, inheritance.baseClass, Collections.EMPTY_LIST); // writeIDProvider(fw.writer); fw.writer.println(" @Override"); fw.writer.println(" public Resource getType(ReadGraph graph) throws DatabaseException {"); @@ -684,7 +711,7 @@ public class ExporterGenerator extends JavaGenerator{ fw.writer.println(" @Override"); fw.writer.println(" public void attributes(ReadGraph graph, WriterElement instance, Collection attributes, XMLStreamWriter writer) throws XMLStreamException, DatabaseException{"); - if (inhertiance.type == InheritanceType.ComplexType) { + if (inheritance.type == InheritanceType.ComplexType) { fw.writer.println(" super.attributes(graph,instance,attributes,writer);"); } fw.writer.println(" "+getOntologyImport()); @@ -713,7 +740,7 @@ public class ExporterGenerator extends JavaGenerator{ // } // fw.writer.println(" }"); - if (inhertiance.type == InheritanceType.AtomicType) { + if (inheritance.type == InheritanceType.AtomicType) { fw.writer.println(); // fw.writer.println(" @Override"); // fw.writer.println(" public void configure(WriteGraph graph, ParserElement element, java.lang.String string) throws DatabaseException {"); @@ -721,7 +748,7 @@ public class ExporterGenerator extends JavaGenerator{ // fw.writer.println(" }"); fw.writer.println(" @Override"); fw.writer.println(" public void characters(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException {"); - fw.writer.println(" writer.writeCharacters("+inhertiance.atomicType.getToString("graph.getValue(instance.getResource(),"+inhertiance.atomicType.binding+")")+");"); + fw.writer.println(" writer.writeCharacters("+inheritance.atomicType.getToString("graph.getValue(instance.getResource(),"+inheritance.atomicType.binding+")")+");"); // fw.writer.println(" graph.claimValue(element.getData(),"+getValueGetter(binding)+", "+binding.binding +");"); fw.writer.println(" }"); } @@ -756,7 +783,7 @@ public class ExporterGenerator extends JavaGenerator{ fw.writer.write(stringWriter.toString()); } - if (inhertiance.type == InheritanceType.ComplexType) { + if (inheritance.type == InheritanceType.ComplexType) { fw.writer.println(" super.children(graph,instance,result);"); } 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 f55f9cf..55ff5ef 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 @@ -11,6 +11,9 @@ import java.util.Map; import javax.xml.namespace.QName; import org.simantics.utils.datastructures.BijectionMap; +import org.simantics.xml.sax.SchemaConversionBase.InheritanceType; +import org.simantics.xml.sax.SchemaConversionBase.RefType; +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; @@ -114,7 +117,9 @@ public class ImporterGenerator extends JavaGenerator{ String name = getName(complexTypeObj); -// if (topLevelComplexType.getName().equals("Reference")) + //if (topLevelComplexType.getName().equals("LocalizedText")) + //if (topLevelComplexType.getName().equals("Reference")) +// if (topLevelComplexType.getName().equals("NodeIdAlias")) // System.out.println(); String className = name;//"_"+name; @@ -126,19 +131,8 @@ public class ImporterGenerator extends JavaGenerator{ throw new RuntimeException(e); } writers.put(complexTypeObj, fw); - - String baseClass = getBaseClass(); - - boolean inherited = false; - QName type = getComplexTypeBase(topLevelComplexType); - if (type != null && !type.getNamespaceURI().equals("http://www.w3.org/2001/XMLSchema")) { - SchemaObject obj = complexTypeName.get(type.getLocalPart()); - if (obj != null) { - baseClass = getName(obj); - inherited = true; - } - } + Inheritance inheritance = getInheritance(complexTypeObj); provider = getIDProvider(topLevelComplexType); List references = getIDReferences(topLevelComplexType); @@ -151,7 +145,7 @@ public class ImporterGenerator extends JavaGenerator{ intrerfaces.add("org.simantics.xml.sax.base.UnrecognizedElementParser"); createClassHeader(fw.writer, false); - writeClass(fw.writer,false, null, className, baseClass, intrerfaces); + writeClass(fw.writer,false, null, className, inheritance.baseClass, intrerfaces); writeIDProvider(fw.writer); @@ -192,9 +186,11 @@ public class ImporterGenerator extends JavaGenerator{ if (stringWriter.getBuffer().length() > 0) { fw.writer.write(stringWriter.toString()); } - - + if (inheritance.type == InheritanceType.ComplexType) { + fw.writer.println(" return super.connectChild(graph,element,child);"); + }else{ fw.writer.println(" return false;"); + } fw.writer.println(" }"); fw.writer.println(); @@ -211,7 +207,7 @@ public class ImporterGenerator extends JavaGenerator{ fw.writer.println(" @Override"); fw.writer.println(" public void configure(WriteGraph graph, Deque parents, ParserElement element) throws DatabaseException {"); - if (inherited) { + if (inheritance.type == InheritanceType.ComplexType) { fw.writer.println(" super.configure(graph,parents,element);"); } fw.writer.println(" "+getOntologyImport()); @@ -221,6 +217,14 @@ public class ImporterGenerator extends JavaGenerator{ fw.writer.println(" }"); + if (inheritance.type == InheritanceType.AtomicType) { + fw.writer.println(); + fw.writer.println(" @Override"); + fw.writer.println(" public void configure(WriteGraph graph, ParserElement element, java.lang.String string) throws DatabaseException {"); + fw.writer.println(" graph.claimValue(element.getData(), "+getValueGetter(inheritance.atomicType,"string")+", "+inheritance.atomicType.binding+");"); + fw.writer.println(" }"); + } + writeIDReferences(fw.writer,name, references); writeUnknownChild(fw.writer,name,unknownChildElement); @@ -234,19 +238,21 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - //protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String ref, QName refType) { protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType referenceType) { FileWriter fw = getWriter(parent); + String objectName; if (referenceType != RefType.Element) { QName refType; if (referenceType == RefType.Type) { refType = element.getElement().getType(); if (refName == null) refName = element.getElement().getName(); + objectName = element.getElement().getName(); } else { refType = element.getElement().getRef(); if (refName == null) refName = refType.getLocalPart(); + objectName = refType.getLocalPart(); } String binding = getBindingFromPrimitiveType(refType); @@ -254,27 +260,35 @@ public class ImporterGenerator extends JavaGenerator{ SchemaObject refElement = elementName.get(refType.getLocalPart()); SchemaObject refComplexType = complexTypeName.get(refType.getLocalPart()); - String className = null; - String refClassName = null; + // prefer element reference over complex type reference + String primaryClassName = null; + String secondaryClassName = null; if (refElement != null) - className = getName(refElement); + primaryClassName = getName(refElement); else - className = getName(refComplexType); + primaryClassName = getName(refComplexType); if (refComplexType != null) { - refClassName = getName(refComplexType); + secondaryClassName = getName(refComplexType); } else { - refClassName = getName(refElement); + secondaryClassName = getName(refElement); } - if (referenceType != RefType.Reference) - fw.writer.println(" addParser(\""+ refName +"\", "+className+".class);"); - else - fw.writer.println(" addParser("+className+".class);"); - if (!className.equals(refClassName)) - fw.writer.println(" addParser("+refClassName+".class);"); + if (referenceType == RefType.Type) { + // create internal class for handling the element and child attachment + secondaryClassName = getName(parent) +"_" +objectName; + fw.writer.println(" addParser(\""+ objectName +"\", "+secondaryClassName+".class);"); + fw.delayedWriter2.println(" public static class " + secondaryClassName +" extends " + primaryClassName +"{"); + fw.delayedWriter2.println(" public "+ secondaryClassName +"(){"); + fw.delayedWriter2.println(" }"); + fw.delayedWriter2.println(" }"); + } else { // referenceType == RefType.Reference + fw.writer.println(" addParser("+primaryClassName+".class);"); + if (!primaryClassName.equals(secondaryClassName)) + fw.writer.println(" addParser("+secondaryClassName+".class);"); + } - fw.delayedWriter.println(" if (child.getElementParser() instanceof "+refClassName+"){"); + fw.delayedWriter.println(" if (child.getElementParser() instanceof "+secondaryClassName+"){"); fw.delayedWriter.println(" graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+refName + ", child.getData());"); if (useElementList(parent, indicator,element, referenceType == RefType.Reference, refName, refType)) { @@ -584,7 +598,7 @@ public class ImporterGenerator extends JavaGenerator{ writers.put(elementObj, fw); boolean isList = false; - Inheritance inhertiance = getElementInheritance(elementObj); + Inheritance inheritance = getInheritance(elementObj); provider = getIDProvider(element); List references = getIDReferences(element); @@ -597,7 +611,7 @@ public class ImporterGenerator extends JavaGenerator{ intrerfaces.add("org.simantics.xml.sax.base.UnrecognizedElementParser"); createClassHeader(fw.writer, isList); - writeClass(fw.writer,false, element.getName(), className, inhertiance.baseClass, intrerfaces); + writeClass(fw.writer,false, element.getName(), className, inheritance.baseClass, intrerfaces); writeIDProvider(fw.writer); fw.writer.println(" @Override"); fw.writer.println(" public Resource create(WriteGraph graph, ParserElement element) throws DatabaseException{"); @@ -623,7 +637,7 @@ public class ImporterGenerator extends JavaGenerator{ fw.writer.println(" @Override"); fw.writer.println(" public void configure(WriteGraph graph, Deque parents, ParserElement element) throws DatabaseException {"); - if (inhertiance.type == InheritanceType.ComplexType) { + if (inheritance.type == InheritanceType.ComplexType) { fw.writer.println(" super.configure(graph,parents,element);"); } fw.writer.println(" "+getOntologyImport()); @@ -637,11 +651,11 @@ public class ImporterGenerator extends JavaGenerator{ } fw.writer.println(" }"); - if (inhertiance.type == InheritanceType.AtomicType) { + if (inheritance.type == InheritanceType.AtomicType) { fw.writer.println(); fw.writer.println(" @Override"); fw.writer.println(" public void configure(WriteGraph graph, ParserElement element, java.lang.String string) throws DatabaseException {"); - fw.writer.println(" graph.claimValue(element.getData(), "+getValueGetter(inhertiance.atomicType,"string")+", "+inhertiance.atomicType.binding+");"); + fw.writer.println(" graph.claimValue(element.getData(), "+getValueGetter(inheritance.atomicType,"string")+", "+inheritance.atomicType.binding+");"); fw.writer.println(" }"); } // if (simpleType != null) { @@ -674,7 +688,7 @@ public class ImporterGenerator extends JavaGenerator{ if (stringWriter.getBuffer().length() > 0) { fw.writer.write(stringWriter.toString()); } - if (inhertiance.type == InheritanceType.ComplexType) { + if (inheritance.type == InheritanceType.ComplexType) { fw.writer.println(" return super.connectChild(graph,element,child);"); } else { fw.writer.println(" return false;"); @@ -767,7 +781,7 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected String getBaseClass() { + protected String getBaseClass(ObjectType type) { return "org.simantics.xml.sax.base.XMLElementNamedChildParserBase"; } diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java index 887bd1e..bf52b9b 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java @@ -65,11 +65,11 @@ public abstract class JavaGenerator extends SchemaConversionBase{ return binding.getValueGetter(); } - public static String getComplexTypePrefix() { + public String getComplexTypePrefix() { return "ComplexTypes_"; } - public static String getAttributeGroupPrefix() { + public String getAttributeGroupPrefix() { return "AttributeGroups_"; } @@ -122,7 +122,7 @@ public abstract class JavaGenerator extends SchemaConversionBase{ } - public static String getName(SchemaObject obj) { + public String getName(SchemaObject obj) { if (obj.getParent() == null) { switch (obj.getType()) { case COMPLEX_TYPE: @@ -162,7 +162,7 @@ public abstract class JavaGenerator extends SchemaConversionBase{ } - public static String getName(SchemaObject obj, String rel) { + public String getName(SchemaObject obj, String rel) { if (obj.getParent() == null) { switch (obj.getType()) { case COMPLEX_TYPE: @@ -199,18 +199,7 @@ public abstract class JavaGenerator extends SchemaConversionBase{ throw new RuntimeException(); } - protected enum InheritanceType{ComplexType,AtomicType,None}; - protected class Inheritance { - public String baseClass; - public InheritanceType type; - public TypeEntry atomicType; - - public Inheritance(String baseClass) { - this.baseClass = baseClass; - this.type = InheritanceType.None; - } - } protected void writeClass(PrintWriter writer,boolean abst, String elementId, String className, String baseClass, List interfaces) { writer.println("@SuppressWarnings(\"unused\")"); @@ -235,63 +224,8 @@ public abstract class JavaGenerator extends SchemaConversionBase{ writer.println(); } - protected abstract String getBaseClass(); - protected Inheritance getElementInheritance(SchemaObject topLevelElementObj) { - Element topLevelElement = topLevelElementObj.getElement(); - Inheritance inheritance = new Inheritance(getBaseClass()); - 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(topLevelElementObj, type.getLocalPart()); - 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(topLevelElementObj, type.getLocalPart()); - inheritance.baseClass = getName(obj); - inheritance.type = InheritanceType.ComplexType; - } else { - TypeEntry entry = getTypeEntry(type); - if (entry != null) { - inheritance.type = InheritanceType.AtomicType; - inheritance.atomicType = entry; - } - } - } - } - - return inheritance; - } + + } 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 e64a59b..741e197 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 @@ -11,6 +11,7 @@ import java.util.Set; import javax.xml.namespace.QName; import org.simantics.utils.datastructures.BijectionMap; +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.IDReference; @@ -140,11 +141,11 @@ public class OntologyGenerator extends SchemaConversionBase { } - public static String getComplexTypePrefix() { + public String getComplexTypePrefix() { return "ComplexTypes."; } - public static String getAttributeGroupPrefix() { + public String getAttributeGroupPrefix() { return "AttributeGroups."; } @@ -163,7 +164,7 @@ public class OntologyGenerator extends SchemaConversionBase { break; } } - String relationName = ontRoot+getName(parent)+".has"+name; + String relationName = getName(parent)+".has"+name; writer.print(relationName); List types = new ArrayList(); @@ -181,7 +182,7 @@ public class OntologyGenerator extends SchemaConversionBase { } if (type == null) { SchemaObject obj = getWithName(parent, refType.getLocalPart()); - types.add(ontRoot+getName(obj,"has")); + types.add(getName(obj,"has")); } } if (types.size() > 0) { @@ -248,21 +249,21 @@ public class OntologyGenerator extends SchemaConversionBase { if (type == null) { obj = getWithName(parent, referenceType.getLocalPart()); - writer.println(ontRoot+getName(parent)+".has"+refName + " " + ontRoot+getName(obj)); + writer.println(getName(parent)+".has"+refName + " " + getName(obj)); } else { - writer.println(ontRoot+getName(parent)+".has"+refName + " " + getType(referenceType)); } if (useElementList(parent, indicator,element, refType == RefType.Reference, refName, referenceType)) { if (type == null) { - writer.println(ontRoot+getName(parent)+"."+refName + "List " + ontRoot+getName(obj)); + writer.println(getName(parent)+".has"+refName + " " + getName(obj)); if (useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) { - writer.println(ontRoot+getName(parent)+"."+refName + "List " + ontRoot+getComplexTypePrefix()+name); - writer.println(" --> " + ontRoot+name); + writer.println(" --> " + name); writer.println(); - String baseType = "XML.ComplexType"; - - QName base = getComplexTypeBase(topLevelComplexType.getComplexType()); - if (base != null) { - baseType = getType(base); - } - base = getSimpleTypeBase(topLevelComplexType.getSimpleType()); - if (base != null) { - baseType = getType(base); +// String baseType = "XML.ComplexType"; +// +// QName base = getComplexTypeBase(topLevelComplexType.getComplexType()); +// if (base != null) { +// baseType = getType(base); +// } +// base = getSimpleTypeBase(topLevelComplexType.getSimpleType()); +// if (base != null) { +// baseType = getType(base); +// } + Inheritance inheritance = getInheritance(topLevelComplexType); + +// writer.println(name+ " references = getIDReferences(element); for (IDReference ref : references) { - writer.println(ontRoot+name+"."+ref.getReference().getName()+ "