X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FOntologyGenerator.java;fp=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FOntologyGenerator.java;h=6e4355876ff4c7f58e5a38162fdec8db4e3dac78;hb=7f59891d2747280b09e77bc833bc70e05eece706;hp=2de5908f48374a85667f66ba1443f5465b56cd6e;hpb=5a290a430279a97270c968f18fd667766672e7ba;p=simantics%2Finterop.git 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 2de5908..6e43558 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 @@ -1,631 +1,636 @@ -package org.simantics.xml.sax; - -import java.io.FileNotFoundException; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.ArrayList; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; - -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.SchemaObject.ObjectType; -import org.simantics.xml.sax.configuration.AttributeComposition; -import org.simantics.xml.sax.configuration.IDReference; -import org.w3._2001.xmlschema.Annotated; -import org.w3._2001.xmlschema.Attribute; -import org.w3._2001.xmlschema.AttributeGroup; -import org.w3._2001.xmlschema.Element; -import org.w3._2001.xmlschema.LocalComplexType; -import org.w3._2001.xmlschema.LocalSimpleType; -import org.w3._2001.xmlschema.NamedAttributeGroup; -import org.w3._2001.xmlschema.NamedGroup; -import org.w3._2001.xmlschema.Restriction; -import org.w3._2001.xmlschema.Schema; -import org.w3._2001.xmlschema.SimpleType; - -//public class OntologyGenerator extends SchemaConversionBase { -public class OntologyGenerator implements SchemaConversionComponent { - SchemaConversionBase base; - - public OntologyGenerator(SchemaConverter converter, SchemaConversionBase base) { - this.base = base; - this.converter = converter; - this.schema = base.schema; - this.ontologyUri = base.ontologyURI; - this.className = base.className; - } - - String ontRoot = "ONT."; - String commentTag = "//"; - - Schema schema; - String ontologyUri; - String className; - - SchemaConverter converter; - - PrintWriter writer = null; - - public void createOntology() throws FileNotFoundException { - StringWriter stringWriter = null; - if (converter.getOntologyFile() == null) { - stringWriter = new StringWriter(); - writer = new PrintWriter(stringWriter); - } else { - writer = new PrintWriter(converter.getOntologyFile()); - } - - handle(); - - writer.flush(); - writer.close(); - if (stringWriter != null) - System.out.println(stringWriter.toString()); - } - - protected void handle() { - ontRoot = converter.shortName; - - for (String s : converter.getHeader()) { - writer.println(commentTag + " " + s); - } - writer.println(); - writer.println("L0 = "); - writer.println("XML = "); - writer.println(); - writer.println(ontRoot + " = <" + ontologyUri +"> : L0.Ontology"); - writer.println(" @L0.new"); - writer.println(" L0.HasResourceClass \"" + className +"\""); - writer.println(); - writer.println(); - - ontRoot += "."; - writer.println(ontRoot+"ComplexTypes : L0.Library"); - writer.println(ontRoot+"AttributeGroups : L0.Library"); - writer.println(); - writer.println(commentTag + " Interpreted from schema"); - writer.println(); - - base.handle(this); - } - - protected String getType(QName qtype) { - String ontType = base.getL0TypeFromPrimitiveType(qtype); - if (ontType != null) - return ontType; - else if (base.isComplexTypeRef(qtype.getLocalPart())) - return ontRoot+getComplexTypePrefix()+qtype.getLocalPart(); - else if (base.isSimpleTypeRef(qtype.getLocalPart())) - return ontRoot+qtype.getLocalPart(); - else if (base.isElementRef(qtype.getLocalPart())) - return ontRoot+qtype.getLocalPart(); - else if (qtype.getPrefix() != null && qtype.getPrefix().length() > 0) { - return ontRoot+qtype.getPrefix()+qtype.getLocalPart(); - } - throw new RuntimeException("Reference to unknown type " + qtype.getLocalPart()); - } - - public String getComplexTypePrefix() { - return "ComplexTypes."; - } - - public String getAttributeGroupPrefix() { - return "AttributeGroups."; - } - - public void handleChoice(SchemaObject parent, SchemaElement indicator, java.util.List elements, String name) { - boolean single = true; - for (SchemaElement e : elements) { - if (e.getRestriction().many()) { - single = false; - break; - } - } - String relationName = getName(parent)+".has"+name; - writer.print(relationName); - - List types = new ArrayList(); - for (SchemaElement e : elements) { - Element localElement = e.getElement(); - QName refType = null; - String type = null; - - if (localElement.getName() != null) { - refType = localElement.getType(); - type = base.getL0TypeFromPrimitiveType(refType); - } else if (localElement.getRef() != null) { - refType = localElement.getRef(); - type = base.getL0TypeFromPrimitiveType(refType); - } - if (type == null) { - SchemaObject obj = base.getWithName(refType); - types.add(getName(obj,"has")); - } - } - if (types.size() > 0) { - for (String type : types) { - writer.print(" " + type); - } - } - if (!single) { - writer.println(ontRoot+name+ "List " + ontRoot+name+"List"); - } - }; - - - - @Override - public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType) { - if (refType != refType.Element) { - QName referenceType = null; - if (refType == RefType.Type) { - referenceType = element.getElement().getType(); - //refName = element.getElement().getName() - SchemaObject eObj = base.getElement(element.getElement());//base.elements.get(element.getElement()); - if (refName == null) - refName = eObj.getName(); - } else { - referenceType = element.getElement().getRef(); - if (refName == null) - refName = referenceType.getLocalPart(); - } - String type = base.getL0TypeFromPrimitiveType(referenceType); - SchemaObject obj = null; - if (type == null) { - obj = base.getWithName(referenceType); - - writer.println(getName(parent)+".has"+refName + " " + getName(obj)); - } else { - writer.println(getName(parent)+".has"+refName + " " + getType(referenceType)); - } - - if (base.useElementList(parent, indicator,element, refType == RefType.Reference, refName, referenceType)) { - - 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 element = (JAXBElement)facetWrap; -// QName elementName = element.getName(); -// Facet facet = (Facet)element.getValue(); -// } - } - - - - writer.println(relationName+ " " + ontType); - } else if (primitiveType != null) { - writer.println(" attributes) { - Attribute compositionAttribute = new Attribute(); - compositionAttribute.setName(composition.getName()); - QName type = new QName(SchemaConversionBase.CONVERSION_NS, composition.getType()); - compositionAttribute.setType(type); - handle(parent, compositionAttribute); - } - - @Override - public void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { - SimpleType simpleType = simpleTypeObj.getSimpleType(); - String name = simpleType.getName(); - - org.w3._2001.xmlschema.List list = simpleType.getList(); - if (list != null) { - // TODO : process restriction in lists - String relationName = ontRoot+"has"+name; - if (parent != null) - relationName = getName(parent)+".has"+name; - writer.println(relationName+ " " + ontType); - } else { - String relationName = ontRoot+"has"+name; - if (parent != null) - relationName = getName(parent)+".has"+name; - - writer.println(relationName+ " " + inheritance.atomicType.l0Type); - -// Restriction restriction = simpleType.getRestriction(); -// if (restriction != null) { -// -// QName base = restriction.getBase(); -// String ontType = getL0Type(base); -// writer.println(" --> " + ontType); -// } else if (simpleType.getId() != null) { -// throw new RuntimeException(simpleType.getName() + " restriction error"); -// } else if (simpleType.getUnion() != null) { -// Union union = simpleType.getUnion(); -// String ontType = null; -// if (union.getMemberTypes().size() > 0) { -// for (QName type : union.getMemberTypes()) { -// String sType = null; -// TypeEntry entry = getTypeEntry(type); -// if (entry == null) { -// SchemaObject obj = simpleTypeName.get(type.getLocalPart()); -// Inheritance inheritance = new Inheritance(""); -// getAtomicTypeInheritance(type, obj, inheritance); -// sType = inheritance.atomicType.l0Type; -// } else { -// sType = entry.l0Type; -// } -// if (ontType == null) -// ontType = sType; -// else if (!ontType.equals(sType)) -// throw new RuntimeException(simpleType.getName() + " union has incompatible member types"); -// } -// } else { -// if (union.getSimpleType().size() == 0) -// throw new RuntimeException(simpleType.getName() + " union error"); -// for (SimpleType s : union.getSimpleType()) { -// if (restriction == null) -// restriction = s.getRestriction(); -// else { -// Restriction r = s.getRestriction(); -// if (!r.getBase().equals(restriction.getBase())) -// throw new RuntimeException(simpleType.getName() + " union has incompatible restriction bases"); -// } -// } -// QName base = restriction.getBase(); -// ontType = getL0Type(base); -// } -// writer.println(" --> " + ontType); -// } else { -// throw new RuntimeException(simpleType.getName() + " restriction error"); -// } - - } - } - - @Override - public void handleComplexType(SchemaObject topLevelComplexType) { - String name = getName(topLevelComplexType); -// if (topLevelComplexType.getName().equals("Reference")) -// System.out.println(); - - String relationName = getName(topLevelComplexType,"has");//ontRoot+"has"+name; - - writer.println(relationName+ " " + ontRoot+getComplexTypePrefix()+name); - writer.println(" --> " + name); - writer.println(); -// String baseType = "XML.ComplexType"; -// -// QName base = getComplexTypeBase(topLevelComplexType.getComplexType()); -// if (base != null) { -// baseType = getType(base); -// } -// base = getSimpleTypeBase(topLevelComplexType.getSimpleType()); -// if (base != null) { -// baseType = getType(base); -// } - Inheritance inheritance = base.getInheritance(topLevelComplexType); - -// writer.println(name+ " types = new LinkedHashSet(); - if (element.getType() != null) { - types.add(getType(element.getType())); - } - QName base = this.base.getElementBase(element); - if (base != null) { - if (base.getNamespaceURI().equals(SchemaConversionBase.SCHEMA_NS)) { - String l0Type = this.base.getL0Type(base); - if (l0Type == null) - throw new RuntimeException("Cannot get L0 type for " + base.getLocalPart()); - types.add(l0Type); - } else if (this.base.isElementRef(base.getLocalPart())) - types.add(ontRoot+base.getLocalPart()); - else - types.add(ontRoot+getComplexTypePrefix()+base.getLocalPart()); - } - QName substitution = element.getSubstitutionGroup(); - if (substitution != null) { - if (this.base.isElementRef(substitution.getLocalPart())) - types.add(ontRoot+substitution.getLocalPart()); - else - types.add( ontRoot+getComplexTypePrefix()+substitution.getLocalPart()); - } - for (String t : types) { - type += " references = this.base.getIDReferences(element); - - for (IDReference ref : references) { - writer.println(name+"."+ref.getReference().getName()+ " "); + writer.println("XML = "); + writer.println(); + if (converter.isPrimary()) { + writer.println(ontRoot + " = <" + ontologyUri +"> : L0.Ontology"); + writer.println(" @L0.new"); + writer.println(" L0.HasResourceClass \"" + className +"\""); + } else { + writer.println(ontRoot + " = <" + ontologyUri +">"); + } + writer.println(); + writer.println(); + + ontRoot += "."; + writer.println(ontRoot+"ComplexTypes : L0.Library"); + writer.println(ontRoot+"AttributeGroups : L0.Library"); + writer.println(); + writer.println(commentTag + " Interpreted from schema"); + writer.println(); + + base.handle(this); + } + + protected String getType(QName qtype) { + String ontType = base.getL0TypeFromPrimitiveType(qtype); + if (ontType != null) + return ontType; + else if (base.isComplexTypeRef(qtype.getLocalPart())) + return ontRoot+getComplexTypePrefix()+qtype.getLocalPart(); + else if (base.isSimpleTypeRef(qtype.getLocalPart())) + return ontRoot+qtype.getLocalPart(); + else if (base.isElementRef(qtype.getLocalPart())) + return ontRoot+qtype.getLocalPart(); + else if (qtype.getPrefix() != null && qtype.getPrefix().length() > 0) { + return ontRoot+qtype.getPrefix()+qtype.getLocalPart(); + } + throw new RuntimeException("Reference to unknown type " + qtype.getLocalPart()); + } + + public String getComplexTypePrefix() { + return "ComplexTypes."; + } + + public String getAttributeGroupPrefix() { + return "AttributeGroups."; + } + + public void handleChoice(SchemaObject parent, SchemaElement indicator, java.util.List elements, String name) { + boolean single = true; + for (SchemaElement e : elements) { + if (e.getRestriction().many()) { + single = false; + break; + } + } + String relationName = getName(parent)+".has"+name; + writer.print(relationName); + + List types = new ArrayList(); + for (SchemaElement e : elements) { + Element localElement = e.getElement(); + QName refType = null; + String type = null; + + if (localElement.getName() != null) { + refType = localElement.getType(); + type = base.getL0TypeFromPrimitiveType(refType); + } else if (localElement.getRef() != null) { + refType = localElement.getRef(); + type = base.getL0TypeFromPrimitiveType(refType); + } + if (type == null) { + SchemaObject obj = base.getWithName(refType); + types.add(getName(obj,"has")); + } + } + if (types.size() > 0) { + for (String type : types) { + writer.print(" " + type); + } + } + if (!single) { + writer.println(ontRoot+name+ "List " + ontRoot+name+"List"); + } + }; + + + + @Override + public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType) { + if (refType != refType.Element) { + QName referenceType = null; + if (refType == RefType.Type) { + referenceType = element.getElement().getType(); + //refName = element.getElement().getName() + SchemaObject eObj = base.getElement(element.getElement());//base.elements.get(element.getElement()); + if (refName == null) + refName = eObj.getName(); + } else { + referenceType = element.getElement().getRef(); + if (refName == null) + refName = referenceType.getLocalPart(); + } + String type = base.getL0TypeFromPrimitiveType(referenceType); + SchemaObject obj = null; + if (type == null) { + obj = base.getWithName(referenceType); + + writer.println(getName(parent)+".has"+refName + " " + getName(obj)); + } else { + writer.println(getName(parent)+".has"+refName + " " + getType(referenceType)); + } + + if (base.useElementList(parent, indicator,element, refType == RefType.Reference, refName, referenceType)) { + + 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 element = (JAXBElement)facetWrap; +// QName elementName = element.getName(); +// Facet facet = (Facet)element.getValue(); +// } + } + + + + writer.println(relationName+ " " + ontType); + } else if (primitiveType != null) { + writer.println(" attributes) { + Attribute compositionAttribute = new Attribute(); + compositionAttribute.setName(composition.getName()); + QName type = new QName(SchemaConversionBase.CONVERSION_NS, composition.getType()); + compositionAttribute.setType(type); + handle(parent, compositionAttribute); + } + + @Override + public void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { + SimpleType simpleType = simpleTypeObj.getSimpleType(); + String name = simpleType.getName(); + + org.w3._2001.xmlschema.List list = simpleType.getList(); + if (list != null) { + // TODO : process restriction in lists + String relationName = ontRoot+"has"+name; + if (parent != null) + relationName = getName(parent)+".has"+name; + writer.println(relationName+ " " + ontType); + } else { + String relationName = ontRoot+"has"+name; + if (parent != null) + relationName = getName(parent)+".has"+name; + + writer.println(relationName+ " " + inheritance.atomicType.l0Type); + +// Restriction restriction = simpleType.getRestriction(); +// if (restriction != null) { +// +// QName base = restriction.getBase(); +// String ontType = getL0Type(base); +// writer.println(" --> " + ontType); +// } else if (simpleType.getId() != null) { +// throw new RuntimeException(simpleType.getName() + " restriction error"); +// } else if (simpleType.getUnion() != null) { +// Union union = simpleType.getUnion(); +// String ontType = null; +// if (union.getMemberTypes().size() > 0) { +// for (QName type : union.getMemberTypes()) { +// String sType = null; +// TypeEntry entry = getTypeEntry(type); +// if (entry == null) { +// SchemaObject obj = simpleTypeName.get(type.getLocalPart()); +// Inheritance inheritance = new Inheritance(""); +// getAtomicTypeInheritance(type, obj, inheritance); +// sType = inheritance.atomicType.l0Type; +// } else { +// sType = entry.l0Type; +// } +// if (ontType == null) +// ontType = sType; +// else if (!ontType.equals(sType)) +// throw new RuntimeException(simpleType.getName() + " union has incompatible member types"); +// } +// } else { +// if (union.getSimpleType().size() == 0) +// throw new RuntimeException(simpleType.getName() + " union error"); +// for (SimpleType s : union.getSimpleType()) { +// if (restriction == null) +// restriction = s.getRestriction(); +// else { +// Restriction r = s.getRestriction(); +// if (!r.getBase().equals(restriction.getBase())) +// throw new RuntimeException(simpleType.getName() + " union has incompatible restriction bases"); +// } +// } +// QName base = restriction.getBase(); +// ontType = getL0Type(base); +// } +// writer.println(" --> " + ontType); +// } else { +// throw new RuntimeException(simpleType.getName() + " restriction error"); +// } + + } + } + + @Override + public void handleComplexType(SchemaObject topLevelComplexType) { + String name = getName(topLevelComplexType); +// if (topLevelComplexType.getName().equals("Reference")) +// System.out.println(); + + String relationName = getName(topLevelComplexType,"has");//ontRoot+"has"+name; + + writer.println(relationName+ " " + ontRoot+getComplexTypePrefix()+name); + writer.println(" --> " + name); + writer.println(); +// String baseType = "XML.ComplexType"; +// +// QName base = getComplexTypeBase(topLevelComplexType.getComplexType()); +// if (base != null) { +// baseType = getType(base); +// } +// base = getSimpleTypeBase(topLevelComplexType.getSimpleType()); +// if (base != null) { +// baseType = getType(base); +// } + Inheritance inheritance = base.getInheritance(topLevelComplexType); + +// writer.println(name+ " types = new LinkedHashSet(); + if (element.getType() != null) { + types.add(getType(element.getType())); + } + QName base = this.base.getElementBase(element); + if (base != null) { + if (base.getNamespaceURI().equals(SchemaConversionBase.SCHEMA_NS)) { + String l0Type = this.base.getL0Type(base); + if (l0Type == null) + throw new RuntimeException("Cannot get L0 type for " + base.getLocalPart()); + types.add(l0Type); + } else if (this.base.isElementRef(base.getLocalPart())) + types.add(ontRoot+base.getLocalPart()); + else + types.add(ontRoot+getComplexTypePrefix()+base.getLocalPart()); + } + QName substitution = element.getSubstitutionGroup(); + if (substitution != null) { + if (this.base.isElementRef(substitution.getLocalPart())) + types.add(ontRoot+substitution.getLocalPart()); + else + types.add( ontRoot+getComplexTypePrefix()+substitution.getLocalPart()); + } + for (String t : types) { + type += " references = this.base.getIDReferences(element); + + for (IDReference ref : references) { + writer.println(name+"."+ref.getReference().getName()+ "