From 90d821c64af7690fe7e108c3f5b89da8fa5395fc Mon Sep 17 00:00:00 2001 From: luukkainen Date: Fri, 27 Jan 2017 11:41:08 +0000 Subject: [PATCH] Refactoring Java generator code refs #6985 git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@33438 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../xml/sax/base/AbstractExporter.java | 9 +- .../simantics/xml/sax/ExporterGenerator.java | 254 ++++++++---------- .../simantics/xml/sax/ImporterGenerator.java | 227 +++++++--------- .../org/simantics/xml/sax/JavaGenerator.java | 57 +++- 4 files changed, 265 insertions(+), 282 deletions(-) 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 3464bda..23831a2 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 @@ -20,6 +20,9 @@ import org.simantics.message.MessageService; public class AbstractExporter { + private static String ENCODING = "UTF-8"; + private static String XML_VERSION = "1.0"; + private Session session; private File file; private ILogger logger; @@ -59,13 +62,13 @@ public class AbstractExporter { private static XMLStreamWriter createIdentXMLStreamWriter(OutputStream textWriter) throws XMLStreamException { XMLOutputFactory output = XMLOutputFactory.newInstance(); - return new IndentingXMLStreamWriter(output.createXMLStreamWriter(textWriter, "UTF-8")); + return new IndentingXMLStreamWriter(output.createXMLStreamWriter(textWriter, ENCODING)); //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"); + return output.createXMLStreamWriter(textWriter, ENCODING); } @@ -80,7 +83,7 @@ public class AbstractExporter { writer =createIdentXMLStreamWriter(os); else writer =createXMLStreamWriter(os); - writer.writeStartDocument("UTF-8", "1.1"); + writer.writeStartDocument(ENCODING, XML_VERSION); xmlWriter.write(root, writer); 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 c5c3f5e..74538ea 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 @@ -276,152 +276,47 @@ public class ExporterGenerator extends JavaGenerator{ provider = null; } - - @Override - protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType referenceType) { + protected void createReferenceIndicator(SchemaObject parent, RefType referenceType, String refName, String objectName, + String primaryClassName, String secondaryClassName, boolean useElementList, boolean useOriginalList) { FileWriter fw = getWriter(parent); - String objectName; - if (referenceType != RefType.Element) { - QName refType; - if (referenceType == RefType.Type) { - refType = element.getElement().getType(); - if (refName == null) - refName = element.getElement().getName(); - objectName = element.getElement().getName(); - } else { - refType = element.getElement().getRef(); - if (refName == null) - refName = refType.getLocalPart(); - objectName = refType.getLocalPart(); - } - - String binding = getBindingFromPrimitiveType(refType); - if (binding == null) { - SchemaObject refElement = elementName.get(refType.getLocalPart()); - SchemaObject refComplexType = complexTypeName.get(refType.getLocalPart()); - - // prefer element reference over complex type reference - String primaryClassName = null; - String secondaryClassName = null; - if (refElement != null) - primaryClassName = getName(refElement); - else - primaryClassName = getName(refComplexType); - - if (refComplexType != null) { - secondaryClassName = getName(refComplexType); - } else { - secondaryClassName = getName(refElement); - } - - if (referenceType == RefType.Type) { - // create internal class for handling the element and child attachment - secondaryClassName = getName(parent) +"_" +objectName; - fw.writer.println(" addRelationWriter("+ontShort+ getName(parent)+"_has"+refName +", "+secondaryClassName+".class);"); - fw.delayedWriter2.println(" public static class " + secondaryClassName +" extends " + primaryClassName +"{"); - fw.delayedWriter2.println(" public "+ secondaryClassName +"(ReadGraph graph){"); - fw.delayedWriter2.println(" super(graph);"); - fw.delayedWriter2.println(" }"); - fw.delayedWriter2.println(" "); - fw.delayedWriter2.println(" @Override"); - fw.delayedWriter2.println(" public java.lang.String getElementId() {"); - fw.delayedWriter2.println(" return \"" + refName +"\";"); - fw.delayedWriter2.println(" }"); - fw.delayedWriter2.println(" }"); - } else { //referenceType == RefType.Reference - fw.writer.println(" addWriter("+primaryClassName+".class);"); - if (!primaryClassName.equals(secondaryClassName)) - fw.writer.println(" addWriter("+secondaryClassName+".class);"); - } - // fw.delayedWriter.println(" if (child.getElementParser() instanceof "+refClassName+"){"); - // fw.delayedWriter.println(" graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+ref + ", child.getData());"); - if (useElementList(parent, indicator,element, referenceType == RefType.Reference, refName, refType)) { - - // 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 { - 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);"); -// fw.delayedWriter.println(" if (list == null) {"); -// fw.delayedWriter.println(" list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));"); -// fw.delayedWriter.println(" graph.claim(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List,list);"); -// fw.delayedWriter.println(" } else {"); -// fw.delayedWriter.println(" org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));"); -// fw.delayedWriter.println(" }"); -// fw.delayedWriter.println(" }"); -// } -// if (useOriginalList(parent, indicator,element, reference, ref, new QName(obj.getName()))) { + if (useElementList) { + + // element type specific list + fw.delayedWriter.println(" {"); + fw.delayedWriter.println(" org.simantics.db.Resource list = graph.getPossibleObject(instance.getResource(),"+ontShort+getName(parent)+"_has"+refName + "List);"); + fw.delayedWriter.println(" if (list != null) {"); + fw.delayedWriter.println(" java.util.List l = org.simantics.db.common.utils.ListUtils.toList(graph, list);"); + fw.delayedWriter.println(" result.addAll(l);"); + fw.delayedWriter.println(" } else {"); + fw.delayedWriter.println(" result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));"); + fw.delayedWriter.println(" }"); + fw.delayedWriter.println(" }"); + } else { + fw.delayedWriter.println(" result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));"); + } +// if (useOriginalList(parent, indicator,element, reference, ref, refType)) { // // generic list // fw.delayedWriter.println(" {"); // fw.delayedWriter.println(" XMLResource XML = XMLResource.getInstance(graph);"); @@ -434,13 +329,82 @@ public class ExporterGenerator extends JavaGenerator{ // fw.delayedWriter.println(" }"); // fw.delayedWriter.println(" }"); // } -// + // fw.delayedWriter.println(" return true;"); // fw.delayedWriter.println(" }"); - } + } + @Override + protected void createPrimitiveIndicator(SchemaObject parent, String refName, String binding) { + FileWriter fw = getWriter(parent); + //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);"); +// fw.delayedWriter.println(" if (list == null) {"); +// fw.delayedWriter.println(" list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));"); +// fw.delayedWriter.println(" graph.claim(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List,list);"); +// fw.delayedWriter.println(" } else {"); +// fw.delayedWriter.println(" org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));"); +// fw.delayedWriter.println(" }"); +// fw.delayedWriter.println(" }"); +// } +// if (useOriginalList(parent, indicator,element, reference, ref, new QName(obj.getName()))) { +// // 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(" }"); + + } @Override protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any) { 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 55ff5ef..80359ba 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 @@ -238,143 +238,104 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType referenceType) { + protected void createReferenceIndicator(SchemaObject parent, RefType referenceType, String refName, String objectName, String primaryClassName, String secondaryClassName, boolean useElementList, boolean useOriginalList) { FileWriter fw = getWriter(parent); - String objectName; - if (referenceType != RefType.Element) { - QName refType; - if (referenceType == RefType.Type) { - refType = element.getElement().getType(); - if (refName == null) - refName = element.getElement().getName(); - objectName = element.getElement().getName(); - } else { - refType = element.getElement().getRef(); - if (refName == null) - refName = refType.getLocalPart(); - objectName = refType.getLocalPart(); - } - - String binding = getBindingFromPrimitiveType(refType); - if (binding == null) { - SchemaObject refElement = elementName.get(refType.getLocalPart()); - SchemaObject refComplexType = complexTypeName.get(refType.getLocalPart()); + if (referenceType == RefType.Type) { + // create internal class for handling the element and child attachment + secondaryClassName = getName(parent) +"_" +objectName; + fw.writer.println(" addParser(\""+ objectName +"\", "+secondaryClassName+".class);"); + fw.delayedWriter2.println(" public static class " + secondaryClassName +" extends " + primaryClassName +"{"); + fw.delayedWriter2.println(" public "+ secondaryClassName +"(){"); + fw.delayedWriter2.println(" }"); + fw.delayedWriter2.println(" }"); + } else { // referenceType == RefType.Reference + fw.writer.println(" addParser("+primaryClassName+".class);"); + if (!primaryClassName.equals(secondaryClassName)) + fw.writer.println(" addParser("+secondaryClassName+".class);"); + } + + fw.delayedWriter.println(" if (child.getElementParser() instanceof "+secondaryClassName+"){"); + fw.delayedWriter.println(" graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+refName + ", child.getData());"); + if (useElementList) { + + // 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) { + // 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(" }"); + } - // prefer element reference over complex type reference - String primaryClassName = null; - String secondaryClassName = null; - if (refElement != null) - primaryClassName = getName(refElement); - else - primaryClassName = getName(refComplexType); - - if (refComplexType != null) { - secondaryClassName = getName(refComplexType); - } else { - secondaryClassName = getName(refElement); - } - - if (referenceType == RefType.Type) { - // create internal class for handling the element and child attachment - secondaryClassName = getName(parent) +"_" +objectName; - fw.writer.println(" addParser(\""+ objectName +"\", "+secondaryClassName+".class);"); - fw.delayedWriter2.println(" public static class " + secondaryClassName +" extends " + primaryClassName +"{"); - fw.delayedWriter2.println(" public "+ secondaryClassName +"(){"); - fw.delayedWriter2.println(" }"); - fw.delayedWriter2.println(" }"); - } else { // referenceType == RefType.Reference - fw.writer.println(" addParser("+primaryClassName+".class);"); - if (!primaryClassName.equals(secondaryClassName)) - fw.writer.println(" addParser("+secondaryClassName+".class);"); - } - - fw.delayedWriter.println(" if (child.getElementParser() instanceof "+secondaryClassName+"){"); - fw.delayedWriter.println(" graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+refName + ", child.getData());"); - if (useElementList(parent, indicator,element, referenceType == RefType.Reference, refName, refType)) { - - // 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 { - //writer.println(commentTag+ontShort+"."+parent+".has"+ref + "