X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FExporterGenerator.java;h=243a1c2415b9f3476c270e9be542a8ff5c441f99;hb=5a2849a5e792c9821201d320cb30b3dc41a586bf;hp=1d97259528bef8569311d85c27508f3e9b0f912a;hpb=dd3b2c7ecd5f4b60734f2602b16637aa8be2a263;p=simantics%2Finterop.git 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 1d97259..243a1c2 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,11 +11,11 @@ import java.util.Map; 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.IDProvider; import org.simantics.xml.sax.configuration.IDReference; -import org.simantics.xml.sax.configuration.Rename; import org.simantics.xml.sax.configuration.UnrecognizedChildElement; import org.w3._2001.xmlschema.Annotated; import org.w3._2001.xmlschema.Attribute; @@ -25,7 +25,6 @@ import org.w3._2001.xmlschema.Element; import org.w3._2001.xmlschema.LocalComplexType; import org.w3._2001.xmlschema.LocalSimpleType; 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.SimpleType; @@ -39,7 +38,7 @@ public class ExporterGenerator extends JavaGenerator{ - public void createParser(Schema schema,String className, SchemaConverter converter) throws IOException { + public void createParser(Schema schema,String ontologyUri, String className, SchemaConverter converter) throws IOException { this.schema = schema; this.ontologyClassName = className; this.converter = converter; @@ -101,6 +100,7 @@ public class ExporterGenerator extends JavaGenerator{ mainWriter.println(" public " + name + parserClassPostfix+"(Session session) throws DatabaseException {"); if (schema.getTargetNamespace() != null) mainWriter.println(" setSchemaURI(\""+schema.getTargetNamespace()+"\");"); + mainWriter.println(" setOntologyURI(\""+ontologyUri+"\");"); mainWriter.println(" session.syncRequest(new ReadRequest() {"); mainWriter.println(" @Override"); mainWriter.println(" public void run(ReadGraph graph) throws DatabaseException {"); @@ -123,16 +123,19 @@ public class ExporterGenerator extends JavaGenerator{ } @Override - protected String getBaseClass() { + protected String getBaseClass(ObjectType type) { return "org.simantics.xml.sax.base.XMLElementNamedChildWriterBase"; } + @Override + protected void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { + } @Override protected void handleComplexType(SchemaObject complexTypeObj) { ComplexType topLevelComplexType = complexTypeObj.getComplexType(); - String name = getName(complexTypeObj);//topLevelComplexType.getName(); + String name = getName(complexTypeObj); String className = name;//"_"+name; @@ -143,22 +146,26 @@ public class ExporterGenerator extends JavaGenerator{ throw new RuntimeException(e); } writers.put(complexTypeObj, fw); - - boolean isList = false; - String baseClass = "org.simantics.xml.sax.base.XMLElementNamedChildWriterBase"; - - boolean inherited = false; - - QName type = getComplexTypeBase(topLevelComplexType); - if (type != null && !type.getNamespaceURI().equals("http://www.w3.org/2001/XMLSchema")) { - baseClass = getName(complexTypeName.get(type.getLocalPart())); - 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); - UnrecognizedChildElement unknownChildElement = getUnknown(topLevelComplexType); +// List references = getIDReferences(topLevelComplexType); +// UnrecognizedChildElement unknownChildElement = getUnknown(topLevelComplexType); // List intrerfaces = new ArrayList(); // if (references.size() > 0) @@ -167,7 +174,7 @@ public class ExporterGenerator extends JavaGenerator{ // intrerfaces.add("org.simantics.xml.sax.base.UnrecognizedElementParser"); createClassHeader(fw.writer); - writeClass(fw.writer,false, null, className, baseClass, Collections.EMPTY_LIST); + writeClass(fw.writer,false, topLevelComplexType.getName(), className, inheritance.baseClass, Collections.EMPTY_LIST); // writeIDProvider(fw.writer); @@ -213,18 +220,17 @@ public class ExporterGenerator extends JavaGenerator{ fw.writer.println(" }"); -// fw.writer.println(" @Override"); -// fw.writer.println(" public boolean connectChild(WriteGraph graph, ParserElement element, ParserElement child) throws DatabaseException{"); -// fw.writer.println(" "+getOntologyImport()); -// -// if (stringWriter.getBuffer().length() > 0) { -// fw.writer.write(stringWriter.toString()); -// } -// -// -// fw.writer.println(" return false;"); -// fw.writer.println(" }"); -// fw.writer.println(); + fw.writer.println(" @Override"); + fw.writer.println(" public void children(ReadGraph graph, WriterElement instance, java.util.Set result) throws XMLStreamException, DatabaseException {"); + fw.writer.println(" "+getOntologyImport()); + + 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(); if (stringWriter2.getBuffer().length() > 0) { fw.writer.write(stringWriter2.toString()); @@ -239,7 +245,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()); @@ -249,6 +255,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); @@ -261,51 +275,51 @@ public class ExporterGenerator extends JavaGenerator{ provider = null; } - - @Override - protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String ref, QName refType) { + protected void createReferenceIndicator(SchemaObject parent, RefType referenceType, String refName, String objectName, + String primaryClassName, String secondaryClassName, boolean useElementList, boolean useOriginalList) { FileWriter fw = getWriter(parent); - String binding = getBindingFromPrimitiveType(refType); - if (binding == null) { - SchemaObject refElement = elementName.get(refType.getLocalPart()); - SchemaObject refComplexType = complexTypeName.get(refType.getLocalPart()); - - String className = null; - String refClassName = null; - if (refElement != null) - className = getName(refElement); - else - className = getName(refComplexType); - - if (refComplexType != null) { - refClassName = getName(refComplexType); - } else { - refClassName = getName(refElement); - } - - if (!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 ("TextOrField".equals(refName)) + System.out.println(); + if (referenceType == RefType.Type) { + // create internal class for handling the element and child attachment + secondaryClassName = getName(parent) +"_" +objectName; + if (objectName.equals(refName)) + fw.writer.println(" addRelationWriter("+ontShort+ getName(parent)+"_has"+refName +", "+secondaryClassName+".class);"); + else // FIXME : type name may not be correct + fw.writer.println(" addRelationTypeWriter("+ontShort+ getName(parent)+"_has"+refName +", " +ontShort+primaryClassName +", "+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, reference, ref, refType)) { -// -// // element type specific list -// fw.delayedWriter.println(" {"); -// fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List);"); -// fw.delayedWriter.println(" if (list == null) {"); -// fw.delayedWriter.println(" list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));"); -// fw.delayedWriter.println(" graph.claim(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List,list);"); -// fw.delayedWriter.println(" } else {"); -// fw.delayedWriter.println(" org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));"); -// fw.delayedWriter.println(" }"); -// fw.delayedWriter.println(" }"); -// } + if (useElementList) { + + // element type specific list + fw.delayedWriter.println(" {"); + fw.delayedWriter.println(" org.simantics.db.Resource list = graph.getPossibleObject(instance.getResource(),"+ontShort+getName(parent)+"_has"+refName + "List);"); + fw.delayedWriter.println(" if (list != null) {"); + fw.delayedWriter.println(" java.util.List l = org.simantics.db.common.utils.ListUtils.toList(graph, list);"); + fw.delayedWriter.println(" result.addAll(l);"); + fw.delayedWriter.println(" } else {"); + fw.delayedWriter.println(" result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));"); + fw.delayedWriter.println(" }"); + fw.delayedWriter.println(" }"); + } else { + fw.delayedWriter.println(" result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));"); + } // if (useOriginalList(parent, indicator,element, reference, ref, refType)) { // // generic list // fw.delayedWriter.println(" {"); @@ -319,50 +333,53 @@ public class ExporterGenerator extends JavaGenerator{ // fw.delayedWriter.println(" }"); // fw.delayedWriter.println(" }"); // } -// + // fw.delayedWriter.println(" return true;"); // fw.delayedWriter.println(" }"); - } else { - SchemaObject refElement = elementName.get(refType.getLocalPart()); - //writer.println(commentTag+ontShort+"."+parent+".has"+ref + " l = org.simantics.db.common.utils.ListUtils.toList(graph, list);"); + fw.delayedWriter.println(" result.addAll(l);"); + fw.delayedWriter.println(" } else {"); + fw.delayedWriter.println(" result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));"); + fw.delayedWriter.println(" }"); + fw.delayedWriter.println(" }"); + } else { + fw.delayedWriter.println(" result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));"); + } // if (useOriginalList(parent, indicator,element, reference, ref, new QName(obj.getName()))) { // // generic list // fw.delayedWriter.println(" {"); @@ -396,10 +413,7 @@ public class ExporterGenerator extends JavaGenerator{ fw.delayedWriter2.println(" return null;"); fw.delayedWriter2.println(" }"); } - - - - + @Override protected void handle(SchemaObject parent, Attribute attribute) { String name = attribute.getName(); @@ -592,10 +606,7 @@ public class ExporterGenerator extends JavaGenerator{ } - @Override - protected void handleSimpleType(SchemaObject parent, SchemaObject simpleType) { - - } + @@ -616,7 +627,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); @@ -629,7 +640,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 {"); @@ -657,7 +668,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()); @@ -669,6 +680,7 @@ public class ExporterGenerator extends JavaGenerator{ fw.writer.println(" }"); fw.writer.println(" }"); + // fw.writer.println(" @Override"); // fw.writer.println(" public void configure(WriteGraph graph, Deque parents, ParserElement element) throws DatabaseException {"); // if (inhertiance.type == InheritanceType.ComplexType) { @@ -685,7 +697,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 {"); @@ -693,7 +705,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(" }"); } @@ -720,21 +732,21 @@ public class ExporterGenerator extends JavaGenerator{ fw.writer.println(" }"); -// fw.writer.println(" @Override"); -// fw.writer.println(" public boolean connectChild(WriteGraph graph, ParserElement element, ParserElement child) throws DatabaseException{"); -// fw.writer.println(" "+getOntologyImport()); -// -// if (stringWriter.getBuffer().length() > 0) { -// fw.writer.write(stringWriter.toString()); -// } -// if (inhertiance.type == InheritanceType.ComplexType) { -// fw.writer.println(" return super.connectChild(graph,element,child);"); -// } else { -// fw.writer.println(" return false;"); -// } -// fw.writer.println(" }"); -// fw.writer.println(); + fw.writer.println(" @Override"); + fw.writer.println(" public void children(ReadGraph graph, WriterElement instance, java.util.Set result) throws XMLStreamException, DatabaseException {"); + fw.writer.println(" "+getOntologyImport()); + + 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(); + if (stringWriter2.getBuffer().length() > 0) { fw.writer.write(stringWriter2.toString()); }