X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FImporterGenerator.java;h=80359ba895c6ea25269785a1907c6f96996141bd;hb=fb01c21839aea801224aeb82b64bd261120619d5;hp=cff6e923c9aa668dc6ae73e467366d9bd0238860;hpb=dd3b2c7ecd5f4b60734f2602b16637aa8be2a263;p=simantics%2Finterop.git diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java index cff6e92..80359ba 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java @@ -5,29 +5,28 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; -import java.util.HashMap; 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.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.IDProvider; import org.simantics.xml.sax.configuration.IDReference; -import org.simantics.xml.sax.configuration.Rename; import org.simantics.xml.sax.configuration.UnrecognizedChildElement; import org.w3._2001.xmlschema.Annotated; import org.w3._2001.xmlschema.Attribute; import org.w3._2001.xmlschema.AttributeGroup; -import org.w3._2001.xmlschema.AttributeGroupRef; import org.w3._2001.xmlschema.ComplexType; 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.OpenAttrs; import org.w3._2001.xmlschema.Restriction; import org.w3._2001.xmlschema.Schema; import org.w3._2001.xmlschema.SimpleType; @@ -39,7 +38,7 @@ public class ImporterGenerator extends JavaGenerator{ super(configuration); } - public void createParser(Schema schema,String className, SchemaConverter converter) throws IOException { + public void createParser(Schema schema,String ontologyUri, String className, SchemaConverter converter) throws IOException { this.schema = schema; this.ontologyClassName = className; this.converter = converter; @@ -106,15 +105,22 @@ public class ImporterGenerator extends JavaGenerator{ } protected void handle(TopLevelAttribute topLevelAttribute) { - } + @Override + protected void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { + } @Override protected void handleComplexType(SchemaObject complexTypeObj) { ComplexType topLevelComplexType = complexTypeObj.getComplexType(); - String name = getName(complexTypeObj);//topLevelComplexType.getName(); + String name = getName(complexTypeObj); + + //if (topLevelComplexType.getName().equals("LocalizedText")) + //if (topLevelComplexType.getName().equals("Reference")) +// if (topLevelComplexType.getName().equals("NodeIdAlias")) +// System.out.println(); String className = name;//"_"+name; @@ -126,17 +132,7 @@ public class ImporterGenerator extends JavaGenerator{ } writers.put(complexTypeObj, fw); - boolean isList = false; - - String baseClass = "org.simantics.xml.sax.base.XMLElementNamedChildParserBase"; - - boolean inherited = false; - - QName type = getComplexTypeBase(topLevelComplexType); - if (type != null && !type.getNamespaceURI().equals("http://www.w3.org/2001/XMLSchema")) { - baseClass = getName(complexTypeName.get(type.getLocalPart())); - inherited = true; - } + Inheritance inheritance = getInheritance(complexTypeObj); provider = getIDProvider(topLevelComplexType); List references = getIDReferences(topLevelComplexType); @@ -148,8 +144,8 @@ public class ImporterGenerator extends JavaGenerator{ if (unknownChildElement != null) intrerfaces.add("org.simantics.xml.sax.base.UnrecognizedElementParser"); - createClassHeader(fw.writer, isList); - writeClass(fw.writer,false, null, className, baseClass, intrerfaces); + createClassHeader(fw.writer, false); + writeClass(fw.writer,false, null, className, inheritance.baseClass, intrerfaces); writeIDProvider(fw.writer); @@ -157,12 +153,8 @@ public class ImporterGenerator extends JavaGenerator{ fw.writer.println(" public Resource create(WriteGraph graph, ParserElement element) throws DatabaseException{"); fw.writer.println(" Layer0 L0 = Layer0.getInstance(graph);"); fw.writer.println(" "+getOntologyImport()); - if (!isList) { fw.writer.println(" Resource res = graph.newResource();"); fw.writer.println(" graph.claim(res, L0.InstanceOf, "+ontShort+name+");"); - } else { - fw.writer.println(" Resource res = ListUtils.create(graph, "+ontShort+name+", Collections.EMPTY_LIST);"); - } fw.writer.println(" return res;"); fw.writer.println(" }"); fw.writer.println(); @@ -194,9 +186,11 @@ public class ImporterGenerator extends JavaGenerator{ if (stringWriter.getBuffer().length() > 0) { fw.writer.write(stringWriter.toString()); } - - + if (inheritance.type == InheritanceType.ComplexType) { + fw.writer.println(" return super.connectChild(graph,element,child);"); + }else{ fw.writer.println(" return false;"); + } fw.writer.println(" }"); fw.writer.println(); @@ -213,7 +207,7 @@ public class ImporterGenerator extends JavaGenerator{ fw.writer.println(" @Override"); fw.writer.println(" public void configure(WriteGraph graph, Deque parents, ParserElement element) throws DatabaseException {"); - if (inherited) { + if (inheritance.type == InheritanceType.ComplexType) { fw.writer.println(" super.configure(graph,parents,element);"); } fw.writer.println(" "+getOntologyImport()); @@ -223,6 +217,14 @@ public class ImporterGenerator extends JavaGenerator{ fw.writer.println(" }"); + if (inheritance.type == InheritanceType.AtomicType) { + fw.writer.println(); + fw.writer.println(" @Override"); + fw.writer.println(" public void configure(WriteGraph graph, ParserElement element, java.lang.String string) throws DatabaseException {"); + fw.writer.println(" graph.claimValue(element.getData(), "+getValueGetter(inheritance.atomicType,"string")+", "+inheritance.atomicType.binding+");"); + fw.writer.println(" }"); + } + writeIDReferences(fw.writer,name, references); writeUnknownChild(fw.writer,name,unknownChildElement); @@ -236,104 +238,89 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String ref, QName refType) { + protected void createReferenceIndicator(SchemaObject parent, RefType referenceType, String refName, String objectName, String primaryClassName, String secondaryClassName, boolean useElementList, boolean useOriginalList) { FileWriter fw = getWriter(parent); - String binding = getBindingFromPrimitiveType(refType); - if (binding == null) { - SchemaObject refElement = elementName.get(refType.getLocalPart()); - SchemaObject refComplexType = complexTypeName.get(refType.getLocalPart()); + if (referenceType == RefType.Type) { + // create internal class for handling the element and child attachment + secondaryClassName = getName(parent) +"_" +objectName; + fw.writer.println(" addParser(\""+ objectName +"\", "+secondaryClassName+".class);"); + fw.delayedWriter2.println(" public static class " + secondaryClassName +" extends " + primaryClassName +"{"); + fw.delayedWriter2.println(" public "+ secondaryClassName +"(){"); + fw.delayedWriter2.println(" }"); + fw.delayedWriter2.println(" }"); + } else { // referenceType == RefType.Reference + fw.writer.println(" addParser("+primaryClassName+".class);"); + if (!primaryClassName.equals(secondaryClassName)) + fw.writer.println(" addParser("+secondaryClassName+".class);"); + } + + fw.delayedWriter.println(" if (child.getElementParser() instanceof "+secondaryClassName+"){"); + fw.delayedWriter.println(" graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+refName + ", child.getData());"); + if (useElementList) { - String className = null; - String refClassName = null; - if (refElement != null) - className = getName(refElement); - else - className = getName(refComplexType); - - if (refComplexType != null) { - refClassName = getName(refComplexType); - } else { - refClassName = getName(refElement); - } - - if (!reference) - fw.writer.println(" addParser(\""+ ref +"\", "+className+".class);"); - else - fw.writer.println(" addParser("+className+".class);"); - if (!className.equals(refClassName)) - fw.writer.println(" addParser("+refClassName+".class);"); - - fw.delayedWriter.println(" if (child.getElementParser() instanceof "+refClassName+"){"); - fw.delayedWriter.println(" graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+ref + ", child.getData());"); - if (useElementList(parent, indicator,element, reference, ref, refType)) { - - // element type specific list - fw.delayedWriter.println(" {"); - fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List);"); - fw.delayedWriter.println(" if (list == null) {"); - fw.delayedWriter.println(" list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));"); - fw.delayedWriter.println(" graph.claim(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List,list);"); - fw.delayedWriter.println(" } else {"); - fw.delayedWriter.println(" org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));"); - fw.delayedWriter.println(" }"); - fw.delayedWriter.println(" }"); - } - if (useOriginalList(parent, indicator,element, reference, ref, refType)) { - // generic list - fw.delayedWriter.println(" {"); - fw.delayedWriter.println(" XMLResource XML = XMLResource.getInstance(graph);"); - fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(), XML.hasOriginalElementList);"); - fw.delayedWriter.println(" if (list == null) {"); - fw.delayedWriter.println(" list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));"); - fw.delayedWriter.println(" graph.claim(element.getData(), XML.hasOriginalElementList,list);"); - fw.delayedWriter.println(" } else {"); - fw.delayedWriter.println(" org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));"); - fw.delayedWriter.println(" }"); - fw.delayedWriter.println(" }"); - } - - fw.delayedWriter.println(" return true;"); - fw.delayedWriter.println(" }"); - } else { - //writer.println(commentTag+ontShort+"."+parent+".has"+ref + " references = getIDReferences(element); @@ -589,7 +572,7 @@ public class ImporterGenerator extends JavaGenerator{ intrerfaces.add("org.simantics.xml.sax.base.UnrecognizedElementParser"); createClassHeader(fw.writer, isList); - writeClass(fw.writer,false, element.getName(), className, inhertiance.baseClass, intrerfaces); + writeClass(fw.writer,false, element.getName(), className, inheritance.baseClass, intrerfaces); writeIDProvider(fw.writer); fw.writer.println(" @Override"); fw.writer.println(" public Resource create(WriteGraph graph, ParserElement element) throws DatabaseException{"); @@ -615,7 +598,7 @@ public class ImporterGenerator extends JavaGenerator{ fw.writer.println(" @Override"); fw.writer.println(" public void configure(WriteGraph graph, Deque parents, ParserElement element) throws DatabaseException {"); - if (inhertiance.type == InheritanceType.ComplexType) { + if (inheritance.type == InheritanceType.ComplexType) { fw.writer.println(" super.configure(graph,parents,element);"); } fw.writer.println(" "+getOntologyImport()); @@ -629,11 +612,11 @@ public class ImporterGenerator extends JavaGenerator{ } fw.writer.println(" }"); - if (inhertiance.type == InheritanceType.AtomicType) { + if (inheritance.type == InheritanceType.AtomicType) { fw.writer.println(); fw.writer.println(" @Override"); fw.writer.println(" public void configure(WriteGraph graph, ParserElement element, java.lang.String string) throws DatabaseException {"); - fw.writer.println(" graph.claimValue(element.getData(), "+getValueGetter(inhertiance.atomicType,"string")+", "+inhertiance.atomicType.binding+");"); + fw.writer.println(" graph.claimValue(element.getData(), "+getValueGetter(inheritance.atomicType,"string")+", "+inheritance.atomicType.binding+");"); fw.writer.println(" }"); } // if (simpleType != null) { @@ -666,7 +649,7 @@ public class ImporterGenerator extends JavaGenerator{ if (stringWriter.getBuffer().length() > 0) { fw.writer.write(stringWriter.toString()); } - if (inhertiance.type == InheritanceType.ComplexType) { + if (inheritance.type == InheritanceType.ComplexType) { fw.writer.println(" return super.connectChild(graph,element,child);"); } else { fw.writer.println(" return false;"); @@ -759,7 +742,7 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected String getBaseClass() { + protected String getBaseClass(ObjectType type) { return "org.simantics.xml.sax.base.XMLElementNamedChildParserBase"; }