From 28cdb08c2dcc6b306d8a0cbea8c0bd791e42d583 Mon Sep 17 00:00:00 2001 From: luukkainen Date: Wed, 25 Jan 2017 12:01:32 +0000 Subject: [PATCH] XML Elements are exported in the original order (if order is stored during import) refs #6985 git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@33435 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../xml/sax/base/AbstractExporter.java | 36 ++- .../sax/base/IndentingXMLStreamWriter.java | 208 +++++++++++++++ .../xml/sax/base/ValueElementWriter.java | 5 +- .../base/XMLElementNamedChildWriterBase.java | 7 +- .../xml/sax/base/XMLElementWriter.java | 4 +- .../org/simantics/xml/sax/base/XMLWriter.java | 69 ++--- .../xml/sax/base/datatypes/literal/Date.java | 2 +- .../sax/base/datatypes/literal/DateTime.java | 2 +- .../xml/sax/base/datatypes/literal/Time.java | 2 +- .../simantics/xml/sax/ExporterGenerator.java | 248 ++++++++++-------- .../simantics/xml/sax/ImporterGenerator.java | 180 +++++++------ .../org/simantics/xml/sax/JavaGenerator.java | 44 ++-- .../simantics/xml/sax/OntologyGenerator.java | 69 +++-- .../xml/sax/SchemaConversionBase.java | 18 +- 14 files changed, 582 insertions(+), 312 deletions(-) create mode 100644 org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/IndentingXMLStreamWriter.java diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractExporter.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractExporter.java index 14a3fca..3464bda 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractExporter.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractExporter.java @@ -1,8 +1,10 @@ package org.simantics.xml.sax.base; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; @@ -23,6 +25,7 @@ public class AbstractExporter { private ILogger logger; private Resource root; private XMLWriter xmlWriter; + private boolean indentOutput = true; public AbstractExporter(Session session, File file , Resource root, XMLWriter xmlWriter) { this.session = session; @@ -41,6 +44,10 @@ public class AbstractExporter { this.xmlWriter = xmlWriter; } + public void setIndentOutput(boolean indentOutput) { + this.indentOutput = indentOutput; + } + public void doExport() throws DatabaseException { doExport(MessageService.getDefault()); } @@ -50,24 +57,47 @@ public class AbstractExporter { session.syncRequest(req); } + private static XMLStreamWriter createIdentXMLStreamWriter(OutputStream textWriter) throws XMLStreamException { + XMLOutputFactory output = XMLOutputFactory.newInstance(); + return new IndentingXMLStreamWriter(output.createXMLStreamWriter(textWriter, "UTF-8")); + //return new com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter(output.createXMLStreamWriter(textWriter, "UTF-8")); + } + + private static XMLStreamWriter createXMLStreamWriter(OutputStream textWriter) throws XMLStreamException { + XMLOutputFactory output = XMLOutputFactory.newInstance(); + return output.createXMLStreamWriter(textWriter, "UTF-8"); + } + private class ExportRequest extends ReadRequest { @Override public void run(ReadGraph graph) throws DatabaseException { XMLStreamWriter writer = null; + OutputStream os = null; try { - XMLOutputFactory output = XMLOutputFactory.newInstance(); - writer = output.createXMLStreamWriter(new FileOutputStream(file),"UTF-8"); - writer.writeStartDocument("UTF-8", "1.0"); + os = new BufferedOutputStream(new FileOutputStream(file)); + if (indentOutput) + writer =createIdentXMLStreamWriter(os); + else + writer =createXMLStreamWriter(os); + writer.writeStartDocument("UTF-8", "1.1"); + xmlWriter.write(root, writer); writer.writeEndDocument(); + writer.flush(); writer.close(); + os.close(); } catch (IOException|XMLStreamException e) { try { writer.close(); } catch (XMLStreamException err) { + } + try { + os.close(); + }catch (IOException err) { + } throw new DatabaseException(e); } 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 new file mode 100644 index 0000000..a912564 --- /dev/null +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/IndentingXMLStreamWriter.java @@ -0,0 +1,208 @@ +package org.simantics.xml.sax.base; + +import javax.xml.namespace.NamespaceContext; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; + +/** + * Rather dummy indenting XML Stream Writer. + * + * @author Marko Luukkainen + * + */ +public class IndentingXMLStreamWriter implements XMLStreamWriter{ + + XMLStreamWriter delegate; + int depth = 0; + + public IndentingXMLStreamWriter(XMLStreamWriter delegate) { + this.delegate = delegate; + } + + @Override + public void close() throws XMLStreamException { + delegate.close(); + } + + @Override + public void flush() throws XMLStreamException { + delegate.close(); + } + + @Override + public NamespaceContext getNamespaceContext() { + return delegate.getNamespaceContext(); + } + + @Override + public String getPrefix(String uri) throws XMLStreamException { + return delegate.getPrefix(uri); + } + + @Override + public Object getProperty(String name) throws IllegalArgumentException { + return delegate.getProperty(name); + } + + @Override + public void setDefaultNamespace(String uri) throws XMLStreamException { + delegate.setDefaultNamespace(uri); + } + + @Override + public void setPrefix(String prefix, String uri) throws XMLStreamException { + delegate.setPrefix(prefix, uri); + } + + private String getIdent() { + String s = ""; + for (int i = 0; i < depth; i++) + s += " "; + return s; + } + + @Override + public void writeStartElement(String localName) throws XMLStreamException { + delegate.writeCharacters("\n"+getIdent()); + delegate.writeStartElement(localName); + depth++; + } + + @Override + public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException { + delegate.writeCharacters("\n"+getIdent()); + delegate.writeStartElement(namespaceURI, localName); + depth++; + } + + @Override + public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { + delegate.writeCharacters("\n"+getIdent()); + delegate.writeStartElement(prefix, localName, namespaceURI); + depth++; + } + + @Override + public void writeStartDocument() throws XMLStreamException { + delegate.writeStartDocument(); + depth++; + } + + @Override + public void writeStartDocument(String version) throws XMLStreamException { + delegate.writeStartDocument(version); + depth++; + } + + @Override + public void writeStartDocument(String encoding, String version) throws XMLStreamException { + delegate.writeStartDocument(encoding, version); + depth++; + } + + @Override + public void writeEndElement() throws XMLStreamException { + depth--; + delegate.writeCharacters("\n"+getIdent()); + delegate.writeEndElement(); + } + + @Override + public void writeEndDocument() throws XMLStreamException { + depth--; + delegate.writeCharacters("\n"+getIdent()); + delegate.writeEndDocument(); + } + + @Override + public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException { + delegate.writeEmptyElement(namespaceURI, localName); + } + + @Override + public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { + delegate.writeEmptyElement(prefix, localName, namespaceURI); + } + + @Override + public void writeEmptyElement(String localName) throws XMLStreamException { + delegate.writeEmptyElement(localName); + } + + @Override + public void writeAttribute(String localName, String value) throws XMLStreamException { + delegate.writeAttribute( localName, value); + + } + + @Override + public void writeAttribute(String prefix, String namespaceURI, String localName, String value) + throws XMLStreamException { + delegate.writeAttribute(prefix, namespaceURI, localName, value); + } + + @Override + public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException { + delegate.writeAttribute(namespaceURI, localName, value); + } + + @Override + public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException { + delegate.writeNamespace(prefix, namespaceURI); + } + + @Override + public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException { + delegate.writeDefaultNamespace(namespaceURI); + } + + @Override + public void writeComment(String data) throws XMLStreamException { + delegate.writeComment(data); + + } + + @Override + public void writeProcessingInstruction(String target) throws XMLStreamException { + delegate.writeProcessingInstruction(target); + } + + @Override + public void writeProcessingInstruction(String target, String data) throws XMLStreamException { + delegate.writeProcessingInstruction(target, data); + } + + @Override + public void writeCData(String data) throws XMLStreamException { + delegate.writeCData(data); + } + + @Override + public void writeDTD(String dtd) throws XMLStreamException { + delegate.writeDTD(dtd); + } + + @Override + public void writeEntityRef(String name) throws XMLStreamException { + delegate.writeEntityRef(name); + } + + + + @Override + public void writeCharacters(String text) throws XMLStreamException { + delegate.writeCharacters(text); + } + + @Override + public void writeCharacters(char[] text, int start, int len) throws XMLStreamException { + delegate.writeCharacters(text, start, len); + } + + @Override + public void setNamespaceContext(NamespaceContext context) throws XMLStreamException { + delegate.setNamespaceContext(context); + } + + +} diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ValueElementWriter.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ValueElementWriter.java index bcc7c95..42aa3c1 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ValueElementWriter.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ValueElementWriter.java @@ -3,6 +3,7 @@ package org.simantics.xml.sax.base; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Set; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; @@ -43,9 +44,9 @@ public class ValueElementWriter implements XMLElementWriter{ } @Override - public List children(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) + public void children(ReadGraph graph, WriterElement instance, Set sorted) throws XMLStreamException, DatabaseException { - return Collections.EMPTY_LIST; + } @Override diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementNamedChildWriterBase.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementNamedChildWriterBase.java index 69b6c8d..9c4ceb6 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementNamedChildWriterBase.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementNamedChildWriterBase.java @@ -1,9 +1,7 @@ package org.simantics.xml.sax.base; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; @@ -13,7 +11,6 @@ import javax.xml.stream.XMLStreamWriter; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; -import org.simantics.scl.compiler.elaboration.errors.ElabNode; public abstract class XMLElementNamedChildWriterBase implements XMLElementNamedChildWriter{ @@ -52,9 +49,9 @@ public abstract class XMLElementNamedChildWriterBase implements XMLElementNamedC } @Override - public List children(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) + public void children(ReadGraph graph, WriterElement instance, Set sorted) throws XMLStreamException, DatabaseException { - return Collections.EMPTY_LIST; + } @Override diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementWriter.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementWriter.java index cde7809..b11c24c 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementWriter.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementWriter.java @@ -1,7 +1,7 @@ package org.simantics.xml.sax.base; import java.util.Collection; -import java.util.List; +import java.util.Set; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; @@ -19,7 +19,7 @@ public interface XMLElementWriter { public void start(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException; public void attributes(ReadGraph graph, WriterElement instance, Collection attributes, XMLStreamWriter writer) throws XMLStreamException, DatabaseException; public void characters(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException; - public List children(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException; + public void children(ReadGraph graph, WriterElement instance, Set writer) throws XMLStreamException, DatabaseException; public void end(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException; } diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLWriter.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLWriter.java index 3fd881b..0881a5d 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLWriter.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLWriter.java @@ -4,6 +4,8 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -73,6 +75,11 @@ public class XMLWriter { XMLElementWriter elementWriter = instance.writer; elementWriter.start(graph, instance, writer); + if (instance.parent == null) { + if(getSchemaURI() != null) { + writer.writeDefaultNamespace(getSchemaURI()); + } + } elementWriter.attributes(graph, instance, graph.getStatements(instance.instance, XML.hasAttribute), writer); if (graph.hasValue(instance.instance)) elementWriter.characters(graph, instance, writer); @@ -85,15 +92,24 @@ public class XMLWriter { loadElement(c); elementMap.put(s.getObject(), c); } - List sorted = elementWriter.children(graph, instance, writer); - + LinkedHashSet sorted = new LinkedHashSet<>(); + if (graph.hasStatement(instance.instance, XML.hasOriginalElementList)) { + Resource originalList = graph.getSingleObject(instance.instance, XML.hasOriginalElementList); + List l = ListUtils.toList(graph, originalList); + sorted.addAll(l); + } + elementWriter.children(graph, instance, sorted); + Set processed = new HashSet<>(); for (Resource r : sorted) { + if (processed.contains(r)) // badly generated writer could report elements several times. + continue; WriterElement child = elementMap.get(r); write(child, writer); + processed.add(r); } //write the rest of the elements (in random order) for (Statement stm : childElements) { - if (sorted.contains(stm.getObject())) + if (processed.contains(stm.getObject())) continue; WriterElement child = elementMap.get(stm.getObject()); write(child, writer); @@ -103,53 +119,6 @@ public class XMLWriter { } -// public void write(Resource root, XMLStreamWriter writer) throws DatabaseException, XMLStreamException { -// XMLResource XML = XMLResource.getInstance(graph); -// -// Deque stack = new ArrayDeque<>(); -// Element rootElement = new Element(root); -// loadElement(rootElement); -// stack.push(rootElement); -// while (!stack.isEmpty()) { -// -// Element instance = stack.pop(); -// XMLElementWriter elementWriter = instance.writer; -// elementWriter.start(graph, instance.instance, writer); -// elementWriter.attributes(graph, graph.getStatements(instance.instance, XML.hasAttribute), writer); -// // get all child elements -// Collection childElements = graph.getStatements(instance.instance, XML.hasElement); -// // get original element order, if available -// Resource originalElementList = graph.getPossibleObject(instance.instance, XML.hasOriginalElementList); -// List originalList = null; -// if (originalElementList != null) { -// originalList = ListUtils.toList(graph, originalElementList); -// } -// if (originalList != null) { -// // check for elements that are missing form the original list (should be empty). -// for (Statement stm : childElements) { -// if (originalList.contains(stm.getObject())) -// continue; -// Element child = new Element(instance,stm.getObject()); -// loadElement(child); -// stack.push(child); -// } -// // process the list in reverse order so that the stack processing processes the items in correct order. -// for (int i = originalList.size()-1; i >= 0; i--) { -// Resource r = originalList.get(i); -// Element child = new Element(instance, r); -// loadElement(child); -// stack.push(child); -// } -// } else { -// for (Statement stm : childElements) { -// Element child = new Element(instance, stm.getObject()); -// loadElement(child); -// stack.push(child); -// } -// } -// } -// } - private void loadElement(WriterElement child) throws DatabaseException { XMLElementWriter childWriter = null; if (child.parent != null && child.parent.writer instanceof XMLElementNamedChildWriter) { diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Date.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Date.java index d551d11..e3e1bb4 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Date.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Date.java @@ -101,7 +101,7 @@ public class Date extends Bean implements org.simantics.xml.sax.base.datatypes.a @Override public String toString() { - return year + "-" + month + "-"+day; + return year + "-" +(month<10?"0":"")+ month + "-"+(day<10?"0":"")+day; } diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/DateTime.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/DateTime.java index a7c8ad6..ab664d8 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/DateTime.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/DateTime.java @@ -133,7 +133,7 @@ public class DateTime extends Bean implements org.simantics.xml.sax.base.datatyp @Override public String toString() { - return year + "-" + month + "-"+day + "T"+hours+ ":" + minutes + ":"+seconds; + return year + "-" +(month<10?"0":"")+ month + "-"+(day<10?"0":"")+day + "T"+(hours<10?"0":"")+hours+ ":" + (minutes<10?"0":"")+ minutes + ":"+(seconds<10?"0":"")+seconds; } } diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Time.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Time.java index 333f4a3..fa64567 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Time.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Time.java @@ -107,7 +107,7 @@ public class Time extends Bean implements org.simantics.xml.sax.base.datatypes.a @Override public String toString() { - return hours+ ":" + minutes + ":"+seconds; + return (hours<10?"0":"")+hours+ ":" + (minutes<10?"0":"")+ minutes + ":"+(seconds<10?"0":"")+seconds; } } 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..c767311 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.SchemaConversionBase.RefType; 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; @@ -213,18 +213,16 @@ 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()); + } + + fw.writer.println(" }"); + fw.writer.println(); if (stringWriter2.getBuffer().length() > 0) { fw.writer.write(stringWriter2.toString()); @@ -264,37 +262,122 @@ 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, boolean reference, String ref, QName refType) { + protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType referenceType) { 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 (referenceType != RefType.Element) { + QName refType; + if (referenceType == RefType.Type) { + refType = element.getElement().getType(); + if (refName == null) + refName = element.getElement().getName(); + } else { + refType = element.getElement().getRef(); + if (refName == null) + refName = refType.getLocalPart(); + } - if (refComplexType != null) { - refClassName = getName(refComplexType); + 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 (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);"); + + // 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)) { + + // 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(" {"); + // fw.delayedWriter.println(" XMLResource XML = XMLResource.getInstance(graph);"); + // fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(), XML.hasOriginalElementList);"); + // 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(), XML.hasOriginalElementList,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(" }"); + // } + + // fw.delayedWriter.println(" return true;"); + // fw.delayedWriter.println(" }"); } else { - refClassName = getName(refElement); + 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 + "));"); + } // // element type specific list // fw.delayedWriter.println(" {"); // fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List);"); @@ -306,7 +389,7 @@ public class ExporterGenerator extends JavaGenerator{ // fw.delayedWriter.println(" }"); // fw.delayedWriter.println(" }"); // } -// if (useOriginalList(parent, indicator,element, reference, ref, refType)) { +// if (useOriginalList(parent, indicator,element, reference, ref, new QName(obj.getName()))) { // // generic list // fw.delayedWriter.println(" {"); // fw.delayedWriter.println(" XMLResource XML = XMLResource.getInstance(graph);"); @@ -322,65 +405,10 @@ public class ExporterGenerator extends JavaGenerator{ // // fw.delayedWriter.println(" return true;"); // fw.delayedWriter.println(" }"); - } else { - SchemaObject refElement = elementName.get(refType.getLocalPart()); - //writer.println(commentTag+ontShort+"."+parent+".has"+ref + " parents, ParserElement element) throws DatabaseException {"); // if (inhertiance.type == InheritanceType.ComplexType) { @@ -720,21 +746,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 (inhertiance.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()); } 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 cff6e92..25fc020 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,6 +12,7 @@ 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.configuration.AttributeComposition; import org.simantics.xml.sax.configuration.Configuration; import org.simantics.xml.sax.configuration.IDProvider; @@ -236,49 +237,117 @@ 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, boolean reference, String ref, QName refType) { + protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType referenceType) { 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 (referenceType != RefType.Element) { + QName refType; + if (referenceType == RefType.Type) { + refType = element.getElement().getType(); + if (refName == null) + refName = element.getElement().getName(); + } else { + refType = element.getElement().getRef(); + if (refName == null) + refName = refType.getLocalPart(); + } - if (refComplexType != null) { - refClassName = getName(refComplexType); + 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 (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);"); + + fw.delayedWriter.println(" if (child.getElementParser() instanceof "+refClassName+"){"); + fw.delayedWriter.println(" graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+refName + ", child.getData());"); + if (useElementList(parent, indicator,element, referenceType == RefType.Reference, refName, refType)) { + + // element type specific list + fw.delayedWriter.println(" {"); + fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(),"+ontShort+getName(parent)+"_has"+refName + "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"+refName + "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 (useOriginalList(parent, indicator,element, referenceType == RefType.Reference, refName, refType)) { + // generic list + fw.delayedWriter.println(" {"); + fw.delayedWriter.println(" XMLResource XML = XMLResource.getInstance(graph);"); + fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(), XML.hasOriginalElementList);"); + 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(), XML.hasOriginalElementList,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(" }"); + } + + fw.delayedWriter.println(" return true;"); + fw.delayedWriter.println(" }"); } else { - refClassName = getName(refElement); + //writer.println(commentTag+ontShort+"."+parent+".has"+ref + " elements) { @@ -106,10 +106,12 @@ public abstract class JavaGenerator extends SchemaConversionBase{ if (localElement.getName() != null) { QName refType = localElement.getType(); if (refType != null) - handleIndicator(parent, indicator, e, false, name, refType); + //handleIndicator(parent, indicator, e, false, name, refType); + handleIndicator(parent, indicator, e, name, RefType.Type); } else if (localElement.getRef() != null) { - QName refType = localElement.getRef(); - handleIndicator(parent, indicator, e, true, name, refType); + //QName refType = localElement.getRef(); + //handleIndicator(parent, indicator, e, true, name, refType); + handleIndicator(parent, indicator, e, name, RefType.Reference); } } } 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 2871562..8e5c3c1 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 @@ -251,38 +251,57 @@ public class OntologyGenerator extends SchemaConversionBase { @Override - protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String refName, QName refType) { - String type = getL0TypeFromPrimitiveType(refType); - SchemaObject obj = null; - if (type == null) { - obj = getWithName(parent, refType.getLocalPart()); + //protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String refName, QName refType) { + protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType) { + if (refType != refType.Element) { + QName referenceType = null; + if (refType == RefType.Type) { + referenceType = element.getElement().getType(); + //refName = element.getElement().getName() + SchemaObject eObj = elements.get(element.getElement()); + if (refName == null) + refName = eObj.getName(); + } else { + referenceType = element.getElement().getRef(); + if (refName == null) + refName = referenceType.getLocalPart(); + } + String type = getL0TypeFromPrimitiveType(referenceType); + SchemaObject obj = null; + if (type == null) { + obj = getWithName(parent, referenceType.getLocalPart()); + + writer.println(ontRoot+getName(parent)+".has"+refName + " " + ontRoot+getName(obj)); + } else { + writer.println(ontRoot+getName(parent)+".has"+refName + " " + getType(referenceType)); + } - writer.println(ontRoot+getName(parent)+".has"+refName + " " + ontRoot+getName(obj)); + if (useElementList(parent, indicator,element, refType == RefType.Reference, refName, referenceType)) { + + if (type == null) { + writer.println(ontRoot+getName(parent)+"."+refName + "List " + getType(refType)); - } - - if (useElementList(parent, indicator,element, reference, refName, refType)) { + Element attrs = element.getElement(); + SchemaObject obj = getWithObj(parent, attrs); + if (refName == null) + refName = obj.getName(); - if (type == null) { + writer.println(ontRoot+getName(parent)+".has"+refName + " " + ontRoot+getName(obj)); + if (useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) { writer.println(ontRoot+getName(parent)+"."+refName + "List " + ontRoot+getName(obj)); - if (useElementList(parent, indicator,element, reference, refName, new QName(obj.getName()))) { - writer.println(ontRoot+getName(parent)+"."+refName + "List elements); @@ -607,17 +608,20 @@ public abstract class SchemaConversionBase { Element localElement = element.getElement(); if (localElement.getName() != null) { 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(); + //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(eObj); - handleIndicator(parent, indicator, element, false, refName, localElement); + //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); } } -- 2.47.1