From d788a05e77b983199f0982a0e56629e7fc30c691 Mon Sep 17 00:00:00 2001 From: luukkainen Date: Wed, 1 Feb 2017 09:37:23 +0000 Subject: [PATCH] Refactoring and preparing the conversion code to handle references between XML schemas. refs #6985 git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@33442 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sax/ui/wizard/SchemaConversionWizard.java | 45 ++- .../simantics/xml/sax/ExporterGenerator.java | 115 +++--- .../simantics/xml/sax/ImporterGenerator.java | 124 +++---- .../org/simantics/xml/sax/JavaGenerator.java | 79 ++-- .../xml/sax/ManualSchemaFileImport.java | 10 + .../simantics/xml/sax/OntologyGenerator.java | 346 +++++++++--------- .../xml/sax/SchemaConversionBase.java | 336 +++++++++++++---- .../xml/sax/SchemaConversionComponent.java | 34 ++ .../simantics/xml/sax/SchemaConverter.java | 151 ++++++-- .../org/simantics/xml/sax/SchemaElement.java | 2 +- .../org/simantics/xml/sax/SchemaObject.java | 19 +- 11 files changed, 827 insertions(+), 434 deletions(-) create mode 100644 org.simantics.xml.sax/src/org/simantics/xml/sax/ManualSchemaFileImport.java create mode 100644 org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionComponent.java diff --git a/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaConversionWizard.java b/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaConversionWizard.java index 627381b..3c27632 100644 --- a/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaConversionWizard.java +++ b/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaConversionWizard.java @@ -1,11 +1,16 @@ package org.simantics.xml.sax.ui.wizard; import java.io.File; +import java.io.IOException; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.Wizard; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.FileDialog; import org.eclipse.ui.IImportWizard; import org.eclipse.ui.IWorkbench; +import org.simantics.xml.sax.ManualSchemaFileImport; import org.simantics.xml.sax.SchemaConverter; public class SchemaConversionWizard extends Wizard implements IImportWizard { @@ -39,18 +44,46 @@ public class SchemaConversionWizard extends Wizard implements IImportWizard { } public void doConversion() throws Exception { - SchemaConverter converter = new SchemaConverter(); - converter.setCreateExporter(inputSelectionPage.createExporter); - converter.setCreateImporter(inputSelectionPage.createImporter); - converter.setCreatePGraph(inputSelectionPage.createPGraph); - + + File inputFile = new File(inputSelectionPage.getSchemaFilename()); File configurationFile = null; if (inputSelectionPage.getConfigurationFilename() != null) configurationFile = new File(inputSelectionPage.getConfigurationFilename()); File outputPlugin = new File(inputSelectionPage.getPluginFilename()); + + SchemaConverter converter = new SchemaConverter(inputFile,configurationFile,outputPlugin); + converter.setCreateExporter(inputSelectionPage.createExporter); + converter.setCreateImporter(inputSelectionPage.createImporter); + converter.setCreatePGraph(inputSelectionPage.createPGraph); + converter.setFileImport(new ManualSchemaFileImport() { + + String filename; + @Override + public File getFileForLocation(final String location) throws IOException { + if (Display.getCurrent() == null) + Display.getDefault().syncExec(new Runnable() { + @Override + public void run() { + showDialog(location); + } + }); + else { + showDialog(location); + } + if (filename == null) + return null; + return new File(filename); + } + + protected void showDialog(String location) { + FileDialog dialog = new FileDialog(SchemaConversionWizard.this.getShell(), SWT.OPEN); + dialog.setText("Open schema file for " + location); + filename = dialog.open(); + } + }); - converter.convertSchema(inputFile,configurationFile,outputPlugin); + converter.convert(); schemaCombinationPage.setOutputPlugin(outputPlugin); } 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 243a1c2..d1f67a4 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java @@ -11,9 +11,12 @@ import java.util.Map; 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.SchemaConversionBase.TypeEntry; import org.simantics.xml.sax.SchemaObject.ObjectType; import org.simantics.xml.sax.configuration.AttributeComposition; -import org.simantics.xml.sax.configuration.Configuration; import org.simantics.xml.sax.configuration.IDProvider; import org.simantics.xml.sax.configuration.IDReference; import org.simantics.xml.sax.configuration.UnrecognizedChildElement; @@ -25,31 +28,18 @@ import org.w3._2001.xmlschema.Element; import org.w3._2001.xmlschema.LocalComplexType; import org.w3._2001.xmlschema.LocalSimpleType; import org.w3._2001.xmlschema.NamedAttributeGroup; +import org.w3._2001.xmlschema.NamedGroup; import org.w3._2001.xmlschema.Restriction; -import org.w3._2001.xmlschema.Schema; import org.w3._2001.xmlschema.SimpleType; -import org.w3._2001.xmlschema.TopLevelAttribute; public class ExporterGenerator extends JavaGenerator{ - public ExporterGenerator(Configuration configuration) { - super(configuration); + public ExporterGenerator(SchemaConverter converter, SchemaConversionBase base) { + super(converter, base); } - - - public void createParser(Schema schema,String ontologyUri, String className, SchemaConverter converter) throws IOException { - this.schema = schema; - this.ontologyClassName = className; - this.converter = converter; - - - String packageParts[] = className.split("\\."); - String name = packageParts[packageParts.length-1]; - + public void createParser() throws IOException { - ontShort = name.substring(0, 3).toUpperCase(); - ontShort +="."; String parserPackagePostfix = "_exp"; String importerClassPostfix = "Exporter"; String parserClassPostfix = "Writer"; @@ -59,7 +49,7 @@ public class ExporterGenerator extends JavaGenerator{ if (!importParserDir.exists()) importParserDir.mkdirs(); - handle(schema); + base.handle(this); // Create Importer class File importerFile = new File(converter.getParserDir().getAbsolutePath()+"/"+name+importerClassPostfix+".java"); PrintWriter mainWriter = createFile(importerFile); @@ -118,21 +108,28 @@ public class ExporterGenerator extends JavaGenerator{ mainWriter.close(); } - protected void handle(TopLevelAttribute topLevelAttribute) { - - } +// @Override +// protected void handle(TopLevelAttribute topLevelAttribute) { +// +// } @Override - protected String getBaseClass(ObjectType type) { + public String getBaseClass(ObjectType type) { return "org.simantics.xml.sax.base.XMLElementNamedChildWriterBase"; } @Override - protected void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { + public void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { + } + + @Override + public void handle(SchemaObject parent, NamedGroup attribute) { + // TODO Auto-generated method stub + } @Override - protected void handleComplexType(SchemaObject complexTypeObj) { + public void handleComplexType(SchemaObject complexTypeObj) { ComplexType topLevelComplexType = complexTypeObj.getComplexType(); String name = getName(complexTypeObj); @@ -161,9 +158,9 @@ public class ExporterGenerator extends JavaGenerator{ // inherited = true; // } // } - Inheritance inheritance = getInheritance(complexTypeObj); + Inheritance inheritance = base.getInheritance(complexTypeObj); - provider = getIDProvider(topLevelComplexType); + provider = base.getIDProvider(topLevelComplexType); // List references = getIDReferences(topLevelComplexType); // UnrecognizedChildElement unknownChildElement = getUnknown(topLevelComplexType); @@ -216,7 +213,7 @@ public class ExporterGenerator extends JavaGenerator{ fw.writer.println(" super(graph);"); fw.writer.println(" "+getOntologyImport()); - handleComplexTypeExtension(complexTypeObj); + base.handleComplexTypeExtension(complexTypeObj); fw.writer.println(" }"); @@ -250,8 +247,8 @@ public class ExporterGenerator extends JavaGenerator{ } fw.writer.println(" "+getOntologyImport()); fw.writer.println(" for (Statement attribute : attributes) {"); - handleComplexTypeAttributes(complexTypeObj); - handleExtensionAttributes(complexTypeObj); + base.handleComplexTypeAttributes(complexTypeObj); + base.handleExtensionAttributes(complexTypeObj); fw.writer.println(" }"); fw.writer.println(" }"); @@ -400,7 +397,7 @@ public class ExporterGenerator extends JavaGenerator{ } @Override - protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any) { + public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any) { // generates overriding method that allows writing any element FileWriter fw = getWriter(parent); fw.delayedWriter2.println(" @Override"); @@ -415,7 +412,9 @@ public class ExporterGenerator extends JavaGenerator{ } @Override - protected void handle(SchemaObject parent, Attribute attribute) { + public void handle(SchemaObject parent, Attribute attribute) { + if (parent == null) + return; String name = attribute.getName(); QName primitiveType = attribute.getType(); SimpleType simpleType = attribute.getSimpleType(); @@ -433,7 +432,7 @@ public class ExporterGenerator extends JavaGenerator{ attrName = ref.getLocalPart(); relationName = ontShort+getName(parent)+"_has"+ref.getLocalPart(); - Attribute referred = getRefAttribute(ref); + Attribute referred = base.getRefAttribute(ref); if (referred != null) { primitiveType = referred.getType(); simpleType = referred.getSimpleType(); @@ -450,14 +449,14 @@ public class ExporterGenerator extends JavaGenerator{ if (primitiveType != null) { //String binding = getBindingFromPrimitiveType(primitiveType); - TypeEntry binding = getTypeEntry(primitiveType); + TypeEntry binding = base.getTypeEntry(primitiveType); if (binding != null) { writeAttribute(fw, attrName, relationName, binding, isReference); return; } else { if (simpleType == null) { - SchemaObject simpleTypeObj = simpleTypeName.get(primitiveType.getLocalPart()); + SchemaObject simpleTypeObj = base.simpleTypeName.get(primitiveType.getLocalPart()); if (simpleTypeObj != null) simpleType = simpleTypeObj.getSimpleType(); } @@ -466,7 +465,7 @@ public class ExporterGenerator extends JavaGenerator{ if (simpleType != null) { org.w3._2001.xmlschema.List list = simpleType.getList(); if (list != null) { - TypeEntry binding = getTypeEntry(new QName(SCHEMA_NS, "string")); + TypeEntry binding = base.getTypeEntry(new QName(SchemaConversionBase.SCHEMA_NS, "string")); writeAttribute(fw, attrName, relationName, binding, isReference); } else { Restriction restriction = simpleType.getRestriction(); @@ -476,7 +475,7 @@ public class ExporterGenerator extends JavaGenerator{ //String binding = getBindingFromPrimitiveType(base); - TypeEntry binding = getTypeEntry(base); + TypeEntry binding = this.base.getTypeEntry(base); writeAttribute(fw, attrName, relationName, binding, isReference); } } else { @@ -484,11 +483,10 @@ public class ExporterGenerator extends JavaGenerator{ //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, getTypeEntry("string"), isReference); + writeAttribute(fw, attrName, relationName, base.getTypeEntry("string"), isReference); } } - //private void writeAttribute(FileWriter fw, String attrName, String relationName, String binding, boolean isReference) { private void writeAttribute(FileWriter fw, String attrName, String relationName, TypeEntry binding, boolean isReference) { fw.writer.println(" {"); fw.writer.println(" if (attribute.getPredicate().equals("+relationName+")) {"); @@ -501,7 +499,7 @@ public class ExporterGenerator extends JavaGenerator{ } @Override - protected void handleAttributes(SchemaObject simpleTypeObj) { + public void handleAttributes(SchemaObject simpleTypeObj) { SchemaObject parent = simpleTypeObj.getParent(); FileWriter fw = getWriter(parent); @@ -513,7 +511,7 @@ public class ExporterGenerator extends JavaGenerator{ //String binding = getBindingFromPrimitiveType(base); - TypeEntry binding = getTypeEntry(base); + TypeEntry binding = this.base.getTypeEntry(base); fw.writer.println(" @Override"); fw.writer.println(" public void characters(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException {"); @@ -523,10 +521,10 @@ public class ExporterGenerator extends JavaGenerator{ } @Override - protected void handle(SchemaObject parent, AttributeGroup attribute) { + public void handle(SchemaObject parent, AttributeGroup attribute) { if (parent != null) { FileWriter fw = getWriter(parent); - NamedAttributeGroup group = getAttributeGroup(attribute.getRef().getLocalPart()); + NamedAttributeGroup group = this.base.getAttributeGroup(attribute.getRef().getLocalPart()); fw.writer.println(commentTag+" AttributeGroup " + group.getName()); SchemaObject obj = new SchemaObject(parent,attribute); for (Annotated annotated : group.getAttributeOrAttributeGroup()) { @@ -544,10 +542,10 @@ public class ExporterGenerator extends JavaGenerator{ } @Override - protected void handleAttributeComposition(SchemaObject parent, AttributeComposition composition, BijectionMap attributes) { + public void handleAttributeComposition(SchemaObject parent, AttributeComposition composition, BijectionMap attributes) { FileWriter fw = getWriter(parent); - QName type = new QName(CONVERSION_NS, composition.getType()); - TypeEntry typeEntry = getTypeEntry(type); + QName type = new QName(SchemaConversionBase.CONVERSION_NS, composition.getType()); + TypeEntry typeEntry = this.base.getTypeEntry(type); String arrayBinding = typeEntry.binding;//getBindingFromPrimitiveType(type); String javaType = typeEntry.javaType;//getJavaTypeFromPrimitiveType(type); String name = composition.getName(); @@ -596,7 +594,7 @@ public class ExporterGenerator extends JavaGenerator{ } protected String getDefaultValue(QName atype) { - Map types = typeMap.get(atype.getNamespaceURI()); + Map types = this.base.typeMap.get(atype.getNamespaceURI()); if (types == null) return null; TypeEntry entry = types.get(atype.getLocalPart()); @@ -605,14 +603,9 @@ public class ExporterGenerator extends JavaGenerator{ return entry.defaultValue; } - - - - - IDProvider provider; @Override - protected void handleElement(SchemaObject elementObj) { + public void handleElement(SchemaObject elementObj) { Element element = elementObj.getElement(); String name = getName(elementObj);//topLevelElement.getName(); @@ -627,11 +620,11 @@ public class ExporterGenerator extends JavaGenerator{ writers.put(elementObj, fw); boolean isList = false; - Inheritance inheritance = getInheritance(elementObj); + Inheritance inheritance = this.base.getInheritance(elementObj); - provider = getIDProvider(element); - List references = getIDReferences(element); - UnrecognizedChildElement unknownChildElement = getUnknown(element); + provider = this.base.getIDProvider(element); + List references = this.base.getIDReferences(element); + UnrecognizedChildElement unknownChildElement = this.base.getUnknown(element); // List intrerfaces = new ArrayList(); // if (references.size() > 0) @@ -674,8 +667,8 @@ public class ExporterGenerator extends JavaGenerator{ fw.writer.println(" "+getOntologyImport()); fw.writer.println(" for (Statement attribute : attributes) {"); if (complexType != null) { - SchemaObject obj = complexTypes.get(complexType); - handleElementComplexTypeAttributes(obj); + SchemaObject obj = this.base.complexTypes.get(complexType); + this.base.handleElementComplexTypeAttributes(obj); } fw.writer.println(" }"); fw.writer.println(" }"); @@ -724,8 +717,8 @@ public class ExporterGenerator extends JavaGenerator{ fw.writer.println(" super(graph);"); fw.writer.println(" "+getOntologyImport()); if (complexType != null) { - SchemaObject obj = complexTypes.get(complexType); - handleComplexTypeExtension(obj); + SchemaObject obj = this.base.complexTypes.get(complexType); + this.base.handleComplexTypeExtension(obj); } else if (simpleType != null) { } 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 80359ba..2e5fb27 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 @@ -6,16 +6,16 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; -import java.util.Map; 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.SchemaConversionBase.TypeEntry; import org.simantics.xml.sax.SchemaObject.ObjectType; import org.simantics.xml.sax.configuration.AttributeComposition; -import org.simantics.xml.sax.configuration.Configuration; import org.simantics.xml.sax.configuration.IDProvider; import org.simantics.xml.sax.configuration.IDReference; import org.simantics.xml.sax.configuration.UnrecognizedChildElement; @@ -27,29 +27,18 @@ import org.w3._2001.xmlschema.Element; import org.w3._2001.xmlschema.LocalComplexType; import org.w3._2001.xmlschema.LocalSimpleType; import org.w3._2001.xmlschema.NamedAttributeGroup; +import org.w3._2001.xmlschema.NamedGroup; import org.w3._2001.xmlschema.Restriction; -import org.w3._2001.xmlschema.Schema; import org.w3._2001.xmlschema.SimpleType; -import org.w3._2001.xmlschema.TopLevelAttribute; public class ImporterGenerator extends JavaGenerator{ - public ImporterGenerator(Configuration configuration) { - super(configuration); + public ImporterGenerator(SchemaConverter converter, SchemaConversionBase base) { + super(converter, base); } - public void createParser(Schema schema,String ontologyUri, String className, SchemaConverter converter) throws IOException { - this.schema = schema; - this.ontologyClassName = className; - this.converter = converter; + public void createParser() throws IOException { - - String packageParts[] = className.split("\\."); - String name = packageParts[packageParts.length-1]; - - - ontShort = name.substring(0, 3).toUpperCase(); - ontShort +="."; String parserPackagePostfix = "_elem"; String importerClassPostfix = "Importer"; String parserClassPostfix = "Parser"; @@ -59,7 +48,7 @@ public class ImporterGenerator extends JavaGenerator{ if (!importParserDir.exists()) importParserDir.mkdirs(); - handle(schema); + base.handle(this); // Create Importer class File importerFile = new File(converter.getParserDir().getAbsolutePath()+"/"+name+importerClassPostfix+".java"); PrintWriter mainWriter = createFile(importerFile); @@ -103,16 +92,18 @@ public class ImporterGenerator extends JavaGenerator{ mainWriter.flush(); mainWriter.close(); } - - protected void handle(TopLevelAttribute topLevelAttribute) { + + @Override + public void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { } @Override - protected void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { + public void handle(SchemaObject parent, NamedGroup attribute) { + // TODO Auto-generated method stub } @Override - protected void handleComplexType(SchemaObject complexTypeObj) { + public void handleComplexType(SchemaObject complexTypeObj) { ComplexType topLevelComplexType = complexTypeObj.getComplexType(); String name = getName(complexTypeObj); @@ -132,11 +123,11 @@ public class ImporterGenerator extends JavaGenerator{ } writers.put(complexTypeObj, fw); - Inheritance inheritance = getInheritance(complexTypeObj); + Inheritance inheritance = this.base.getInheritance(complexTypeObj); - provider = getIDProvider(topLevelComplexType); - List references = getIDReferences(topLevelComplexType); - UnrecognizedChildElement unknownChildElement = getUnknown(topLevelComplexType); + provider = this.base.getIDProvider(topLevelComplexType); + List references = this.base.getIDReferences(topLevelComplexType); + UnrecognizedChildElement unknownChildElement = this.base.getUnknown(topLevelComplexType); List intrerfaces = new ArrayList(); if (references.size() > 0) @@ -175,7 +166,7 @@ public class ImporterGenerator extends JavaGenerator{ fw.writer.println(" public " + className + "() {"); fw.writer.println(" super();"); - handleComplexTypeExtension(complexTypeObj); + this.base.handleComplexTypeExtension(complexTypeObj); fw.writer.println(" }"); @@ -212,8 +203,8 @@ public class ImporterGenerator extends JavaGenerator{ } fw.writer.println(" "+getOntologyImport()); - handleComplexTypeAttributes(complexTypeObj); - handleExtensionAttributes(complexTypeObj); + this.base.handleComplexTypeAttributes(complexTypeObj); + this.base.handleExtensionAttributes(complexTypeObj); fw.writer.println(" }"); @@ -238,7 +229,7 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected void createReferenceIndicator(SchemaObject parent, RefType referenceType, String refName, String objectName, String primaryClassName, String secondaryClassName, boolean useElementList, boolean useOriginalList) { + public void createReferenceIndicator(SchemaObject parent, RefType referenceType, String refName, String objectName, String primaryClassName, String secondaryClassName, boolean useElementList, boolean useOriginalList) { FileWriter fw = getWriter(parent); if (referenceType == RefType.Type) { // create internal class for handling the element and child attachment @@ -287,6 +278,7 @@ public class ImporterGenerator extends JavaGenerator{ fw.delayedWriter.println(" }"); } + @Override protected void createPrimitiveIndicator(SchemaObject parent, String refName, String binding) { FileWriter fw = getWriter(parent); fw.writer.println(" addParser(\""+ refName +"\", "+getName(parent) +"_" +refName+".class);"); @@ -298,6 +290,7 @@ public class ImporterGenerator extends JavaGenerator{ fw.delayedWriter2.println(" }"); } + @Override protected void createElementIndicator(SchemaObject parent, boolean useElementList, String refName, String className, boolean useOriginalList) { FileWriter fw = getWriter(parent); //if (!reference) @@ -339,7 +332,7 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any) { + public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any) { // generates overriding method that allows parsing any element FileWriter fw = getWriter(parent); fw.delayedWriter2.println(" @Override"); @@ -353,7 +346,9 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected void handle(SchemaObject parent, Attribute attribute) { + public void handle(SchemaObject parent, Attribute attribute) { + if (parent == null) + return; String name = attribute.getName(); QName primitiveType = attribute.getType(); SimpleType simpleType = attribute.getSimpleType(); @@ -371,7 +366,7 @@ public class ImporterGenerator extends JavaGenerator{ attrName = ref.getLocalPart(); relationName = ontShort+getName(parent)+"_has"+ref.getLocalPart(); - Attribute referred = getRefAttribute(ref); + Attribute referred = this.base.getRefAttribute(ref); if (referred != null) { primitiveType = referred.getType(); simpleType = referred.getSimpleType(); @@ -388,14 +383,14 @@ public class ImporterGenerator extends JavaGenerator{ if (primitiveType != null) { //String binding = getBindingFromPrimitiveType(primitiveType); - TypeEntry binding = getTypeEntry(primitiveType); + TypeEntry binding = this.base.getTypeEntry(primitiveType); if (binding != null) { writeAttribute(fw, attrName, relationName, binding, isReference); return; } else { if (simpleType == null) { - SchemaObject simpleTypeObj = simpleTypeName.get(primitiveType.getLocalPart()); + SchemaObject simpleTypeObj = this.base.simpleTypeName.get(primitiveType.getLocalPart()); if (simpleTypeObj != null) simpleType = simpleTypeObj.getSimpleType(); } @@ -404,7 +399,7 @@ public class ImporterGenerator extends JavaGenerator{ if (simpleType != null) { org.w3._2001.xmlschema.List list = simpleType.getList(); if (list != null) { - TypeEntry binding = getTypeEntry(new QName(SCHEMA_NS, "string")); + TypeEntry binding = this.base.getTypeEntry(new QName(SchemaConversionBase.SCHEMA_NS, "string")); writeAttribute(fw, attrName, relationName, binding, isReference); } else { Restriction restriction = simpleType.getRestriction(); @@ -414,7 +409,7 @@ public class ImporterGenerator extends JavaGenerator{ //String binding = getBindingFromPrimitiveType(base); - TypeEntry binding = getTypeEntry(base); + TypeEntry binding = this.base.getTypeEntry(base); writeAttribute(fw, attrName, relationName, binding, isReference); } } else { @@ -422,11 +417,10 @@ public class ImporterGenerator extends JavaGenerator{ //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, getTypeEntry("string"), isReference); + writeAttribute(fw, attrName, relationName, this.base.getTypeEntry("string"), isReference); } } - //private void writeAttribute(FileWriter fw, String attrName, String relationName, String binding, boolean isReference) { private void writeAttribute(FileWriter fw, String attrName, String relationName, TypeEntry binding, boolean isReference) { fw.writer.println(" {"); fw.writer.println(" Attribute a = element.getAttribute(\"" +attrName+"\");"); @@ -439,7 +433,7 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected void handleAttributes(SchemaObject simpleTypeObj) { + public void handleAttributes(SchemaObject simpleTypeObj) { SchemaObject parent = simpleTypeObj.getParent(); FileWriter fw = getWriter(parent); @@ -451,7 +445,7 @@ public class ImporterGenerator extends JavaGenerator{ //String binding = getBindingFromPrimitiveType(base); - TypeEntry binding = getTypeEntry(base); + TypeEntry binding = this.base.getTypeEntry(base); fw.writer.println(" @Override"); fw.writer.println(" public void configure(WriteGraph graph, ParserElement element, java.lang.String value) throws DatabaseException {"); //fw.writer.println(" graph.claimValue(element.getData(),"+getValueGetter(binding)+", Bindings."+binding+");"); @@ -461,10 +455,10 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected void handle(SchemaObject parent, AttributeGroup attribute) { + public void handle(SchemaObject parent, AttributeGroup attribute) { if (parent != null) { FileWriter fw = getWriter(parent); - NamedAttributeGroup group = getAttributeGroup(attribute.getRef().getLocalPart()); + NamedAttributeGroup group = this.base.getAttributeGroup(attribute.getRef().getLocalPart()); fw.writer.println(commentTag+" AttributeGroup " + group.getName()); SchemaObject obj = new SchemaObject(parent,attribute); for (Annotated annotated : group.getAttributeOrAttributeGroup()) { @@ -482,11 +476,11 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected void handleAttributeComposition(SchemaObject parent, AttributeComposition composition, BijectionMap attributes) { + public void handleAttributeComposition(SchemaObject parent, AttributeComposition composition, BijectionMap attributes) { FileWriter fw = getWriter(parent); - QName type = new QName(CONVERSION_NS, composition.getType()); - String arrayBinding = getBindingFromPrimitiveType(type); - String javaType = getJavaTypeFromPrimitiveType(type); + QName type = new QName(SchemaConversionBase.CONVERSION_NS, composition.getType()); + String arrayBinding = this.base.getBindingFromPrimitiveType(type); + String javaType = this.base.getJavaTypeFromPrimitiveType(type); String name = composition.getName(); String relationName; @@ -508,13 +502,13 @@ public class ImporterGenerator extends JavaGenerator{ for (org.simantics.xml.sax.configuration.Attribute a : composition.getAttribute()) { Attribute attribute = ((Attribute)attributes.getRight(a)); //QName atype = getBaseType(attribute); - QName atype = getPrimitiveType(attribute); + QName atype = this.base.getPrimitiveType(attribute); String defaultValue = attribute.getDefault(); if (defaultValue == null) - defaultValue = getDefaultValue(atype); + defaultValue = this.base.getDefaultValue(atype); //String binding = getBindingFromPrimitiveType(atype); - TypeEntry binding = getTypeEntry(atype); + TypeEntry binding = this.base.getTypeEntry(atype); if (i > 0) fw.writer.print(","); if (defaultValue != null) @@ -528,15 +522,7 @@ public class ImporterGenerator extends JavaGenerator{ } - protected String getDefaultValue(QName atype) { - Map types = typeMap.get(atype.getNamespaceURI()); - if (types == null) - return null; - TypeEntry entry = types.get(atype.getLocalPart()); - if (entry == null) - return null; - return entry.defaultValue; - } + @@ -544,7 +530,7 @@ public class ImporterGenerator extends JavaGenerator{ IDProvider provider; @Override - protected void handleElement(SchemaObject elementObj) { + public void handleElement(SchemaObject elementObj) { Element element = elementObj.getElement(); String name = getName(elementObj);//topLevelElement.getName(); @@ -559,11 +545,11 @@ public class ImporterGenerator extends JavaGenerator{ writers.put(elementObj, fw); boolean isList = false; - Inheritance inheritance = getInheritance(elementObj); + Inheritance inheritance = this.base.getInheritance(elementObj); - provider = getIDProvider(element); - List references = getIDReferences(element); - UnrecognizedChildElement unknownChildElement = getUnknown(element); + provider = this.base.getIDProvider(element); + List references = this.base.getIDReferences(element); + UnrecognizedChildElement unknownChildElement = this.base.getUnknown(element); List intrerfaces = new ArrayList(); if (references.size() > 0) @@ -607,8 +593,8 @@ public class ImporterGenerator extends JavaGenerator{ LocalSimpleType simpleType = element.getSimpleType(); if (complexType != null) { - SchemaObject obj = complexTypes.get(complexType); - handleElementComplexTypeAttributes(obj); + SchemaObject obj = this.base.complexTypes.get(complexType); + this.base.handleElementComplexTypeAttributes(obj); } fw.writer.println(" }"); @@ -634,8 +620,8 @@ public class ImporterGenerator extends JavaGenerator{ fw.writer.println(" super();"); if (complexType != null) { - SchemaObject obj = complexTypes.get(complexType); - handleComplexTypeExtension(obj); + SchemaObject obj = this.base.complexTypes.get(complexType); + this.base.handleComplexTypeExtension(obj); } else if (simpleType != null) { } @@ -742,7 +728,7 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected String getBaseClass(ObjectType type) { + public String getBaseClass(ObjectType type) { return "org.simantics.xml.sax.base.XMLElementNamedChildParserBase"; } 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 dfcdfd3..126dcf4 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,30 +10,44 @@ import java.util.Map; import javax.xml.namespace.QName; -import org.simantics.xml.sax.configuration.Configuration; +import org.simantics.xml.sax.SchemaConversionBase.RefType; +import org.simantics.xml.sax.SchemaConversionBase.TypeEntry; import org.w3._2001.xmlschema.AttributeGroupRef; import org.w3._2001.xmlschema.Element; import org.w3._2001.xmlschema.Schema; -public abstract class JavaGenerator extends SchemaConversionBase{ +//public abstract class JavaGenerator extends SchemaConversionBase{ +public abstract class JavaGenerator implements SchemaConversionComponent { String commentTag = "//"; Schema schema; String ontologyClassName; + String ontologyUri; SchemaConverter converter; + SchemaConversionBase base; List ruleClassNames = new ArrayList(); String ontShort = "ONT"; + String name; File importParserDir; String elementPackageName; Map writers = new HashMap(); - public JavaGenerator(Configuration configuration) { - super(configuration); + public JavaGenerator(SchemaConverter converter, SchemaConversionBase base) { + this.converter = converter; + this.base = base; + + this.schema = base.schema; + this.ontologyClassName = base.className; + this.ontologyUri = base.ontologyURI; + this.converter = converter; + this.name = converter.name; + ontShort = converter.shortName; + ontShort +="."; } @@ -59,41 +73,37 @@ public abstract class JavaGenerator extends SchemaConversionBase{ return name; return binding.getValueGetter(name); } + protected String getValueGetter(TypeEntry binding) { if (binding == null) return "value"; return binding.getValueGetter(); } + @Override public String getComplexTypePrefix() { return "ComplexTypes_"; } + @Override public String getAttributeGroupPrefix() { return "AttributeGroups_"; } - + @Override - protected void handle(SchemaObject parent, SchemaElement indicator, List elements) { - if (indicator.getType() == SchemaElement.ElementType.SEQUENCE || (indicator.getType() == SchemaElement.ElementType.CHOICE && indicator.getRestriction().many())) { - for (SchemaElement e : elements) { - handle(parent, indicator, e); - } - } else if (indicator.getType() == SchemaElement.ElementType.CHOICE) { - String name = getChoiceName(elements); + 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); - } + 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); } } @@ -121,7 +131,7 @@ public abstract class JavaGenerator extends SchemaConversionBase{ return null; } - + @Override public String getName(SchemaObject obj) { if (obj.getParent() == null) { switch (obj.getType()) { @@ -224,12 +234,13 @@ public abstract class JavaGenerator extends SchemaConversionBase{ writer.println(); } + 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 createElementIndicator(SchemaObject parent, boolean useElementList, String refName, String className, boolean useOriginalList); @Override - protected 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 objectName; if (referenceType != RefType.Element) { QName refType; @@ -245,10 +256,10 @@ public abstract class JavaGenerator extends SchemaConversionBase{ objectName = refType.getLocalPart(); } - String binding = getBindingFromPrimitiveType(refType); + String binding = this.base.getBindingFromPrimitiveType(refType); if (binding == null) { - SchemaObject refElement = elementName.get(refType.getLocalPart()); - SchemaObject refComplexType = complexTypeName.get(refType.getLocalPart()); + SchemaObject refElement = this.base.elementName.get(refType.getLocalPart()); + SchemaObject refComplexType = this.base.complexTypeName.get(refType.getLocalPart()); // prefer element reference over complex type reference String primaryClassName = null; @@ -263,22 +274,22 @@ public abstract class JavaGenerator extends SchemaConversionBase{ } else { secondaryClassName = getName(refElement); } - boolean useElementList = useElementList(parent, indicator,element, referenceType == RefType.Reference, refName, refType); - boolean useOriginalList = useOriginalList(parent, indicator,element, referenceType == RefType.Reference, refName, refType); + 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); createReferenceIndicator(parent, referenceType, refName, objectName, primaryClassName, secondaryClassName, useElementList, useOriginalList); } else { createPrimitiveIndicator(parent, refName, binding); } } else { Element attrs= element.getElement(); - SchemaObject obj = getWithObj(parent, attrs); + SchemaObject obj = this.base.getWithObj(parent, attrs); String className = getName(obj); if (refName == null) refName = attrs.getName(); - boolean useElementList = useElementList(parent, indicator,element, false, refName, new QName(obj.getName())); - boolean useOriginalList = useOriginalList(parent, indicator,element, false, refName, new QName(obj.getName())); + boolean useElementList = this.base.useElementList(parent, indicator,element, false, refName, new QName(obj.getName())); + boolean useOriginalList = this.base.useOriginalList(parent, indicator,element, false, refName, new QName(obj.getName())); createElementIndicator(parent, useElementList, refName, className, useOriginalList); } } diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/ManualSchemaFileImport.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/ManualSchemaFileImport.java new file mode 100644 index 0000000..a0dfb1d --- /dev/null +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/ManualSchemaFileImport.java @@ -0,0 +1,10 @@ +package org.simantics.xml.sax; + +import java.io.File; +import java.io.IOException; + +public interface ManualSchemaFileImport { + + + public File getFileForLocation(String location) throws IOException; +} 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 741e197..f98a85f 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 @@ -11,9 +11,11 @@ import java.util.Set; 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; -import org.simantics.xml.sax.configuration.Configuration; import org.simantics.xml.sax.configuration.IDReference; import org.w3._2001.xmlschema.Annotated; import org.w3._2001.xmlschema.Attribute; @@ -22,15 +24,21 @@ import org.w3._2001.xmlschema.Element; import org.w3._2001.xmlschema.LocalComplexType; import org.w3._2001.xmlschema.LocalSimpleType; import org.w3._2001.xmlschema.NamedAttributeGroup; +import org.w3._2001.xmlschema.NamedGroup; import org.w3._2001.xmlschema.Restriction; import org.w3._2001.xmlschema.Schema; import org.w3._2001.xmlschema.SimpleType; -import org.w3._2001.xmlschema.TopLevelAttribute; -public class OntologyGenerator extends SchemaConversionBase { +//public class OntologyGenerator extends SchemaConversionBase { +public class OntologyGenerator implements SchemaConversionComponent { + SchemaConversionBase base; - public OntologyGenerator(Configuration configuration) { - super(configuration); + public OntologyGenerator(SchemaConverter converter, SchemaConversionBase base) { + this.base = base; + this.converter = converter; + this.schema = base.schema; + this.ontologyUri = base.ontologyURI; + this.className = base.className; } String ontRoot = "ONT."; @@ -44,30 +52,7 @@ public class OntologyGenerator extends SchemaConversionBase { PrintWriter writer = null; - - - - - public void createOntology(Schema schema, String ontologyName, String className, SchemaConverter converter) throws FileNotFoundException { - this.schema = schema; - this.converter = converter; - this.ontologyUri = ontologyName; - this.className = className; - -// for (OpenAttrs attrs : schema.getIncludeOrImportOrRedefine()) { -// if (attrs instanceof Annotation) { -// Annotation ann = (Annotation)attrs; -// for (Object o : ann.getAppinfoOrDocumentation()) { -// if (o instanceof Documentation) { -// Documentation doc = (Documentation)o; -// } else if (o instanceof Appinfo) { -// Appinfo info = (Appinfo)o; -// } -// } -// } -// } - - + public void createOntology() throws FileNotFoundException { StringWriter stringWriter = null; if (converter.getOntologyFile() == null) { stringWriter = new StringWriter(); @@ -76,7 +61,7 @@ public class OntologyGenerator extends SchemaConversionBase { writer = new PrintWriter(converter.getOntologyFile()); } - handle(schema); + handle(); writer.flush(); writer.close(); @@ -84,10 +69,8 @@ public class OntologyGenerator extends SchemaConversionBase { System.out.println(stringWriter.toString()); } - protected void handle(Schema schema) { - String parts[] = ontologyUri.split("/"); - String name = parts[parts.length-1]; - ontRoot = name.substring(0, Math.min(3, name.length())).toUpperCase(); + protected void handle() { + ontRoot = converter.shortName; for (String s : converter.getHeader()) { writer.println(commentTag + " " + s); @@ -109,18 +92,18 @@ public class OntologyGenerator extends SchemaConversionBase { writer.println(commentTag + " Interpreted from schema"); writer.println(); - super.handle(schema); + base.handle(this); } protected String getType(QName qtype) { - String ontType = getL0TypeFromPrimitiveType(qtype); + String ontType = base.getL0TypeFromPrimitiveType(qtype); if (ontType != null) return ontType; - else if (isComplexTypeRef(qtype.getLocalPart())) + else if (base.isComplexTypeRef(qtype.getLocalPart())) return ontRoot+getComplexTypePrefix()+qtype.getLocalPart(); - else if (isSimpleTypeRef(qtype.getLocalPart())) + else if (base.isSimpleTypeRef(qtype.getLocalPart())) return ontRoot+qtype.getLocalPart(); - else if (isElementRef(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(); @@ -128,19 +111,6 @@ public class OntologyGenerator extends SchemaConversionBase { throw new RuntimeException("Reference to unknown type " + qtype.getLocalPart()); } - @Override - protected void handle(TopLevelAttribute topLevelAttribute) { - super.handle(topLevelAttribute); - writer.println(); - } - - @Override - protected void handleSimpleType(SchemaObject topLevelSimpleType) { - super.handleSimpleType(topLevelSimpleType); - writer.println(); - } - - public String getComplexTypePrefix() { return "ComplexTypes."; } @@ -149,94 +119,85 @@ public class OntologyGenerator extends SchemaConversionBase { return "AttributeGroups."; } - @Override - protected void handle(SchemaObject parent, SchemaElement indicator, List elements) { - if (indicator.getType() == SchemaElement.ElementType.SEQUENCE || (indicator.getType() == SchemaElement.ElementType.CHOICE && indicator.getRestriction().many())) { - for (SchemaElement e : elements) { - handle(parent, indicator, e); - } - } else if (indicator.getType() == SchemaElement.ElementType.CHOICE) { - String name = getChoiceName(elements); - boolean single = true; - for (SchemaElement e : elements) { - if (e.getRestriction().many()) { - single = false; - break; - } + public void handleChoice(SchemaObject parent, SchemaElement indicator, java.util.List elements, String name) { + boolean single = true; + for (SchemaElement e : elements) { + if (e.getRestriction().many()) { + single = false; + break; } - String relationName = getName(parent)+".has"+name; - writer.print(relationName); + } + 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; - 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 = getL0TypeFromPrimitiveType(refType); - } else if (localElement.getRef() != null) { - refType = localElement.getRef(); - type = getL0TypeFromPrimitiveType(refType); - } - if (type == null) { - SchemaObject obj = getWithName(parent, refType.getLocalPart()); - types.add(getName(obj,"has")); - } + if (localElement.getName() != null) { + refType = localElement.getType(); + type = base.getL0TypeFromPrimitiveType(refType); + } else if (localElement.getRef() != null) { + refType = localElement.getRef(); + type = base.getL0TypeFromPrimitiveType(refType); } - if (types.size() > 0) { - for (String type : types) { - writer.print(" 0) { + for (String type : types) { + writer.print(" " + type); - } + } else { + writer.print(" " + type); + } + } + if (!single) { + writer.println(ontRoot+name+ "List " + ontRoot+name+"List"); + writer.println(" : L0.FunctionalRelation"); } + writer.println(" --> " + ontRoot+name+"List"); } - - } + }; @Override - protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType) { + public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType) { if (refType != refType.Element) { QName referenceType = null; if (refType == RefType.Type) { referenceType = element.getElement().getType(); //refName = element.getElement().getName() - SchemaObject eObj = elements.get(element.getElement()); + SchemaObject eObj = base.elements.get(element.getElement()); if (refName == null) refName = eObj.getName(); } else { @@ -244,10 +205,10 @@ public class OntologyGenerator extends SchemaConversionBase { if (refName == null) refName = referenceType.getLocalPart(); } - String type = getL0TypeFromPrimitiveType(referenceType); + String type = base.getL0TypeFromPrimitiveType(referenceType); SchemaObject obj = null; if (type == null) { - obj = getWithName(parent, referenceType.getLocalPart()); + obj = base.getWithName(parent, referenceType.getLocalPart()); writer.println(getName(parent)+".has"+refName + " " + getName(obj)); @@ -256,7 +217,7 @@ public class OntologyGenerator extends SchemaConversionBase { writer.println(" --> " + getType(referenceType)); } - if (useElementList(parent, indicator,element, refType == RefType.Reference, refName, referenceType)) { + if (base.useElementList(parent, indicator,element, refType == RefType.Reference, refName, referenceType)) { if (type == null) { writer.println(getName(parent)+"."+refName + "List " + getName(obj)); - if (useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) { + if (base.useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) { writer.println(getName(parent)+"."+refName + "List element = (JAXBElement)facetWrap; @@ -348,7 +315,7 @@ public class OntologyGenerator extends SchemaConversionBase { } @Override - protected void handleAttributes(SchemaObject simpleTypeObj) { + public void handleAttributes(SchemaObject simpleTypeObj) { // SchemaObject parent = simpleTypeObj.getParent(); // SimpleType simpleType = simpleTypeObj.getSimpleType(); // Restriction restriction = simpleType.getRestriction(); @@ -357,7 +324,7 @@ public class OntologyGenerator extends SchemaConversionBase { } @Override - protected void handle(SchemaObject parent, AttributeGroup attributeGroup) { + public void handle(SchemaObject parent, AttributeGroup attributeGroup) { if (parent == null) { NamedAttributeGroup group = (NamedAttributeGroup)attributeGroup; writer.println(ontRoot+getAttributeGroupPrefix()+group.getName()+ " attributes) { + public void handleAttributeComposition(SchemaObject parent, AttributeComposition composition, BijectionMap attributes) { Attribute compositionAttribute = new Attribute(); compositionAttribute.setName(composition.getName()); - QName type = new QName(CONVERSION_NS, composition.getType()); + QName type = new QName(SchemaConversionBase.CONVERSION_NS, composition.getType()); compositionAttribute.setType(type); handle(parent, compositionAttribute); } @Override - protected void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { + public void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { SimpleType simpleType = simpleTypeObj.getSimpleType(); String name = simpleType.getName(); @@ -398,27 +366,75 @@ public class OntologyGenerator extends SchemaConversionBase { relationName = getName(parent)+".has"+name; writer.println(relationName+ " " + ontType); } else { - Restriction restriction = simpleType.getRestriction(); - if (restriction == null || simpleType.getUnion() != null || simpleType.getId() != null) - throw new RuntimeException(simpleType.getName() + " restriction error"); - QName base = restriction.getBase(); - String relationName = ontRoot+"has"+name; if (parent != null) relationName = getName(parent)+".has"+name; writer.println(relationName+ " " + ontType); + QName base = this.base.getSimpleTypeBase(simpleType); + Inheritance inheritance = new Inheritance(""); + this.base.getAtomicTypeInheritance(base, inheritance); + if (inheritance.atomicType == null) + throw new RuntimeException("Could not locate atomic type for SimpleType " + simpleType.getName()); + writer.println(" --> " + inheritance.atomicType.l0Type); + +// Restriction restriction = simpleType.getRestriction(); +// if (restriction != null) { +// +// QName base = restriction.getBase(); +// String ontType = getL0Type(base); +// writer.println(" --> " + ontType); +// } else if (simpleType.getId() != null) { +// throw new RuntimeException(simpleType.getName() + " restriction error"); +// } else if (simpleType.getUnion() != null) { +// Union union = simpleType.getUnion(); +// String ontType = null; +// if (union.getMemberTypes().size() > 0) { +// for (QName type : union.getMemberTypes()) { +// String sType = null; +// TypeEntry entry = getTypeEntry(type); +// if (entry == null) { +// SchemaObject obj = simpleTypeName.get(type.getLocalPart()); +// Inheritance inheritance = new Inheritance(""); +// getAtomicTypeInheritance(type, obj, inheritance); +// sType = inheritance.atomicType.l0Type; +// } else { +// sType = entry.l0Type; +// } +// if (ontType == null) +// ontType = sType; +// else if (!ontType.equals(sType)) +// throw new RuntimeException(simpleType.getName() + " union has incompatible member types"); +// } +// } else { +// if (union.getSimpleType().size() == 0) +// throw new RuntimeException(simpleType.getName() + " union error"); +// for (SimpleType s : union.getSimpleType()) { +// if (restriction == null) +// restriction = s.getRestriction(); +// else { +// Restriction r = s.getRestriction(); +// if (!r.getBase().equals(restriction.getBase())) +// throw new RuntimeException(simpleType.getName() + " union has incompatible restriction bases"); +// } +// } +// QName base = restriction.getBase(); +// ontType = getL0Type(base); +// } +// writer.println(" --> " + ontType); +// } else { +// throw new RuntimeException(simpleType.getName() + " restriction error"); +// } + } } @Override - protected void handleComplexType(SchemaObject topLevelComplexType) { + public void handleComplexType(SchemaObject topLevelComplexType) { String name = getName(topLevelComplexType); // if (topLevelComplexType.getName().equals("Reference")) // System.out.println(); @@ -440,7 +456,7 @@ public class OntologyGenerator extends SchemaConversionBase { // if (base != null) { // baseType = getType(base); // } - Inheritance inheritance = getInheritance(topLevelComplexType); + Inheritance inheritance = base.getInheritance(topLevelComplexType); // writer.println(name+ " references = getIDReferences(element); + List references = this.base.getIDReferences(element); for (IDReference ref : references) { writer.println(name+"."+ref.getReference().getName()+ " > typeMap; - public SchemaConversionBase(Configuration configuration) { - this.configuration = configuration; - typeMap = new HashMap>(); + protected String ontologyURI; + protected String className; + + public SchemaConversionBase(SchemaConverter converter, String ontologyUri, String className) { + this.converter = converter; + this.configuration = converter.getConfiguration(); + this.ontologyURI = ontologyUri; + this.className = className; + initTypes(); + } + + protected void initTypes() { + typeMap = new HashMap>(); Map schemaTypes = new HashMap(); typeMap.put(SCHEMA_NS, schemaTypes); Map l0Types = new HashMap(); @@ -69,6 +84,8 @@ public abstract class SchemaConversionBase { schemaTypes.put("token", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); schemaTypes.put("ID", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","",true)); schemaTypes.put("IDREF", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); + schemaTypes.put("Name", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); + schemaTypes.put("NCName", new TypeEntry("L0.String", "Bindings.STRING", "java.lang.String", "","","","","")); schemaTypes.put("date", new TypeEntry("XML.Date", "org.simantics.xml.sax.base.datatypes.literal.Date.BINDING", "org.simantics.xml.sax.base.datatypes.literal.Date", "","org.simantics.xml.sax.base.datatypes.literal.Date.parseDate(",")","(",").toString()")); schemaTypes.put("time", new TypeEntry("XML.Time", "org.simantics.xml.sax.base.datatypes.literal.Time.BINDING", "org.simantics.xml.sax.base.datatypes.literal.Time", "","org.simantics.xml.sax.base.datatypes.literal.Time.parseTime(",")","(",").toString()")); schemaTypes.put("dateTime", new TypeEntry("XML.DateTime", "org.simantics.xml.sax.base.datatypes.literal.DateTime.BINDING", "org.simantics.xml.sax.base.datatypes.literal.DateTime", "","org.simantics.xml.sax.base.datatypes.literal.DateTime.parseDateTime(",")","(",").toString()")); @@ -92,13 +109,10 @@ public abstract class SchemaConversionBase { 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)")); - - 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,"","")); } - protected TypeEntry getTypeEntry(QName type) { Map types = typeMap.get(type.getNamespaceURI()); if (types == null) @@ -154,10 +168,14 @@ public abstract class SchemaConversionBase { } - protected void handle(Schema schema) { + public void init(Schema schema) { this.schema = schema; - preload(); + preload(); + } + + public void handle(SchemaConversionComponent component) { + this.component = component; for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { if (attrs instanceof TopLevelAttribute) { handle((TopLevelAttribute)attrs); @@ -170,18 +188,22 @@ public abstract class SchemaConversionBase { handleSimpleType(simpleTypes.get(attrs)); } else if (attrs instanceof NamedAttributeGroup) { handle((NamedAttributeGroup)attrs); + } else if (attrs instanceof NamedGroup) { + handle((NamedGroup)attrs); } else { System.out.println(attrs.getClass().getName()); } } } - protected Map elementName = new HashMap(); - protected Map complexTypeName = new HashMap(); - protected Map simpleTypeName = new HashMap(); - protected Map elements = new HashMap(); - protected Map complexTypes = new HashMap(); - protected Map simpleTypes = new HashMap(); + protected Map elementName = new HashMap<>(); + protected Map complexTypeName = new HashMap<>(); + protected Map simpleTypeName = new HashMap<>(); + protected Map modelGroupName = new HashMap<>(); + protected Map elements = new HashMap<>(); + protected Map complexTypes = new HashMap<>(); + protected Map simpleTypes = new HashMap<>(); + protected Map modelGroups = new HashMap<>(); protected SchemaObject getWithName(SchemaObject referrer, String name) { @@ -196,6 +218,18 @@ public abstract class SchemaConversionBase { return obj; } + protected SchemaObject getWithName(String name) { + SchemaObject obj = elementName.get(name); + if (obj == null) + obj = complexTypeName.get(name); + if (obj == null) + obj = simpleTypeName.get(name); + if (obj == null) { + throw new RuntimeException("Cannot locate referred type " + name); + } + return obj; + } + protected SchemaObject getWithObj(SchemaObject referrer, OpenAttrs attrs) { SchemaObject obj = null; if (attrs instanceof Element) @@ -228,12 +262,23 @@ public abstract class SchemaConversionBase { SimpleType simpleType = (SimpleType)attrs; SchemaObject obj = new SchemaObject(simpleType); stack.push(obj); + } else if (attrs instanceof Attribute) { + // Attributes are not cached + } else if (attrs instanceof AttributeGroup) { + // Attribute groups are not cached + } else if (attrs instanceof NamedGroup) { + NamedGroup group = (NamedGroup)attrs; + SchemaObject obj = new SchemaObject(group); + stack.push(obj); + } else { + System.out.println(attrs.getClass().getName()); } } while (!stack.isEmpty()) { SchemaObject object = stack.pop(); - if (object.getType() == ObjectType.COMPLEX_TYPE) { + switch (object.getType()) { + case COMPLEX_TYPE:{ ComplexType ct = object.getComplexType(); if (ct.getName() != null && ct.getName().length() > 0 && ct instanceof TopLevelComplexType) complexTypeName.put(ct.getName(), object); @@ -283,7 +328,9 @@ public abstract class SchemaConversionBase { throw new RuntimeException("Groups not supported"); } } - } else if (object.getType() == ObjectType.ELEMENT) { + break; + } + case ELEMENT:{ Element e = object.getElement(); if (e instanceof TopLevelElement) elementName.put(e.getName(), object); @@ -292,13 +339,23 @@ public abstract class SchemaConversionBase { stack.push(new SchemaObject(object,e.getComplexType())); if (e.getSimpleType() != null) stack.push(new SchemaObject(object,e.getSimpleType())); - } else if (object.getType() == ObjectType.SIMPLE_TYPE) { + break; + } + case SIMPLE_TYPE:{ SimpleType e = object.getSimpleType(); if (e instanceof TopLevelSimpleType) simpleTypeName.put(e.getName(), object); simpleTypes.put(e, object); + break; + } + case MODEL_GROUP:{ + NamedGroup e = object.getModelGroup(); + modelGroupName.put(e.getName(), object); + modelGroups.put(e, object); + break; } - } + } + } // while } private void preload(SchemaObject parent,ExplicitGroup eg, Deque stack) { @@ -310,10 +367,10 @@ public abstract class SchemaConversionBase { SchemaObject obj = new SchemaObject(parent,(Element)elemValue); obj.setRename(getRename((Element)elemValue)); stack.add(obj); - } else if (elemValue instanceof All) { - preload(parent,(All)elemValue, stack); } else if (elemValue instanceof ExplicitGroup) { preload(parent,(ExplicitGroup)elemValue, stack); + } else if (elemValue instanceof RealGroup) { + preload(parent,(RealGroup)elemValue, stack); } else { throw new RuntimeException("Unknown ExplicitGroup element " + elemValue.getClass().getName()); } @@ -325,6 +382,15 @@ public abstract class SchemaConversionBase { } } + private void preload(SchemaObject parent, RealGroup eg, Deque stack) { + System.out.println(eg); + if (eg instanceof NamedGroup) { + SchemaObject obj = new SchemaObject(parent,(NamedGroup)eg); + stack.add(obj); + } + } + + protected void handle(TopLevelAttribute topLevelAttribute) { handle(null, topLevelAttribute); } @@ -337,6 +403,10 @@ public abstract class SchemaConversionBase { handle(null, namedAttributeGroup); } + protected void handle(NamedGroup namedAttributeGroup){ + handle(null, namedAttributeGroup); + } + protected QName getComplexTypeBase(ComplexType complexType) { if (complexType == null) return null; @@ -352,9 +422,63 @@ public abstract class SchemaConversionBase { } protected QName getSimpleTypeBase(SimpleType simpleType) { - if (simpleType == null) - return null; - return simpleType.getRestriction().getBase(); +// if (simpleType == null) +// return null; +// return simpleType.getRestriction().getBase(); + + Restriction restriction = simpleType.getRestriction(); + if (restriction != null) { + QName base = restriction.getBase(); + return base; + } else if (simpleType.getId() != null) { + throw new RuntimeException(simpleType.getName() + " restriction error"); + } else if (simpleType.getUnion() != null) { + Union union = simpleType.getUnion(); + if (union.getMemberTypes().size() > 0) { + QName base = null; + for (QName type : union.getMemberTypes()) { + QName sType = null; + TypeEntry entry = getTypeEntry(type); + if (entry == null) { + SchemaObject obj = simpleTypeName.get(type.getLocalPart()); + if (obj == null) + throw new RuntimeException(simpleType.getName() + " union has unresolved reference " + type.getLocalPart()); + sType = getSimpleTypeBase(obj.getSimpleType()); + } else { + sType = type; + } + if (base == null) + base = sType; + else if (!base.equals(sType)) { + //FIXME : throw new RuntimeException(simpleType.getName() + " union has incompatible member types"); + // fall back to string. + base = new QName(SCHEMA_NS, "string"); + + } + } + return base; + } else { + if (union.getSimpleType().size() == 0) + throw new RuntimeException(simpleType.getName() + " union error"); + for (SimpleType s : union.getSimpleType()) { + if (restriction == null) + restriction = s.getRestriction(); + else { + Restriction r = s.getRestriction(); + if (!r.getBase().equals(restriction.getBase())) + throw new RuntimeException(simpleType.getName() + " union has incompatible restriction bases"); + } + } + QName base = restriction.getBase(); + return base; + } + } else if (simpleType.getList() != null) { + // FIXME: callers cannot get the information that we have a list. + org.w3._2001.xmlschema.List list = simpleType.getList(); + return list.getItemType(); + } else { + throw new RuntimeException(simpleType.getName() + " restriction error"); + } } protected QName getElementBase(Element element) { @@ -393,7 +517,9 @@ public abstract class SchemaConversionBase { } } - protected abstract void handleAttributes(SchemaObject simpleTypeObj); + protected void handleAttributes(SchemaObject simpleTypeObj) { + component.handleAttributes(simpleTypeObj); + } protected void handleExtensionAttributes(SchemaObject complexType) { ComplexContent complexContent = complexType.getComplexType().getComplexContent(); @@ -503,22 +629,51 @@ public abstract class SchemaConversionBase { return null; } - protected abstract void handleAttributeComposition(SchemaObject obj, AttributeComposition composition, BijectionMap attributes); + //protected abstract void handleAttributeComposition(SchemaObject obj, AttributeComposition composition, BijectionMap attributes); + protected void handleAttributeComposition(SchemaObject obj, AttributeComposition composition, BijectionMap attributes) { + component.handleAttributeComposition(obj, composition, attributes); + } protected void handleComplexType(SchemaObject complexType) { - handleComplexTypeAttributes(complexType); - handleComplexTypeExtension(complexType); - handleExtensionAttributes(complexType); +// handleComplexTypeAttributes(complexType); +// handleComplexTypeExtension(complexType); +// handleExtensionAttributes(complexType); + component.handleComplexType(complexType); + } + + protected void handleElement(SchemaObject topLevelElement) { +// LocalComplexType complexType = topLevelElement.getElement().getComplexType(); +// +// if (complexType != null) { +// SchemaObject complextTypeObj = complexTypes.get(complexType); +// handleElementComplexTypeAttributes(complextTypeObj); +// handleComplexTypeExtension(complextTypeObj); +// } + component.handleElement(topLevelElement); } protected enum RefType{Element,Reference,Type}; - protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType); - protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any); - protected abstract void handle(SchemaObject parent, SchemaElement indicator, List elements); + 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 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())) { + for (SchemaElement e : elements) { + handle(parent, indicator, e); + } + } else if (indicator.getType() == SchemaElement.ElementType.CHOICE) { + String name = getChoiceName(elements); + component.handleChoice(parent, indicator, elements, name); + } + } protected void handle(SchemaObject parent, ExplicitGroup eg, SchemaElement.ElementType indicator) { handle(parent, new SchemaElement(eg, indicator)); @@ -532,6 +687,7 @@ public abstract class SchemaConversionBase { List sequences = new ArrayList(); List alls = new ArrayList(); List anys = new ArrayList(); + List groups = new ArrayList(); for (Object o : indicator.getGroup().getParticle()) { if (o instanceof JAXBElement) { @@ -549,6 +705,14 @@ public abstract class SchemaConversionBase { } else if ("sequence".equals(qname.getLocalPart())) { sequences.add(new SchemaElement(indicator,(ExplicitGroup)elemValue, ElementType.SEQUENCE)); } + } else if (elemValue instanceof RealGroup) { + if (elemValue instanceof GroupRef) { + groups.add(new SchemaElement(indicator,(GroupRef)elemValue, ElementType.GROUP_REF)); + } else if (elemValue instanceof NamedGroup) { + groups.add(new SchemaElement(indicator,(NamedGroup)elemValue, ElementType.NAMED_GROUP)); + } else { + throw new RuntimeException("Unknown ExplicitGroup element " + elemValue.getClass().getName()); + } } else { throw new RuntimeException("Unknown ExplicitGroup element " + elemValue.getClass().getName()); } @@ -559,7 +723,7 @@ public abstract class SchemaConversionBase { } } - if (elements.size() == 0 && choices.size() == 0 && sequences.size() == 0 && alls.size() == 0 && anys.size() == 0) { + if (elements.size() == 0 && choices.size() == 0 && sequences.size() == 0 && alls.size() == 0 && anys.size() == 0 && groups.size() == 0) { return; } @@ -576,6 +740,10 @@ public abstract class SchemaConversionBase { for (SchemaElement c : alls) { handle(parent, c); } + + for (SchemaElement c : groups) { + handle(parent, c); + } handle(parent, indicator, elements); for (SchemaElement a : anys) { handleIndicator(parent, indicator, a); @@ -590,9 +758,12 @@ public abstract class SchemaConversionBase { for (SchemaElement a : anys) { handleIndicator(parent, indicator, a); } + for (SchemaElement c : groups) { + handle(parent, c); + } } } else { - if (sequences.size() > 0 || choices.size() > 0 || alls.size() > 0) { + if (sequences.size() > 0 || choices.size() > 0 || alls.size() > 0 || groups.size() > 0) { throw new RuntimeException("Cannot handle Sequence with inner ExplicitGroups"); } handle(parent, indicator, elements); @@ -603,13 +774,13 @@ public abstract class SchemaConversionBase { } else if (indicator.getType() == SchemaElement.ElementType.CHOICE){ if (indicator.getRestriction().single()) { - if (sequences.size()> 0 || choices.size() > 0 || alls.size() > 0 || anys.size() > 0) { + if (sequences.size()> 0 || choices.size() > 0 || alls.size() > 0 || anys.size() > 0 || groups.size() > 0) { throw new RuntimeException("Cannot handle Choice that contains something else than Elements"); } handle(parent, indicator, elements); } else { - if (sequences.size() > 0 || choices.size() > 0) { + if (sequences.size() > 0 || choices.size() > 0 || alls.size() > 0 || groups.size() > 0) { throw new RuntimeException("Cannot handle Choice with inner ExplicitGroups"); } handle(parent, indicator, elements); @@ -618,7 +789,7 @@ public abstract class SchemaConversionBase { } } } else if (indicator.getType() == ElementType.ALL) { - if (sequences.size()> 0 || choices.size() > 0 || alls.size() > 0 || anys.size() > 0) { + if (sequences.size()> 0 || choices.size() > 0 || alls.size() > 0 || anys.size() > 0 || groups.size() > 0) { throw new RuntimeException("Cannot handle All that contains something else than Elements"); } if (!indicator.getRestriction().single()) { @@ -678,12 +849,20 @@ public abstract class SchemaConversionBase { } return name; } + + protected void handle(SchemaObject parent, Attribute attribute) { + component.handle(parent, attribute); + } + protected void handle(SchemaObject parent, AttributeGroup attribute) { + component.handle(parent, attribute); + } + protected void handle(SchemaObject parent, NamedGroup attribute){ + component.handle(parent, attribute); + }; - - protected abstract void handle(SchemaObject parent, Attribute attribute) ; - protected abstract void handle(SchemaObject parent, AttributeGroup attribute) ; - - protected abstract void handleSimpleType(SchemaObject parent, SchemaObject simpleType); + protected void handleSimpleType(SchemaObject parent, SchemaObject simpleType) { + component.handleSimpleType(parent, simpleType); + } @@ -723,32 +902,20 @@ public abstract class SchemaConversionBase { } } - protected void handleElement(SchemaObject topLevelElement) { - LocalComplexType complexType = topLevelElement.getElement().getComplexType(); - - if (complexType != null) { - SchemaObject complextTypeObj = complexTypes.get(complexType); - handleElementComplexTypeAttributes(complextTypeObj); - handleComplexTypeExtension(complextTypeObj); - } - - - } - - protected boolean isElementRef(String ref) { + public boolean isElementRef(String ref) { return elementName.containsKey(ref); } - protected boolean isComplexTypeRef(String ref) { + public boolean isComplexTypeRef(String ref) { return complexTypeName.containsKey(ref); } - protected boolean isSimpleTypeRef(String ref) { + public boolean isSimpleTypeRef(String ref) { return simpleTypeName.containsKey(ref); } - protected NamedAttributeGroup getAttributeGroup(String name) { + public NamedAttributeGroup getAttributeGroup(String name) { for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { if (attrs instanceof NamedAttributeGroup) { NamedAttributeGroup group = (NamedAttributeGroup)attrs; @@ -759,7 +926,7 @@ public abstract class SchemaConversionBase { return null; } - protected IDProvider getIDProvider(Element element) { + public IDProvider getIDProvider(Element element) { List idProviders = new ArrayList(2); for (JAXBElement e : configuration.getConversionRule()) { if (e.getValue() instanceof IDProvider) { @@ -779,7 +946,7 @@ public abstract class SchemaConversionBase { return idProviders.get(0); } - protected IDProvider getIDProvider(ComplexType complexType) { + public IDProvider getIDProvider(ComplexType complexType) { List idProviders = new ArrayList(2); for (JAXBElement e : configuration.getConversionRule()) { if (e.getValue() instanceof IDProvider) { @@ -799,7 +966,7 @@ public abstract class SchemaConversionBase { return idProviders.get(0); } - protected List getIDReferences(Element element) { + public List getIDReferences(Element element) { List idReferences = new ArrayList(2); for (JAXBElement e : configuration.getConversionRule()) { if (e.getValue() instanceof IDReference) { @@ -814,7 +981,7 @@ public abstract class SchemaConversionBase { return idReferences; } - protected List getIDReferences(ComplexType complexType) { + public List getIDReferences(ComplexType complexType) { List idReferences = new ArrayList(2); for (JAXBElement e : configuration.getConversionRule()) { if (e.getValue() instanceof IDReference) { @@ -1070,17 +1237,22 @@ public abstract class SchemaConversionBase { } } - public abstract String getComplexTypePrefix(); - - public abstract String getAttributeGroupPrefix(); - - public abstract String getName(SchemaObject obj); - - protected abstract String getBaseClass(ObjectType type); + public String getComplexTypePrefix() { + return component.getComplexTypePrefix(); + } + public String getAttributeGroupPrefix() { + return component.getAttributeGroupPrefix(); + } + public String getName(SchemaObject obj) { + return component.getName(obj); + } + public String getBaseClass(ObjectType type) { + return component.getBaseClass(type); + } - protected Inheritance getInheritance(SchemaObject topLevelObj) { + public Inheritance getInheritance(SchemaObject topLevelObj) { Inheritance inheritance = null; if (topLevelObj.getType() == ObjectType.ELEMENT) { Element topLevelElement = topLevelObj.getElement(); @@ -1151,7 +1323,7 @@ public abstract class SchemaConversionBase { ExtensionType extensionType = simpleContent.getExtension(); if (extensionType != null) { type = extensionType.getBase(); - getAtomicTypeInheritance(type, topLevelObj, inheritance); + getAtomicTypeInheritance(type, inheritance); } } } @@ -1164,14 +1336,14 @@ public abstract class SchemaConversionBase { * @param topLevelObj * @param inheritance */ - protected void getAtomicTypeInheritance(QName type, SchemaObject topLevelObj, Inheritance inheritance) { + public void getAtomicTypeInheritance(QName type, Inheritance inheritance) { if (!type.getNamespaceURI().equals(SCHEMA_NS)) { - SchemaObject obj = getWithName(topLevelObj, type.getLocalPart()); + SchemaObject obj = getWithName(type.getLocalPart()); if (obj.getType() != ObjectType.SIMPLE_TYPE) throw new RuntimeException("SimpleContent does not use SimpleType definition"); SimpleType simpleType = obj.getSimpleType(); type = getSimpleTypeBase(simpleType); - getAtomicTypeInheritance(type, topLevelObj, inheritance); + getAtomicTypeInheritance(type, inheritance); } else { TypeEntry entry = getTypeEntry(type); if (entry != null) { @@ -1180,5 +1352,15 @@ public abstract class SchemaConversionBase { } } } + + public String getDefaultValue(QName atype) { + Map types = typeMap.get(atype.getNamespaceURI()); + if (types == null) + return null; + TypeEntry entry = types.get(atype.getLocalPart()); + if (entry == null) + return null; + return entry.defaultValue; + } } 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 new file mode 100644 index 0000000..89d8e86 --- /dev/null +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionComponent.java @@ -0,0 +1,34 @@ +package org.simantics.xml.sax; + +import java.util.List; + +import org.simantics.utils.datastructures.BijectionMap; +import org.simantics.xml.sax.SchemaConversionBase.RefType; +import org.simantics.xml.sax.SchemaObject.ObjectType; +import org.simantics.xml.sax.configuration.AttributeComposition; +import org.w3._2001.xmlschema.Annotated; +import org.w3._2001.xmlschema.Attribute; +import org.w3._2001.xmlschema.AttributeGroup; +import org.w3._2001.xmlschema.NamedGroup; + + +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 any); + //void handle(SchemaObject parent, SchemaElement indicator, List elements); + void 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 getComplexTypePrefix(); + String getAttributeGroupPrefix(); + String getName(SchemaObject obj); + String getBaseClass(ObjectType type); +} 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 3b2c1a0..c716754 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 @@ -2,9 +2,14 @@ package org.simantics.xml.sax; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; @@ -12,6 +17,10 @@ import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import org.simantics.xml.sax.configuration.Configuration; +import org.w3._2001.xmlschema.Annotation; +import org.w3._2001.xmlschema.Import; +import org.w3._2001.xmlschema.Include; +import org.w3._2001.xmlschema.OpenAttrs; import org.w3._2001.xmlschema.Schema; /** @@ -22,6 +31,7 @@ import org.w3._2001.xmlschema.Schema; */ public class SchemaConverter { + File outputPlugin; File schemaFile; File conversionFile; File ontologyFile; @@ -37,10 +47,29 @@ public class SchemaConverter { boolean createImporter = true; boolean createExporter = true; + private List parent = new ArrayList<>(); + private List subConverters = new ArrayList<>(); + private Map nameMap; + String ontologyUri; + String className; + String name; + String shortName; - - public void convertSchema(File schemaFile, File conversionFile, File outputPlugin) throws JAXBException, IOException { + SchemaConversionBase base; + + private ManualSchemaFileImport fileImport; + + public SchemaConverter(File schemaFile, File conversionFile, File outputPlugin) throws IOException { + this(null,schemaFile,conversionFile,outputPlugin); + } + + public SchemaConverter(SchemaConverter parent,File schemaFile, File conversionFile, File outputPlugin) throws IOException { + + this.outputPlugin = outputPlugin; + this.schemaFile = schemaFile; + this.conversionFile = conversionFile; + pluginName = outputPlugin.getName(); String packageParts[] = pluginName.split("\\."); String outputLoc = outputPlugin.getAbsolutePath(); @@ -51,21 +80,23 @@ public class SchemaConverter { outputGraph += "/graph"; outputGraph += "/" + schemaFile.getName().substring(0, schemaFile.getName().length()-4) +".pgraph"; - this.schemaFile = schemaFile; - this.conversionFile = conversionFile; + this.ontologyFile = new File(outputGraph); this.parserDir = new File(outputLoc); - if (!ontologyFile.exists()) { - ontologyFile.getParentFile().mkdirs(); - ontologyFile.createNewFile(); + if (parent != null) { + this.parent.add(parent); + parent.subConverters.add(this); + } else { + nameMap = new HashMap<>(); } - if (!parserDir.exists()) - parserDir.mkdirs(); - - convert(); - + getRoot().nameMap.put(schemaFile.getAbsolutePath(), this); + } + + public void setFileImport(ManualSchemaFileImport fileImport) { + this.fileImport = fileImport; } + public void setCreateExporter(boolean createExporter) { this.createExporter = createExporter; @@ -79,7 +110,47 @@ public class SchemaConverter { this.createPGraph = createPGraph; } - private void convert() throws JAXBException, IOException { + protected SchemaConverter createSubConverter(String location) throws JAXBException, IOException { + File directory = schemaFile.getParentFile(); + File schemaFile = new File(directory.getAbsolutePath()+File.separator+location); + if (!schemaFile.exists()) { + if (getRoot().fileImport != null) { + schemaFile = getRoot().fileImport.getFileForLocation(location); + } + if (!schemaFile.exists()) + throw new FileNotFoundException(schemaFile.getAbsolutePath()); + } + SchemaConverter subConverter = getRoot().nameMap.get((schemaFile.getAbsolutePath())); + if (subConverter == null) { + subConverter = new SchemaConverter(this,schemaFile, conversionFile, outputPlugin); + subConverter.createPGraph = this.createPGraph; + subConverter.createImporter = this.createImporter; + subConverter.createExporter = this.createExporter; + } else { + subConverter.parent.add(this); + } + return subConverter; + } + + protected SchemaConverter getRoot() { + SchemaConverter s = this; + while (s.parent.size() > 0) + s = s.parent.get(0); + return s; + } + + public void convert() throws JAXBException, IOException { + + init(); + doConvert(); + } + + boolean init = false; + + protected void init() throws IOException, JAXBException { + if (init) + return; + init = true; JAXBContext jc = JAXBContext.newInstance("org.w3._2001.xmlschema"); Unmarshaller u = jc.createUnmarshaller(); //u.setSchema(schema); @@ -102,7 +173,7 @@ public class SchemaConverter { header[3] = "Date " + new Date().toString(); - String ontologyUri = schema.getTargetNamespace(); + ontologyUri = schema.getTargetNamespace(); if (ontologyUri == null) { ontologyUri = getSchemaFile().getName(); @@ -112,7 +183,7 @@ public class SchemaConverter { } ontologyUri = ontologyUri.replaceAll(" ", "_"); String parts[] = ontologyUri.split("/"); - String name = parts[parts.length-1]; + name = parts[parts.length-1]; name = name.replaceAll("\\.", "_"); if (!ontologyUri.startsWith("http://")) ontologyUri = "http://" + ontologyUri.replaceAll("/", "_"); @@ -125,20 +196,56 @@ public class SchemaConverter { ontologyUri +="-"+ version; - String className = getPluginName() + "." + name; + className = getPluginName() + "." + name; + shortName = name.substring(0, 3).toUpperCase(); + + + for (OpenAttrs attrs : schema.getIncludeOrImportOrRedefine()) { + if (attrs instanceof Import) { + Import imp = (Import)attrs; + String location = imp.getSchemaLocation(); + SchemaConverter sc = createSubConverter(location); + sc.init(); + } else if (attrs instanceof Include) { + Include inc = (Include)attrs; + String location = inc.getSchemaLocation(); + SchemaConverter sc = createSubConverter(location); + sc.init(); + } else if (attrs instanceof Annotation) { + + } else { + throw new IOException("Cannot handle schema file " + schemaFile.getName() + ", the schema uses redefine elements."); + } + } + } + + protected void doConvert() throws IOException, JAXBException { + if (!ontologyFile.exists()) { + ontologyFile.getParentFile().mkdirs(); + ontologyFile.createNewFile(); + } + if (!parserDir.exists()) + parserDir.mkdirs(); + + for (SchemaConverter sc : subConverters) + sc.doConvert(); + + base = new SchemaConversionBase(this,ontologyUri,className); + base.init(schema); if (createPGraph) { - OntologyGenerator ontologyGenerator = new OntologyGenerator(configuration); - ontologyGenerator.createOntology(schema, ontologyUri, className, this); + OntologyGenerator ontologyGenerator = new OntologyGenerator(this,base); + ontologyGenerator.createOntology(); } if (createImporter) { - ImporterGenerator importerGenerator = new ImporterGenerator(configuration); - importerGenerator.createParser(schema, ontologyUri, className, this); + ImporterGenerator importerGenerator = new ImporterGenerator(this,base); + importerGenerator.createParser(); } if (createExporter) { - ExporterGenerator exporterGenerator = new ExporterGenerator(configuration); - exporterGenerator.createParser(schema, ontologyUri,className, this); + ExporterGenerator exporterGenerator = new ExporterGenerator(this,base); + exporterGenerator.createParser(); } + base.component = null; } public File getOntologyFile() { diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaElement.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaElement.java index dc43781..070c496 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaElement.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaElement.java @@ -6,7 +6,7 @@ import org.w3._2001.xmlschema.Group; public class SchemaElement { - public enum ElementType{CHOICE,SEQUENCE,ALL,ANY,ELEMENT} + public enum ElementType{CHOICE,SEQUENCE,ALL,ANY,ELEMENT,NAMED_GROUP, GROUP_REF} private Group gelement; private Any aelement; diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaObject.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaObject.java index c081b81..4138222 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaObject.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaObject.java @@ -6,11 +6,12 @@ import org.w3._2001.xmlschema.AttributeGroupRef; import org.w3._2001.xmlschema.ComplexType; import org.w3._2001.xmlschema.Element; import org.w3._2001.xmlschema.NamedAttributeGroup; +import org.w3._2001.xmlschema.NamedGroup; import org.w3._2001.xmlschema.OpenAttrs; import org.w3._2001.xmlschema.SimpleType; public class SchemaObject { - enum ObjectType{ELEMENT,COMPLEX_TYPE,SIMPLE_TYPE,ATTRIBUTE_GROUP}; + enum ObjectType{ELEMENT,COMPLEX_TYPE,SIMPLE_TYPE,ATTRIBUTE_GROUP,MODEL_GROUP}; private SchemaObject parent; private ObjectType type; @@ -33,6 +34,10 @@ public class SchemaObject { this(null, simpleType); } + public SchemaObject(NamedGroup namedGroup) { + this(null, namedGroup); + } + public SchemaObject(SchemaObject parent, Element element) { this.parent = parent; this.obj = element; @@ -51,6 +56,12 @@ public class SchemaObject { this.type = ObjectType.ATTRIBUTE_GROUP; } + public SchemaObject(SchemaObject parent, NamedGroup namedGroup) { + this.parent = parent; + this.obj = namedGroup; + this.type = ObjectType.MODEL_GROUP; + } + public SchemaObject(SchemaObject parent, SimpleType simpleType) { this.parent = parent; @@ -82,6 +93,12 @@ public class SchemaObject { return (AttributeGroup)obj; } + public NamedGroup getModelGroup() { + if (type != ObjectType.MODEL_GROUP) + return null; + return (NamedGroup)obj; + } + public SchemaObject getParent() { return parent; } -- 2.45.2