1 package org.simantics.xml.sax;
3 import java.io.FileNotFoundException;
4 import java.io.PrintWriter;
5 import java.io.StringWriter;
6 import java.util.LinkedHashSet;
10 import javax.xml.namespace.QName;
12 import org.simantics.utils.datastructures.BijectionMap;
13 import org.simantics.xml.sax.SchemaConversionBase.Inheritance;
14 import org.simantics.xml.sax.SchemaConversionBase.RefType;
15 import org.simantics.xml.sax.SchemaObject.ObjectType;
16 import org.simantics.xml.sax.configuration.AttributeComposition;
17 import org.simantics.xml.sax.configuration.IDReference;
18 import org.w3._2001.xmlschema.Annotated;
19 import org.w3._2001.xmlschema.Attribute;
20 import org.w3._2001.xmlschema.AttributeGroup;
21 import org.w3._2001.xmlschema.Element;
22 import org.w3._2001.xmlschema.LocalComplexType;
23 import org.w3._2001.xmlschema.LocalSimpleType;
24 import org.w3._2001.xmlschema.NamedAttributeGroup;
25 import org.w3._2001.xmlschema.NamedGroup;
26 import org.w3._2001.xmlschema.Schema;
27 import org.w3._2001.xmlschema.SimpleType;
29 //public class OntologyGenerator extends SchemaConversionBase {
30 public class OntologyGenerator implements SchemaConversionComponent {
31 SchemaConversionBase base;
33 public OntologyGenerator(SchemaConverter converter, SchemaConversionBase base) {
35 this.converter = converter;
36 this.schema = base.schema;
37 this.ontologyUri = base.ontologyURI;
38 this.className = base.className;
41 String ontRoot = "ONT.";
42 String commentTag = "//";
48 SchemaConverter converter;
50 PrintWriter writer = null;
52 public void createOntology() throws FileNotFoundException {
53 StringWriter stringWriter = null;
54 if (converter.getOntologyFile() == null) {
55 stringWriter = new StringWriter();
56 writer = new PrintWriter(stringWriter);
58 writer = new PrintWriter(converter.getOntologyFile());
65 if (stringWriter != null)
66 System.out.println(stringWriter.toString());
69 protected void handle() {
70 ontRoot = converter.shortName;
72 for (String s : converter.getHeader()) {
73 writer.println(commentTag + " " + s);
76 writer.println("L0 = <http://www.simantics.org/Layer0-1.1>");
77 writer.println("XML = <http://www.simantics.org/XML-1.0>");
79 if (converter.isPrimary()) {
80 writer.println(ontRoot + " = <" + ontologyUri +"> : L0.Ontology");
81 writer.println(" @L0.new");
82 writer.println(" L0.HasResourceClass \"" + className +"\"");
84 writer.println(ontRoot + " = <" + ontologyUri +">");
90 writer.println(ontRoot+"SimpleTypes : L0.Library");
91 writer.println(ontRoot+"ComplexTypes : L0.Library");
92 writer.println(ontRoot+"AttributeGroups : L0.Library");
94 writer.println(commentTag + " Interpreted from schema");
100 protected String getType(QName qtype, String rel) {
101 String ontType = base.getL0TypeFromPrimitiveType(qtype);
105 if (base.isComplexTypeRef(qtype.getLocalPart()))
106 return getName(base.getComplexType(qtype), rel);
107 else if (base.isSimpleTypeRef(qtype.getLocalPart()))
108 return getName(base.getSimpleType(qtype), rel);
109 else if (base.isElementRef(qtype.getLocalPart()))
110 return getName(base.getElement(qtype), rel);
112 throw new RuntimeException("Reference to unknown type " + qtype.getLocalPart());
115 public String getSimpleTypePrefix() {
116 return "SimpleTypes.";
119 public String getComplexTypePrefix() {
120 return "ComplexTypes.";
123 public String getAttributeGroupPrefix() {
124 return "AttributeGroups.";
127 public String handleChoice(SchemaObject parent, SchemaElement indicator, java.util.List<SchemaElement> elements, String name) {
128 boolean single = true;
129 for (SchemaElement e : elements) {
130 if (e.getRestriction().many()) {
135 String relationName = getName(parent)+".has"+name;
136 writer.print(relationName);
137 writer.print(" <R XML.hasElement");
141 String elementListType = getName(parent) + "." + name + "_List";
142 writer.println(elementListType + " <T XML.ElementList");
143 writer.println(relationName + "_List <R XML.hasElementList : L0.FunctionalRelation");
144 writer.println(" --> " + elementListType);
153 public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType, String baseRelationName) {
154 if (refType != RefType.Element) {
155 QName referenceType = null;
156 if (refType == RefType.Type) {
157 referenceType = element.getElement().getType();
158 //refName = element.getElement().getName()
159 SchemaObject eObj = base.getElement(element.getElement());//base.elements.get(element.getElement());
161 refName = eObj.getName();
163 referenceType = element.getElement().getRef();
165 refName = referenceType.getLocalPart();
167 String type = base.getL0TypeFromPrimitiveType(referenceType);
168 SchemaObject obj = null;
170 obj = base.getWithName(referenceType);
171 writer.print(getName(parent)+".has"+refName + " <R XML.hasElement <R " + getName(obj,"has"));
172 if (baseRelationName != null) writer.print(" <R " + baseRelationName);
175 writer.print(getName(parent)+".has"+refName + " <R XML.hasElement");
176 if (baseRelationName != null) writer.print(" <R " + baseRelationName);
178 writer.println(" --> " + getType(referenceType, ""));
181 if (base.useElementList(parent, indicator,element, refType == RefType.Reference, refName, referenceType)) {
184 writer.println(getName(parent)+"."+refName + "List <T XML.ElementList");
185 writer.println(getName(parent)+".has"+refName + "_List <R " + getName(obj,"has")+"_List : L0.FunctionalRelation");
187 writer.println(getName(parent)+"."+refName + "_List <T XML.ElementList");
188 writer.println(getName(parent)+".has"+refName + "_List <R XML.hasElementList : L0.FunctionalRelation");
192 Element attrs = element.getElement();
193 SchemaObject obj = base.getWithObj(parent, attrs);
195 refName = obj.getName();
197 writer.print(getName(parent)+".has"+refName + " <R " + getName(obj,"has"));
198 if (baseRelationName != null) writer.print(" <R " + baseRelationName);
200 writer.println(" --> " + getName(obj));
201 if (base.useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) {
202 writer.println(getName(parent)+"."+refName + "_List <T XML.ElementList");
203 writer.println(getName(parent)+".has"+refName + "_List <R " + getName(obj,"has")+"_List : L0.FunctionalRelation");
210 public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any) {
215 public void handle(SchemaObject parent, NamedGroup attribute) {
216 // TODO Auto-generated method stub
221 public void handle(SchemaObject parent, Attribute attribute) {
222 String name = attribute.getName();
223 QName primitiveType = attribute.getType();
224 LocalSimpleType simpleType = attribute.getSimpleType();
225 QName ref = attribute.getRef();
230 relationName = ontRoot+"has"+name;
232 relationName = getName(parent)+".has"+name;
233 relationType = "XML.hasAttribute";
235 else if (ref != null && parent != null) {
236 relationName = getName(parent)+".has"+ref.getLocalPart();
237 relationType = ontRoot+"has"+ref.getLocalPart();
239 throw new RuntimeException();
242 String ontType = null;
243 if (primitiveType != null) {
244 ontType = base.getL0TypeFromPrimitiveType(primitiveType);
245 if (ontType != null) {
246 id = base.getTypeEntry(primitiveType).id;
248 relationType = "XML.hasID";
252 } else if (simpleType != null){
253 // Restriction restriction = simpleType.getRestriction();
254 // if (restriction == null || simpleType.getUnion() != null || simpleType.getName() != null || simpleType.getId() != null)
255 // throw new RuntimeException();
256 // QName base = restriction.getBase();
257 QName base = this.base.getSimpleTypeBase(simpleType);
260 ontType = this.base.getL0TypeFromPrimitiveType(base);
262 // for (Object facetWrap : restriction.getFacets()) {
263 // JAXBElement<?> element = (JAXBElement<?>)facetWrap;
264 // QName elementName = element.getName();
265 // Facet facet = (Facet)element.getValue();
271 writer.println(relationName+ " <R " + relationType + ": L0.FunctionalRelation");
273 // no need to add range restriction
274 } else if (ontType != null) {
275 writer.println(" --> " + ontType);
276 } else if (primitiveType != null) {
277 writer.println(" <R " + getType(primitiveType, "has"));
282 public void handleAttributes(SchemaObject simpleTypeObj) {
283 // SchemaObject parent = simpleTypeObj.getParent();
284 // SimpleType simpleType = simpleTypeObj.getSimpleType();
285 // Restriction restriction = simpleType.getRestriction();
286 // QName base = restriction.getBase();
287 // String ontType = getL0TypeFromPrimitiveType(base);
291 public void handle(SchemaObject parent, AttributeGroup attributeGroup) {
292 if (parent == null) {
293 NamedAttributeGroup group = (NamedAttributeGroup)attributeGroup;
294 writer.println(ontRoot+getAttributeGroupPrefix()+group.getName()+ " <T XML.AttributeGroup");
295 SchemaObject obj = new SchemaObject(parent,attributeGroup);
296 for (Annotated annotated : group.getAttributeOrAttributeGroup()) {
297 if (annotated instanceof Attribute) {
298 //handle(getAttributeGroupPrefix()+group.getName(),(Attribute)annotated);
299 handle(obj,(Attribute)annotated);
300 } else if (annotated instanceof AttributeGroup) {
301 handle(obj,(AttributeGroup)annotated);
302 //throw new RuntimeException("Cannot handle nested attribute groups");
306 writer.println(getName(parent) +" L0.Inherits " + ontRoot + getAttributeGroupPrefix() + attributeGroup.getRef().getLocalPart());
312 public void handleAttributeComposition(SchemaObject parent, AttributeComposition composition, BijectionMap<org.simantics.xml.sax.configuration.Attribute, Annotated> attributes) {
313 Attribute compositionAttribute = new Attribute();
314 compositionAttribute.setName(composition.getName());
315 QName type = new QName(SchemaConversionBase.CONVERSION_NS, composition.getType());
316 compositionAttribute.setType(type);
317 handle(parent, compositionAttribute);
321 public void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) {
322 SimpleType simpleType = simpleTypeObj.getSimpleType();
323 String name = simpleType.getName();
325 org.w3._2001.xmlschema.List list = simpleType.getList();
327 // TODO : process restriction in lists
328 String relationName = getName(simpleTypeObj, "has");
329 writer.println(relationName+ " <R XML.hasAttribute : L0.FunctionalRelation");
331 String ontType = base.getL0Type(new QName(SchemaConversionBase.SCHEMA_NS, "string"));
332 writer.println(" --> " + ontType);
334 QName base = this.base.getSimpleTypeBase(simpleType);
335 writer.println(getName(simpleTypeObj) + " <T " + getType(base, ""));
337 String relationName = getName(simpleTypeObj, "has");
339 writer.println(relationName+ " : L0.FunctionalRelation");
340 writer.println(" --> " + getName(simpleTypeObj));
342 // Restriction restriction = simpleType.getRestriction();
343 // if (restriction != null) {
345 // QName base = restriction.getBase();
346 // String ontType = getL0Type(base);
347 // writer.println(" --> " + ontType);
348 // } else if (simpleType.getId() != null) {
349 // throw new RuntimeException(simpleType.getName() + " restriction error");
350 // } else if (simpleType.getUnion() != null) {
351 // Union union = simpleType.getUnion();
352 // String ontType = null;
353 // if (union.getMemberTypes().size() > 0) {
354 // for (QName type : union.getMemberTypes()) {
355 // String sType = null;
356 // TypeEntry entry = getTypeEntry(type);
357 // if (entry == null) {
358 // SchemaObject obj = simpleTypeName.get(type.getLocalPart());
359 // Inheritance inheritance = new Inheritance("");
360 // getAtomicTypeInheritance(type, obj, inheritance);
361 // sType = inheritance.atomicType.l0Type;
363 // sType = entry.l0Type;
365 // if (ontType == null)
367 // else if (!ontType.equals(sType))
368 // throw new RuntimeException(simpleType.getName() + " union has incompatible member types");
371 // if (union.getSimpleType().size() == 0)
372 // throw new RuntimeException(simpleType.getName() + " union error");
373 // for (SimpleType s : union.getSimpleType()) {
374 // if (restriction == null)
375 // restriction = s.getRestriction();
377 // Restriction r = s.getRestriction();
378 // if (!r.getBase().equals(restriction.getBase()))
379 // throw new RuntimeException(simpleType.getName() + " union has incompatible restriction bases");
382 // QName base = restriction.getBase();
383 // ontType = getL0Type(base);
385 // writer.println(" --> " + ontType);
387 // throw new RuntimeException(simpleType.getName() + " restriction error");
394 public void handleComplexType(SchemaObject topLevelComplexType) {
395 String name = getName(topLevelComplexType);
396 // if (topLevelComplexType.getName().equals("Reference"))
397 // System.out.println();
399 // String baseType = "XML.ComplexType";
401 // QName base = getComplexTypeBase(topLevelComplexType.getComplexType());
402 // if (base != null) {
403 // baseType = getType(base);
405 // base = getSimpleTypeBase(topLevelComplexType.getSimpleType());
406 // if (base != null) {
407 // baseType = getType(base);
409 Inheritance inheritance = base.getInheritance(topLevelComplexType);
411 // writer.println(name+ " <T "+baseType);
414 if (inheritance.additionalClass != null) {
415 writer.println(name+ " <T " + inheritance.baseClass + " <T " + inheritance.additionalClass);
416 } else if (inheritance.atomicType != null){
417 writer.println(name+ " <T " + inheritance.baseClass + " <T " + inheritance.atomicType.l0Type);
419 writer.println(name+ " <T " + inheritance.baseClass);
421 writer.println(name + "_List <T XML.ElementList");
424 String relationName = getName(topLevelComplexType,"has");
425 writer.println(relationName+ " <R XML.hasComplexType");
426 writer.println(" --> " + name);
427 writer.println(relationName+ "_List <R XML.hasElementList");
428 writer.println(" --> " + name + "_List");
432 // if (!baseType.equals(inheritance.baseClass))
433 // System.out.println();
434 //super.handleComplexType(topLevelComplexType);
435 base.handleComplexTypeAttributes(topLevelComplexType);
436 base.handleComplexTypeExtension(topLevelComplexType);
437 base.handleExtensionAttributes(topLevelComplexType);
442 public void handleElement(SchemaObject elementObj) {
443 Element element = elementObj.getElement();
444 String name = getName(elementObj);//element.getName();
446 if ("Text".equals(name))
447 System.out.println();
449 String type = "XML.Element";
450 Set<String> types = new LinkedHashSet<String>();
451 if (element.getType() != null) {
452 types.add(getType(element.getType(), ""));
454 QName base = this.base.getElementBase(element);
456 if (base.getNamespaceURI().equals(SchemaConversionBase.SCHEMA_NS)) {
457 String l0Type = this.base.getL0Type(base);
459 throw new RuntimeException("Cannot get L0 type for " + base.getLocalPart());
461 } else if (this.base.isElementRef(base.getLocalPart()))
462 types.add(ontRoot+base.getLocalPart());
464 types.add(ontRoot+getComplexTypePrefix()+base.getLocalPart());
466 QName substitution = element.getSubstitutionGroup();
467 if (substitution != null) {
468 if (this.base.isElementRef(substitution.getLocalPart()))
469 types.add(ontRoot+substitution.getLocalPart());
471 types.add( ontRoot+getComplexTypePrefix()+substitution.getLocalPart());
473 for (String t : types) {
477 String relationName = getName(elementObj,"has");//ontRoot+"has"+name;
478 // if (elementObj.getParent() != null) {
479 // //relationName = ontRoot+getComplexTypePrefix()+"has"+name.substring(getComplexTypePrefix().length());
480 // relationName = ontRoot+getName(elementObj.getParent()) + "has"+element.getName();
482 writer.println(relationName+ " <R XML.hasElement");
483 writer.println(relationName+ "_List <R XML.hasElementList");
485 writer.println(name+ " <T "+type);
487 LocalComplexType complexType = element.getComplexType();
488 LocalSimpleType simpleType = element.getSimpleType();
490 if (complexType != null) {
491 SchemaObject complexTypeObj = this.base.getComplexType(complexType);
492 this.base.handleElementComplexTypeAttributes(complexTypeObj);
493 this.base.handleComplexTypeExtension(complexTypeObj);
494 } else if (simpleType != null) {
495 SchemaObject simpleTypeObj = this.base.getSimpleType(simpleType);
496 this.base.handleElementSimpleTypeAttributes(simpleTypeObj);
499 List<IDReference> references = this.base.getIDReferences(element);
501 for (IDReference ref : references) {
502 writer.println(name+"."+ref.getReference().getName()+ " <R XML.hasReference");
509 public String getBaseClass(ObjectType type) {
510 if (type == ObjectType.ELEMENT)
511 return "XML.Element";
512 if (type == ObjectType.COMPLEX_TYPE)
513 return "XML.ComplexType";
514 throw new RuntimeException("ObjectType " + type + " has no base class");
518 public String getName(SchemaObject obj) {
519 if (obj.getParent() == null) {
520 switch (obj.getType()) {
522 return ontRoot+getComplexTypePrefix()+obj.getName();
524 return ontRoot+obj.getName();
525 case ATTRIBUTE_GROUP:
526 return ontRoot+getAttributeGroupPrefix()+obj.getName();
528 return ontRoot+getSimpleTypePrefix()+obj.getName();
531 SchemaObject o = obj;
532 SchemaObject prev = null;
535 if (o.getName() != null)
536 name = o.getName()+"."+name;
540 name = name.substring(0, name.length()-1);
541 switch (prev.getType()) {
543 return ontRoot+getComplexTypePrefix()+name;
546 case ATTRIBUTE_GROUP:
547 return ontRoot+getAttributeGroupPrefix()+name;
549 return ontRoot+getSimpleTypePrefix()+name;
552 throw new RuntimeException();
556 public String getName(SchemaObject parent, SchemaElement e, String rel) {
557 QName ref = e.getElement().getRef();
559 return converter.getShortName(ref.getNamespaceURI()) + rel + ref.getLocalPart();
562 return getName(parent, "") + "." + rel + e.getElement().getName();
566 public String getName(SchemaObject obj, String rel) {
567 if (obj.getParent() == null) {
568 switch (obj.getType()) {
570 return ontRoot+getComplexTypePrefix()+rel+obj.getName();
572 return ontRoot+rel+obj.getName();
573 case ATTRIBUTE_GROUP:
574 return ontRoot+getAttributeGroupPrefix()+rel+obj.getName();
576 return ontRoot+getSimpleTypePrefix()+rel+obj.getName();
579 SchemaObject o = obj;
580 SchemaObject prev = null;
583 if (o.getName() != null)
584 name = o.getName()+"."+name;
588 name = name.substring(0, name.length()-1);
589 switch (prev.getType()) {
591 return ontRoot+getComplexTypePrefix()+rel+name;
593 return ontRoot+rel+name;
594 case ATTRIBUTE_GROUP:
595 return ontRoot+getAttributeGroupPrefix()+rel+name;
597 return ontRoot+getSimpleTypePrefix()+rel+name;
600 throw new RuntimeException();