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;h=741e197f0e2ff50bf9bb6b41c619fe94fd78471e;hb=fb01c21839aea801224aeb82b64bd261120619d5;hp=0b7aa3c00aefa89263005c0a2fd87bf2fd3f849a;hpb=bcfe73ff5f614ff83f20ce7bf5bfc0fa1b045978;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 0b7aa3c..741e197 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 @@ -11,6 +11,7 @@ import java.util.Set; import javax.xml.namespace.QName; import org.simantics.utils.datastructures.BijectionMap; +import org.simantics.xml.sax.SchemaObject.ObjectType; import org.simantics.xml.sax.configuration.AttributeComposition; import org.simantics.xml.sax.configuration.Configuration; import org.simantics.xml.sax.configuration.IDReference; @@ -21,7 +22,6 @@ import org.w3._2001.xmlschema.Element; import org.w3._2001.xmlschema.LocalComplexType; import org.w3._2001.xmlschema.LocalSimpleType; import org.w3._2001.xmlschema.NamedAttributeGroup; -import org.w3._2001.xmlschema.OpenAttrs; import org.w3._2001.xmlschema.Restriction; import org.w3._2001.xmlschema.Schema; import org.w3._2001.xmlschema.SimpleType; @@ -37,20 +37,22 @@ public class OntologyGenerator extends SchemaConversionBase { String commentTag = "//"; Schema schema; - + String ontologyUri; String className; SchemaConverter converter; PrintWriter writer = null; - public String getClassName() { - return className; - } - public void createOntology(Schema schema, SchemaConverter converter) throws FileNotFoundException { + + + + public void createOntology(Schema schema, String ontologyName, String className, SchemaConverter converter) throws FileNotFoundException { this.schema = schema; this.converter = converter; + this.ontologyUri = ontologyName; + this.className = className; // for (OpenAttrs attrs : schema.getIncludeOrImportOrRedefine()) { // if (attrs instanceof Annotation) { @@ -83,59 +85,24 @@ public class OntologyGenerator extends SchemaConversionBase { } protected void handle(Schema schema) { - String ontologyName = schema.getTargetNamespace(); - if (ontologyName == null) { - ontologyName = converter.getSchemaFile().getName(); - - int index = ontologyName.lastIndexOf("."); - if (index > 0) - ontologyName = ontologyName.substring(0, index); - } - if (!ontologyName.startsWith("http")) - ontologyName = "http://" + ontologyName; - String parts[] = ontologyName.split("/"); + String parts[] = ontologyUri.split("/"); String name = parts[parts.length-1]; ontRoot = name.substring(0, Math.min(3, name.length())).toUpperCase(); - String version = schema.getVersion(); - if (version == null) - version = "1.0"; - - name = name.replaceAll("\\.", "_"); - name = name.replaceAll(" ", "_"); - className = converter.getPluginName() + "." + name; - + for (String s : converter.getHeader()) { writer.println(commentTag + " " + s); } writer.println(); writer.println("L0 = "); + writer.println("XML = "); writer.println(); - writer.println(ontRoot + " = <" + ontologyName +"-"+ version+"> : L0.Ontology"); + writer.println(ontRoot + " = <" + ontologyUri +"> : L0.Ontology"); writer.println(" @L0.new"); writer.println(" L0.HasResourceClass \"" + className +"\""); writer.println(); writer.println(); ontRoot += "."; - // TODO : these could be created in separate base ontology. - writer.println(commentTag + " Built-in types"); - writer.println(); - writer.println(ontRoot+"XML : L0.Library"); - writer.println(ontRoot+"XML.hasAttribute L0.String"); - writer.println(ontRoot+"XML.ComplexType 0) { @@ -172,11 +141,11 @@ public class OntologyGenerator extends SchemaConversionBase { } - public static String getComplexTypePrefix() { + public String getComplexTypePrefix() { return "ComplexTypes."; } - public static String getAttributeGroupPrefix() { + public String getAttributeGroupPrefix() { return "AttributeGroups."; } @@ -195,7 +164,7 @@ public class OntologyGenerator extends SchemaConversionBase { break; } } - String relationName = ontRoot+getName(parent)+".has"+name; + String relationName = getName(parent)+".has"+name; writer.print(relationName); List types = new ArrayList(); @@ -213,7 +182,7 @@ public class OntologyGenerator extends SchemaConversionBase { } if (type == null) { SchemaObject obj = getWithName(parent, refType.getLocalPart()); - types.add(ontRoot+getName(obj,"has")); + types.add(getName(obj,"has")); } } if (types.size() > 0) { @@ -221,7 +190,7 @@ public class OntologyGenerator extends SchemaConversionBase { writer.print(" " + getName(obj)); + } else { + writer.println(getName(parent)+".has"+refName + " " + getType(referenceType)); + } - writer.println(ontRoot+getName(parent)+".has"+refName + " " + ontRoot+getName(obj)); + if (useElementList(parent, indicator,element, refType == RefType.Reference, refName, referenceType)) { + + if (type == null) { + writer.println(getName(parent)+"."+refName + "List " + getType(refType)); - } - - if (useElementList(parent, indicator,element, reference, refName, refType)) { + Element attrs = element.getElement(); + SchemaObject obj = getWithObj(parent, attrs); + if (refName == null) + refName = obj.getName(); - if (type == null) { - writer.println(ontRoot+getName(parent)+"."+refName + "List " + getName(obj)); + if (useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) { + writer.println(getName(parent)+"."+refName + "List " + ontRoot+getName(obj)); - if (useElementList(parent, indicator,element, reference, refName, new QName(obj.getName()))) { - writer.println(ontRoot+getName(parent)+"."+refName + "List " + ontType); + 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 { + Restriction restriction = simpleType.getRestriction(); + if (restriction == null || simpleType.getUnion() != null || simpleType.getId() != null) + throw new RuntimeException(simpleType.getName() + " restriction error"); + QName base = restriction.getBase(); + + String relationName = ontRoot+"has"+name; + if (parent != null) + relationName = getName(parent)+".has"+name; + + writer.println(relationName+ " " + ontType); + } } @Override protected void handleComplexType(SchemaObject topLevelComplexType) { String name = getName(topLevelComplexType); - if (name.contains("Pcurve")) - System.out.println(); -// if (parent != null) -// name = parent +"_"+name; +// if (topLevelComplexType.getName().equals("Reference")) +// System.out.println(); - String relationName = ontRoot+getName(topLevelComplexType,"has");//ontRoot+"has"+name; + String relationName = getName(topLevelComplexType,"has");//ontRoot+"has"+name; - writer.println(relationName+ " " + ontRoot+getComplexTypePrefix()+name); - writer.println(" --> " + ontRoot+name); + writer.println(" --> " + name); writer.println(); - String baseType = ontRoot+"XML.ComplexType"; - - QName base = getComplexTypeBase(topLevelComplexType.getComplexType()); - if (base != null) { - baseType = getType(base); - } - base = getSimpleTypeBase(topLevelComplexType.getSimpleType()); - if (base != null) { - baseType = getType(base); +// 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 = getInheritance(topLevelComplexType); + +// writer.println(name+ " types = new LinkedHashSet(); if (element.getType() != null) { types.add(getType(element.getType())); @@ -487,15 +491,15 @@ public class OntologyGenerator extends SchemaConversionBase { type += " references = getIDReferences(element); for (IDReference ref : references) { - writer.println(ontRoot+name+"."+ref.getReference().getName()+ "