From 04f3fb6796cd6225be090e1e1feb866ae0d03173 Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Tue, 25 Apr 2017 23:28:26 +0300 Subject: [PATCH] Several updates to interoperability. Declaration of simpleTypes as literal types. Support for value elements with simple datatypes. Fixed Base64 support. Improved support for choice indicators. Fixes for schemas without target namespace. Generation of empty string literals for empty elements. Change-Id: I8ae3b4fa47690c7c58df5a44eaf3191cc13f1fa2 --- .../interop/stubs/InteropResource.java | 182 ++++++------- .../interop/test/TypeComparator.java | 8 + .../xml/sax/base/ValueElementWriter.java | 34 +-- .../org/simantics/xml/sax/base/XMLParser.java | 3 +- .../xml/sax/ontology/XMLResource.java | 248 +++++++++--------- .../simantics/xml/sax/ExporterGenerator.java | 18 +- .../simantics/xml/sax/ImporterGenerator.java | 57 ++-- .../org/simantics/xml/sax/JavaGenerator.java | 103 +++++--- .../simantics/xml/sax/OntologyGenerator.java | 173 +++++------- .../xml/sax/SchemaConversionBase.java | 64 +++-- .../xml/sax/SchemaConversionComponent.java | 5 +- .../simantics/xml/sax/SchemaConverter.java | 23 +- 12 files changed, 469 insertions(+), 449 deletions(-) diff --git a/org.simantics.interop/src/org/simantics/interop/stubs/InteropResource.java b/org.simantics.interop/src/org/simantics/interop/stubs/InteropResource.java index af29831..c8445be 100644 --- a/org.simantics.interop/src/org/simantics/interop/stubs/InteropResource.java +++ b/org.simantics.interop/src/org/simantics/interop/stubs/InteropResource.java @@ -1,91 +1,91 @@ -package org.simantics.interop.stubs; - -import org.simantics.db.RequestProcessor; -import org.simantics.db.Resource; -import org.simantics.db.ReadGraph; -import org.simantics.db.request.Read; -import org.simantics.db.Session; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.service.QueryControl; - -public class InteropResource { - - public final Resource HasDestination; - public final Resource HasGeneratorID; - public final Resource HasGeneratorID_Inverse; - public final Resource HasImportData; - public final Resource HasInputResource; - public final Resource HasSource; - public final Resource INodeType; - public final Resource ImportDataOf; - public final Resource ImportLibrary; - public final Resource InteropBrowseContext; - public final Resource InteropBrowseContext_InteropChildRule; - public final Resource InteropBrowseContext_InteropNode; - - public static class URIs { - public static final String HasDestination = "http://www.simantics.org/Interop-1.0/HasDestination"; - public static final String HasGeneratorID = "http://www.simantics.org/Interop-1.0/HasGeneratorID"; - public static final String HasGeneratorID_Inverse = "http://www.simantics.org/Interop-1.0/HasGeneratorID/Inverse"; - public static final String HasImportData = "http://www.simantics.org/Interop-1.0/HasImportData"; - public static final String HasInputResource = "http://www.simantics.org/Interop-1.0/HasInputResource"; - public static final String HasSource = "http://www.simantics.org/Interop-1.0/HasSource"; - public static final String INodeType = "http://www.simantics.org/Interop-1.0/INodeType"; - public static final String ImportDataOf = "http://www.simantics.org/Interop-1.0/ImportDataOf"; - public static final String ImportLibrary = "http://www.simantics.org/Interop-1.0/ImportLibrary"; - public static final String InteropBrowseContext = "http://www.simantics.org/Interop-1.0/InteropBrowseContext"; - public static final String InteropBrowseContext_InteropChildRule = "http://www.simantics.org/Interop-1.0/InteropBrowseContext/InteropChildRule"; - public static final String InteropBrowseContext_InteropNode = "http://www.simantics.org/Interop-1.0/InteropBrowseContext/InteropNode"; - } - - public static Resource getResourceOrNull(ReadGraph graph, String uri) { - try { - return graph.getResource(uri); - } catch(DatabaseException e) { - System.err.println(e.getMessage()); - return null; - } - } - - public InteropResource(ReadGraph graph) { - HasDestination = getResourceOrNull(graph, URIs.HasDestination); - HasGeneratorID = getResourceOrNull(graph, URIs.HasGeneratorID); - HasGeneratorID_Inverse = getResourceOrNull(graph, URIs.HasGeneratorID_Inverse); - HasImportData = getResourceOrNull(graph, URIs.HasImportData); - HasInputResource = getResourceOrNull(graph, URIs.HasInputResource); - HasSource = getResourceOrNull(graph, URIs.HasSource); - INodeType = getResourceOrNull(graph, URIs.INodeType); - ImportDataOf = getResourceOrNull(graph, URIs.ImportDataOf); - ImportLibrary = getResourceOrNull(graph, URIs.ImportLibrary); - InteropBrowseContext = getResourceOrNull(graph, URIs.InteropBrowseContext); - InteropBrowseContext_InteropChildRule = getResourceOrNull(graph, URIs.InteropBrowseContext_InteropChildRule); - InteropBrowseContext_InteropNode = getResourceOrNull(graph, URIs.InteropBrowseContext_InteropNode); - } - - public static InteropResource getInstance(ReadGraph graph) { - Session session = graph.getSession(); - InteropResource ret = session.peekService(InteropResource.class); - if(ret == null) { - QueryControl qc = graph.getService(QueryControl.class); - ret = new InteropResource(qc.getIndependentGraph(graph)); - session.registerService(InteropResource.class, ret); - } - return ret; - } - - public static InteropResource getInstance(RequestProcessor session) throws DatabaseException { - InteropResource ret = session.peekService(InteropResource.class); - if(ret == null) { - ret = session.syncRequest(new Read() { - public InteropResource perform(ReadGraph graph) throws DatabaseException { - QueryControl qc = graph.getService(QueryControl.class); - return new InteropResource(qc.getIndependentGraph(graph)); - } - }); - session.registerService(InteropResource.class, ret); - } - return ret; - } - -} - +package org.simantics.interop.stubs; + +import org.simantics.db.RequestProcessor; +import org.simantics.db.Resource; +import org.simantics.db.ReadGraph; +import org.simantics.db.request.Read; +import org.simantics.db.Session; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.service.QueryControl; + +public class InteropResource { + + public final Resource HasDestination; + public final Resource HasGeneratorID; + public final Resource HasGeneratorID_Inverse; + public final Resource HasImportData; + public final Resource HasInputResource; + public final Resource HasSource; + public final Resource INodeType; + public final Resource ImportDataOf; + public final Resource ImportLibrary; + public final Resource InteropBrowseContext; + public final Resource InteropBrowseContext_InteropChildRule; + public final Resource InteropBrowseContext_InteropNode; + + public static class URIs { + public static final String HasDestination = "http://www.simantics.org/Interop-1.0/HasDestination"; + public static final String HasGeneratorID = "http://www.simantics.org/Interop-1.0/HasGeneratorID"; + public static final String HasGeneratorID_Inverse = "http://www.simantics.org/Interop-1.0/HasGeneratorID/Inverse"; + public static final String HasImportData = "http://www.simantics.org/Interop-1.0/HasImportData"; + public static final String HasInputResource = "http://www.simantics.org/Interop-1.0/HasInputResource"; + public static final String HasSource = "http://www.simantics.org/Interop-1.0/HasSource"; + public static final String INodeType = "http://www.simantics.org/Interop-1.0/INodeType"; + public static final String ImportDataOf = "http://www.simantics.org/Interop-1.0/ImportDataOf"; + public static final String ImportLibrary = "http://www.simantics.org/Interop-1.0/ImportLibrary"; + public static final String InteropBrowseContext = "http://www.simantics.org/Interop-1.0/InteropBrowseContext"; + public static final String InteropBrowseContext_InteropChildRule = "http://www.simantics.org/Interop-1.0/InteropBrowseContext/InteropChildRule"; + public static final String InteropBrowseContext_InteropNode = "http://www.simantics.org/Interop-1.0/InteropBrowseContext/InteropNode"; + } + + public static Resource getResourceOrNull(ReadGraph graph, String uri) { + try { + return graph.getResource(uri); + } catch(DatabaseException e) { + System.err.println(e.getMessage()); + return null; + } + } + + public InteropResource(ReadGraph graph) { + HasDestination = getResourceOrNull(graph, URIs.HasDestination); + HasGeneratorID = getResourceOrNull(graph, URIs.HasGeneratorID); + HasGeneratorID_Inverse = getResourceOrNull(graph, URIs.HasGeneratorID_Inverse); + HasImportData = getResourceOrNull(graph, URIs.HasImportData); + HasInputResource = getResourceOrNull(graph, URIs.HasInputResource); + HasSource = getResourceOrNull(graph, URIs.HasSource); + INodeType = getResourceOrNull(graph, URIs.INodeType); + ImportDataOf = getResourceOrNull(graph, URIs.ImportDataOf); + ImportLibrary = getResourceOrNull(graph, URIs.ImportLibrary); + InteropBrowseContext = getResourceOrNull(graph, URIs.InteropBrowseContext); + InteropBrowseContext_InteropChildRule = getResourceOrNull(graph, URIs.InteropBrowseContext_InteropChildRule); + InteropBrowseContext_InteropNode = getResourceOrNull(graph, URIs.InteropBrowseContext_InteropNode); + } + + public static InteropResource getInstance(ReadGraph graph) { + Session session = graph.getSession(); + InteropResource ret = session.peekService(InteropResource.class); + if(ret == null) { + QueryControl qc = graph.getService(QueryControl.class); + ret = new InteropResource(qc.getIndependentGraph(graph)); + session.registerService(InteropResource.class, ret); + } + return ret; + } + + public static InteropResource getInstance(RequestProcessor session) throws DatabaseException { + InteropResource ret = session.peekService(InteropResource.class); + if(ret == null) { + ret = session.syncRequest(new Read() { + public InteropResource perform(ReadGraph graph) throws DatabaseException { + QueryControl qc = graph.getService(QueryControl.class); + return new InteropResource(qc.getIndependentGraph(graph)); + } + }); + session.registerService(InteropResource.class, ret); + } + return ret; + } + +} + diff --git a/org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java b/org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java index e3251ac..3bd21b5 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java +++ b/org.simantics.interop/src/org/simantics/interop/test/TypeComparator.java @@ -28,6 +28,14 @@ public class TypeComparator extends ResourceComparator { ArrayList rs1 = new ArrayList(); ArrayList rs2 = new ArrayList(); + @Override + public int compare(ReadGraph g, Resource o1, Resource o2, boolean local) throws DatabaseException { + if (local) + return new TypeComparator().compare(g, o1, o2); + else + return compare(g, o1, o2); + } + @Override public int compare(ReadGraph g, Resource o1, Resource o2) throws DatabaseException{ if (!compareType(g,o1, o2)) { diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ValueElementWriter.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ValueElementWriter.java index 42aa3c1..3035ae8 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ValueElementWriter.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ValueElementWriter.java @@ -1,8 +1,6 @@ package org.simantics.xml.sax.base; import java.util.Collection; -import java.util.Collections; -import java.util.List; import java.util.Set; import javax.xml.stream.XMLStreamException; @@ -13,9 +11,8 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Statement; import org.simantics.db.exception.DatabaseException; -import org.simantics.layer0.Layer0; -public class ValueElementWriter implements XMLElementWriter{ +public abstract class ValueElementWriter implements XMLElementWriter{ String name; Binding binding; @@ -61,36 +58,13 @@ public class ValueElementWriter implements XMLElementWriter{ } + public abstract String decode(Object value); + @Override public void characters(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); Object value = graph.getValue(instance.getResource(), binding); - writer.writeCharacters(value.toString()); -// if (binding == Bindings.STRING) { -// graph.claim(element.getData(), l0.InstanceOf, l0.String); -// graph.claimValue(element.getData(), string, binding); -// } else if (binding == Bindings.INTEGER) { -// graph.claim(element.getData(), l0.InstanceOf, l0.Integer); -// graph.claimValue(element.getData(), Integer.parseInt(string), binding); -// } else if (binding == Bindings.FLOAT) { -// graph.claim(element.getData(), l0.InstanceOf, l0.Float); -// graph.claimValue(element.getData(), Float.parseFloat(string), binding); -// } else if (binding == Bindings.DOUBLE) { -// graph.claim(element.getData(), l0.InstanceOf, l0.Double); -// graph.claimValue(element.getData(), Double.parseDouble(string), binding); -// } else if (binding == Bindings.BOOLEAN) { -// graph.claim(element.getData(), l0.InstanceOf, l0.Boolean); -// graph.claimValue(element.getData(), Boolean.parseBoolean(string), binding); -// } else if (binding == Bindings.LONG) { -// graph.claim(element.getData(), l0.InstanceOf, l0.Long); -// graph.claimValue(element.getData(), Long.parseLong(string), binding); -// } else if (binding == Bindings.BYTE) { -// graph.claim(element.getData(), l0.InstanceOf, l0.Byte); -// graph.claimValue(element.getData(), Byte.parseByte(string), binding); -// } else { -// throw new DatabaseException("Unknown datatype " + binding); -// } + writer.writeCharacters(decode(value)); } } diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLParser.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLParser.java index 01b8deb..9c04001 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLParser.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLParser.java @@ -247,8 +247,7 @@ public class XMLParser extends DefaultHandler implements Serializable { e = current.pop(); } if (e != null) { - if (charactersValue.length() > 0) - handleCharacters(e, charactersValue.toString()); + handleCharacters(e, charactersValue.toString()); handleElement(current,e); } if (current.isEmpty()) { diff --git a/org.simantics.xml.sax.ontology/src/org/simantics/xml/sax/ontology/XMLResource.java b/org.simantics.xml.sax.ontology/src/org/simantics/xml/sax/ontology/XMLResource.java index 01b5156..1bb9dbb 100644 --- a/org.simantics.xml.sax.ontology/src/org/simantics/xml/sax/ontology/XMLResource.java +++ b/org.simantics.xml.sax.ontology/src/org/simantics/xml/sax/ontology/XMLResource.java @@ -1,124 +1,124 @@ -package org.simantics.xml.sax.ontology; - -import org.simantics.db.RequestProcessor; -import org.simantics.db.Resource; -import org.simantics.db.ReadGraph; -import org.simantics.db.request.Read; -import org.simantics.db.Session; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.service.QueryControl; - -public class XMLResource { - - public final Resource AttributeGroup; - public final Resource ComplexType; - public final Resource Date; - public final Resource DateTime; - public final Resource Element; - public final Resource ElementList; - public final Resource Entity; - public final Resource Time; - public final Resource externalReferenceOf; - public final Resource hasAttribute; - public final Resource hasAttribute_Inverse; - public final Resource hasComplexType; - public final Resource hasComplexType_Inverse; - public final Resource hasElement; - public final Resource hasElementList; - public final Resource hasElementList_Inverse; - public final Resource hasElement_Inverse; - public final Resource hasExternalReference; - public final Resource hasID; - public final Resource hasID_Inverse; - public final Resource hasOriginalElementList; - public final Resource hasOriginalElementList_Inverse; - public final Resource hasReference; - - public static class URIs { - public static final String AttributeGroup = "http://www.simantics.org/XML-1.0/AttributeGroup"; - public static final String ComplexType = "http://www.simantics.org/XML-1.0/ComplexType"; - public static final String Date = "http://www.simantics.org/XML-1.0/Date"; - public static final String DateTime = "http://www.simantics.org/XML-1.0/DateTime"; - public static final String Element = "http://www.simantics.org/XML-1.0/Element"; - public static final String ElementList = "http://www.simantics.org/XML-1.0/ElementList"; - public static final String Entity = "http://www.simantics.org/XML-1.0/Entity"; - public static final String Time = "http://www.simantics.org/XML-1.0/Time"; - public static final String externalReferenceOf = "http://www.simantics.org/XML-1.0/externalReferenceOf"; - public static final String hasAttribute = "http://www.simantics.org/XML-1.0/hasAttribute"; - public static final String hasAttribute_Inverse = "http://www.simantics.org/XML-1.0/hasAttribute/Inverse"; - public static final String hasComplexType = "http://www.simantics.org/XML-1.0/hasComplexType"; - public static final String hasComplexType_Inverse = "http://www.simantics.org/XML-1.0/hasComplexType/Inverse"; - public static final String hasElement = "http://www.simantics.org/XML-1.0/hasElement"; - public static final String hasElementList = "http://www.simantics.org/XML-1.0/hasElementList"; - public static final String hasElementList_Inverse = "http://www.simantics.org/XML-1.0/hasElementList/Inverse"; - public static final String hasElement_Inverse = "http://www.simantics.org/XML-1.0/hasElement/Inverse"; - public static final String hasExternalReference = "http://www.simantics.org/XML-1.0/hasExternalReference"; - public static final String hasID = "http://www.simantics.org/XML-1.0/hasID"; - public static final String hasID_Inverse = "http://www.simantics.org/XML-1.0/hasID/Inverse"; - public static final String hasOriginalElementList = "http://www.simantics.org/XML-1.0/hasOriginalElementList"; - public static final String hasOriginalElementList_Inverse = "http://www.simantics.org/XML-1.0/hasOriginalElementList/Inverse"; - public static final String hasReference = "http://www.simantics.org/XML-1.0/hasReference"; - } - - public static Resource getResourceOrNull(ReadGraph graph, String uri) { - try { - return graph.getResource(uri); - } catch(DatabaseException e) { - System.err.println(e.getMessage()); - return null; - } - } - - public XMLResource(ReadGraph graph) { - AttributeGroup = getResourceOrNull(graph, URIs.AttributeGroup); - ComplexType = getResourceOrNull(graph, URIs.ComplexType); - Date = getResourceOrNull(graph, URIs.Date); - DateTime = getResourceOrNull(graph, URIs.DateTime); - Element = getResourceOrNull(graph, URIs.Element); - ElementList = getResourceOrNull(graph, URIs.ElementList); - Entity = getResourceOrNull(graph, URIs.Entity); - Time = getResourceOrNull(graph, URIs.Time); - externalReferenceOf = getResourceOrNull(graph, URIs.externalReferenceOf); - hasAttribute = getResourceOrNull(graph, URIs.hasAttribute); - hasAttribute_Inverse = getResourceOrNull(graph, URIs.hasAttribute_Inverse); - hasComplexType = getResourceOrNull(graph, URIs.hasComplexType); - hasComplexType_Inverse = getResourceOrNull(graph, URIs.hasComplexType_Inverse); - hasElement = getResourceOrNull(graph, URIs.hasElement); - hasElementList = getResourceOrNull(graph, URIs.hasElementList); - hasElementList_Inverse = getResourceOrNull(graph, URIs.hasElementList_Inverse); - hasElement_Inverse = getResourceOrNull(graph, URIs.hasElement_Inverse); - hasExternalReference = getResourceOrNull(graph, URIs.hasExternalReference); - hasID = getResourceOrNull(graph, URIs.hasID); - hasID_Inverse = getResourceOrNull(graph, URIs.hasID_Inverse); - hasOriginalElementList = getResourceOrNull(graph, URIs.hasOriginalElementList); - hasOriginalElementList_Inverse = getResourceOrNull(graph, URIs.hasOriginalElementList_Inverse); - hasReference = getResourceOrNull(graph, URIs.hasReference); - } - - public static XMLResource getInstance(ReadGraph graph) { - Session session = graph.getSession(); - XMLResource ret = session.peekService(XMLResource.class); - if(ret == null) { - QueryControl qc = graph.getService(QueryControl.class); - ret = new XMLResource(qc.getIndependentGraph(graph)); - session.registerService(XMLResource.class, ret); - } - return ret; - } - - public static XMLResource getInstance(RequestProcessor session) throws DatabaseException { - XMLResource ret = session.peekService(XMLResource.class); - if(ret == null) { - ret = session.syncRequest(new Read() { - public XMLResource perform(ReadGraph graph) throws DatabaseException { - QueryControl qc = graph.getService(QueryControl.class); - return new XMLResource(qc.getIndependentGraph(graph)); - } - }); - session.registerService(XMLResource.class, ret); - } - return ret; - } - -} - +package org.simantics.xml.sax.ontology; + +import org.simantics.db.RequestProcessor; +import org.simantics.db.Resource; +import org.simantics.db.ReadGraph; +import org.simantics.db.request.Read; +import org.simantics.db.Session; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.service.QueryControl; + +public class XMLResource { + + public final Resource AttributeGroup; + public final Resource ComplexType; + public final Resource Date; + public final Resource DateTime; + public final Resource Element; + public final Resource ElementList; + public final Resource Entity; + public final Resource Time; + public final Resource externalReferenceOf; + public final Resource hasAttribute; + public final Resource hasAttribute_Inverse; + public final Resource hasComplexType; + public final Resource hasComplexType_Inverse; + public final Resource hasElement; + public final Resource hasElementList; + public final Resource hasElementList_Inverse; + public final Resource hasElement_Inverse; + public final Resource hasExternalReference; + public final Resource hasID; + public final Resource hasID_Inverse; + public final Resource hasOriginalElementList; + public final Resource hasOriginalElementList_Inverse; + public final Resource hasReference; + + public static class URIs { + public static final String AttributeGroup = "http://www.simantics.org/XML-1.0/AttributeGroup"; + public static final String ComplexType = "http://www.simantics.org/XML-1.0/ComplexType"; + public static final String Date = "http://www.simantics.org/XML-1.0/Date"; + public static final String DateTime = "http://www.simantics.org/XML-1.0/DateTime"; + public static final String Element = "http://www.simantics.org/XML-1.0/Element"; + public static final String ElementList = "http://www.simantics.org/XML-1.0/ElementList"; + public static final String Entity = "http://www.simantics.org/XML-1.0/Entity"; + public static final String Time = "http://www.simantics.org/XML-1.0/Time"; + public static final String externalReferenceOf = "http://www.simantics.org/XML-1.0/externalReferenceOf"; + public static final String hasAttribute = "http://www.simantics.org/XML-1.0/hasAttribute"; + public static final String hasAttribute_Inverse = "http://www.simantics.org/XML-1.0/hasAttribute/Inverse"; + public static final String hasComplexType = "http://www.simantics.org/XML-1.0/hasComplexType"; + public static final String hasComplexType_Inverse = "http://www.simantics.org/XML-1.0/hasComplexType/Inverse"; + public static final String hasElement = "http://www.simantics.org/XML-1.0/hasElement"; + public static final String hasElementList = "http://www.simantics.org/XML-1.0/hasElementList"; + public static final String hasElementList_Inverse = "http://www.simantics.org/XML-1.0/hasElementList/Inverse"; + public static final String hasElement_Inverse = "http://www.simantics.org/XML-1.0/hasElement/Inverse"; + public static final String hasExternalReference = "http://www.simantics.org/XML-1.0/hasExternalReference"; + public static final String hasID = "http://www.simantics.org/XML-1.0/hasID"; + public static final String hasID_Inverse = "http://www.simantics.org/XML-1.0/hasID/Inverse"; + public static final String hasOriginalElementList = "http://www.simantics.org/XML-1.0/hasOriginalElementList"; + public static final String hasOriginalElementList_Inverse = "http://www.simantics.org/XML-1.0/hasOriginalElementList/Inverse"; + public static final String hasReference = "http://www.simantics.org/XML-1.0/hasReference"; + } + + public static Resource getResourceOrNull(ReadGraph graph, String uri) { + try { + return graph.getResource(uri); + } catch(DatabaseException e) { + System.err.println(e.getMessage()); + return null; + } + } + + public XMLResource(ReadGraph graph) { + AttributeGroup = getResourceOrNull(graph, URIs.AttributeGroup); + ComplexType = getResourceOrNull(graph, URIs.ComplexType); + Date = getResourceOrNull(graph, URIs.Date); + DateTime = getResourceOrNull(graph, URIs.DateTime); + Element = getResourceOrNull(graph, URIs.Element); + ElementList = getResourceOrNull(graph, URIs.ElementList); + Entity = getResourceOrNull(graph, URIs.Entity); + Time = getResourceOrNull(graph, URIs.Time); + externalReferenceOf = getResourceOrNull(graph, URIs.externalReferenceOf); + hasAttribute = getResourceOrNull(graph, URIs.hasAttribute); + hasAttribute_Inverse = getResourceOrNull(graph, URIs.hasAttribute_Inverse); + hasComplexType = getResourceOrNull(graph, URIs.hasComplexType); + hasComplexType_Inverse = getResourceOrNull(graph, URIs.hasComplexType_Inverse); + hasElement = getResourceOrNull(graph, URIs.hasElement); + hasElementList = getResourceOrNull(graph, URIs.hasElementList); + hasElementList_Inverse = getResourceOrNull(graph, URIs.hasElementList_Inverse); + hasElement_Inverse = getResourceOrNull(graph, URIs.hasElement_Inverse); + hasExternalReference = getResourceOrNull(graph, URIs.hasExternalReference); + hasID = getResourceOrNull(graph, URIs.hasID); + hasID_Inverse = getResourceOrNull(graph, URIs.hasID_Inverse); + hasOriginalElementList = getResourceOrNull(graph, URIs.hasOriginalElementList); + hasOriginalElementList_Inverse = getResourceOrNull(graph, URIs.hasOriginalElementList_Inverse); + hasReference = getResourceOrNull(graph, URIs.hasReference); + } + + public static XMLResource getInstance(ReadGraph graph) { + Session session = graph.getSession(); + XMLResource ret = session.peekService(XMLResource.class); + if(ret == null) { + QueryControl qc = graph.getService(QueryControl.class); + ret = new XMLResource(qc.getIndependentGraph(graph)); + session.registerService(XMLResource.class, ret); + } + return ret; + } + + public static XMLResource getInstance(RequestProcessor session) throws DatabaseException { + XMLResource ret = session.peekService(XMLResource.class); + if(ret == null) { + ret = session.syncRequest(new Read() { + public XMLResource perform(ReadGraph graph) throws DatabaseException { + QueryControl qc = graph.getService(QueryControl.class); + return new XMLResource(qc.getIndependentGraph(graph)); + } + }); + session.registerService(XMLResource.class, ret); + } + return ret; + } + +} + 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 dc82a9f..121a635 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 @@ -337,17 +337,21 @@ public class ExporterGenerator extends JavaGenerator{ } @Override - protected void createPrimitiveIndicator(SchemaObject parent, String refName, String binding) { + protected void createPrimitiveIndicator(SchemaObject parent, String refName, TypeEntry typeEntry, QName typeName) { FileWriter fw = getWriter(parent); - //SchemaObject refElement = elementName.get(refType.getLocalPart()); - //writer.println(commentTag+ontShort+"."+parent+".has"+ref + " parents, ParserElement element) throws DatabaseException {"); if (inheritance.type == InheritanceType.ComplexType) { - fw.writer.println(" super.configure(graph,parents,element);"); + fw.writer.println(" super.configure(graph,parents,element);"); } - fw.writer.println(" "+getOntologyImport()); - + fw.writer.println(" "+getOntologyImport()); + fw.writer.println(" Layer0 L0 = Layer0.getInstance(graph);"); this.base.handleComplexTypeAttributes(complexTypeObj); this.base.handleExtensionAttributes(complexTypeObj); @@ -279,14 +279,30 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected void createPrimitiveIndicator(SchemaObject parent, String refName, String binding) { + protected void createPrimitiveIndicator(SchemaObject parent, String refName, TypeEntry typeEntry, QName typeName) { FileWriter fw = getWriter(parent); - fw.writer.println(" addParser(\""+ refName +"\", "+getName(parent) +"_" +refName+".class);"); + String className = getName(parent) +"_" +refName; + fw.writer.println(" addParser(\"" + refName + "\", " + className + ".class);"); + fw.delayedWriter2.println(" public static class " + className + " extends org.simantics.xml.sax.base.ValueElementParser {"); + fw.delayedWriter2.println(" public "+ className +"(){"); + fw.delayedWriter2.println(" super(\""+refName+"\"," +this.ontologyClassName+".URIs."+getName(parent) + "_has"+refName+", "+typeEntry.binding+");"); + fw.delayedWriter2.println(" }\n"); + fw.delayedWriter2.println(" @Override"); + fw.delayedWriter2.println(" public void configure(WriteGraph graph, ParserElement element, String value) throws DatabaseException {"); + fw.delayedWriter2.println(" Layer0 L0 = Layer0.getInstance(graph);"); + + SchemaObject simpleType = base.getSimpleType(typeName); + String ontologyClassName = converter.getOntologyClassName(typeName.getNamespaceURI()); + if (ontologyClassName != null && simpleType != null) + fw.delayedWriter2.println(" graph.claim(element.getData(), L0.InstanceOf, " + ontologyClassName + ".getInstance(graph)." + getName(simpleType, "") + ");"); + else { + if (typeEntry.l0Type.startsWith("XML")) + fw.delayedWriter2.println(" XMLResource XML = XMLResource.getInstance(graph);"); + fw.delayedWriter2.println(" graph.claim(element.getData(), L0.InstanceOf, " + typeEntry.l0Type + ");"); + } - fw.delayedWriter2.println(" public static class " + getName(parent) +"_" +refName+" extends org.simantics.xml.sax.base.ValueElementParser {"); - fw.delayedWriter2.println(" public "+ getName(parent) +"_" +refName +"(){"); - fw.delayedWriter2.println(" super(\""+refName+"\"," +this.ontologyClassName+".URIs."+getName(parent) + "_has"+refName+", "+binding+");"); - fw.delayedWriter2.println(" }"); + fw.delayedWriter2.println(" graph.claimValue(element.getData(), " +getValueGetter(typeEntry) + ", " + typeEntry.binding + ");"); + fw.delayedWriter2.println(" }"); fw.delayedWriter2.println(" }"); } @@ -386,7 +402,7 @@ public class ImporterGenerator extends JavaGenerator{ TypeEntry binding = this.base.getTypeEntry(primitiveType); if (binding != null) { - writeAttribute(fw, attrName, relationName, binding, isReference); + writeAttribute(fw, attrName, relationName, binding, isReference, primitiveType); return; } else { if (simpleType == null) { @@ -399,8 +415,9 @@ public class ImporterGenerator extends JavaGenerator{ if (simpleType != null) { org.w3._2001.xmlschema.List list = simpleType.getList(); if (list != null) { - TypeEntry binding = this.base.getTypeEntry(new QName(SchemaConversionBase.SCHEMA_NS, "string")); - writeAttribute(fw, attrName, relationName, binding, isReference); + QName typeName = new QName(SchemaConversionBase.SCHEMA_NS, "string"); + TypeEntry binding = this.base.getTypeEntry(typeName); + writeAttribute(fw, attrName, relationName, binding, isReference, typeName); } else { // Restriction restriction = simpleType.getRestriction(); // if (restriction == null) @@ -411,22 +428,24 @@ public class ImporterGenerator extends JavaGenerator{ //String binding = getBindingFromPrimitiveType(base); TypeEntry binding = this.base.getTypeEntry(base); - writeAttribute(fw, attrName, relationName, binding, isReference); + writeAttribute(fw, attrName, relationName, binding, isReference, base); } } else { // TODO : using default String attribute should be configured with rules. //throw new RuntimeException("Cannot resolve type for Attribute " + attrName + " -> " + primitiveType.getLocalPart()); fw.writer.println(" //FIXME: Cannot resolve type for Attribute " + attrName + " Using default type String"); //writeAttribute(fw, attrName, relationName, "STRING", isReference); - writeAttribute(fw, attrName, relationName, this.base.getTypeEntry("string"), isReference); + writeAttribute(fw, attrName, relationName, this.base.getTypeEntry("string"), isReference, new QName(SchemaConversionBase.SCHEMA_NS, "string")); } } - private void writeAttribute(FileWriter fw, String attrName, String relationName, TypeEntry binding, boolean isReference) { + private void writeAttribute(FileWriter fw, String attrName, String relationName, TypeEntry binding, boolean isReference, QName typeName) { + String ontologyClassName = converter.getOntologyClassName(typeName.getNamespaceURI()); + String ontologyType = ontologyClassName != null ? ontologyClassName + ".getInstance(graph)." + getName(base.getSimpleType(typeName)) : binding.l0Type; fw.writer.println(" {"); fw.writer.println(" Attribute a = element.getAttribute(\"" +attrName+"\");"); fw.writer.println(" if (a != null) {"); - fw.writer.println(" graph.claimLiteral(element.getData(),"+relationName+","+getValueGetterMethod(binding,"a")+", "+binding.binding+");"); + fw.writer.println(" graph.claimLiteral(element.getData(), " + relationName + ", " + ontologyType + ", " + getValueGetterMethod(binding,"a") + ", " + binding.binding + ");"); if (isReference) fw.writer.println(" idProviderValue = a.getValue();"); fw.writer.println(" }"); @@ -589,9 +608,11 @@ public class ImporterGenerator extends JavaGenerator{ fw.writer.println(" @Override"); fw.writer.println(" public void configure(WriteGraph graph, Deque parents, ParserElement element) throws DatabaseException {"); if (inheritance.type == InheritanceType.ComplexType) { - fw.writer.println(" super.configure(graph,parents,element);"); + fw.writer.println(" super.configure(graph,parents,element);"); } - fw.writer.println(" "+getOntologyImport()); + fw.writer.println(" Layer0 L0 = Layer0.getInstance(graph);"); + fw.writer.println(" XMLResource XML = XMLResource.getInstance(graph);"); + fw.writer.println(" "+getOntologyImport()); LocalComplexType complexType = element.getComplexType(); LocalSimpleType simpleType = element.getSimpleType(); diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java index 72b2956..4869e07 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java @@ -10,6 +10,7 @@ import java.util.Map; import javax.xml.namespace.QName; +import org.simantics.xml.sax.SchemaConversionBase.Inheritance; import org.simantics.xml.sax.SchemaConversionBase.RefType; import org.simantics.xml.sax.SchemaConversionBase.TypeEntry; import org.w3._2001.xmlschema.AttributeGroupRef; @@ -80,6 +81,11 @@ public abstract class JavaGenerator implements SchemaConversionComponent { return binding.getValueGetter(); } + @Override + public String getSimpleTypePrefix() { + return "SimpleTypes_"; + } + @Override public String getComplexTypePrefix() { return "ComplexTypes_"; @@ -91,22 +97,24 @@ public abstract class JavaGenerator implements SchemaConversionComponent { } @Override - public void handleChoice(SchemaObject parent, SchemaElement indicator, List elements, String name) { - - for (SchemaElement e : elements) { - Element localElement = e.getElement(); - if (localElement.getName() != null) { - QName refType = localElement.getType(); - if (refType != null) - //handleIndicator(parent, indicator, e, false, name, refType); - handleIndicator(parent, indicator, e, name, RefType.Type); - } else if (localElement.getRef() != null) { - //QName refType = localElement.getRef(); - //handleIndicator(parent, indicator, e, true, name, refType); - handleIndicator(parent, indicator, e, name, RefType.Reference); - } - } + public String handleChoice(SchemaObject parent, SchemaElement indicator, List elements, String name) { + String baseRelationName = getName(parent) + ".has" + name; + +// for (SchemaElement e : elements) { +// Element localElement = e.getElement(); +// if (localElement.getName() != null) { +// QName refType = localElement.getType(); +// if (refType != null) +// //handleIndicator(parent, indicator, e, false, name, refType); +// handleIndicator(parent, indicator, e, name, RefType.Type, baseRelationName); +// } else if (localElement.getRef() != null) { +// //QName refType = localElement.getRef(); +// //handleIndicator(parent, indicator, e, true, name, refType); +// handleIndicator(parent, indicator, e, name, RefType.Reference, baseRelationName); +// } +// } + return baseRelationName; } protected String getOntologyImport() { @@ -142,7 +150,7 @@ public abstract class JavaGenerator implements SchemaConversionComponent { case ATTRIBUTE_GROUP: return getAttributeGroupPrefix()+obj.getName(); case SIMPLE_TYPE: - return obj.getName(); + return getSimpleTypePrefix()+obj.getName(); } } else { SchemaObject o = obj; @@ -165,7 +173,7 @@ public abstract class JavaGenerator implements SchemaConversionComponent { case ATTRIBUTE_GROUP: return getAttributeGroupPrefix()+name; case SIMPLE_TYPE: - return name; + return getSimpleTypePrefix()+name; } } throw new RuntimeException(); @@ -182,7 +190,7 @@ public abstract class JavaGenerator implements SchemaConversionComponent { case ATTRIBUTE_GROUP: return getAttributeGroupPrefix()+rel+obj.getName(); case SIMPLE_TYPE: - return rel+obj.getName(); + return getSimpleTypePrefix()+rel+obj.getName(); } } else { SchemaObject o = obj; @@ -203,7 +211,7 @@ public abstract class JavaGenerator implements SchemaConversionComponent { case ATTRIBUTE_GROUP: return getAttributeGroupPrefix()+rel+name; case SIMPLE_TYPE: - return rel+name; + return getSimpleTypePrefix()+rel+name; } } throw new RuntimeException(); @@ -236,49 +244,60 @@ public abstract class JavaGenerator implements SchemaConversionComponent { protected abstract void createReferenceIndicator(SchemaObject parent, RefType referenceType, String refName, String objectName, String primaryClassName, String secondaryClassName, boolean useElementList, boolean useOriginalList); - protected abstract void createPrimitiveIndicator(SchemaObject parent, String refName, String binding); + protected abstract void createPrimitiveIndicator(SchemaObject parent, String refName, TypeEntry typeEntry, QName typeName); protected abstract void createElementIndicator(SchemaObject parent, boolean useElementList, String refName, String className, boolean useOriginalList); @Override - public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType referenceType) { + public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType referenceType, String baseRelationName) { String objectName; if (referenceType != RefType.Element) { - QName refType; + QName refTypeName; + SchemaObject refObject = null; if (referenceType == RefType.Type) { - refType = element.getElement().getType(); + refTypeName = element.getElement().getType(); if (refName == null) refName = element.getElement().getName(); objectName = element.getElement().getName(); + refObject = this.base.getComplexType(refTypeName); + if (refObject == null) this.base.getSimpleType(refTypeName); } else { - refType = element.getElement().getRef(); + refTypeName = element.getElement().getRef(); if (refName == null) - refName = refType.getLocalPart(); - objectName = refType.getLocalPart(); + refName = refTypeName.getLocalPart(); + objectName = refTypeName.getLocalPart(); + refObject = this.base.getElement(refTypeName); } - String binding = this.base.getBindingFromPrimitiveType(refType); - if (binding == null) { - SchemaObject refElement = this.base.getElement(refType); - SchemaObject refComplexType = this.base.getComplexType(refType); - + TypeEntry typeEntry = this.base.getTypeEntry(refTypeName); + if (typeEntry == null) { // prefer element reference over complex type reference String primaryClassName = null; String secondaryClassName = null; - if (refElement != null) - primaryClassName = getName(refElement); - else - primaryClassName = getName(refComplexType); + if (refObject != null) + primaryClassName = getName(refObject); + else if (this.base.getSimpleType(refTypeName) != null) { + Inheritance inheritance = new Inheritance(""); + this.base.getAtomicTypeInheritance(refTypeName, inheritance); + if (inheritance.atomicType != null) { + createPrimitiveIndicator(parent, refName, inheritance.atomicType, refTypeName); + return; + } + else { + throw new RuntimeException("No supported atomic type found for simple type " + refTypeName.toString()); + } + } + else { + throw new RuntimeException("Type that is neither complex nor simple??"); + } - if (refComplexType != null) { - secondaryClassName = getName(refComplexType); - } else { - secondaryClassName = getName(refElement); + if (refObject != null) { + secondaryClassName = getName(refObject); } - boolean useElementList = this.base.useElementList(parent, indicator,element, referenceType == RefType.Reference, refName, refType); - boolean useOriginalList = this.base.useOriginalList(parent, indicator,element, referenceType == RefType.Reference, refName, refType); + boolean useElementList = this.base.useElementList(parent, indicator,element, referenceType == RefType.Reference, refName, refTypeName); + boolean useOriginalList = this.base.useOriginalList(parent, indicator,element, referenceType == RefType.Reference, refName, refTypeName); createReferenceIndicator(parent, referenceType, refName, objectName, primaryClassName, secondaryClassName, useElementList, useOriginalList); } else { - createPrimitiveIndicator(parent, refName, binding); + createPrimitiveIndicator(parent, refName, typeEntry, refTypeName); } } else { Element attrs= element.getElement(); diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java index 6e43558..3e0ffb1 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java @@ -3,7 +3,6 @@ package org.simantics.xml.sax; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.io.StringWriter; -import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -12,7 +11,6 @@ import javax.xml.namespace.QName; import org.simantics.utils.datastructures.BijectionMap; import org.simantics.xml.sax.SchemaConversionBase.Inheritance; -import org.simantics.xml.sax.SchemaConversionBase.InheritanceType; import org.simantics.xml.sax.SchemaConversionBase.RefType; import org.simantics.xml.sax.SchemaObject.ObjectType; import org.simantics.xml.sax.configuration.AttributeComposition; @@ -25,7 +23,6 @@ import org.w3._2001.xmlschema.LocalComplexType; import org.w3._2001.xmlschema.LocalSimpleType; import org.w3._2001.xmlschema.NamedAttributeGroup; import org.w3._2001.xmlschema.NamedGroup; -import org.w3._2001.xmlschema.Restriction; import org.w3._2001.xmlschema.Schema; import org.w3._2001.xmlschema.SimpleType; @@ -90,6 +87,7 @@ public class OntologyGenerator implements SchemaConversionComponent { writer.println(); ontRoot += "."; + writer.println(ontRoot+"SimpleTypes : L0.Library"); writer.println(ontRoot+"ComplexTypes : L0.Library"); writer.println(ontRoot+"AttributeGroups : L0.Library"); writer.println(); @@ -99,22 +97,25 @@ public class OntologyGenerator implements SchemaConversionComponent { base.handle(this); } - protected String getType(QName qtype) { + protected String getType(QName qtype, String rel) { String ontType = base.getL0TypeFromPrimitiveType(qtype); if (ontType != null) return ontType; - else if (base.isComplexTypeRef(qtype.getLocalPart())) - return ontRoot+getComplexTypePrefix()+qtype.getLocalPart(); - else if (base.isSimpleTypeRef(qtype.getLocalPart())) - return ontRoot+qtype.getLocalPart(); - else if (base.isElementRef(qtype.getLocalPart())) - return ontRoot+qtype.getLocalPart(); - else if (qtype.getPrefix() != null && qtype.getPrefix().length() > 0) { - return ontRoot+qtype.getPrefix()+qtype.getLocalPart(); + else { + if (base.isComplexTypeRef(qtype.getLocalPart())) + return getName(base.getComplexType(qtype), rel); + else if (base.isSimpleTypeRef(qtype.getLocalPart())) + return getName(base.getSimpleType(qtype), rel); + else if (base.isElementRef(qtype.getLocalPart())) + return getName(base.getElement(qtype), rel); } throw new RuntimeException("Reference to unknown type " + qtype.getLocalPart()); } + public String getSimpleTypePrefix() { + return "SimpleTypes."; + } + public String getComplexTypePrefix() { return "ComplexTypes."; } @@ -123,7 +124,7 @@ public class OntologyGenerator implements SchemaConversionComponent { return "AttributeGroups."; } - public void handleChoice(SchemaObject parent, SchemaElement indicator, java.util.List elements, String name) { + public String handleChoice(SchemaObject parent, SchemaElement indicator, java.util.List elements, String name) { boolean single = true; for (SchemaElement e : elements) { if (e.getRestriction().many()) { @@ -133,70 +134,24 @@ public class OntologyGenerator implements SchemaConversionComponent { } String relationName = getName(parent)+".has"+name; writer.print(relationName); - - List types = new ArrayList(); - for (SchemaElement e : elements) { - Element localElement = e.getElement(); - QName refType = null; - String type = null; - - if (localElement.getName() != null) { - refType = localElement.getType(); - type = base.getL0TypeFromPrimitiveType(refType); - } else if (localElement.getRef() != null) { - refType = localElement.getRef(); - type = base.getL0TypeFromPrimitiveType(refType); - } - if (type == null) { - SchemaObject obj = base.getWithName(refType); - types.add(getName(obj,"has")); - } - } - if (types.size() > 0) { - for (String type : types) { - writer.print(" " + type); - } - } if (!single) { - writer.println(ontRoot+name+ "List " + ontRoot+name+"List"); + String elementListType = getName(parent) + "." + name + "List"; + writer.println(elementListType + " " + elementListType); } + + return relationName; }; @Override - public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType) { - if (refType != refType.Element) { + public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType, String baseRelationName) { + if (refType != RefType.Element) { QName referenceType = null; if (refType == RefType.Type) { referenceType = element.getElement().getType(); @@ -213,12 +168,14 @@ public class OntologyGenerator implements SchemaConversionComponent { SchemaObject obj = null; if (type == null) { obj = base.getWithName(referenceType); - - writer.println(getName(parent)+".has"+refName + " " + getName(obj)); + writer.print(getName(parent)+".has"+refName + " " + getType(referenceType)); + writer.print(getName(parent)+".has"+refName + " " + getType(referenceType, "")); } if (base.useElementList(parent, indicator,element, refType == RefType.Reference, refName, referenceType)) { @@ -237,7 +194,9 @@ public class OntologyGenerator implements SchemaConversionComponent { if (refName == null) refName = obj.getName(); - writer.println(getName(parent)+".has"+refName + " " + getName(obj)); if (base.useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) { writer.println(getName(parent)+"."+refName + "List " + ontType); } else if (primitiveType != null) { - writer.println(" " + ontType); } else { - String relationName = ontRoot+"has"+name; - if (parent != null) - relationName = getName(parent)+".has"+name; + QName base = this.base.getSimpleTypeBase(simpleType); + writer.println(getName(simpleTypeObj) + " " + inheritance.atomicType.l0Type); + writer.println(relationName+ " : L0.FunctionalRelation"); + writer.println(" --> " + getName(simpleTypeObj)); // Restriction restriction = simpleType.getRestriction(); // if (restriction != null) { @@ -444,13 +396,6 @@ public class OntologyGenerator implements SchemaConversionComponent { // if (topLevelComplexType.getName().equals("Reference")) // System.out.println(); - String relationName = getName(topLevelComplexType,"has");//ontRoot+"has"+name; - - writer.println(relationName+ " " + ontRoot+getComplexTypePrefix()+name); - writer.println(" --> " + name); - writer.println(); // String baseType = "XML.ComplexType"; // // QName base = getComplexTypeBase(topLevelComplexType.getComplexType()); @@ -465,11 +410,19 @@ public class OntologyGenerator implements SchemaConversionComponent { // writer.println(name+ " " + name); + writer.println(relationName+ "List " + name + "List"); + writer.println(); + + // Attributes // if (!baseType.equals(inheritance.baseClass)) // System.out.println(); //super.handleComplexType(topLevelComplexType); @@ -490,7 +443,7 @@ public class OntologyGenerator implements SchemaConversionComponent { String type = "XML.Element"; Set types = new LinkedHashSet(); if (element.getType() != null) { - types.add(getType(element.getType())); + types.add(getType(element.getType(), "")); } QName base = this.base.getElementBase(element); if (base != null) { @@ -566,7 +519,7 @@ public class OntologyGenerator implements SchemaConversionComponent { case ATTRIBUTE_GROUP: return ontRoot+getAttributeGroupPrefix()+obj.getName(); case SIMPLE_TYPE: - return ontRoot+obj.getName(); + return ontRoot+getSimpleTypePrefix()+obj.getName(); } } else { SchemaObject o = obj; @@ -587,13 +540,23 @@ public class OntologyGenerator implements SchemaConversionComponent { case ATTRIBUTE_GROUP: return ontRoot+getAttributeGroupPrefix()+name; case SIMPLE_TYPE: - return ontRoot+name; + return ontRoot+getSimpleTypePrefix()+name; } } throw new RuntimeException(); } + public String getName(SchemaObject parent, SchemaElement e, String rel) { + QName ref = e.getElement().getRef(); + if (ref != null) { + return converter.getShortName(ref.getNamespaceURI()) + rel + ref.getLocalPart(); + } + else { + return getName(parent, "") + "." + rel + e.getElement().getName(); + } + } + public String getName(SchemaObject obj, String rel) { if (obj.getParent() == null) { switch (obj.getType()) { @@ -604,7 +567,7 @@ public class OntologyGenerator implements SchemaConversionComponent { case ATTRIBUTE_GROUP: return ontRoot+getAttributeGroupPrefix()+rel+obj.getName(); case SIMPLE_TYPE: - return ontRoot+rel+obj.getName(); + return ontRoot+getSimpleTypePrefix()+rel+obj.getName(); } } else { SchemaObject o = obj; @@ -625,7 +588,7 @@ public class OntologyGenerator implements SchemaConversionComponent { case ATTRIBUTE_GROUP: return ontRoot+getAttributeGroupPrefix()+rel+name; case SIMPLE_TYPE: - return ontRoot+rel+name; + return ontRoot+getSimpleTypePrefix()+rel+name; } } throw new RuntimeException(); diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java index 1e13951..f349cde 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java @@ -111,7 +111,7 @@ public final class SchemaConversionBase { schemaTypes.put("unsignedByte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","java.lang.Byte.parseByte(",")","java.lang.Byte.toString(",")")); schemaTypes.put("long", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")","java.lang.Long.toString(",")")); schemaTypes.put("unsignedLong", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")","java.lang.Long.toString(",")")); - schemaTypes.put("base64Binary", new TypeEntry("L0.ByteArray", "Bindings.BYTE_ARRAY", "byte[]", "new byte[0]","",".getBytes(org.simantics.databoard.util.binary.UTF8.CHARSET)","new java.lang.String(",", org.simantics.databoard.util.binary.UTF8.CHARSET)")); + schemaTypes.put("base64Binary", new TypeEntry("L0.ByteArray", "Bindings.BYTE_ARRAY", "byte[]", "new byte[0]","java.util.Base64.getDecoder().decode(",")","java.util.Base64.getEncoder().encodeToString(",")")); l0Types.put("doubleArray", new TypeEntry("L0.DoubleArray", "Bindings.DOUBLE_ARRAY", "double[]", null,null,null,"java.lang.Double.toString(",")")); l0Types.put("stringArray", new TypeEntry("L0.StringArray", "Bindings.STRING_ARRAY", "string[]", null,null,null,"","")); @@ -769,21 +769,26 @@ public final class SchemaConversionBase { protected enum RefType{Element,Reference,Type}; - protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType) { - component.handleIndicator(parent, indicator, element, refName, refType); + protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType, String baseRelationName) { + component.handleIndicator(parent, indicator, element, refName, refType, baseRelationName); } protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any) { component.handleIndicator(parent, indicator, any); } protected void handle(SchemaObject parent, SchemaElement indicator, List elements) { //component.handle(parent, indicator, elements); - if (indicator.getType() == SchemaElement.ElementType.SEQUENCE || (indicator.getType() == SchemaElement.ElementType.CHOICE && indicator.getRestriction().many())) { + // Generate combined relation + String baseRelationName = null; + if (indicator.getType() == SchemaElement.ElementType.CHOICE) { + String name = getChoiceName(elements); + if (name != null) + baseRelationName = component.handleChoice(parent, indicator, elements, name); + } + + if (indicator.getType() == SchemaElement.ElementType.SEQUENCE || indicator.getType() == SchemaElement.ElementType.CHOICE) { for (SchemaElement e : elements) { - handle(parent, indicator, e); + handle(parent, indicator, e, baseRelationName); } - } else if (indicator.getType() == SchemaElement.ElementType.CHOICE) { - String name = getChoiceName(elements); - component.handleChoice(parent, indicator, elements, name); } } @@ -931,19 +936,19 @@ public final class SchemaConversionBase { } - protected void handle(SchemaObject parent, SchemaElement indicator, SchemaElement element) { + protected void handle(SchemaObject parent, SchemaElement indicator, SchemaElement element, String baseRelationName) { Element localElement = element.getElement(); if (localElement.getName() != null) { - SchemaObject eObj = elements.get(localElement); + SchemaObject eObj = elements.get(localElement); QName refType = localElement.getType(); if (refType != null) - handleIndicator(parent, indicator, element, null, RefType.Type); + handleIndicator(parent, indicator, element, null, RefType.Type, baseRelationName); else { handleElement(eObj); - handleIndicator(parent, indicator, element, null, RefType.Element); - } + handleIndicator(parent, indicator, element, null, RefType.Element, baseRelationName); + } } else if (localElement.getRef() != null) { - handleIndicator(parent, indicator,element, null, RefType.Reference); + handleIndicator(parent, indicator,element, null, RefType.Reference, baseRelationName); } } @@ -963,22 +968,27 @@ public final class SchemaConversionBase { protected String getChoiceName(List elements) { if (elements.size() == 1) { - return getElementName(elements.get(0).getElement()); + return null; } - List names = new ArrayList(); - for (SchemaElement e : elements) { - String name = getElementName(e.getElement()); - if (name != null) - names.add(name); + else if (elements.size() > 0 && elements.size() <= 3) { + List names = new ArrayList(); + for (SchemaElement e : elements) { + String name = getElementName(e.getElement()); + if (name != null) + names.add(name); + } + String name = ""; + for (int i = 0; i < names.size(); i++) { + if (i == 0) + name = names.get(i); + else + name += "Or"+names.get(i); + } + return name; } - String name = ""; - for (int i = 0; i < names.size(); i++) { - if (i == 0) - name = names.get(i); - else - name += "Or"+names.get(i); + else { + return "SubElement"; } - return name; } protected void handle(SchemaObject parent, Attribute attribute) { diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionComponent.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionComponent.java index 89d8e86..9a2243b 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionComponent.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionComponent.java @@ -17,16 +17,17 @@ public interface SchemaConversionComponent { void handleAttributes(SchemaObject simpleTypeObj); void handleAttributeComposition(SchemaObject obj, AttributeComposition composition, BijectionMap attributes); - void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType); + void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType, String baseRelationName); void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any); //void handle(SchemaObject parent, SchemaElement indicator, List elements); - void handleChoice(SchemaObject parent, SchemaElement indicator, List elements, String name); + String handleChoice(SchemaObject parent, SchemaElement indicator, List elements, String name); void handle(SchemaObject parent, Attribute attribute) ; void handle(SchemaObject parent, AttributeGroup attribute) ; void handle(SchemaObject parent, NamedGroup attribute); void handleSimpleType(SchemaObject parent, SchemaObject simpleType); void handleComplexType(SchemaObject complexTypeObj); void handleElement(SchemaObject complexTypeObj); + String getSimpleTypePrefix(); String getComplexTypePrefix(); String getAttributeGroupPrefix(); String getName(SchemaObject obj); diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java index 1b2b372..adab7f2 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java @@ -16,6 +16,7 @@ import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; +import javax.xml.namespace.QName; import org.simantics.utils.datastructures.MapList; import org.simantics.xml.sax.configuration.Configuration; @@ -221,6 +222,7 @@ public class SchemaConverter { header[3] = "Date " + new Date().toString(); schemaNs = schema.getTargetNamespace(); + ontologyUri = schemaNs; if (ontologyUri == null) { ontologyUri = getSchemaFile().getName(); @@ -228,6 +230,7 @@ public class SchemaConverter { int index = ontologyUri.lastIndexOf("."); if (index > 0) ontologyUri = ontologyUri.substring(0, index); + schemaNs = ""; } ontologyUri = ontologyUri.replaceAll(" ", "_"); String parts[] = ontologyUri.split("/"); @@ -335,7 +338,25 @@ public class SchemaConverter { } public boolean isPrimary() { - return getRoot().schemaNSMap.getValues(schemaNs).indexOf(this) == 0; + return getRoot() == this; + } + + public String getShortName(String namespaceURI) { + List converters = getRoot().getConverter(namespaceURI); + for (SchemaConverter conv : converters) { + if (conv.shortName != null) + return conv.shortName; + } + return null; + } + + public String getOntologyClassName(String namespaceURI) { + List converters = getRoot().getConverter(namespaceURI); + for (SchemaConverter conv : converters) { + if (conv.className != null) + return conv.className; + } + return null; } -- 2.43.2