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=2e5fb272438b917481ab6fd67fe24d3aa76ea939;hb=d788a05e77b983199f0982a0e56629e7fc30c691;hp=55ff5ef7e52f32ab0b830e9efa5a8b6ad689d2c1;hpb=09fb8eafbc9046ead17f0529eebad0b0caf55e2b;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 55ff5ef..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,147 +229,110 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType referenceType) { + public void createReferenceIndicator(SchemaObject parent, RefType referenceType, String refName, String objectName, String primaryClassName, String secondaryClassName, boolean useElementList, boolean useOriginalList) { FileWriter fw = getWriter(parent); - String objectName; - if (referenceType != RefType.Element) { - QName refType; - if (referenceType == RefType.Type) { - refType = element.getElement().getType(); - if (refName == null) - refName = element.getElement().getName(); - objectName = element.getElement().getName(); - } else { - refType = element.getElement().getRef(); - if (refName == null) - refName = refType.getLocalPart(); - objectName = refType.getLocalPart(); - } - - String binding = getBindingFromPrimitiveType(refType); - if (binding == null) { - SchemaObject refElement = elementName.get(refType.getLocalPart()); - SchemaObject refComplexType = complexTypeName.get(refType.getLocalPart()); + if (referenceType == RefType.Type) { + // create internal class for handling the element and child attachment + secondaryClassName = getName(parent) +"_" +objectName; + fw.writer.println(" addParser(\""+ objectName +"\", "+secondaryClassName+".class);"); + fw.delayedWriter2.println(" public static class " + secondaryClassName +" extends " + primaryClassName +"{"); + fw.delayedWriter2.println(" public "+ secondaryClassName +"(){"); + fw.delayedWriter2.println(" }"); + fw.delayedWriter2.println(" }"); + } else { // referenceType == RefType.Reference + fw.writer.println(" addParser("+primaryClassName+".class);"); + if (!primaryClassName.equals(secondaryClassName)) + fw.writer.println(" addParser("+secondaryClassName+".class);"); + } + + fw.delayedWriter.println(" if (child.getElementParser() instanceof "+secondaryClassName+"){"); + fw.delayedWriter.println(" graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+refName + ", child.getData());"); + if (useElementList) { + + // element type specific list + fw.delayedWriter.println(" {"); + fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(),"+ontShort+getName(parent)+"_has"+refName + "List);"); + fw.delayedWriter.println(" if (list == null) {"); + fw.delayedWriter.println(" list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));"); + fw.delayedWriter.println(" graph.claim(element.getData(),"+ontShort+getName(parent)+"_has"+refName + "List,list);"); + fw.delayedWriter.println(" } else {"); + fw.delayedWriter.println(" org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));"); + fw.delayedWriter.println(" }"); + fw.delayedWriter.println(" }"); + } + if (useOriginalList) { + // generic list + fw.delayedWriter.println(" {"); + fw.delayedWriter.println(" XMLResource XML = XMLResource.getInstance(graph);"); + fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(), XML.hasOriginalElementList);"); + fw.delayedWriter.println(" if (list == null) {"); + fw.delayedWriter.println(" list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));"); + fw.delayedWriter.println(" graph.claim(element.getData(), XML.hasOriginalElementList,list);"); + fw.delayedWriter.println(" } else {"); + fw.delayedWriter.println(" org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));"); + fw.delayedWriter.println(" }"); + fw.delayedWriter.println(" }"); + } + + fw.delayedWriter.println(" return true;"); + fw.delayedWriter.println(" }"); + } - // prefer element reference over complex type reference - String primaryClassName = null; - String secondaryClassName = null; - if (refElement != null) - primaryClassName = getName(refElement); - else - primaryClassName = getName(refComplexType); - - if (refComplexType != null) { - secondaryClassName = getName(refComplexType); - } else { - secondaryClassName = getName(refElement); - } - - if (referenceType == RefType.Type) { - // create internal class for handling the element and child attachment - secondaryClassName = getName(parent) +"_" +objectName; - fw.writer.println(" addParser(\""+ objectName +"\", "+secondaryClassName+".class);"); - fw.delayedWriter2.println(" public static class " + secondaryClassName +" extends " + primaryClassName +"{"); - fw.delayedWriter2.println(" public "+ secondaryClassName +"(){"); - fw.delayedWriter2.println(" }"); - fw.delayedWriter2.println(" }"); - } else { // referenceType == RefType.Reference - fw.writer.println(" addParser("+primaryClassName+".class);"); - if (!primaryClassName.equals(secondaryClassName)) - fw.writer.println(" addParser("+secondaryClassName+".class);"); - } - - fw.delayedWriter.println(" if (child.getElementParser() instanceof "+secondaryClassName+"){"); - fw.delayedWriter.println(" graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+refName + ", child.getData());"); - if (useElementList(parent, indicator,element, referenceType == RefType.Reference, refName, refType)) { - - // element type specific list - fw.delayedWriter.println(" {"); - fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(),"+ontShort+getName(parent)+"_has"+refName + "List);"); - fw.delayedWriter.println(" if (list == null) {"); - fw.delayedWriter.println(" list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));"); - fw.delayedWriter.println(" graph.claim(element.getData(),"+ontShort+getName(parent)+"_has"+refName + "List,list);"); - fw.delayedWriter.println(" } else {"); - fw.delayedWriter.println(" org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));"); - fw.delayedWriter.println(" }"); - fw.delayedWriter.println(" }"); - } - if (useOriginalList(parent, indicator,element, referenceType == RefType.Reference, refName, refType)) { - // generic list - fw.delayedWriter.println(" {"); - fw.delayedWriter.println(" XMLResource XML = XMLResource.getInstance(graph);"); - fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(), XML.hasOriginalElementList);"); - fw.delayedWriter.println(" if (list == null) {"); - fw.delayedWriter.println(" list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));"); - fw.delayedWriter.println(" graph.claim(element.getData(), XML.hasOriginalElementList,list);"); - fw.delayedWriter.println(" } else {"); - fw.delayedWriter.println(" org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));"); - fw.delayedWriter.println(" }"); - fw.delayedWriter.println(" }"); - } - - fw.delayedWriter.println(" return true;"); - fw.delayedWriter.println(" }"); - } else { - //writer.println(commentTag+ontShort+"."+parent+".has"+ref + " " + 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+"\");"); @@ -478,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); @@ -490,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+");"); @@ -500,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()) { @@ -521,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; @@ -547,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) @@ -567,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; - } + @@ -583,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(); @@ -598,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) @@ -646,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(" }"); @@ -673,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) { } @@ -781,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"; }