X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FImporterGenerator.java;h=3d137cf33b7357cba1decc999bc3e32852c419ea;hb=f11cbe76b3f4be142c9f84ef9a7b6bc9dcc8ff23;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..3d137cf 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,52 +5,40 @@ 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.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.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.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 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"; @@ -60,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); @@ -104,17 +92,26 @@ public class ImporterGenerator extends JavaGenerator{ mainWriter.flush(); mainWriter.close(); } - - protected void handle(TopLevelAttribute topLevelAttribute) { - + + @Override + 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);//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,21 +123,11 @@ public class ImporterGenerator extends JavaGenerator{ } writers.put(complexTypeObj, fw); - boolean isList = false; - - String baseClass = "org.simantics.xml.sax.base.XMLElementNamedChildParserBase"; - - boolean inherited = false; + Inheritance inheritance = this.base.getInheritance(complexTypeObj); - QName type = getComplexTypeBase(topLevelComplexType); - if (type != null && !type.getNamespaceURI().equals("http://www.w3.org/2001/XMLSchema")) { - baseClass = getName(complexTypeName.get(type.getLocalPart())); - inherited = true; - } - - 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) @@ -148,8 +135,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 +144,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(); @@ -183,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(" }"); @@ -194,9 +177,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,16 +198,24 @@ 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()); - handleComplexTypeAttributes(complexTypeObj); - handleExtensionAttributes(complexTypeObj); + this.base.handleComplexTypeAttributes(complexTypeObj); + this.base.handleExtensionAttributes(complexTypeObj); 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 +229,91 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, boolean reference, String ref, QName refType) { + public 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 + " " + 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+"\");"); @@ -453,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); @@ -465,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+");"); @@ -475,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()); fw.writer.println(commentTag+" AttributeGroup " + group.getName()); SchemaObject obj = new SchemaObject(parent,attribute); for (Annotated annotated : group.getAttributeOrAttributeGroup()) { @@ -490,17 +470,20 @@ public class ImporterGenerator extends JavaGenerator{ handle(obj,(AttributeGroup)annotated); } } + //dd + if(true) + System.out.println(); fw.writer.println(commentTag+" End of AttributeGroup " + group.getName()); } } @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; @@ -522,13 +505,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) @@ -542,26 +525,15 @@ 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; - } - @Override - protected void handleSimpleType(SchemaObject parent, SchemaObject simpleType) { - - } + + IDProvider provider; @Override - protected void handleElement(SchemaObject elementObj) { + public void handleElement(SchemaObject elementObj) { Element element = elementObj.getElement(); String name = getName(elementObj);//topLevelElement.getName(); @@ -576,11 +548,11 @@ public class ImporterGenerator extends JavaGenerator{ writers.put(elementObj, fw); boolean isList = false; - Inheritance inhertiance = getElementInheritance(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) @@ -589,7 +561,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 +587,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()); @@ -624,16 +596,16 @@ public class ImporterGenerator extends JavaGenerator{ LocalSimpleType simpleType = element.getSimpleType(); if (complexType != null) { - SchemaObject obj = complexTypes.get(complexType); - handleElementComplexTypeAttributes(obj); + SchemaObject obj = this.base.getComplexType(complexType); + this.base.handleElementComplexTypeAttributes(obj); } 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) { @@ -651,8 +623,8 @@ public class ImporterGenerator extends JavaGenerator{ fw.writer.println(" super();"); if (complexType != null) { - SchemaObject obj = complexTypes.get(complexType); - handleComplexTypeExtension(obj); + SchemaObject obj = this.base.getComplexType(complexType); + this.base.handleComplexTypeExtension(obj); } else if (simpleType != null) { } @@ -666,7 +638,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 +731,7 @@ public class ImporterGenerator extends JavaGenerator{ } @Override - protected String getBaseClass() { + public String getBaseClass(ObjectType type) { return "org.simantics.xml.sax.base.XMLElementNamedChildParserBase"; }