1 package org.simantics.xml.sax;
\r
3 import java.io.FileNotFoundException;
\r
4 import java.io.PrintWriter;
\r
5 import java.io.StringWriter;
\r
6 import java.util.ArrayList;
\r
7 import java.util.LinkedHashSet;
\r
8 import java.util.List;
\r
9 import java.util.Set;
\r
11 import javax.xml.namespace.QName;
\r
13 import org.simantics.utils.datastructures.BijectionMap;
\r
14 import org.simantics.xml.sax.SchemaConversionBase.Inheritance;
\r
15 import org.simantics.xml.sax.SchemaConversionBase.InheritanceType;
\r
16 import org.simantics.xml.sax.SchemaConversionBase.RefType;
\r
17 import org.simantics.xml.sax.SchemaObject.ObjectType;
\r
18 import org.simantics.xml.sax.configuration.AttributeComposition;
\r
19 import org.simantics.xml.sax.configuration.IDReference;
\r
20 import org.w3._2001.xmlschema.Annotated;
\r
21 import org.w3._2001.xmlschema.Attribute;
\r
22 import org.w3._2001.xmlschema.AttributeGroup;
\r
23 import org.w3._2001.xmlschema.Element;
\r
24 import org.w3._2001.xmlschema.LocalComplexType;
\r
25 import org.w3._2001.xmlschema.LocalSimpleType;
\r
26 import org.w3._2001.xmlschema.NamedAttributeGroup;
\r
27 import org.w3._2001.xmlschema.NamedGroup;
\r
28 import org.w3._2001.xmlschema.Restriction;
\r
29 import org.w3._2001.xmlschema.Schema;
\r
30 import org.w3._2001.xmlschema.SimpleType;
\r
32 //public class OntologyGenerator extends SchemaConversionBase {
\r
33 public class OntologyGenerator implements SchemaConversionComponent {
\r
34 SchemaConversionBase base;
\r
36 public OntologyGenerator(SchemaConverter converter, SchemaConversionBase base) {
\r
38 this.converter = converter;
\r
39 this.schema = base.schema;
\r
40 this.ontologyUri = base.ontologyURI;
\r
41 this.className = base.className;
\r
44 String ontRoot = "ONT.";
\r
45 String commentTag = "//";
\r
51 SchemaConverter converter;
\r
53 PrintWriter writer = null;
\r
55 public void createOntology() throws FileNotFoundException {
\r
56 StringWriter stringWriter = null;
\r
57 if (converter.getOntologyFile() == null) {
\r
58 stringWriter = new StringWriter();
\r
59 writer = new PrintWriter(stringWriter);
\r
61 writer = new PrintWriter(converter.getOntologyFile());
\r
68 if (stringWriter != null)
\r
69 System.out.println(stringWriter.toString());
\r
72 protected void handle() {
\r
73 ontRoot = converter.shortName;
\r
75 for (String s : converter.getHeader()) {
\r
76 writer.println(commentTag + " " + s);
\r
79 writer.println("L0 = <http://www.simantics.org/Layer0-1.1>");
\r
80 writer.println("XML = <http://www.simantics.org/XML-1.0>");
\r
82 writer.println(ontRoot + " = <" + ontologyUri +"> : L0.Ontology");
\r
83 writer.println(" @L0.new");
\r
84 writer.println(" L0.HasResourceClass \"" + className +"\"");
\r
89 writer.println(ontRoot+"ComplexTypes : L0.Library");
\r
90 writer.println(ontRoot+"AttributeGroups : L0.Library");
\r
92 writer.println(commentTag + " Interpreted from schema");
\r
98 protected String getType(QName qtype) {
\r
99 String ontType = base.getL0TypeFromPrimitiveType(qtype);
\r
100 if (ontType != null)
\r
102 else if (base.isComplexTypeRef(qtype.getLocalPart()))
\r
103 return ontRoot+getComplexTypePrefix()+qtype.getLocalPart();
\r
104 else if (base.isSimpleTypeRef(qtype.getLocalPart()))
\r
105 return ontRoot+qtype.getLocalPart();
\r
106 else if (base.isElementRef(qtype.getLocalPart()))
\r
107 return ontRoot+qtype.getLocalPart();
\r
108 else if (qtype.getPrefix() != null && qtype.getPrefix().length() > 0) {
\r
109 return ontRoot+qtype.getPrefix()+qtype.getLocalPart();
\r
111 throw new RuntimeException("Reference to unknown type " + qtype.getLocalPart());
\r
114 public String getComplexTypePrefix() {
\r
115 return "ComplexTypes.";
\r
118 public String getAttributeGroupPrefix() {
\r
119 return "AttributeGroups.";
\r
122 public void handleChoice(SchemaObject parent, SchemaElement indicator, java.util.List<SchemaElement> elements, String name) {
\r
123 boolean single = true;
\r
124 for (SchemaElement e : elements) {
\r
125 if (e.getRestriction().many()) {
\r
130 String relationName = getName(parent)+".has"+name;
\r
131 writer.print(relationName);
\r
133 List<String> types = new ArrayList<String>();
\r
134 for (SchemaElement e : elements) {
\r
135 Element localElement = e.getElement();
\r
136 QName refType = null;
\r
137 String type = null;
\r
139 if (localElement.getName() != null) {
\r
140 refType = localElement.getType();
\r
141 type = base.getL0TypeFromPrimitiveType(refType);
\r
142 } else if (localElement.getRef() != null) {
\r
143 refType = localElement.getRef();
\r
144 type = base.getL0TypeFromPrimitiveType(refType);
\r
146 if (type == null) {
\r
147 SchemaObject obj = base.getWithName(parent, refType.getLocalPart());
\r
148 types.add(getName(obj,"has"));
\r
151 if (types.size() > 0) {
\r
152 for (String type : types) {
\r
153 writer.print(" <R " + type);
\r
156 writer.print(" <R XML.hasElement");
\r
161 for (SchemaElement e : elements) {
\r
162 Element localElement = e.getElement();
\r
163 QName refType = null;
\r
164 String type = null;
\r
165 if (localElement.getName() != null) {
\r
166 refType = localElement.getType();
\r
167 type = getType(refType);
\r
168 } else if (localElement.getRef() != null) {
\r
169 refType = localElement.getRef();
\r
170 type = getType(refType);
\r
172 if (type != null) {
\r
173 writer.println(" --> " + type);
\r
177 writer.println(ontRoot+name+ "List <T XML.ElementList");
\r
178 if (types.size() == 0) {
\r
179 writer.println(relationName+ "List <R XML.hasElementList : L0.FunctionalRelation");
\r
181 writer.print(relationName+ "List");
\r
182 for (String type : types) {
\r
183 writer.print(" <R " + type+"List");
\r
185 writer.println(" : L0.FunctionalRelation");
\r
187 writer.println(" --> " + ontRoot+name+"List");
\r
194 public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType) {
\r
195 if (refType != refType.Element) {
\r
196 QName referenceType = null;
\r
197 if (refType == RefType.Type) {
\r
198 referenceType = element.getElement().getType();
\r
199 //refName = element.getElement().getName()
\r
200 SchemaObject eObj = base.elements.get(element.getElement());
\r
201 if (refName == null)
\r
202 refName = eObj.getName();
\r
204 referenceType = element.getElement().getRef();
\r
205 if (refName == null)
\r
206 refName = referenceType.getLocalPart();
\r
208 String type = base.getL0TypeFromPrimitiveType(referenceType);
\r
209 SchemaObject obj = null;
\r
210 if (type == null) {
\r
211 obj = base.getWithName(parent, referenceType.getLocalPart());
\r
213 writer.println(getName(parent)+".has"+refName + " <R " + getName(obj,"has"));
\r
214 writer.println(" --> " + getName(obj));
\r
216 writer.println(getName(parent)+".has"+refName + " <R XML.hasElement");
\r
217 writer.println(" --> " + getType(referenceType));
\r
220 if (base.useElementList(parent, indicator,element, refType == RefType.Reference, refName, referenceType)) {
\r
222 if (type == null) {
\r
223 writer.println(getName(parent)+"."+refName + "List <T XML.ElementList");
\r
224 writer.println(getName(parent)+".has"+refName + "List <R " + getName(obj,"has")+"List : L0.FunctionalRelation");
\r
226 writer.println(getName(parent)+"."+refName + "List <T XML.ElementList");
\r
227 writer.println(getName(parent)+".has"+refName + "List <R XML.hasElementList : L0.FunctionalRelation");
\r
231 Element attrs = element.getElement();
\r
232 SchemaObject obj = base.getWithObj(parent, attrs);
\r
233 if (refName == null)
\r
234 refName = obj.getName();
\r
236 writer.println(getName(parent)+".has"+refName + " <R " + getName(obj,"has"));
\r
237 writer.println(" --> " + getName(obj));
\r
238 if (base.useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) {
\r
239 writer.println(getName(parent)+"."+refName + "List <T XML.ElementList");
\r
240 writer.println(getName(parent)+".has"+refName + "List <R " + getName(obj,"has")+"List : L0.FunctionalRelation");
\r
247 public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any) {
\r
252 public void handle(SchemaObject parent, NamedGroup attribute) {
\r
253 // TODO Auto-generated method stub
\r
258 public void handle(SchemaObject parent, Attribute attribute) {
\r
259 String name = attribute.getName();
\r
260 QName primitiveType = attribute.getType();
\r
261 LocalSimpleType simpleType = attribute.getSimpleType();
\r
262 QName ref = attribute.getRef();
\r
264 String relationName;
\r
265 String relationType;
\r
266 if (name != null) {
\r
267 relationName = ontRoot+"has"+name;
\r
268 if (parent != null)
\r
269 relationName = getName(parent)+".has"+name;
\r
270 relationType = "XML.hasAttribute";
\r
272 else if (ref != null && parent != null) {
\r
273 relationName = getName(parent)+".has"+ref.getLocalPart();
\r
274 relationType = ontRoot+"has"+ref.getLocalPart();
\r
276 throw new RuntimeException();
\r
278 boolean id = false;
\r
279 String ontType = null;
\r
280 if (primitiveType != null) {
\r
281 ontType = base.getL0TypeFromPrimitiveType(primitiveType);
\r
282 if (ontType != null) {
\r
283 id = base.getTypeEntry(primitiveType).id;
\r
285 relationType = "XML.hasID";
\r
289 } else if (simpleType != null){
\r
290 Restriction restriction = simpleType.getRestriction();
\r
291 if (restriction == null || simpleType.getUnion() != null || simpleType.getName() != null || simpleType.getId() != null)
\r
292 throw new RuntimeException();
\r
293 QName base = restriction.getBase();
\r
296 ontType = this.base.getL0TypeFromPrimitiveType(base);
\r
298 // for (Object facetWrap : restriction.getFacets()) {
\r
299 // JAXBElement<?> element = (JAXBElement<?>)facetWrap;
\r
300 // QName elementName = element.getName();
\r
301 // Facet facet = (Facet)element.getValue();
\r
307 writer.println(relationName+ " <R " + relationType + ": L0.FunctionalRelation");
\r
309 // no need to add range restriction
\r
310 } else if (ontType != null) {
\r
311 writer.println(" --> " + ontType);
\r
312 } else if (primitiveType != null) {
\r
313 writer.println(" <R "+ontRoot+"has"+primitiveType.getLocalPart());
\r
318 public void handleAttributes(SchemaObject simpleTypeObj) {
\r
319 // SchemaObject parent = simpleTypeObj.getParent();
\r
320 // SimpleType simpleType = simpleTypeObj.getSimpleType();
\r
321 // Restriction restriction = simpleType.getRestriction();
\r
322 // QName base = restriction.getBase();
\r
323 // String ontType = getL0TypeFromPrimitiveType(base);
\r
327 public void handle(SchemaObject parent, AttributeGroup attributeGroup) {
\r
328 if (parent == null) {
\r
329 NamedAttributeGroup group = (NamedAttributeGroup)attributeGroup;
\r
330 writer.println(ontRoot+getAttributeGroupPrefix()+group.getName()+ " <T XML.AttributeGroup");
\r
331 SchemaObject obj = new SchemaObject(parent,attributeGroup);
\r
332 for (Annotated annotated : group.getAttributeOrAttributeGroup()) {
\r
333 if (annotated instanceof Attribute) {
\r
334 //handle(getAttributeGroupPrefix()+group.getName(),(Attribute)annotated);
\r
335 handle(obj,(Attribute)annotated);
\r
336 } else if (annotated instanceof AttributeGroup) {
\r
337 handle(obj,(AttributeGroup)annotated);
\r
338 //throw new RuntimeException("Cannot handle nested attribute groups");
\r
342 writer.println(getName(parent) +" L0.Inherits " + ontRoot + getAttributeGroupPrefix() + attributeGroup.getRef().getLocalPart());
\r
348 public void handleAttributeComposition(SchemaObject parent, AttributeComposition composition, BijectionMap<org.simantics.xml.sax.configuration.Attribute, Annotated> attributes) {
\r
349 Attribute compositionAttribute = new Attribute();
\r
350 compositionAttribute.setName(composition.getName());
\r
351 QName type = new QName(SchemaConversionBase.CONVERSION_NS, composition.getType());
\r
352 compositionAttribute.setType(type);
\r
353 handle(parent, compositionAttribute);
\r
357 public void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) {
\r
358 SimpleType simpleType = simpleTypeObj.getSimpleType();
\r
359 String name = simpleType.getName();
\r
361 org.w3._2001.xmlschema.List list = simpleType.getList();
\r
362 if (list != null) {
\r
363 // TODO : process restriction in lists
\r
364 String relationName = ontRoot+"has"+name;
\r
365 if (parent != null)
\r
366 relationName = getName(parent)+".has"+name;
\r
367 writer.println(relationName+ " <R XML.hasAttribute : L0.FunctionalRelation");
\r
369 String ontType = base.getL0Type(new QName(SchemaConversionBase.SCHEMA_NS, "string"));
\r
370 writer.println(" --> " + ontType);
\r
372 String relationName = ontRoot+"has"+name;
\r
373 if (parent != null)
\r
374 relationName = getName(parent)+".has"+name;
\r
376 writer.println(relationName+ " <R XML.hasAttribute : L0.FunctionalRelation");
\r
378 QName base = this.base.getSimpleTypeBase(simpleType);
\r
379 Inheritance inheritance = new Inheritance("");
\r
380 this.base.getAtomicTypeInheritance(base, inheritance);
\r
381 if (inheritance.atomicType == null)
\r
382 throw new RuntimeException("Could not locate atomic type for SimpleType " + simpleType.getName());
\r
383 writer.println(" --> " + inheritance.atomicType.l0Type);
\r
385 // Restriction restriction = simpleType.getRestriction();
\r
386 // if (restriction != null) {
\r
388 // QName base = restriction.getBase();
\r
389 // String ontType = getL0Type(base);
\r
390 // writer.println(" --> " + ontType);
\r
391 // } else if (simpleType.getId() != null) {
\r
392 // throw new RuntimeException(simpleType.getName() + " restriction error");
\r
393 // } else if (simpleType.getUnion() != null) {
\r
394 // Union union = simpleType.getUnion();
\r
395 // String ontType = null;
\r
396 // if (union.getMemberTypes().size() > 0) {
\r
397 // for (QName type : union.getMemberTypes()) {
\r
398 // String sType = null;
\r
399 // TypeEntry entry = getTypeEntry(type);
\r
400 // if (entry == null) {
\r
401 // SchemaObject obj = simpleTypeName.get(type.getLocalPart());
\r
402 // Inheritance inheritance = new Inheritance("");
\r
403 // getAtomicTypeInheritance(type, obj, inheritance);
\r
404 // sType = inheritance.atomicType.l0Type;
\r
406 // sType = entry.l0Type;
\r
408 // if (ontType == null)
\r
409 // ontType = sType;
\r
410 // else if (!ontType.equals(sType))
\r
411 // throw new RuntimeException(simpleType.getName() + " union has incompatible member types");
\r
414 // if (union.getSimpleType().size() == 0)
\r
415 // throw new RuntimeException(simpleType.getName() + " union error");
\r
416 // for (SimpleType s : union.getSimpleType()) {
\r
417 // if (restriction == null)
\r
418 // restriction = s.getRestriction();
\r
420 // Restriction r = s.getRestriction();
\r
421 // if (!r.getBase().equals(restriction.getBase()))
\r
422 // throw new RuntimeException(simpleType.getName() + " union has incompatible restriction bases");
\r
425 // QName base = restriction.getBase();
\r
426 // ontType = getL0Type(base);
\r
428 // writer.println(" --> " + ontType);
\r
430 // throw new RuntimeException(simpleType.getName() + " restriction error");
\r
437 public void handleComplexType(SchemaObject topLevelComplexType) {
\r
438 String name = getName(topLevelComplexType);
\r
439 // if (topLevelComplexType.getName().equals("Reference"))
\r
440 // System.out.println();
\r
442 String relationName = getName(topLevelComplexType,"has");//ontRoot+"has"+name;
\r
444 writer.println(relationName+ " <R XML.hasComplexType");
\r
445 writer.println(relationName+ "List <R XML.hasElementList");
\r
446 //writer.println(" --> " + ontRoot+getComplexTypePrefix()+name);
\r
447 writer.println(" --> " + name);
\r
449 // String baseType = "XML.ComplexType";
\r
451 // QName base = getComplexTypeBase(topLevelComplexType.getComplexType());
\r
452 // if (base != null) {
\r
453 // baseType = getType(base);
\r
455 // base = getSimpleTypeBase(topLevelComplexType.getSimpleType());
\r
456 // if (base != null) {
\r
457 // baseType = getType(base);
\r
459 Inheritance inheritance = base.getInheritance(topLevelComplexType);
\r
461 // writer.println(name+ " <T "+baseType);
\r
463 if(inheritance.type == InheritanceType.AtomicType) {
\r
464 writer.println(name+ " <T "+inheritance.baseClass + " <T "+inheritance.atomicType.l0Type);
\r
466 writer.println(name+ " <T "+inheritance.baseClass);
\r
468 // if (!baseType.equals(inheritance.baseClass))
\r
469 // System.out.println();
\r
470 //super.handleComplexType(topLevelComplexType);
\r
471 base.handleComplexTypeAttributes(topLevelComplexType);
\r
472 base.handleComplexTypeExtension(topLevelComplexType);
\r
473 base.handleExtensionAttributes(topLevelComplexType);
\r
478 public void handleElement(SchemaObject elementObj) {
\r
479 Element element = elementObj.getElement();
\r
480 String name = getName(elementObj);//element.getName();
\r
482 if ("Text".equals(name))
\r
483 System.out.println();
\r
485 String type = "XML.Element";
\r
486 Set<String> types = new LinkedHashSet<String>();
\r
487 if (element.getType() != null) {
\r
488 types.add(getType(element.getType()));
\r
490 QName base = this.base.getElementBase(element);
\r
491 if (base != null) {
\r
492 if (base.getNamespaceURI().equals(SchemaConversionBase.SCHEMA_NS)) {
\r
493 String l0Type = this.base.getL0Type(base);
\r
494 if (l0Type == null)
\r
495 throw new RuntimeException("Cannot get L0 type for " + base.getLocalPart());
\r
497 } else if (this.base.isElementRef(base.getLocalPart()))
\r
498 types.add(ontRoot+base.getLocalPart());
\r
500 types.add(ontRoot+getComplexTypePrefix()+base.getLocalPart());
\r
502 QName substitution = element.getSubstitutionGroup();
\r
503 if (substitution != null) {
\r
504 if (this.base.isElementRef(substitution.getLocalPart()))
\r
505 types.add(ontRoot+substitution.getLocalPart());
\r
507 types.add( ontRoot+getComplexTypePrefix()+substitution.getLocalPart());
\r
509 for (String t : types) {
\r
510 type += " <T " + t;
\r
513 String relationName = getName(elementObj,"has");//ontRoot+"has"+name;
\r
514 // if (elementObj.getParent() != null) {
\r
515 // //relationName = ontRoot+getComplexTypePrefix()+"has"+name.substring(getComplexTypePrefix().length());
\r
516 // relationName = ontRoot+getName(elementObj.getParent()) + "has"+element.getName();
\r
518 writer.println(relationName+ " <R XML.hasElement");
\r
519 writer.println(relationName+ "List <R XML.hasElementList");
\r
521 writer.println(name+ " <T "+type);
\r
523 LocalComplexType complexType = element.getComplexType();
\r
524 LocalSimpleType simpleType = element.getSimpleType();
\r
526 if (complexType != null) {
\r
527 SchemaObject complexTypeObj = this.base.complexTypes.get(complexType);
\r
528 this.base.handleElementComplexTypeAttributes(complexTypeObj);
\r
529 this.base.handleComplexTypeExtension(complexTypeObj);
\r
530 } else if (simpleType != null) {
\r
531 SchemaObject simpleTypeObj = this.base.simpleTypes.get(simpleType);
\r
532 this.base.handleElementSimpleTypeAttributes(simpleTypeObj);
\r
535 List<IDReference> references = this.base.getIDReferences(element);
\r
537 for (IDReference ref : references) {
\r
538 writer.println(name+"."+ref.getReference().getName()+ " <R XML.hasReference");
\r
545 public String getBaseClass(ObjectType type) {
\r
546 if (type == ObjectType.ELEMENT)
\r
547 return "XML.Element";
\r
548 if (type == ObjectType.COMPLEX_TYPE)
\r
549 return "XML.ComplexType";
\r
550 throw new RuntimeException("ObjectType " + type + " has no base class");
\r
554 public String getName(SchemaObject obj) {
\r
555 if (obj.getParent() == null) {
\r
556 switch (obj.getType()) {
\r
558 return ontRoot+getComplexTypePrefix()+obj.getName();
\r
560 return ontRoot+obj.getName();
\r
561 case ATTRIBUTE_GROUP:
\r
562 return ontRoot+getAttributeGroupPrefix()+obj.getName();
\r
564 return ontRoot+obj.getName();
\r
567 SchemaObject o = obj;
\r
568 SchemaObject prev = null;
\r
571 if (o.getName() != null)
\r
572 name = o.getName()+"."+name;
\r
576 name = name.substring(0, name.length()-1);
\r
577 switch (prev.getType()) {
\r
579 return ontRoot+getComplexTypePrefix()+name;
\r
581 return ontRoot+name;
\r
582 case ATTRIBUTE_GROUP:
\r
583 return ontRoot+getAttributeGroupPrefix()+name;
\r
585 return ontRoot+name;
\r
588 throw new RuntimeException();
\r
592 public String getName(SchemaObject obj, String rel) {
\r
593 if (obj.getParent() == null) {
\r
594 switch (obj.getType()) {
\r
596 return ontRoot+getComplexTypePrefix()+rel+obj.getName();
\r
598 return ontRoot+rel+obj.getName();
\r
599 case ATTRIBUTE_GROUP:
\r
600 return ontRoot+getAttributeGroupPrefix()+rel+obj.getName();
\r
602 return ontRoot+rel+obj.getName();
\r
605 SchemaObject o = obj;
\r
606 SchemaObject prev = null;
\r
609 if (o.getName() != null)
\r
610 name = o.getName()+"."+name;
\r
614 name = name.substring(0, name.length()-1);
\r
615 switch (prev.getType()) {
\r
617 return ontRoot+getComplexTypePrefix()+rel+name;
\r
619 return ontRoot+rel+name;
\r
620 case ATTRIBUTE_GROUP:
\r
621 return ontRoot+getAttributeGroupPrefix()+rel+name;
\r
623 return ontRoot+rel+name;
\r
626 throw new RuntimeException();
\r