From 5ebeeffcca96d6631fd3d378e150885f2c8497e9 Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Thu, 4 May 2017 13:28:35 +0300 Subject: [PATCH] Schema conversion changes Multiline base64 data import fixed Use '_List' postfix with LinkedList Processing a SimpleType when schema contains an Element with the same name failed Change-Id: I76607517d2e60dc87c0fc6e07c7b81b65e6d4d8a --- .../simantics/xml/sax/ExporterGenerator.java | 1710 ++++++++--------- .../simantics/xml/sax/ImporterGenerator.java | 11 +- .../simantics/xml/sax/OntologyGenerator.java | 22 +- .../xml/sax/SchemaConversionBase.java | 8 +- 4 files changed, 876 insertions(+), 875 deletions(-) diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java index 121a635..deb5f11 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java @@ -1,855 +1,855 @@ -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.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 ExporterGenerator extends JavaGenerator{ - - public ExporterGenerator(SchemaConverter converter, SchemaConversionBase base) { - super(converter, base); - } - - public void createParser() throws IOException { - - String parserPackagePostfix = "_exp"; - String importerClassPostfix = "Exporter"; - String parserClassPostfix = "Writer"; - 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.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(); - } - -// @Override -// protected void handle(TopLevelAttribute topLevelAttribute) { -// -// } - - @Override - public String getBaseClass(ObjectType type) { - return "org.simantics.xml.sax.base.XMLElementNamedChildWriterBase"; - } - - @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); - - 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 = base.getInheritance(complexTypeObj); - - provider = base.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()); - - base.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) {"); - base.handleComplexTypeAttributes(complexTypeObj); - base.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, TypeEntry typeEntry, QName typeName) { - FileWriter fw = getWriter(parent); - String ontologyClassName = converter.getOntologyClassName(typeName.getNamespaceURI()); - String typeURI = ontologyClassName != null ? ontologyClassName + ".URIs." + getName(base.getSimpleType(typeName)) : "null"; - - fw.writer.println(" addRelationWriter("+ ontShort +getName(parent) + "_has"+refName+", "+getName(parent) +"_" +refName+".class);"); - - fw.delayedWriter2.println(" public static class " + getName(parent) +"_" +refName+" extends org.simantics.xml.sax.base.ValueElementWriter {"); - fw.delayedWriter2.println(" public "+ getName(parent) +"_" +refName +"(){"); - fw.delayedWriter2.println(" super(\""+refName+"\", " + typeURI + ", " + typeEntry.binding+");"); - fw.delayedWriter2.println(" }\n"); - fw.delayedWriter2.println(" @Override"); - fw.delayedWriter2.println(" public String decode(Object value) {"); - fw.delayedWriter2.println(" return " + typeEntry.stringPrefix + "(" + typeEntry.javaType + ")value" + typeEntry.stringPostfix + ";"); - 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(" addTypeWriter("+ ontShort+className +", "+className+".class);"); -// else -// fw.writer.println(" addWriter("+className+".class);"); - -// fw.delayedWriter.println(" if (child.getElementParser() instanceof "+className+"){"); -// 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, 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 - public 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 - 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 = 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 = base.getTypeEntry(primitiveType); - - if (binding != null) { - writeAttribute(fw, attrName, relationName, binding, isReference); - return; - } else { - if (simpleType == null) { - SchemaObject simpleTypeObj = base.getSimpleType(primitiveType); - if (simpleTypeObj != null) - simpleType = simpleTypeObj.getSimpleType(); - } - } - } - if (simpleType != null) { - org.w3._2001.xmlschema.List list = simpleType.getList(); - if (list != null) { - TypeEntry binding = base.getTypeEntry(new QName(SchemaConversionBase.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 = this.base.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, base.getTypeEntry("string"), 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 - 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 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 - 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); - } - } - 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()); - TypeEntry typeEntry = this.base.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 = this.base.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 - 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); - 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 = this.base.getComplexType(complexType); - this.base.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 = this.base.getComplexType(complexType); - this.base.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(); - } - - - -} +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.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 ExporterGenerator extends JavaGenerator{ + + public ExporterGenerator(SchemaConverter converter, SchemaConversionBase base) { + super(converter, base); + } + + public void createParser() throws IOException { + + String parserPackagePostfix = "_exp"; + String importerClassPostfix = "Exporter"; + String parserClassPostfix = "Writer"; + 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.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(); + } + +// @Override +// protected void handle(TopLevelAttribute topLevelAttribute) { +// +// } + + @Override + public String getBaseClass(ObjectType type) { + return "org.simantics.xml.sax.base.XMLElementNamedChildWriterBase"; + } + + @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); + + 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 = base.getInheritance(complexTypeObj); + + provider = base.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()); + + base.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) {"); + base.handleComplexTypeAttributes(complexTypeObj); + base.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, TypeEntry typeEntry, QName typeName) { + FileWriter fw = getWriter(parent); + String ontologyClassName = converter.getOntologyClassName(typeName.getNamespaceURI()); + String typeURI = ontologyClassName != null ? ontologyClassName + ".URIs." + getName(base.getSimpleType(typeName)) : "null"; + + fw.writer.println(" addRelationWriter("+ ontShort +getName(parent) + "_has"+refName+", "+getName(parent) +"_" +refName+".class);"); + + fw.delayedWriter2.println(" public static class " + getName(parent) +"_" +refName+" extends org.simantics.xml.sax.base.ValueElementWriter {"); + fw.delayedWriter2.println(" public "+ getName(parent) +"_" +refName +"(){"); + fw.delayedWriter2.println(" super(\""+refName+"\", " + typeURI + ", " + typeEntry.binding+");"); + fw.delayedWriter2.println(" }\n"); + fw.delayedWriter2.println(" @Override"); + fw.delayedWriter2.println(" public java.lang.String decode(java.lang.Object value) {"); + fw.delayedWriter2.println(" return " + typeEntry.stringPrefix + "(" + typeEntry.javaType + ")value" + typeEntry.stringPostfix + ";"); + 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(" addTypeWriter("+ ontShort+className +", "+className+".class);"); +// else +// fw.writer.println(" addWriter("+className+".class);"); + +// fw.delayedWriter.println(" if (child.getElementParser() instanceof "+className+"){"); +// 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, 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 + public 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 + 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 = 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 = base.getTypeEntry(primitiveType); + + if (binding != null) { + writeAttribute(fw, attrName, relationName, binding, isReference); + return; + } else { + if (simpleType == null) { + SchemaObject simpleTypeObj = base.getSimpleType(primitiveType); + if (simpleTypeObj != null) + simpleType = simpleTypeObj.getSimpleType(); + } + } + } + if (simpleType != null) { + org.w3._2001.xmlschema.List list = simpleType.getList(); + if (list != null) { + TypeEntry binding = base.getTypeEntry(new QName(SchemaConversionBase.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 = this.base.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, base.getTypeEntry("string"), 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 + 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 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 + 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); + } + } + 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()); + TypeEntry typeEntry = this.base.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 = this.base.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 + 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); + 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 = this.base.getComplexType(complexType); + this.base.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 = this.base.getComplexType(complexType); + this.base.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(); + } + + + +} 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 ba1eff9..206acc0 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 @@ -203,6 +203,7 @@ public class ImporterGenerator extends JavaGenerator{ } 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); @@ -251,10 +252,10 @@ public class ImporterGenerator extends JavaGenerator{ // 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(" 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(" 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(" }"); @@ -288,7 +289,7 @@ public class ImporterGenerator extends JavaGenerator{ 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, String value) throws DatabaseException {"); + 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); @@ -320,10 +321,10 @@ public class ImporterGenerator extends JavaGenerator{ // 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(" 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(" 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(" }"); diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java index 3e0ffb1..64def5d 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java @@ -138,9 +138,9 @@ public class OntologyGenerator implements SchemaConversionComponent { writer.println(); if (!single) { - String elementListType = getName(parent) + "." + name + "List"; + String elementListType = getName(parent) + "." + name + "_List"; writer.println(elementListType + " " + elementListType); } @@ -182,10 +182,10 @@ public class OntologyGenerator implements SchemaConversionComponent { if (type == null) { writer.println(getName(parent)+"."+refName + "List " + getName(obj)); if (base.useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) { - writer.println(getName(parent)+"."+refName + "List " + name); - writer.println(relationName+ "List " + name + "List"); + writer.println(relationName+ "_List " + name + "_List"); writer.println(); // Attributes @@ -474,7 +474,7 @@ public class OntologyGenerator implements SchemaConversionComponent { // relationName = ontRoot+getName(elementObj.getParent()) + "has"+element.getName(); // } writer.println(relationName+ "