package org.simantics.xml.sax; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Collections; import java.util.List; import java.util.Map; import javax.xml.namespace.QName; import org.simantics.utils.datastructures.BijectionMap; 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; 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.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 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]; ontShort = name.substring(0, 3).toUpperCase(); ontShort +="."; String parserPackagePostfix = "_exp"; String importerClassPostfix = "Exporter"; String parserClassPostfix = "Writer"; elementPackageName = name+parserPackagePostfix; importParserDir= new File(converter.getParserDir().getAbsolutePath()+"/"+elementPackageName); if (!importParserDir.exists()) importParserDir.mkdirs(); handle(schema); // 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.Resource;"); mainWriter.println("import org.simantics.db.Session;"); mainWriter.println("import org.simantics.db.exception.DatabaseException;"); mainWriter.println("import org.simantics.xml.sax.base.AbstractExporter;"); mainWriter.println(); mainWriter.println("public class " + name + importerClassPostfix+" extends AbstractExporter {"); mainWriter.println(); mainWriter.println(" public " + name + importerClassPostfix+"(Session session, File file, Resource root) throws DatabaseException {"); mainWriter.println(" super(session, file, root);"); mainWriter.println(" setXmlWriter(new "+name + parserClassPostfix+"(session));"); 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.XMLWriter;"); mainWriter.println("import org.simantics.db.ReadGraph;"); mainWriter.println("import org.simantics.db.Session;"); mainWriter.println("import org.simantics.db.exception.DatabaseException;"); mainWriter.println("import org.simantics.db.common.request.ReadRequest;"); mainWriter.println(); mainWriter.println("public class " + name + parserClassPostfix+" extends XMLWriter {"); mainWriter.println(); mainWriter.println(" public " + name + parserClassPostfix+"(Session session) throws DatabaseException {"); if (schema.getTargetNamespace() != null) mainWriter.println(" setSchemaURI(\""+schema.getTargetNamespace()+"\");"); mainWriter.println(" setOntologyURI(\""+ontologyUri+"\");"); mainWriter.println(" session.syncRequest(new ReadRequest() {"); mainWriter.println(" @Override"); mainWriter.println(" public void run(ReadGraph graph) throws DatabaseException {"); mainWriter.println(" setGraph(graph);"); for (String s : ruleClassNames) { mainWriter.println(" add(new "+s+"(graph));"); } mainWriter.println(" }"); mainWriter.println(" });"); mainWriter.println(" }"); mainWriter.println("}"); mainWriter.println(); mainWriter.flush(); mainWriter.close(); } protected void handle(TopLevelAttribute topLevelAttribute) { } @Override protected String getBaseClass(ObjectType type) { return "org.simantics.xml.sax.base.XMLElementNamedChildWriterBase"; } @Override protected void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { } @Override protected void handleComplexType(SchemaObject complexTypeObj) { ComplexType topLevelComplexType = complexTypeObj.getComplexType(); String name = getName(complexTypeObj); 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); // String baseClass = getBaseClass(ObjectType.COMPLEX_TYPE); // // boolean inherited = false; // QName type = getComplexTypeBase(topLevelComplexType); // if (type != null && !type.getNamespaceURI().equals("http://www.w3.org/2001/XMLSchema")) { // SchemaObject obj = complexTypeName.get(type.getLocalPart()); //// if (obj == null) //// obj = simpleTypeName.get(type.getLocalPart()); // if (obj != null) { // baseClass = getName(obj); // inherited = true; // } // } Inheritance inheritance = getInheritance(complexTypeObj); provider = getIDProvider(topLevelComplexType); // List references = getIDReferences(topLevelComplexType); // UnrecognizedChildElement unknownChildElement = 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); writeClass(fw.writer,false, topLevelComplexType.getName(), className, inheritance.baseClass, Collections.EMPTY_LIST); // writeIDProvider(fw.writer); fw.writer.println(" @Override"); fw.writer.println(" public Resource getType(ReadGraph graph) throws DatabaseException {"); fw.writer.println(" "+getOntologyImport()); fw.writer.println(" return "+ontShort+name+";"); fw.writer.println(" }"); fw.writer.println(); // 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(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 + "(ReadGraph graph) {"); fw.writer.println(" super(graph);"); fw.writer.println(" "+getOntologyImport()); handleComplexTypeExtension(complexTypeObj); fw.writer.println(" }"); fw.writer.println(" @Override"); fw.writer.println(" public void children(ReadGraph graph, WriterElement instance, java.util.Set result) throws XMLStreamException, DatabaseException {"); fw.writer.println(" "+getOntologyImport()); if (stringWriter.getBuffer().length() > 0) { fw.writer.write(stringWriter.toString()); } if (inheritance.type == InheritanceType.ComplexType) fw.writer.println(" super.children(graph,instance,result);"); 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 attributes(ReadGraph graph, WriterElement instance, Collection attributes, XMLStreamWriter writer) throws XMLStreamException, DatabaseException{"); if (inheritance.type == InheritanceType.ComplexType) { fw.writer.println(" super.attributes(graph,instance,attributes,writer);"); } fw.writer.println(" "+getOntologyImport()); fw.writer.println(" for (Statement attribute : attributes) {"); handleComplexTypeAttributes(complexTypeObj); handleExtensionAttributes(complexTypeObj); fw.writer.println(" }"); fw.writer.println(" }"); if (inheritance.type == InheritanceType.AtomicType) { fw.writer.println(); fw.writer.println(" @Override"); fw.writer.println(" public void characters(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException {"); fw.writer.println(" writer.writeCharacters("+inheritance.atomicType.getToString("graph.getValue(instance.getResource(),"+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 protected void createReferenceIndicator(SchemaObject parent, RefType referenceType, String refName, String objectName, String primaryClassName, String secondaryClassName, boolean useElementList, boolean useOriginalList) { FileWriter fw = getWriter(parent); if ("TextOrField".equals(refName)) System.out.println(); if (referenceType == RefType.Type) { // create internal class for handling the element and child attachment secondaryClassName = getName(parent) +"_" +objectName; if (objectName.equals(refName)) fw.writer.println(" addRelationWriter("+ontShort+ getName(parent)+"_has"+refName +", "+secondaryClassName+".class);"); else // FIXME : type name may not be correct fw.writer.println(" addRelationTypeWriter("+ontShort+ getName(parent)+"_has"+refName +", " +ontShort+primaryClassName +", "+secondaryClassName+".class);"); fw.delayedWriter2.println(" public static class " + secondaryClassName +" extends " + primaryClassName +"{"); fw.delayedWriter2.println(" public "+ secondaryClassName +"(ReadGraph graph){"); fw.delayedWriter2.println(" super(graph);"); fw.delayedWriter2.println(" }"); fw.delayedWriter2.println(" "); fw.delayedWriter2.println(" @Override"); fw.delayedWriter2.println(" public java.lang.String getElementId() {"); fw.delayedWriter2.println(" return \"" + refName +"\";"); fw.delayedWriter2.println(" }"); fw.delayedWriter2.println(" }"); } else { //referenceType == RefType.Reference fw.writer.println(" addWriter("+primaryClassName+".class);"); if (!primaryClassName.equals(secondaryClassName)) fw.writer.println(" addWriter("+secondaryClassName+".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) { // element type specific list fw.delayedWriter.println(" {"); fw.delayedWriter.println(" org.simantics.db.Resource list = graph.getPossibleObject(instance.getResource(),"+ontShort+getName(parent)+"_has"+refName + "List);"); fw.delayedWriter.println(" if (list != null) {"); fw.delayedWriter.println(" java.util.List l = org.simantics.db.common.utils.ListUtils.toList(graph, list);"); fw.delayedWriter.println(" result.addAll(l);"); fw.delayedWriter.println(" } else {"); fw.delayedWriter.println(" result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));"); fw.delayedWriter.println(" }"); fw.delayedWriter.println(" }"); } else { fw.delayedWriter.println(" result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));"); } // 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(" }"); } @Override protected void createPrimitiveIndicator(SchemaObject parent, String refName, String binding) { FileWriter fw = getWriter(parent); //SchemaObject refElement = elementName.get(refType.getLocalPart()); //writer.println(commentTag+ontShort+"."+parent+".has"+ref + " l = org.simantics.db.common.utils.ListUtils.toList(graph, list);"); fw.delayedWriter.println(" result.addAll(l);"); fw.delayedWriter.println(" } else {"); fw.delayedWriter.println(" result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));"); fw.delayedWriter.println(" }"); fw.delayedWriter.println(" }"); } else { fw.delayedWriter.println(" result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));"); } // if (useOriginalList(parent, indicator,element, reference, ref, new QName(obj.getName()))) { // // 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(" }"); } @Override protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any) { // generates overriding method that allows writing any element FileWriter fw = getWriter(parent); fw.delayedWriter2.println(" @Override"); fw.delayedWriter2.println(" public Class getWriter(ReadGraph graph, java.util.Map writers, WriterElement child) throws DatabaseException{"); fw.delayedWriter2.println(" Class writerClass = super.getWriter(graph, writers, child);"); fw.delayedWriter2.println(" if (writerClass != null) return writerClass;"); fw.delayedWriter2.println(" Resource type = graph.getSingleType(child.instance);"); fw.delayedWriter2.println(" org.simantics.xml.sax.base.XMLElementWriter writer = writers.get(type);"); fw.delayedWriter2.println(" if (writer != null) return writer.getClass();"); fw.delayedWriter2.println(" return null;"); fw.delayedWriter2.println(" }"); } @Override protected void handle(SchemaObject parent, Attribute attribute) { 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 = 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 = getTypeEntry(primitiveType); if (binding != null) { writeAttribute(fw, attrName, relationName, binding, isReference); return; } else { if (simpleType == null) { SchemaObject simpleTypeObj = simpleTypeName.get(primitiveType.getLocalPart()); if (simpleTypeObj != null) simpleType = simpleTypeObj.getSimpleType(); } } } if (simpleType != null) { org.w3._2001.xmlschema.List list = simpleType.getList(); if (list != null) { TypeEntry binding = getTypeEntry(new QName(SCHEMA_NS, "string")); writeAttribute(fw, attrName, relationName, binding, isReference); } 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(); //String binding = getBindingFromPrimitiveType(base); TypeEntry binding = getTypeEntry(base); writeAttribute(fw, attrName, relationName, binding, isReference); } } 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, 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+")) {"); fw.writer.println(" writer.writeAttribute(\""+attrName+"\", " + binding.getToString("graph.getValue(attribute.getObject(),"+binding.binding+")")+");"); // fw.writer.println(" graph.claimLiteral(element.getData(),"+relationName+","+getValueGetterMethod(binding,"a")+", "+binding.binding+");"); // if (isReference) // fw.writer.println(" idProviderValue = a.getValue();"); fw.writer.println(" }"); fw.writer.println(" }"); } @Override protected 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 = getTypeEntry(base); fw.writer.println(" @Override"); fw.writer.println(" public void characters(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException {"); fw.writer.println(" writer.writeCharacters("+ binding.getToString("graph.getValue(attribute.getObject(),"+binding.binding+")")+");"); // fw.writer.println(" graph.claimValue(element.getData(),"+getValueGetter(binding)+", "+binding.binding +");"); fw.writer.println(" }"); } @Override protected void handle(SchemaObject parent, AttributeGroup attribute) { if (parent != null) { FileWriter fw = getWriter(parent); NamedAttributeGroup group = getAttributeGroup(attribute.getRef().getLocalPart()); 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); } } fw.writer.println(commentTag+" End of AttributeGroup " + group.getName()); } } @Override protected void handleAttributeComposition(SchemaObject parent, AttributeComposition composition, BijectionMap attributes) { FileWriter fw = getWriter(parent); QName type = new QName(CONVERSION_NS, composition.getType()); TypeEntry typeEntry = getTypeEntry(type); String arrayBinding = typeEntry.binding;//getBindingFromPrimitiveType(type); String javaType = typeEntry.javaType;//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(" if (attribute.getPredicate().equals("+relationName+")) {"); fw.writer.println(" "+javaType+" value = ("+javaType+")graph.getValue(attribute.getObject(),"+arrayBinding+");"); //fw.writer.println(" writer.writeAttribute("+attrName+", graph.getValue(attribute.getObject(),"+binding.binding+").toString());"); int i = 0; for (org.simantics.xml.sax.configuration.Attribute a : composition.getAttribute()) fw.writer.println(" writer.writeAttribute(\""+a.getName()+"\","+typeEntry.getElementToString("value["+(i++)+"]")+");"); //fw.writer.println(" writer.writeAttribute(\""+a.getName()+"\",value["+(i++)+"].toString());"); // 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 = getPrimitiveType(attribute); // String defaultValue = attribute.getDefault(); // if (defaultValue == null) // defaultValue = getDefaultValue(atype); // // //String binding = getBindingFromPrimitiveType(atype); // TypeEntry binding = 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(" }"); } 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; } IDProvider provider; @Override protected 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 = getInheritance(elementObj); provider = getIDProvider(element); List references = getIDReferences(element); UnrecognizedChildElement unknownChildElement = 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); writeClass(fw.writer,false, element.getName(), className, inheritance.baseClass, Collections.EMPTY_LIST); // writeIDProvider(fw.writer); fw.writer.println(" @Override"); fw.writer.println(" public Resource getType(ReadGraph graph) throws DatabaseException {"); fw.writer.println(" "+getOntologyImport()); fw.writer.println(" return "+ontShort+name+";"); // 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(); LocalComplexType complexType = element.getComplexType(); LocalSimpleType simpleType = element.getSimpleType(); fw.writer.println(" @Override"); fw.writer.println(" public void attributes(ReadGraph graph, WriterElement instance, Collection attributes, XMLStreamWriter writer) throws XMLStreamException, DatabaseException{"); if (inheritance.type == InheritanceType.ComplexType) { fw.writer.println(" super.attributes(graph,instance,attributes,writer);"); } fw.writer.println(" "+getOntologyImport()); fw.writer.println(" for (Statement attribute : attributes) {"); if (complexType != null) { SchemaObject obj = complexTypes.get(complexType); handleElementComplexTypeAttributes(obj); } 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 (inhertiance.type == InheritanceType.ComplexType) { // fw.writer.println(" super.configure(graph,parents,element);"); // } // fw.writer.println(" "+getOntologyImport()); // // LocalComplexType complexType = element.getComplexType(); // LocalSimpleType simpleType = element.getSimpleType(); // // if (complexType != null) { // SchemaObject obj = complexTypes.get(complexType); // 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(inhertiance.atomicType,"string")+", "+inhertiance.atomicType.binding+");"); // fw.writer.println(" }"); fw.writer.println(" @Override"); fw.writer.println(" public void characters(ReadGraph graph, WriterElement instance, XMLStreamWriter writer) throws XMLStreamException, DatabaseException {"); fw.writer.println(" writer.writeCharacters("+inheritance.atomicType.getToString("graph.getValue(instance.getResource(),"+inheritance.atomicType.binding+")")+");"); // fw.writer.println(" graph.claimValue(element.getData(),"+getValueGetter(binding)+", "+binding.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 + "(ReadGraph graph) {"); fw.writer.println(" super(graph);"); fw.writer.println(" "+getOntologyImport()); if (complexType != null) { SchemaObject obj = complexTypes.get(complexType); handleComplexTypeExtension(obj); } else if (simpleType != null) { } fw.writer.println(" }"); fw.writer.println(" @Override"); fw.writer.println(" public void children(ReadGraph graph, WriterElement instance, java.util.Set result) throws XMLStreamException, DatabaseException {"); fw.writer.println(" "+getOntologyImport()); if (stringWriter.getBuffer().length() > 0) { fw.writer.write(stringWriter.toString()); } if (inheritance.type == InheritanceType.ComplexType) { fw.writer.println(" super.children(graph,instance,result);"); } 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(); // // } protected void createClassHeader(PrintWriter writer) { writer.println("package " + converter.getPluginName() +"."+elementPackageName+";"); writer.println(); writer.println("import org.simantics.databoard.Bindings;"); writer.println("import org.simantics.db.Resource;"); writer.println("import org.simantics.db.ReadGraph;"); writer.println("import org.simantics.db.Statement;"); writer.println("import org.simantics.db.exception.DatabaseException;"); writer.println("import org.simantics.xml.sax.base.WriterElement;"); writer.println("import org.simantics.xml.sax.ontology.XMLResource;"); writer.println("import java.util.Collection;"); writer.println("import javax.xml.stream.XMLStreamException;"); writer.println("import javax.xml.stream.XMLStreamWriter;"); // 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(); } }