package org.simantics.xml.sax; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; 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.IDProvider; import org.simantics.xml.sax.configuration.IDReference; 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.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.NamedGroup; import org.w3._2001.xmlschema.Restriction; import org.w3._2001.xmlschema.SimpleType; public class ImporterGenerator extends JavaGenerator{ public ImporterGenerator(SchemaConverter converter, SchemaConversionBase base) { super(converter, base); } public void createParser() throws IOException { String parserPackagePostfix = "_elem"; String importerClassPostfix = "Importer"; String parserClassPostfix = "Parser"; elementPackageName = name+parserPackagePostfix; importParserDir= new File(converter.getParserDir().getAbsolutePath()+"/"+elementPackageName); if (!importParserDir.exists()) importParserDir.mkdirs(); base.handle(this); // Create Importer class File importerFile = new File(converter.getParserDir().getAbsolutePath()+"/"+name+importerClassPostfix+".java"); PrintWriter mainWriter = createFile(importerFile); mainWriter.println("package " + converter.getPluginName() +";"); mainWriter.println(); mainWriter.println("import java.io.File;"); mainWriter.println("import org.simantics.db.Session;"); mainWriter.println("import org.simantics.xml.sax.base.AbstractImporter;"); mainWriter.println(); mainWriter.println("public class " + name + importerClassPostfix+" extends AbstractImporter {"); mainWriter.println(); mainWriter.println(" public " + name + importerClassPostfix+"(Session session, File file) {"); mainWriter.println(" super(session, file, new "+name + parserClassPostfix+"());"); mainWriter.println(" }"); mainWriter.println(); mainWriter.println("}"); mainWriter.println(); mainWriter.flush(); mainWriter.close(); // Create Parser class File parserFile = new File(converter.getParserDir().getAbsolutePath()+"/"+name+parserClassPostfix+".java"); mainWriter = createFile(parserFile); mainWriter.println("package " + converter.getPluginName() +";"); mainWriter.println(); mainWriter.println("import org.simantics.xml.sax.base.XMLParser;"); mainWriter.println(); mainWriter.println("public class " + name + parserClassPostfix+" extends XMLParser {"); mainWriter.println(); mainWriter.println(" public " + name + parserClassPostfix+"() {"); if (schema.getTargetNamespace() != null) mainWriter.println(" setSchemaURI(\""+schema.getTargetNamespace()+"\");"); for (String s : ruleClassNames) { mainWriter.println(" add(new "+s+"());"); } mainWriter.println(" }"); mainWriter.println("}"); mainWriter.println(); mainWriter.flush(); mainWriter.close(); } @Override public void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { } @Override public void handle(SchemaObject parent, NamedGroup attribute) { // TODO Auto-generated method stub } @Override public void handleComplexType(SchemaObject complexTypeObj) { ComplexType topLevelComplexType = complexTypeObj.getComplexType(); 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; FileWriter fw = new FileWriter(); try { fw.writer = createFile(new File(importParserDir.getAbsolutePath()+"/"+className+".java")); } catch (IOException e) { throw new RuntimeException(e); } writers.put(complexTypeObj, fw); Inheritance inheritance = this.base.getInheritance(complexTypeObj); 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) intrerfaces.add("org.simantics.xml.sax.base.IDReferenceParser"); if (unknownChildElement != null) intrerfaces.add("org.simantics.xml.sax.base.UnrecognizedElementParser"); createClassHeader(fw.writer, false); writeClass(fw.writer,false, null, className, inheritance.baseClass, intrerfaces); writeIDProvider(fw.writer); fw.writer.println(" @Override"); fw.writer.println(" public Resource create(WriteGraph graph, ParserElement element) throws DatabaseException{"); fw.writer.println(" Layer0 L0 = Layer0.getInstance(graph);"); fw.writer.println(" "+getOntologyImport()); fw.writer.println(" Resource res = graph.newResource();"); fw.writer.println(" graph.claim(res, L0.InstanceOf, "+ontShort+name+");"); fw.writer.println(" return res;"); fw.writer.println(" }"); fw.writer.println(); fw.writer.println(" @Override"); fw.writer.println(" public boolean connectParent(WriteGraph graph, ParserElement parent, ParserElement element) throws DatabaseException{"); fw.writer.println(" "+getOntologyImport()); fw.writer.println(" graph.claim(parent.getData(), "+this.ontShort+getName(complexTypeObj,"has")+", element.getData());"); fw.writer.println(" return true;"); fw.writer.println(" }"); fw.writer.println(); StringWriter stringWriter = new StringWriter(); fw.delayedWriter = new PrintWriter(stringWriter); StringWriter stringWriter2 = new StringWriter(); fw.delayedWriter2 = new PrintWriter(stringWriter2); fw.writer.println(" public " + className + "() {"); fw.writer.println(" super();"); this.base.handleComplexTypeExtension(complexTypeObj); fw.writer.println(" }"); fw.writer.println(" @Override"); fw.writer.println(" public boolean connectChild(WriteGraph graph, ParserElement element, ParserElement child) throws DatabaseException{"); fw.writer.println(" "+getOntologyImport()); 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(); if (stringWriter2.getBuffer().length() > 0) { fw.writer.write(stringWriter2.toString()); } stringWriter = null; fw.delayedWriter.close(); fw.delayedWriter=null; stringWriter2 = null; fw.delayedWriter2.close(); fw.delayedWriter2 = null; fw.writer.println(" @Override"); fw.writer.println(" public void configure(WriteGraph graph, Deque parents, ParserElement element) throws DatabaseException {"); if (inheritance.type == InheritanceType.ComplexType) { fw.writer.println(" super.configure(graph,parents,element);"); } fw.writer.println(" "+getOntologyImport()); fw.writer.println(" Layer0 L0 = Layer0.getInstance(graph);"); fw.writer.println(" XMLResource XML = XMLResource.getInstance(graph);"); 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); fw.writer.println("}"); fw.writer.println(); fw.writer.flush(); fw.writer.close(); fw.writer = null; writers.remove(complexTypeObj); provider = null; } @Override 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 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(" element.registerListChild("+ontShort+getName(parent)+"_has"+refName + "_List, child);"); // 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(" element.registerListChild(XML.hasOriginalElementList, child);"); // 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(" }"); } @Override protected void createPrimitiveIndicator(SchemaObject parent, String refName, TypeEntry typeEntry, QName typeName) { FileWriter fw = getWriter(parent); String className = getName(parent) +"_" +refName; fw.writer.println(" addParser(\"" + refName + "\", " + className + ".class);"); fw.delayedWriter2.println(" public static class " + className + " extends org.simantics.xml.sax.base.ValueElementParser {"); fw.delayedWriter2.println(" public "+ className +"(){"); fw.delayedWriter2.println(" super(\""+refName+"\"," +this.ontologyClassName+".URIs."+getName(parent) + "_has"+refName+", "+typeEntry.binding+");"); fw.delayedWriter2.println(" }\n"); fw.delayedWriter2.println(" @Override"); fw.delayedWriter2.println(" public void configure(WriteGraph graph, ParserElement element, java.lang.String value) throws DatabaseException {"); fw.delayedWriter2.println(" Layer0 L0 = Layer0.getInstance(graph);"); SchemaObject simpleType = base.getSimpleType(typeName); String ontologyClassName = converter.getOntologyClassName(typeName.getNamespaceURI()); if (ontologyClassName != null && simpleType != null) fw.delayedWriter2.println(" graph.claim(element.getData(), L0.InstanceOf, " + ontologyClassName + ".getInstance(graph)." + getName(simpleType, "") + ");"); else { if (typeEntry.l0Type.startsWith("XML")) fw.delayedWriter2.println(" XMLResource XML = XMLResource.getInstance(graph);"); fw.delayedWriter2.println(" graph.claim(element.getData(), L0.InstanceOf, " + typeEntry.l0Type + ");"); } fw.delayedWriter2.println(" graph.claimValue(element.getData(), " +getValueGetter(typeEntry) + ", " + typeEntry.binding + ");"); fw.delayedWriter2.println(" }"); fw.delayedWriter2.println(" }"); } @Override protected void createElementIndicator(SchemaObject parent, boolean useElementList, String refName, String className, boolean useOriginalList) { FileWriter fw = getWriter(parent); //if (!reference) fw.writer.println(" addParser(\""+ refName +"\", "+className+".class);"); // else // fw.writer.println(" addParser("+className+".class);"); fw.delayedWriter.println(" if (child.getElementParser() instanceof "+className+"){"); 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(" element.registerListChild(" + ontShort+getName(parent)+"_has"+refName + "_List, child);"); // 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(" element.registerListChild(XML.hasOriginalElementList, child);"); // 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(" }"); } @Override 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"); fw.delayedWriter2.println(" public Class getParser(java.util.Map parsers, ParserElement element, ParserElement child) {"); fw.delayedWriter2.println(" Class parserClass = super.getParser(parsers, element, child);"); fw.delayedWriter2.println(" if (parserClass != null) return parserClass;"); fw.delayedWriter2.println(" org.simantics.xml.sax.base.XMLElementParser parser = parsers.get(child.getQName());"); fw.delayedWriter2.println(" if (parser != null) return parser.getClass();"); fw.delayedWriter2.println(" return null;"); fw.delayedWriter2.println(" }"); } @Override public void handle(SchemaObject parent, Attribute attribute) { if (parent == null) return; String name = attribute.getName(); QName primitiveType = attribute.getType(); SimpleType simpleType = attribute.getSimpleType(); QName ref = attribute.getRef(); String relationName; String attrName; if (name != null) { attrName = name; relationName = ontShort+"has"+name; if (parent != null) relationName = ontShort+getName(parent)+"_has"+name; } else if (ref != null && parent != null) { attrName = ref.getLocalPart(); relationName = ontShort+getName(parent)+"_has"+ref.getLocalPart(); Attribute referred = this.base.getRefAttribute(ref); if (referred != null) { primitiveType = referred.getType(); simpleType = referred.getSimpleType(); } } else { throw new RuntimeException(); } boolean isReference = false; if (provider!= null && provider.getAttribute().getName().equals(attrName)) isReference = true; FileWriter fw = getWriter(parent); if (primitiveType != null) { //String binding = getBindingFromPrimitiveType(primitiveType); TypeEntry binding = this.base.getTypeEntry(primitiveType); if (binding != null) { writeAttribute(fw, attrName, relationName, binding, isReference, primitiveType); return; } else { if (simpleType == null) { SchemaObject simpleTypeObj = this.base.getSimpleType(primitiveType);//this.base.simpleTypeName.get(primitiveType.getLocalPart()); if (simpleTypeObj != null) simpleType = simpleTypeObj.getSimpleType(); } } } if (simpleType != null) { org.w3._2001.xmlschema.List list = simpleType.getList(); if (list != null) { QName typeName = new QName(SchemaConversionBase.SCHEMA_NS, "string"); TypeEntry binding = this.base.getTypeEntry(typeName); writeAttribute(fw, attrName, relationName, binding, isReference, typeName); } else { // Restriction restriction = simpleType.getRestriction(); // if (restriction == null) // throw new RuntimeException("Cannot resolve type for Attribute " + attrName + " -> " + primitiveType.getLocalPart()+ ", SimpleType restriction is unset"); // QName base = restriction.getBase(); QName base = this.base.getSimpleTypeBase(simpleType); //String binding = getBindingFromPrimitiveType(base); TypeEntry binding = this.base.getTypeEntry(base); writeAttribute(fw, attrName, relationName, binding, isReference, base); } } else { // TODO : using default String attribute should be configured with rules. //throw new RuntimeException("Cannot resolve type for Attribute " + attrName + " -> " + primitiveType.getLocalPart()); fw.writer.println(" //FIXME: Cannot resolve type for Attribute " + attrName + " Using default type String"); //writeAttribute(fw, attrName, relationName, "STRING", isReference); writeAttribute(fw, attrName, relationName, this.base.getTypeEntry("string"), isReference, new QName(SchemaConversionBase.SCHEMA_NS, "string")); } } private void writeAttribute(FileWriter fw, String attrName, String relationName, TypeEntry binding, boolean isReference, QName typeName) { String ontologyClassName = converter.getOntologyClassName(typeName.getNamespaceURI()); String ontologyType = ontologyClassName != null ? ontologyClassName + ".getInstance(graph)." + getName(base.getSimpleType(typeName)) : binding.l0Type; fw.writer.println(" {"); fw.writer.println(" Attribute a = element.getAttribute(\"" +attrName+"\");"); fw.writer.println(" if (a != null) {"); fw.writer.println(" graph.claimLiteral(element.getData(), " + relationName + ", " + ontologyType + ", " + getValueGetterMethod(binding,"a") + ", " + binding.binding + ");"); if (isReference) fw.writer.println(" idProviderValue = a.getValue();"); fw.writer.println(" }"); fw.writer.println(" }"); } @Override public void handleAttributes(SchemaObject simpleTypeObj) { SchemaObject parent = simpleTypeObj.getParent(); FileWriter fw = getWriter(parent); SimpleType simpleType = simpleTypeObj.getSimpleType(); Restriction restriction = simpleType.getRestriction(); if (restriction == null) throw new RuntimeException("Cannot resolve type for Element " + getName(parent)); QName base = restriction.getBase(); //String binding = getBindingFromPrimitiveType(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+");"); fw.writer.println(" graph.claimValue(element.getData(),"+getValueGetter(binding)+", "+binding.binding +");"); fw.writer.println(" }"); } @Override public void handle(SchemaObject parent, AttributeGroup attribute) { if (parent != null) { FileWriter fw = getWriter(parent); 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()) { if (annotated instanceof Attribute) { //handle("AttributeGroups_"+group.getName(),(Attribute)annotated); handle(obj,(Attribute)annotated); } else if (annotated instanceof AttributeGroup) { //handle("AttributeGroups_"+group.getName(),(AttributeGroup)annotated); handle(obj,(AttributeGroup)annotated); } } //dd if(true) System.out.println(); fw.writer.println(commentTag+" End of AttributeGroup " + group.getName()); } } @Override public void handleAttributeComposition(SchemaObject parent, AttributeComposition composition, BijectionMap attributes) { FileWriter fw = getWriter(parent); 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; if (name != null) { relationName = ontShort+"has"+name; if (parent != null) relationName = ontShort+getName(parent)+"_has"+name; } else { throw new RuntimeException(); } fw.writer.println(" {"); int i = 0; for (org.simantics.xml.sax.configuration.Attribute a : composition.getAttribute()) fw.writer.println(" Attribute a"+(i++)+" = element.getAttribute(\"" +a.getName()+"\");"); fw.writer.print (" "+javaType + " value = new "+javaType+"{"); i = 0; // FIXME : handle optional attributes properly. for (org.simantics.xml.sax.configuration.Attribute a : composition.getAttribute()) { Attribute attribute = ((Attribute)attributes.getRight(a)); //QName atype = getBaseType(attribute); QName atype = this.base.getPrimitiveType(attribute); String defaultValue = attribute.getDefault(); if (defaultValue == null) defaultValue = this.base.getDefaultValue(atype); //String binding = getBindingFromPrimitiveType(atype); TypeEntry binding = this.base.getTypeEntry(atype); if (i > 0) fw.writer.print(","); if (defaultValue != null) fw.writer.print("a"+(i)+"!= null ? "+ getValueGetterMethod(binding,"a"+(i++)) + " : " +defaultValue); else fw.writer.print(getValueGetterMethod(binding,"a"+(i++))); } fw.writer.println("};"); fw.writer.println(" graph.claimLiteral(element.getData(),"+relationName+", value, "+arrayBinding+");"); fw.writer.println(" }"); } IDProvider provider; @Override public void handleElement(SchemaObject elementObj) { Element element = elementObj.getElement(); String name = getName(elementObj);//topLevelElement.getName(); String className = name; FileWriter fw = new FileWriter(); try { fw.writer = createFile(new File(importParserDir.getAbsolutePath()+"/"+className+".java")); } catch (IOException e) { throw new RuntimeException(e); } writers.put(elementObj, fw); boolean isList = false; Inheritance inheritance = this.base.getInheritance(elementObj); 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) intrerfaces.add("org.simantics.xml.sax.base.IDReferenceParser"); if (unknownChildElement != null) intrerfaces.add("org.simantics.xml.sax.base.UnrecognizedElementParser"); createClassHeader(fw.writer, isList); 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{"); 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(); fw.writer.println(" @Override"); fw.writer.println(" public boolean connectParent(WriteGraph graph, ParserElement parent, ParserElement element) throws DatabaseException{"); fw.writer.println(" "+getOntologyImport()); fw.writer.println(" graph.claim(parent.getData(), "+this.ontShort+getName(elementObj,"has")+", element.getData());"); fw.writer.println(" return true;"); fw.writer.println(" }"); fw.writer.println(); fw.writer.println(" @Override"); fw.writer.println(" public void configure(WriteGraph graph, Deque parents, ParserElement element) throws DatabaseException {"); if (inheritance.type == InheritanceType.ComplexType) { fw.writer.println(" super.configure(graph,parents,element);"); } fw.writer.println(" Layer0 L0 = Layer0.getInstance(graph);"); fw.writer.println(" XMLResource XML = XMLResource.getInstance(graph);"); fw.writer.println(" "+getOntologyImport()); LocalComplexType complexType = element.getComplexType(); LocalSimpleType simpleType = element.getSimpleType(); if (complexType != null) { SchemaObject obj = this.base.getComplexType(complexType); this.base.handleElementComplexTypeAttributes(obj); } 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(" }"); } // if (simpleType != null) { // SchemaObject obj = simpleTypes.get(simpleType); // handleElementSimpleTypeAttributes(obj); // } StringWriter stringWriter = new StringWriter(); fw.delayedWriter = new PrintWriter(stringWriter); StringWriter stringWriter2 = new StringWriter(); fw.delayedWriter2 = new PrintWriter(stringWriter2); fw.writer.println(" public " + className + "() {"); fw.writer.println(" super();"); if (complexType != null) { SchemaObject obj = this.base.getComplexType(complexType); this.base.handleComplexTypeExtension(obj); } else if (simpleType != null) { } fw.writer.println(" }"); fw.writer.println(" @Override"); fw.writer.println(" public boolean connectChild(WriteGraph graph, ParserElement element, ParserElement child) throws DatabaseException{"); fw.writer.println(" "+getOntologyImport()); 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(); if (stringWriter2.getBuffer().length() > 0) { fw.writer.write(stringWriter2.toString()); } stringWriter = null; fw.delayedWriter.close(); fw.delayedWriter = null; stringWriter2 = null; fw.delayedWriter2.close(); fw.delayedWriter2 = null; writeIDReferences(fw.writer,name, references); writeUnknownChild(fw.writer,name,unknownChildElement); fw.writer.println("}"); fw.writer.println(); fw.writer.flush(); fw.writer.close(); fw.writer = null; writers.remove(elementObj); provider = null; ruleClassNames.add(converter.getPluginName()+"."+elementPackageName+"."+name); } private void writeIDProvider(PrintWriter writer) { if (provider != null) { writer.println(" java.lang.String idProviderValue = null;"); writer.println(); writer.println(" @Override"); writer.println(" public java.lang.String getID() {"); writer.println(" java.lang.String s = idProviderValue;"); writer.println(" idProviderValue = null;"); writer.println(" return s;"); writer.println(" }"); writer.println(); if (provider.getPriority() != null) { writer.println(" @Override"); writer.println(" public int idPriority() {"); writer.println(" return " + provider.getPriority().intValue()+";"); writer.println(" }"); } } } private void writeIDReferences(PrintWriter writer,String name, List references) { if (references.size() > 0) { writer.println(" @Override"); writer.println(" public boolean connectReferences(WriteGraph graph, ParserElement element, java.util.Map map) throws DatabaseException {"); writer.println(" "+getOntologyImport()); writer.println(" boolean result = true;"); for (IDReference ref : references) { writer.println(" {"); writer.println(" Attribute a = element.getAttribute(\"" + ref.getIDSource().getName() +"\");"); writer.println(" if (a != null) {"); writer.println(" ParserElement refEle = map.get(a.value);"); writer.println(" if (refEle != null) {"); writer.println(" Resource ref = refEle.getData();"); writer.println(" graph.claim(element.getData(), "+ontShort+name+"_"+ref.getReference().getName()+", ref);"); writer.println(" } else {"); writer.println(" result = false;"); writer.println(" }"); writer.println(" }"); writer.println(" }"); } writer.println(" return result;"); writer.println(" }"); writer.println(); } } private void writeUnknownChild(PrintWriter writer,String name, UnrecognizedChildElement unknownChildElement) { if (unknownChildElement == null) return; writer.println(" @Override"); writer.println(" public void configureChild(WriteGraph graph, Deque parents, ParserElement element, ParserElement child) throws DatabaseException {"); writer.println(" " + unknownChildElement.getJavaMethod()); writer.println(" }"); writer.println(); } @Override public String getBaseClass(ObjectType type) { return "org.simantics.xml.sax.base.XMLElementNamedChildParserBase"; } protected void createClassHeader(PrintWriter writer, boolean isList) { writer.println("package " + converter.getPluginName() +"."+elementPackageName+";"); writer.println(); writer.println("import java.util.Deque;"); writer.println("import org.simantics.databoard.Bindings;"); writer.println("import org.simantics.db.Resource;"); writer.println("import org.simantics.db.WriteGraph;"); writer.println("import org.simantics.db.exception.DatabaseException;"); writer.println("import org.simantics.xml.sax.base.Attribute;"); writer.println("import org.simantics.xml.sax.base.ParserElement;"); writer.println("import org.simantics.xml.sax.ontology.XMLResource;"); if (!isList) { writer.println("import org.simantics.layer0.Layer0;"); } else { writer.println("import java.util.Collections;"); writer.println("import org.simantics.db.common.utils.ListUtils;"); } writer.println(); } }