]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
XML Elements are exported in the original order (if order is stored during import)
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 25 Jan 2017 12:01:32 +0000 (12:01 +0000)
committerMarko Luukkainen <marko.luukkainen@vtt.fi>
Thu, 2 Feb 2017 12:01:35 +0000 (14:01 +0200)
refs #6985

git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@33435 ac1ea38d-2e2b-0410-8846-a27921b304fc

14 files changed:
org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractExporter.java
org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/IndentingXMLStreamWriter.java [new file with mode: 0644]
org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ValueElementWriter.java
org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementNamedChildWriterBase.java
org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementWriter.java
org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLWriter.java
org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Date.java
org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/DateTime.java
org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Time.java
org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java
org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java
org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java
org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java
org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java

index 14a3fca9f327c5dfd6e80f3e26e5ede095d7fba7..3464bda5c7865f11a32c7ae9a8f6716377258648 100644 (file)
@@ -1,8 +1,10 @@
 package org.simantics.xml.sax.base;\r
 \r
+import java.io.BufferedOutputStream;\r
 import java.io.File;\r
 import java.io.FileOutputStream;\r
 import java.io.IOException;\r
+import java.io.OutputStream;\r
 \r
 import javax.xml.stream.XMLOutputFactory;\r
 import javax.xml.stream.XMLStreamException;\r
@@ -23,6 +25,7 @@ public class AbstractExporter {
        private ILogger logger;\r
        private Resource root;\r
        private XMLWriter xmlWriter;\r
+       private boolean indentOutput = true;\r
        \r
        public AbstractExporter(Session session, File file , Resource root, XMLWriter xmlWriter)  {\r
                this.session = session;\r
@@ -41,6 +44,10 @@ public class AbstractExporter {
                this.xmlWriter = xmlWriter;\r
        }\r
        \r
+       public void setIndentOutput(boolean indentOutput) {\r
+               this.indentOutput = indentOutput;\r
+       }\r
+       \r
        public void doExport() throws DatabaseException {\r
                doExport(MessageService.getDefault());  \r
        }\r
@@ -50,24 +57,47 @@ public class AbstractExporter {
                session.syncRequest(req);\r
        }\r
        \r
+       private static XMLStreamWriter createIdentXMLStreamWriter(OutputStream textWriter) throws XMLStreamException {\r
+               XMLOutputFactory output = XMLOutputFactory.newInstance();\r
+               return new IndentingXMLStreamWriter(output.createXMLStreamWriter(textWriter, "UTF-8"));\r
+               //return new com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter(output.createXMLStreamWriter(textWriter, "UTF-8"));\r
+       }\r
+       \r
+       private static XMLStreamWriter createXMLStreamWriter(OutputStream textWriter) throws XMLStreamException {\r
+               XMLOutputFactory output = XMLOutputFactory.newInstance();\r
+               return output.createXMLStreamWriter(textWriter, "UTF-8");\r
+       }\r
+       \r
        \r
        private class ExportRequest extends ReadRequest {\r
                @Override\r
                public void run(ReadGraph graph) throws DatabaseException {\r
                        XMLStreamWriter writer = null;\r
+                       OutputStream os = null;\r
                        try {\r
-                               XMLOutputFactory output = XMLOutputFactory.newInstance();\r
-                               writer = output.createXMLStreamWriter(new FileOutputStream(file),"UTF-8");\r
-                               writer.writeStartDocument("UTF-8", "1.0");\r
+                               os = new BufferedOutputStream(new FileOutputStream(file));\r
+                               if (indentOutput)\r
+                                       writer =createIdentXMLStreamWriter(os);\r
+                               else\r
+                                       writer =createXMLStreamWriter(os);\r
+                               writer.writeStartDocument("UTF-8", "1.1");\r
+                               \r
                                xmlWriter.write(root, writer);\r
                                \r
                                writer.writeEndDocument();\r
+                               writer.flush();\r
                                writer.close();\r
+                               os.close();\r
                        } catch (IOException|XMLStreamException e) {\r
                                try {\r
                                        writer.close();\r
                                } catch (XMLStreamException err) {\r
                                        \r
+                               }\r
+                               try {\r
+                                        os.close();\r
+                               }catch (IOException err) {\r
+                                       \r
                                }\r
                                throw new DatabaseException(e);\r
                        } \r
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 (file)
index 0000000..a912564
--- /dev/null
@@ -0,0 +1,208 @@
+package org.simantics.xml.sax.base;\r
+\r
+import javax.xml.namespace.NamespaceContext;\r
+import javax.xml.stream.XMLStreamException;\r
+import javax.xml.stream.XMLStreamWriter;\r
+\r
+/**\r
+ * Rather dummy indenting XML Stream Writer.\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public class IndentingXMLStreamWriter implements XMLStreamWriter{\r
+       \r
+       XMLStreamWriter delegate;\r
+       int depth = 0;\r
+       \r
+       public IndentingXMLStreamWriter(XMLStreamWriter delegate) {\r
+               this.delegate = delegate;\r
+       }\r
+       \r
+       @Override\r
+       public void close() throws XMLStreamException {\r
+               delegate.close();\r
+       }\r
+       \r
+       @Override\r
+       public void flush() throws XMLStreamException {\r
+               delegate.close();\r
+       }\r
+       \r
+       @Override\r
+       public NamespaceContext getNamespaceContext() {\r
+               return delegate.getNamespaceContext();\r
+       }\r
+       \r
+       @Override\r
+       public String getPrefix(String uri) throws XMLStreamException {\r
+               return delegate.getPrefix(uri);\r
+       }\r
+       \r
+       @Override\r
+       public Object getProperty(String name) throws IllegalArgumentException {\r
+               return delegate.getProperty(name);\r
+       }\r
+       \r
+       @Override\r
+       public void setDefaultNamespace(String uri) throws XMLStreamException {\r
+               delegate.setDefaultNamespace(uri);\r
+       }\r
+       \r
+       @Override\r
+       public void setPrefix(String prefix, String uri) throws XMLStreamException {\r
+               delegate.setPrefix(prefix, uri);\r
+       }\r
+       \r
+       private String getIdent() {\r
+               String s = "";\r
+               for (int i = 0; i < depth; i++)\r
+                       s += "  ";\r
+               return s;\r
+       }\r
+\r
+       @Override\r
+       public void writeStartElement(String localName) throws XMLStreamException {\r
+               delegate.writeCharacters("\n"+getIdent());\r
+               delegate.writeStartElement(localName);\r
+               depth++;\r
+       }\r
+\r
+       @Override\r
+       public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {\r
+               delegate.writeCharacters("\n"+getIdent());\r
+               delegate.writeStartElement(namespaceURI, localName);\r
+               depth++;\r
+       }\r
+\r
+       @Override\r
+       public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {\r
+               delegate.writeCharacters("\n"+getIdent());\r
+               delegate.writeStartElement(prefix, localName, namespaceURI);\r
+               depth++;\r
+       }\r
+\r
+       @Override\r
+       public void writeStartDocument() throws XMLStreamException {\r
+               delegate.writeStartDocument();\r
+               depth++;\r
+       }\r
+\r
+       @Override\r
+       public void writeStartDocument(String version) throws XMLStreamException {\r
+               delegate.writeStartDocument(version);\r
+               depth++;\r
+       }\r
+\r
+       @Override\r
+       public void writeStartDocument(String encoding, String version) throws XMLStreamException {\r
+               delegate.writeStartDocument(encoding, version);\r
+               depth++;\r
+       }\r
+\r
+       @Override\r
+       public void writeEndElement() throws XMLStreamException {\r
+               depth--;\r
+               delegate.writeCharacters("\n"+getIdent());\r
+               delegate.writeEndElement();\r
+       }\r
+       \r
+       @Override\r
+       public void writeEndDocument() throws XMLStreamException {\r
+               depth--;\r
+               delegate.writeCharacters("\n"+getIdent());\r
+               delegate.writeEndDocument();\r
+       }\r
+       \r
+       @Override\r
+       public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {\r
+               delegate.writeEmptyElement(namespaceURI, localName);\r
+       }\r
+\r
+       @Override\r
+       public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {\r
+               delegate.writeEmptyElement(prefix, localName, namespaceURI);\r
+       }\r
+\r
+       @Override\r
+       public void writeEmptyElement(String localName) throws XMLStreamException {\r
+               delegate.writeEmptyElement(localName);\r
+       }\r
+\r
+       @Override\r
+       public void writeAttribute(String localName, String value) throws XMLStreamException {\r
+               delegate.writeAttribute( localName, value);\r
+               \r
+       }\r
+\r
+       @Override\r
+       public void writeAttribute(String prefix, String namespaceURI, String localName, String value)\r
+                       throws XMLStreamException {\r
+               delegate.writeAttribute(prefix, namespaceURI, localName, value);\r
+       }\r
+\r
+       @Override\r
+       public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException {\r
+               delegate.writeAttribute(namespaceURI, localName, value);\r
+       }\r
+\r
+       @Override\r
+       public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException {\r
+               delegate.writeNamespace(prefix, namespaceURI);  \r
+       }\r
+\r
+       @Override\r
+       public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException {\r
+               delegate.writeDefaultNamespace(namespaceURI);\r
+       }\r
+\r
+       @Override\r
+       public void writeComment(String data) throws XMLStreamException {\r
+               delegate.writeComment(data);\r
+               \r
+       }\r
+\r
+       @Override\r
+       public void writeProcessingInstruction(String target) throws XMLStreamException {\r
+               delegate.writeProcessingInstruction(target);\r
+       }\r
+\r
+       @Override\r
+       public void writeProcessingInstruction(String target, String data) throws XMLStreamException {\r
+               delegate.writeProcessingInstruction(target, data);\r
+       }\r
+\r
+       @Override\r
+       public void writeCData(String data) throws XMLStreamException {\r
+               delegate.writeCData(data);\r
+       }\r
+\r
+       @Override\r
+       public void writeDTD(String dtd) throws XMLStreamException {\r
+               delegate.writeDTD(dtd); \r
+       }\r
+\r
+       @Override\r
+       public void writeEntityRef(String name) throws XMLStreamException {\r
+               delegate.writeEntityRef(name);\r
+       }\r
+\r
+       \r
+\r
+       @Override\r
+       public void writeCharacters(String text) throws XMLStreamException {\r
+               delegate.writeCharacters(text);\r
+       }\r
+\r
+       @Override\r
+       public void writeCharacters(char[] text, int start, int len) throws XMLStreamException {\r
+               delegate.writeCharacters(text, start, len);\r
+       }\r
+\r
+       @Override\r
+       public void setNamespaceContext(NamespaceContext context) throws XMLStreamException {\r
+               delegate.setNamespaceContext(context);\r
+       }\r
+       \r
+\r
+}\r
index bcc7c959b4d2ba78b653f2420ad2bc2cf485ceac..42aa3c16a3af32f4178dec1dfebdf561b0de372e 100644 (file)
@@ -3,6 +3,7 @@ package org.simantics.xml.sax.base;
 import java.util.Collection;\r
 import java.util.Collections;\r
 import java.util.List;\r
+import java.util.Set;\r
 \r
 import javax.xml.stream.XMLStreamException;\r
 import javax.xml.stream.XMLStreamWriter;\r
@@ -43,9 +44,9 @@ public class ValueElementWriter implements XMLElementWriter{
        }\r
        \r
        @Override\r
-       public List<Resource> children(ReadGraph graph, WriterElement instance, XMLStreamWriter writer)\r
+       public void children(ReadGraph graph, WriterElement instance, Set<Resource> sorted)\r
                        throws XMLStreamException, DatabaseException {\r
-               return Collections.EMPTY_LIST;\r
+               \r
        }\r
        \r
        @Override\r
index 69b6c8df0c7fe22e6352f1ab80b6bdeb62531e62..9c4ceb6b9a26a4963b143d31d6e4655762948ab2 100644 (file)
@@ -1,9 +1,7 @@
 package org.simantics.xml.sax.base;\r
 \r
-import java.util.Collections;\r
 import java.util.HashMap;\r
 import java.util.HashSet;\r
-import java.util.List;\r
 import java.util.Map;\r
 import java.util.Set;\r
 \r
@@ -13,7 +11,6 @@ import javax.xml.stream.XMLStreamWriter;
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.scl.compiler.elaboration.errors.ElabNode;\r
 \r
 public abstract class XMLElementNamedChildWriterBase implements XMLElementNamedChildWriter{\r
 \r
@@ -52,9 +49,9 @@ public abstract class XMLElementNamedChildWriterBase implements XMLElementNamedC
        }\r
        \r
        @Override\r
-       public List<Resource> children(ReadGraph graph, WriterElement instance, XMLStreamWriter writer)\r
+       public void children(ReadGraph graph, WriterElement instance, Set<Resource> sorted)\r
                        throws XMLStreamException, DatabaseException {\r
-               return Collections.EMPTY_LIST;\r
+       \r
        }\r
        \r
        @Override\r
index cde7809cb1637c980ceb6a52a4623685b72f2078..b11c24ce8e49fc6bddaf4a75af1f7d8f5a19e32a 100644 (file)
@@ -1,7 +1,7 @@
 package org.simantics.xml.sax.base;\r
 \r
 import java.util.Collection;\r
-import java.util.List;\r
+import java.util.Set;\r
 \r
 import javax.xml.stream.XMLStreamException;\r
 import javax.xml.stream.XMLStreamWriter;\r
@@ -19,7 +19,7 @@ public interface XMLElementWriter {
        public void start(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException;\r
        public void attributes(ReadGraph graph, WriterElement instance, Collection<Statement> attributes, XMLStreamWriter writer) throws XMLStreamException, DatabaseException;\r
        public void characters(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException;\r
-       public List<Resource> children(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException;\r
+       public void children(ReadGraph graph, WriterElement instance, Set<Resource> writer) throws XMLStreamException, DatabaseException;\r
        public void end(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException;\r
        \r
 }\r
index 3fd881ba3c3d86da100a06c87190c32a06f54512..0881a5d109d3b3b56ba7e287e594da67c282078e 100644 (file)
@@ -4,6 +4,8 @@ import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;\r
 import java.util.Collection;\r
 import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.LinkedHashSet;\r
 import java.util.List;\r
 import java.util.Map;\r
 import java.util.Set;\r
@@ -73,6 +75,11 @@ public class XMLWriter {
 \r
                XMLElementWriter elementWriter = instance.writer;\r
                elementWriter.start(graph, instance, writer);\r
+               if (instance.parent == null) {\r
+                       if(getSchemaURI() != null) {\r
+                               writer.writeDefaultNamespace(getSchemaURI());\r
+                       }\r
+               }\r
                elementWriter.attributes(graph, instance, graph.getStatements(instance.instance, XML.hasAttribute), writer);\r
                if (graph.hasValue(instance.instance))\r
                        elementWriter.characters(graph, instance, writer);\r
@@ -85,15 +92,24 @@ public class XMLWriter {
                        loadElement(c);\r
                        elementMap.put(s.getObject(), c);\r
                }\r
-               List<Resource> sorted = elementWriter.children(graph, instance, writer);\r
-                       \r
+               LinkedHashSet<Resource> sorted = new LinkedHashSet<>();\r
+               if (graph.hasStatement(instance.instance, XML.hasOriginalElementList)) {\r
+                       Resource originalList = graph.getSingleObject(instance.instance, XML.hasOriginalElementList);\r
+                       List<Resource> l = ListUtils.toList(graph, originalList);\r
+                       sorted.addAll(l);\r
+               }\r
+               elementWriter.children(graph, instance, sorted);\r
+               Set<Resource> processed = new HashSet<>();\r
                for (Resource r : sorted) {\r
+                       if (processed.contains(r)) // badly generated writer could report elements several times. \r
+                               continue;\r
                        WriterElement child = elementMap.get(r);\r
                        write(child, writer);\r
+                       processed.add(r);\r
                }\r
                //write the rest of the elements (in random order) \r
                for (Statement stm : childElements) {\r
-                       if (sorted.contains(stm.getObject()))\r
+                       if (processed.contains(stm.getObject()))\r
                                continue;\r
                        WriterElement child = elementMap.get(stm.getObject());\r
                        write(child, writer);\r
@@ -103,53 +119,6 @@ public class XMLWriter {
                \r
        }\r
        \r
-//     public void write(Resource root, XMLStreamWriter writer) throws DatabaseException, XMLStreamException {\r
-//             XMLResource XML = XMLResource.getInstance(graph);\r
-//             \r
-//             Deque<Element> stack = new ArrayDeque<>();\r
-//             Element rootElement = new Element(root);\r
-//             loadElement(rootElement);\r
-//             stack.push(rootElement);\r
-//             while (!stack.isEmpty()) {\r
-//                     \r
-//                     Element instance = stack.pop();\r
-//                     XMLElementWriter elementWriter = instance.writer;\r
-//                     elementWriter.start(graph, instance.instance, writer);\r
-//                     elementWriter.attributes(graph, graph.getStatements(instance.instance, XML.hasAttribute), writer);\r
-//                     // get all child elements\r
-//                     Collection<Statement> childElements = graph.getStatements(instance.instance, XML.hasElement);\r
-//                     // get original element order, if available\r
-//                     Resource originalElementList = graph.getPossibleObject(instance.instance, XML.hasOriginalElementList);\r
-//                     List<Resource> originalList = null;\r
-//                     if (originalElementList != null) {\r
-//                             originalList = ListUtils.toList(graph, originalElementList);\r
-//                     }\r
-//                     if (originalList != null) {\r
-//                             // check for elements that are missing form the original list (should be empty).  \r
-//                             for (Statement stm : childElements) {\r
-//                                     if (originalList.contains(stm.getObject()))\r
-//                                             continue;\r
-//                                     Element child = new Element(instance,stm.getObject());\r
-//                                     loadElement(child);\r
-//                                     stack.push(child);\r
-//                             }\r
-//                             // process the list in reverse order so that the stack processing processes the items in correct order.\r
-//                             for (int i = originalList.size()-1; i >= 0; i--) {\r
-//                                     Resource r = originalList.get(i);\r
-//                                     Element child = new Element(instance, r);\r
-//                                     loadElement(child);\r
-//                                     stack.push(child);\r
-//                             }\r
-//                     } else {\r
-//                             for (Statement stm : childElements) {\r
-//                                     Element child = new Element(instance, stm.getObject());\r
-//                                     loadElement(child);\r
-//                                     stack.push(child);\r
-//                             }\r
-//                     }\r
-//             }\r
-//     }\r
-       \r
        private void loadElement(WriterElement child) throws DatabaseException {\r
                XMLElementWriter childWriter = null;\r
                if (child.parent != null && child.parent.writer instanceof XMLElementNamedChildWriter) {\r
index d551d117133b47f583d2437e21fe04784fba02fe..e3e1bb4caa653073fb1206327436d6869a8ebfd8 100644 (file)
@@ -101,7 +101,7 @@ public class Date extends Bean implements org.simantics.xml.sax.base.datatypes.a
        \r
        @Override\r
        public String toString() {\r
-               return year + "-" + month + "-"+day;\r
+               return year + "-" +(month<10?"0":"")+ month + "-"+(day<10?"0":"")+day;\r
        }\r
        \r
        \r
index a7c8ad64c807289ba0bcc05877ab304e07e88262..ab664d8bd8b478f4449f22f1dc1e2706e7f6f47f 100644 (file)
@@ -133,7 +133,7 @@ public class DateTime extends Bean implements org.simantics.xml.sax.base.datatyp
        \r
        @Override\r
        public String toString() {\r
-               return year + "-" + month + "-"+day + "T"+hours+ ":" + minutes + ":"+seconds;\r
+               return year + "-" +(month<10?"0":"")+ month + "-"+(day<10?"0":"")+day + "T"+(hours<10?"0":"")+hours+ ":" + (minutes<10?"0":"")+ minutes + ":"+(seconds<10?"0":"")+seconds;\r
        }\r
 \r
 }\r
index 333f4a3de1a5a782fdfdee99ca0af9192690d604..fa6456772edc53a183b17593055371259377ad0e 100644 (file)
@@ -107,7 +107,7 @@ public class Time extends Bean implements org.simantics.xml.sax.base.datatypes.a
        \r
        @Override\r
        public String toString() {\r
-               return hours+ ":" + minutes + ":"+seconds;\r
+               return (hours<10?"0":"")+hours+ ":" + (minutes<10?"0":"")+ minutes + ":"+(seconds<10?"0":"")+seconds;\r
        }\r
 \r
 }\r
index 1d97259528bef8569311d85c27508f3e9b0f912a..c767311b66be7a42a951e00d28fff9d4fee0c7aa 100644 (file)
@@ -11,11 +11,11 @@ import java.util.Map;
 import javax.xml.namespace.QName;\r
 \r
 import org.simantics.utils.datastructures.BijectionMap;\r
+import org.simantics.xml.sax.SchemaConversionBase.RefType;\r
 import org.simantics.xml.sax.configuration.AttributeComposition;\r
 import org.simantics.xml.sax.configuration.Configuration;\r
 import org.simantics.xml.sax.configuration.IDProvider;\r
 import org.simantics.xml.sax.configuration.IDReference;\r
-import org.simantics.xml.sax.configuration.Rename;\r
 import org.simantics.xml.sax.configuration.UnrecognizedChildElement;\r
 import org.w3._2001.xmlschema.Annotated;\r
 import org.w3._2001.xmlschema.Attribute;\r
@@ -213,18 +213,16 @@ public class ExporterGenerator extends JavaGenerator{
                \r
                fw.writer.println("   }");\r
                \r
-//             fw.writer.println("   @Override");\r
-//             fw.writer.println("   public boolean connectChild(WriteGraph graph, ParserElement element, ParserElement child) throws DatabaseException{");\r
-//             fw.writer.println("      "+getOntologyImport());\r
-//             \r
-//             if (stringWriter.getBuffer().length() > 0) {\r
-//                     fw.writer.write(stringWriter.toString());\r
-//             }\r
-//             \r
-//             \r
-//             fw.writer.println("      return false;");\r
-//             fw.writer.println("   }");\r
-//             fw.writer.println();\r
+               fw.writer.println("   @Override");\r
+               fw.writer.println("   public void children(ReadGraph graph, WriterElement instance, java.util.Set<org.simantics.db.Resource> result) throws XMLStreamException, DatabaseException {");\r
+               fw.writer.println("      "+getOntologyImport());\r
+               \r
+               if (stringWriter.getBuffer().length() > 0) {\r
+                       fw.writer.write(stringWriter.toString());\r
+               }\r
+               \r
+               fw.writer.println("   }");\r
+               fw.writer.println();\r
                \r
                if (stringWriter2.getBuffer().length() > 0) {\r
                        fw.writer.write(stringWriter2.toString());\r
@@ -264,37 +262,122 @@ public class ExporterGenerator extends JavaGenerator{
        \r
        \r
        @Override\r
-       protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element,  boolean reference, String ref, QName refType) {\r
+       //protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element,  boolean reference, String ref, QName refType) {\r
+       protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType referenceType) {\r
                FileWriter fw = getWriter(parent);\r
-               String binding = getBindingFromPrimitiveType(refType);\r
-               if (binding == null) {\r
-                       SchemaObject refElement = elementName.get(refType.getLocalPart());\r
-                       SchemaObject refComplexType = complexTypeName.get(refType.getLocalPart());\r
-\r
-                       String className = null;\r
-                       String refClassName = null;\r
-                       if (refElement != null)\r
-                               className = getName(refElement);\r
-                       else\r
-                               className = getName(refComplexType);\r
+               if (referenceType != RefType.Element) {\r
+                       QName refType;\r
+                       if (referenceType == RefType.Type) {\r
+                               refType = element.getElement().getType();\r
+                               if (refName == null)\r
+                                       refName = element.getElement().getName();\r
+                       } else {\r
+                               refType = element.getElement().getRef();\r
+                               if (refName == null)\r
+                                       refName = refType.getLocalPart();\r
+                       }\r
                        \r
-                       if (refComplexType != null) {\r
-                               refClassName = getName(refComplexType);\r
+                       String binding = getBindingFromPrimitiveType(refType);\r
+                       if (binding == null) {\r
+                               SchemaObject refElement = elementName.get(refType.getLocalPart());\r
+                               SchemaObject refComplexType = complexTypeName.get(refType.getLocalPart());\r
+       \r
+                               String className = null;\r
+                               String refClassName = null;\r
+                               if (refElement != null)\r
+                                       className = getName(refElement);\r
+                               else\r
+                                       className = getName(refComplexType);\r
+                               \r
+                               if (refComplexType != null) {\r
+                                       refClassName = getName(refComplexType);\r
+                               } else {\r
+                                       refClassName = getName(refElement);\r
+                               }\r
+                               \r
+                               if (referenceType != RefType.Reference)\r
+                               fw.writer.println("        addTypeWriter("+ontShort+ className +", "+className+".class);");\r
+                               else\r
+                               fw.writer.println("        addWriter("+className+".class);");\r
+                               if (!className.equals(refClassName))\r
+                               fw.writer.println("        addWriter("+refClassName+".class);");\r
+                               \r
+       //                      fw.delayedWriter.println("         if (child.getElementParser() instanceof "+refClassName+"){");\r
+       //                      fw.delayedWriter.println("            graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+ref + ", child.getData());");\r
+                               if (useElementList(parent, indicator,element, referenceType == RefType.Reference, refName, refType)) {\r
+               \r
+                               // element type specific list\r
+                               fw.delayedWriter.println("            {");\r
+                               fw.delayedWriter.println("               org.simantics.db.Resource list = graph.getPossibleObject(instance.getResource(),"+ontShort+getName(parent)+"_has"+refName + "List);");\r
+                               fw.delayedWriter.println("               if (list != null) {");\r
+                               fw.delayedWriter.println("                  java.util.List<org.simantics.db.Resource> l = org.simantics.db.common.utils.ListUtils.toList(graph, list);");\r
+                               fw.delayedWriter.println("                  result.addAll(l);");\r
+                               fw.delayedWriter.println("               } else {");\r
+                               fw.delayedWriter.println("                  result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));");\r
+                               fw.delayedWriter.println("               }");\r
+                               fw.delayedWriter.println("            }");\r
+                               } else {\r
+                               fw.delayedWriter.println("         result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));");\r
+                               }\r
+       //                      if (useOriginalList(parent, indicator,element, reference, ref, refType)) {\r
+       //                      // generic list\r
+       //                      fw.delayedWriter.println("            {");\r
+       //                      fw.delayedWriter.println("               XMLResource XML = XMLResource.getInstance(graph);");\r
+       //                      fw.delayedWriter.println("               Resource list = graph.getPossibleObject(element.getData(), XML.hasOriginalElementList);");\r
+       //                      fw.delayedWriter.println("               if (list == null) {");\r
+       //                      fw.delayedWriter.println("                  list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));");\r
+       //                      fw.delayedWriter.println("                  graph.claim(element.getData(), XML.hasOriginalElementList,list);");\r
+       //                      fw.delayedWriter.println("               } else {");\r
+       //                      fw.delayedWriter.println("                  org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));");\r
+       //                      fw.delayedWriter.println("               }");\r
+       //                      fw.delayedWriter.println("            }");\r
+       //                      }\r
+                               \r
+       //                      fw.delayedWriter.println("            return true;");\r
+       //                      fw.delayedWriter.println("         }");\r
                        } else {\r
-                               refClassName = getName(refElement);\r
+                               SchemaObject refElement = elementName.get(refType.getLocalPart());\r
+                               //writer.println(commentTag+ontShort+"."+parent+".has"+ref + " <R " +  ontShort+".hasElement");\r
+                               fw.writer.println("        addRelationWriter("+ ontShort +getName(parent) + "_has"+refName+", "+getName(parent) +"_" +refName+".class);");\r
+                               \r
+                               fw.delayedWriter2.println("   public static class " + getName(parent) +"_" +refName+" extends org.simantics.xml.sax.base.ValueElementWriter {");\r
+                               fw.delayedWriter2.println("      public "+ getName(parent) +"_" +refName +"(){");\r
+                               //fw.delayedWriter2.println("           super(\""+ref+"\"," +this.ontologyClassName+".URIs."+getName(parent) + "_has"+ref+", "+binding+");");\r
+                               fw.delayedWriter2.println("           super(\""+refName+"\",null, "+binding+");");\r
+                               fw.delayedWriter2.println("       }");\r
+                               fw.delayedWriter2.println("   }");\r
                        }\r
+               } else {\r
+                       Element attrs= element.getElement();\r
+                       SchemaObject obj = getWithObj(parent, attrs);\r
                        \r
-                       if (!reference)\r
-                       fw.writer.println("        addTypeWriter("+ontShort+ className +", "+className+".class);");\r
-                       else\r
-                       fw.writer.println("        addWriter("+className+".class);");\r
-                       if (!className.equals(refClassName))\r
-                       fw.writer.println("        addWriter("+refClassName+".class);");\r
+                       String className = getName(obj);\r
+                       if (refName == null) {\r
+                               refName = obj.getName();//attrs.getName();\r
+                       }\r
+\r
+                       //if (!reference)\r
+                       fw.writer.println("        addTypeWriter("+ ontShort+getName(obj) +", "+className+".class);");\r
+//                     else\r
+//                     fw.writer.println("        addWriter("+className+".class);");\r
                        \r
-//                     fw.delayedWriter.println("         if (child.getElementParser() instanceof "+refClassName+"){");\r
+//                     fw.delayedWriter.println("         if (child.getElementParser() instanceof "+className+"){");\r
 //                     fw.delayedWriter.println("            graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+ref + ", child.getData());");\r
-//                     if (useElementList(parent, indicator,element, reference, ref, refType)) {\r
-//     \r
+                       if (useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) {\r
+                               \r
+                       // element type specific list\r
+                       fw.delayedWriter.println("            {");\r
+                       fw.delayedWriter.println("               org.simantics.db.Resource list = graph.getPossibleObject(instance.getResource(),"+ontShort+getName(parent)+"_has"+refName + "List);");\r
+                       fw.delayedWriter.println("               if (list != null) {");\r
+                       fw.delayedWriter.println("                  java.util.List<org.simantics.db.Resource> l = org.simantics.db.common.utils.ListUtils.toList(graph, list);");\r
+                       fw.delayedWriter.println("                  result.addAll(l);");\r
+                       fw.delayedWriter.println("               } else {");\r
+                       fw.delayedWriter.println("                  result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));");\r
+                       fw.delayedWriter.println("               }");\r
+                       fw.delayedWriter.println("            }");\r
+                       } else {\r
+                       fw.delayedWriter.println("         result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));");\r
+                       }\r
 //                     // element type specific list\r
 //                     fw.delayedWriter.println("            {");\r
 //                     fw.delayedWriter.println("               Resource list = graph.getPossibleObject(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List);");\r
@@ -306,7 +389,7 @@ public class ExporterGenerator extends JavaGenerator{
 //                     fw.delayedWriter.println("               }");\r
 //                     fw.delayedWriter.println("            }");\r
 //                     }\r
-//                     if (useOriginalList(parent, indicator,element, reference, ref, refType)) {\r
+//                     if (useOriginalList(parent, indicator,element, reference, ref, new QName(obj.getName()))) {\r
 //                     // generic list\r
 //                     fw.delayedWriter.println("            {");\r
 //                     fw.delayedWriter.println("               XMLResource XML = XMLResource.getInstance(graph);");\r
@@ -322,65 +405,10 @@ public class ExporterGenerator extends JavaGenerator{
 //                     \r
 //                     fw.delayedWriter.println("            return true;");\r
 //                     fw.delayedWriter.println("         }");\r
-               } else {\r
-                       SchemaObject refElement = elementName.get(refType.getLocalPart());\r
-                       //writer.println(commentTag+ontShort+"."+parent+".has"+ref + " <R " +  ontShort+".hasElement");\r
-                       fw.writer.println("        addRelationWriter("+ ontShort +getName(parent) + "_has"+ref+", "+getName(parent) +"_" +ref+".class);");\r
-                       \r
-                       fw.delayedWriter2.println("   public static class " + getName(parent) +"_" +ref+" extends org.simantics.xml.sax.base.ValueElementWriter {");\r
-                       fw.delayedWriter2.println("      public "+ getName(parent) +"_" +ref +"(){");\r
-                       //fw.delayedWriter2.println("           super(\""+ref+"\"," +this.ontologyClassName+".URIs."+getName(parent) + "_has"+ref+", "+binding+");");\r
-                       fw.delayedWriter2.println("           super(\""+ref+"\",null, "+binding+");");\r
-                       fw.delayedWriter2.println("       }");\r
-                       fw.delayedWriter2.println("   }");\r
                }\r
        }\r
        \r
-       @Override\r
-       protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element,  boolean reference, String ref, OpenAttrs attrs) {\r
-               FileWriter fw = getWriter(parent);\r
-               SchemaObject obj = getWithObj(parent, attrs);\r
        \r
-               String className = getName(obj);\r
-\r
-               if (!reference)\r
-               fw.writer.println("        addTypeWriter("+ ontShort+getName(obj) +", "+className+".class);");\r
-               else\r
-               fw.writer.println("        addWriter("+className+".class);");\r
-               \r
-               fw.delayedWriter.println("         if (child.getElementParser() instanceof "+className+"){");\r
-               fw.delayedWriter.println("            graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+ref + ", child.getData());");\r
-//             if (useElementList(parent, indicator,element, reference, ref, new QName(obj.getName()))) {\r
-//\r
-//             // element type specific list\r
-//             fw.delayedWriter.println("            {");\r
-//             fw.delayedWriter.println("               Resource list = graph.getPossibleObject(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List);");\r
-//             fw.delayedWriter.println("               if (list == null) {");\r
-//             fw.delayedWriter.println("                  list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));");\r
-//             fw.delayedWriter.println("                  graph.claim(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List,list);");\r
-//             fw.delayedWriter.println("               } else {");\r
-//             fw.delayedWriter.println("                  org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));");\r
-//             fw.delayedWriter.println("               }");\r
-//             fw.delayedWriter.println("            }");\r
-//             }\r
-//             if (useOriginalList(parent, indicator,element, reference, ref, new QName(obj.getName()))) {\r
-//             // generic list\r
-//             fw.delayedWriter.println("            {");\r
-//             fw.delayedWriter.println("               XMLResource XML = XMLResource.getInstance(graph);");\r
-//             fw.delayedWriter.println("               Resource list = graph.getPossibleObject(element.getData(), XML.hasOriginalElementList);");\r
-//             fw.delayedWriter.println("               if (list == null) {");\r
-//             fw.delayedWriter.println("                  list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));");\r
-//             fw.delayedWriter.println("                  graph.claim(element.getData(), XML.hasOriginalElementList,list);");\r
-//             fw.delayedWriter.println("               } else {");\r
-//             fw.delayedWriter.println("                  org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));");\r
-//             fw.delayedWriter.println("               }");\r
-//             fw.delayedWriter.println("            }");\r
-//             }\r
-//             \r
-//             fw.delayedWriter.println("            return true;");\r
-//             fw.delayedWriter.println("         }");\r
-               \r
-       }\r
        \r
        @Override\r
        protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any) {               \r
@@ -396,10 +424,7 @@ public class ExporterGenerator extends JavaGenerator{
                fw.delayedWriter2.println("      return null;");\r
                fw.delayedWriter2.println("   }");\r
        }\r
-       \r
-       \r
-       \r
-       \r
+\r
        @Override       \r
        protected void handle(SchemaObject parent, Attribute attribute) {\r
                String name = attribute.getName();\r
@@ -669,6 +694,7 @@ public class ExporterGenerator extends JavaGenerator{
                fw.writer.println("        }");\r
                fw.writer.println("   }");\r
                \r
+               \r
 //             fw.writer.println("   @Override");\r
 //             fw.writer.println("   public void configure(WriteGraph graph, Deque<ParserElement> parents, ParserElement element) throws DatabaseException {");\r
 //             if (inhertiance.type == InheritanceType.ComplexType) {\r
@@ -720,21 +746,21 @@ public class ExporterGenerator extends JavaGenerator{
                \r
                fw.writer.println("   }");\r
                \r
-//             fw.writer.println("   @Override");\r
-//             fw.writer.println("   public boolean connectChild(WriteGraph graph, ParserElement element, ParserElement child) throws DatabaseException{");\r
-//             fw.writer.println("      "+getOntologyImport());\r
-//             \r
-//             if (stringWriter.getBuffer().length() > 0) {\r
-//                     fw.writer.write(stringWriter.toString());\r
-//             }\r
-//             if (inhertiance.type == InheritanceType.ComplexType) {\r
-//             fw.writer.println("      return super.connectChild(graph,element,child);");     \r
-//             } else {\r
-//             fw.writer.println("      return false;");\r
-//             }\r
-//             fw.writer.println("   }");\r
-//             fw.writer.println();\r
+               fw.writer.println("   @Override");\r
+               fw.writer.println("   public void children(ReadGraph graph, WriterElement instance, java.util.Set<org.simantics.db.Resource> result) throws XMLStreamException, DatabaseException {");\r
+               fw.writer.println("      "+getOntologyImport());\r
                \r
+               if (stringWriter.getBuffer().length() > 0) {\r
+                       fw.writer.write(stringWriter.toString());\r
+               }\r
+               \r
+               if (inhertiance.type == InheritanceType.ComplexType) {\r
+               fw.writer.println("      super.children(graph,instance,result);");\r
+               }\r
+               \r
+               fw.writer.println("   }");\r
+               fw.writer.println();\r
+                               \r
                if (stringWriter2.getBuffer().length() > 0) {\r
                        fw.writer.write(stringWriter2.toString());\r
                }\r
index cff6e923c9aa668dc6ae73e467366d9bd0238860..25fc020084cf89c66a2a6f05d55f7e04fbef5363 100644 (file)
@@ -12,6 +12,7 @@ import java.util.Map;
 import javax.xml.namespace.QName;\r
 \r
 import org.simantics.utils.datastructures.BijectionMap;\r
+import org.simantics.xml.sax.SchemaConversionBase.RefType;\r
 import org.simantics.xml.sax.configuration.AttributeComposition;\r
 import org.simantics.xml.sax.configuration.Configuration;\r
 import org.simantics.xml.sax.configuration.IDProvider;\r
@@ -236,49 +237,117 @@ public class ImporterGenerator extends JavaGenerator{
        }\r
        \r
        @Override\r
-       protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element,  boolean reference, String ref, QName refType) {\r
+       //protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element,  boolean reference, String ref, QName refType) {\r
+       protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType referenceType) {\r
                FileWriter fw = getWriter(parent);\r
-               String binding = getBindingFromPrimitiveType(refType);\r
-               if (binding == null) {\r
-                       SchemaObject refElement = elementName.get(refType.getLocalPart());\r
-                       SchemaObject refComplexType = complexTypeName.get(refType.getLocalPart());\r
-\r
-                       String className = null;\r
-                       String refClassName = null;\r
-                       if (refElement != null)\r
-                               className = getName(refElement);\r
-                       else\r
-                               className = getName(refComplexType);\r
+               if (referenceType != RefType.Element) {\r
+                       QName refType;\r
+                       if (referenceType == RefType.Type) {\r
+                               refType = element.getElement().getType();\r
+                               if (refName == null)\r
+                                       refName = element.getElement().getName();\r
+                       } else {\r
+                               refType = element.getElement().getRef();\r
+                               if (refName == null)\r
+                                       refName = refType.getLocalPart();\r
+                       }\r
                        \r
-                       if (refComplexType != null) {\r
-                               refClassName = getName(refComplexType);\r
+                       String binding = getBindingFromPrimitiveType(refType);\r
+                       if (binding == null) {\r
+                               SchemaObject refElement = elementName.get(refType.getLocalPart());\r
+                               SchemaObject refComplexType = complexTypeName.get(refType.getLocalPart());\r
+       \r
+                               String className = null;\r
+                               String refClassName = null;\r
+                               if (refElement != null)\r
+                                       className = getName(refElement);\r
+                               else\r
+                                       className = getName(refComplexType);\r
+                               \r
+                               if (refComplexType != null) {\r
+                                       refClassName = getName(refComplexType);\r
+                               } else {\r
+                                       refClassName = getName(refElement);\r
+                               }\r
+                               \r
+                               if (referenceType != RefType.Reference)\r
+                               fw.writer.println("        addParser(\""+ refName +"\", "+className+".class);");\r
+                               else\r
+                               fw.writer.println("        addParser("+className+".class);");\r
+                               if (!className.equals(refClassName))\r
+                               fw.writer.println("        addParser("+refClassName+".class);");\r
+                               \r
+                               fw.delayedWriter.println("         if (child.getElementParser() instanceof "+refClassName+"){");\r
+                               fw.delayedWriter.println("            graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+refName + ", child.getData());");\r
+                               if (useElementList(parent, indicator,element, referenceType == RefType.Reference, refName, refType)) {\r
+               \r
+                               // element type specific list\r
+                               fw.delayedWriter.println("            {");\r
+                               fw.delayedWriter.println("               Resource list = graph.getPossibleObject(element.getData(),"+ontShort+getName(parent)+"_has"+refName + "List);");\r
+                               fw.delayedWriter.println("               if (list == null) {");\r
+                               fw.delayedWriter.println("                  list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));");\r
+                               fw.delayedWriter.println("                  graph.claim(element.getData(),"+ontShort+getName(parent)+"_has"+refName + "List,list);");\r
+                               fw.delayedWriter.println("               } else {");\r
+                               fw.delayedWriter.println("                  org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));");\r
+                               fw.delayedWriter.println("               }");\r
+                               fw.delayedWriter.println("            }");\r
+                               }\r
+                               if (useOriginalList(parent, indicator,element, referenceType == RefType.Reference, refName, refType)) {\r
+                               // generic list\r
+                               fw.delayedWriter.println("            {");\r
+                               fw.delayedWriter.println("               XMLResource XML = XMLResource.getInstance(graph);");\r
+                               fw.delayedWriter.println("               Resource list = graph.getPossibleObject(element.getData(), XML.hasOriginalElementList);");\r
+                               fw.delayedWriter.println("               if (list == null) {");\r
+                               fw.delayedWriter.println("                  list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));");\r
+                               fw.delayedWriter.println("                  graph.claim(element.getData(), XML.hasOriginalElementList,list);");\r
+                               fw.delayedWriter.println("               } else {");\r
+                               fw.delayedWriter.println("                  org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));");\r
+                               fw.delayedWriter.println("               }");\r
+                               fw.delayedWriter.println("            }");\r
+                               }\r
+                               \r
+                               fw.delayedWriter.println("            return true;");\r
+                               fw.delayedWriter.println("         }");\r
                        } else {\r
-                               refClassName = getName(refElement);\r
+                               //writer.println(commentTag+ontShort+"."+parent+".has"+ref + " <R " +  ontShort+".hasElement");\r
+                               fw.writer.println("        addParser(\""+ refName +"\", "+getName(parent) +"_" +refName+".class);");\r
+                               \r
+                               fw.delayedWriter2.println("   public static class " + getName(parent) +"_" +refName+" extends org.simantics.xml.sax.base.ValueElementParser {");\r
+                               fw.delayedWriter2.println("      public "+ getName(parent) +"_" +refName +"(){");\r
+                               fw.delayedWriter2.println("           super(\""+refName+"\"," +this.ontologyClassName+".URIs."+getName(parent) + "_has"+refName+", "+binding+");");\r
+                               fw.delayedWriter2.println("       }");\r
+                               fw.delayedWriter2.println("   }");\r
                        }\r
+               } else {\r
+                       Element attrs= element.getElement();\r
+                       SchemaObject obj = getWithObj(parent, attrs);\r
                        \r
-                       if (!reference)\r
-                       fw.writer.println("        addParser(\""+ ref +"\", "+className+".class);");\r
-                       else\r
-                       fw.writer.println("        addParser("+className+".class);");\r
-                       if (!className.equals(refClassName))\r
-                       fw.writer.println("        addParser("+refClassName+".class);");\r
+                       String className = getName(obj);\r
+                       if (refName == null)\r
+                               refName = attrs.getName();\r
                        \r
-                       fw.delayedWriter.println("         if (child.getElementParser() instanceof "+refClassName+"){");\r
-                       fw.delayedWriter.println("            graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+ref + ", child.getData());");\r
-                       if (useElementList(parent, indicator,element, reference, ref, refType)) {\r
-       \r
+                       \r
+                       //if (!reference)\r
+                       fw.writer.println("        addParser(\""+ refName +"\", "+className+".class);");\r
+//                     else\r
+//                     fw.writer.println("        addParser("+className+".class);");\r
+                       \r
+                       fw.delayedWriter.println("         if (child.getElementParser() instanceof "+className+"){");\r
+                       fw.delayedWriter.println("            graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+refName + ", child.getData());");\r
+                       if (useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) {\r
+\r
                        // element type specific list\r
                        fw.delayedWriter.println("            {");\r
-                       fw.delayedWriter.println("               Resource list = graph.getPossibleObject(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List);");\r
+                       fw.delayedWriter.println("               Resource list = graph.getPossibleObject(element.getData(),"+ontShort+getName(parent)+"_has"+refName + "List);");\r
                        fw.delayedWriter.println("               if (list == null) {");\r
                        fw.delayedWriter.println("                  list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));");\r
-                       fw.delayedWriter.println("                  graph.claim(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List,list);");\r
+                       fw.delayedWriter.println("                  graph.claim(element.getData(),"+ontShort+getName(parent)+"_has"+refName + "List,list);");\r
                        fw.delayedWriter.println("               } else {");\r
                        fw.delayedWriter.println("                  org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));");\r
                        fw.delayedWriter.println("               }");\r
                        fw.delayedWriter.println("            }");\r
                        }\r
-                       if (useOriginalList(parent, indicator,element, reference, ref, refType)) {\r
+                       if (useOriginalList(parent, indicator,element, false, refName, new QName(obj.getName()))) {\r
                        // generic list\r
                        fw.delayedWriter.println("            {");\r
                        fw.delayedWriter.println("               XMLResource XML = XMLResource.getInstance(graph);");\r
@@ -294,62 +363,7 @@ public class ImporterGenerator extends JavaGenerator{
                        \r
                        fw.delayedWriter.println("            return true;");\r
                        fw.delayedWriter.println("         }");\r
-               } else {\r
-                       //writer.println(commentTag+ontShort+"."+parent+".has"+ref + " <R " +  ontShort+".hasElement");\r
-                       fw.writer.println("        addParser(\""+ ref +"\", "+getName(parent) +"_" +ref+".class);");\r
-                       \r
-                       fw.delayedWriter2.println("   public static class " + getName(parent) +"_" +ref+" extends org.simantics.xml.sax.base.ValueElementParser {");\r
-                       fw.delayedWriter2.println("      public "+ getName(parent) +"_" +ref +"(){");\r
-                       fw.delayedWriter2.println("           super(\""+ref+"\"," +this.ontologyClassName+".URIs."+getName(parent) + "_has"+ref+", "+binding+");");\r
-                       fw.delayedWriter2.println("       }");\r
-                       fw.delayedWriter2.println("   }");\r
-               }\r
-       }\r
-       \r
-       @Override\r
-       protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element,  boolean reference, String ref, OpenAttrs attrs) {\r
-               FileWriter fw = getWriter(parent);\r
-               SchemaObject obj = getWithObj(parent, attrs);\r
-       \r
-               String className = getName(obj);\r
-\r
-               if (!reference)\r
-               fw.writer.println("        addParser(\""+ ref +"\", "+className+".class);");\r
-               else\r
-               fw.writer.println("        addParser("+className+".class);");\r
-               \r
-               fw.delayedWriter.println("         if (child.getElementParser() instanceof "+className+"){");\r
-               fw.delayedWriter.println("            graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+ref + ", child.getData());");\r
-               if (useElementList(parent, indicator,element, reference, ref, new QName(obj.getName()))) {\r
-\r
-               // element type specific list\r
-               fw.delayedWriter.println("            {");\r
-               fw.delayedWriter.println("               Resource list = graph.getPossibleObject(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List);");\r
-               fw.delayedWriter.println("               if (list == null) {");\r
-               fw.delayedWriter.println("                  list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));");\r
-               fw.delayedWriter.println("                  graph.claim(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List,list);");\r
-               fw.delayedWriter.println("               } else {");\r
-               fw.delayedWriter.println("                  org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));");\r
-               fw.delayedWriter.println("               }");\r
-               fw.delayedWriter.println("            }");\r
-               }\r
-               if (useOriginalList(parent, indicator,element, reference, ref, new QName(obj.getName()))) {\r
-               // generic list\r
-               fw.delayedWriter.println("            {");\r
-               fw.delayedWriter.println("               XMLResource XML = XMLResource.getInstance(graph);");\r
-               fw.delayedWriter.println("               Resource list = graph.getPossibleObject(element.getData(), XML.hasOriginalElementList);");\r
-               fw.delayedWriter.println("               if (list == null) {");\r
-               fw.delayedWriter.println("                  list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));");\r
-               fw.delayedWriter.println("                  graph.claim(element.getData(), XML.hasOriginalElementList,list);");\r
-               fw.delayedWriter.println("               } else {");\r
-               fw.delayedWriter.println("                  org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));");\r
-               fw.delayedWriter.println("               }");\r
-               fw.delayedWriter.println("            }");\r
                }\r
-               \r
-               fw.delayedWriter.println("            return true;");\r
-               fw.delayedWriter.println("         }");\r
-               \r
        }\r
        \r
        @Override\r
index 646773472100b2467267be71a2d2ea3ae47d540a..b6b4fedbe0218e435fc157e180d4c7a90fbff6cf 100644 (file)
@@ -73,24 +73,24 @@ public abstract class JavaGenerator extends SchemaConversionBase{
                return "AttributeGroups_";\r
        }\r
        \r
-       @Override\r
-       protected void handle(SchemaObject parent, SchemaElement indicator, SchemaElement element) {\r
-               Element localElement = element.getElement();\r
-               if (localElement.getName() != null) {\r
-                       SchemaObject eObj = elements.get(localElement);\r
-                       String refName = localElement.getName();\r
-                       QName refType = localElement.getType();\r
-                       if (refType != null)\r
-                               handleIndicator(parent, indicator, element, false, refName, refType);\r
-                       else {\r
-                               handleElement(eObj);\r
-                               handleIndicator(parent, indicator, element, false, refName, localElement);\r
-                       }\r
-               } else if (localElement.getRef() != null) {\r
-                       QName refType = localElement.getRef();\r
-                       handleIndicator(parent, indicator,element, true, refType.getLocalPart(), refType);\r
-               }\r
-       }\r
+//     @Override\r
+//     protected void handle(SchemaObject parent, SchemaElement indicator, SchemaElement element) {\r
+//             Element localElement = element.getElement();\r
+//             if (localElement.getName() != null) {\r
+//                     SchemaObject eObj = elements.get(localElement);\r
+//                     String refName = localElement.getName();\r
+//                     QName refType = localElement.getType();\r
+//                     if (refType != null)\r
+//                             handleIndicator(parent, indicator, element, false, refName, refType);\r
+//                     else {\r
+//                             handleElement(eObj);\r
+//                             handleIndicator(parent, indicator, element, false, refName, localElement);\r
+//                     }\r
+//             } else if (localElement.getRef() != null) {\r
+//                     QName refType = localElement.getRef();\r
+//                     handleIndicator(parent, indicator,element, true, refType.getLocalPart(), refType);\r
+//             }\r
+//     }\r
        \r
        @Override\r
        protected void handle(SchemaObject parent, SchemaElement indicator, List<SchemaElement> elements) {\r
@@ -106,10 +106,12 @@ public abstract class JavaGenerator extends SchemaConversionBase{
                                if (localElement.getName() != null) {\r
                                        QName refType = localElement.getType();\r
                                        if (refType != null)\r
-                                               handleIndicator(parent, indicator, e, false, name, refType);\r
+                                               //handleIndicator(parent, indicator, e, false, name, refType);\r
+                                               handleIndicator(parent, indicator, e, name, RefType.Type);\r
                                } else if (localElement.getRef() != null) {\r
-                                       QName refType = localElement.getRef();\r
-                                       handleIndicator(parent, indicator, e, true, name, refType);\r
+                                       //QName refType = localElement.getRef();\r
+                                       //handleIndicator(parent, indicator, e, true, name, refType);\r
+                                       handleIndicator(parent, indicator, e, name, RefType.Reference);\r
                                }\r
                        }\r
                }\r
index 287156299dfe0213ee43f5f858ec44afd29ad04a..8e5c3c1fdeb7dfb85a684f855927c4d568410768 100644 (file)
@@ -251,38 +251,57 @@ public class OntologyGenerator extends SchemaConversionBase {
        \r
        \r
        @Override\r
-       protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String refName, QName refType) {\r
-               String type = getL0TypeFromPrimitiveType(refType);\r
-               SchemaObject obj = null;\r
-               if (type == null) {\r
-                       obj = getWithName(parent, refType.getLocalPart());\r
+       //protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String refName, QName refType) {\r
+       protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType) {\r
+               if (refType != refType.Element) {\r
+                       QName referenceType = null;\r
+                       if (refType == RefType.Type) {\r
+                               referenceType = element.getElement().getType();\r
+                               //refName = element.getElement().getName()\r
+                               SchemaObject eObj = elements.get(element.getElement());\r
+                               if (refName == null)\r
+                                       refName = eObj.getName();\r
+                       } else {\r
+                               referenceType = element.getElement().getRef();\r
+                               if (refName == null)\r
+                                       refName = referenceType.getLocalPart();\r
+                       }\r
+                       String type = getL0TypeFromPrimitiveType(referenceType);\r
+                       SchemaObject obj = null;\r
+                       if (type == null) {\r
+                               obj = getWithName(parent, referenceType.getLocalPart());\r
+                               \r
+                               writer.println(ontRoot+getName(parent)+".has"+refName + " <R " +  ontRoot+ getName(obj,"has"));\r
+                               writer.println("   --> " + ontRoot+getName(obj));\r
+                       } else {\r
+                               writer.println(ontRoot+getName(parent)+".has"+refName + " <R XML.hasElement");\r
+                               writer.println("   --> " + getType(referenceType));\r
+                       }\r
                        \r
-                       writer.println(ontRoot+getName(parent)+".has"+refName + " <R " +  ontRoot+ getName(obj,"has"));\r
-                       writer.println("   --> " + ontRoot+getName(obj));\r
+                       if (useElementList(parent, indicator,element, refType == RefType.Reference, refName, referenceType)) {\r
+                               \r
+                               if (type == null) {\r
+                                       writer.println(ontRoot+getName(parent)+"."+refName + "List <T XML.ElementList");\r
+                                       writer.println(ontRoot+getName(parent)+".has"+refName + "List <R " +  ontRoot+getName(obj,"has")+"List : L0.FunctionalRelation");\r
+                               } else {\r
+                                       writer.println(ontRoot+getName(parent)+"."+refName + "List <T XML.ElementList");\r
+                                       writer.println(ontRoot+getName(parent)+".has"+refName + "List <R XML.hasElementList : L0.FunctionalRelation");  \r
+                               }\r
+                       }\r
                } else {\r
-                       writer.println(ontRoot+getName(parent)+".has"+refName + " <R XML.hasElement");\r
-                       writer.println("   --> " + getType(refType));\r
-               }\r
-               \r
-               if (useElementList(parent, indicator,element, reference, refName, refType)) {\r
+                       Element attrs = element.getElement();\r
+                       SchemaObject obj = getWithObj(parent, attrs);\r
+                       if (refName == null)\r
+                               refName = obj.getName();\r
                        \r
-                       if (type == null) {\r
+                       writer.println(ontRoot+getName(parent)+".has"+refName + " <R " +  ontRoot+ getName(obj,"has"));\r
+                       writer.println("   --> " + ontRoot+getName(obj));\r
+                       if (useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) {\r
                                writer.println(ontRoot+getName(parent)+"."+refName + "List <T XML.ElementList");\r
                                writer.println(ontRoot+getName(parent)+".has"+refName + "List <R " +  ontRoot+getName(obj,"has")+"List : L0.FunctionalRelation");\r
-                       } else {\r
-                               writer.println(ontRoot+getName(parent)+"."+refName + "List <T XML.ElementList");\r
-                               writer.println(ontRoot+getName(parent)+".has"+refName + "List <R XML.hasElementList : L0.FunctionalRelation");  \r
                        }\r
                }\r
-       }\r
-       protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String refName, OpenAttrs attrs) {\r
-               SchemaObject obj = getWithObj(parent, attrs);\r
-               writer.println(ontRoot+getName(parent)+".has"+refName + " <R " +  ontRoot+ getName(obj,"has"));\r
-               writer.println("   --> " + ontRoot+getName(obj));\r
-               if (useElementList(parent, indicator,element, reference, refName, new QName(obj.getName()))) {\r
-                       writer.println(ontRoot+getName(parent)+"."+refName + "List <T XML.ElementList");\r
-                       writer.println(ontRoot+getName(parent)+".has"+refName + "List <R " +  ontRoot+getName(obj,"has")+"List : L0.FunctionalRelation");\r
-               }\r
+               \r
        }\r
        \r
        @Override\r
index 600d13fd7dc91e35989375d8f3df4afe86fb63bb..d80f94ce6b03950accf6c3673852826634182b75 100644 (file)
@@ -489,8 +489,9 @@ public abstract class SchemaConversionBase {
                handleExtensionAttributes(complexType);\r
        }\r
        \r
-       protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String refName, QName refType);\r
-       protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String refName, OpenAttrs ref);\r
+       protected enum RefType{Element,Reference,Type};\r
+       \r
+       protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType);\r
        protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any);\r
        protected abstract void handle(SchemaObject parent, SchemaElement indicator, List<SchemaElement> elements);\r
        \r
@@ -607,17 +608,20 @@ public abstract class SchemaConversionBase {
                Element localElement = element.getElement();\r
                if (localElement.getName() != null) {\r
                        SchemaObject eObj = elements.get(localElement); // FIXME: handleIndicator should be refactored, not this methdof must be overridden in JavaGenerator to handle renaming of Elements properly\r
-                       String refName = eObj.getName();//localElement.getName();\r
+                       //String refName = eObj.getName();//localElement.getName();\r
                        QName refType = localElement.getType();\r
                        if (refType != null)\r
-                               handleIndicator(parent, indicator, element, false, refName, refType);\r
+                               //handleIndicator(parent, indicator, element, false, refName, refType);\r
+                               handleIndicator(parent, indicator, element, null, RefType.Type);\r
                        else {\r
                                handleElement(eObj);\r
-                               handleIndicator(parent, indicator, element, false, refName, localElement);\r
+                               //handleIndicator(parent, indicator, element, false, refName, localElement);\r
+                               handleIndicator(parent, indicator, element, null, RefType.Element);\r
                        }\r
                } else if (localElement.getRef() != null) {\r
-                       QName refType = localElement.getRef();\r
-                       handleIndicator(parent, indicator,element, true, refType.getLocalPart(), refType);\r
+                       //QName refType = localElement.getRef();\r
+                       //handleIndicator(parent, indicator,element, true, refType.getLocalPart(), refType);\r
+                       handleIndicator(parent, indicator,element, null, RefType.Reference);\r
                }\r
        }\r
        \r