From: Marko Luukkainen Date: Thu, 2 Feb 2017 12:45:59 +0000 (+0200) Subject: Schema parsing can access data from other schemas (work in progress) X-Git-Tag: v1.31.0~39 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=f11cbe76b3f4be142c9f84ef9a7b6bc9dcc8ff23;p=simantics%2Finterop.git Schema parsing can access data from other schemas (work in progress) refs #6985 Change-Id: Ieb98b942605b85ab81e25c7fa93fcc62e387ed32 --- 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 d1f67a4..dc82a9f 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 @@ -456,7 +456,7 @@ public class ExporterGenerator extends JavaGenerator{ return; } else { if (simpleType == null) { - SchemaObject simpleTypeObj = base.simpleTypeName.get(primitiveType.getLocalPart()); + SchemaObject simpleTypeObj = base.getSimpleType(primitiveType); if (simpleTypeObj != null) simpleType = simpleTypeObj.getSimpleType(); } @@ -524,7 +524,7 @@ public class ExporterGenerator extends JavaGenerator{ public void handle(SchemaObject parent, AttributeGroup attribute) { if (parent != null) { FileWriter fw = getWriter(parent); - NamedAttributeGroup group = this.base.getAttributeGroup(attribute.getRef().getLocalPart()); + 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()) { @@ -667,7 +667,7 @@ public class ExporterGenerator extends JavaGenerator{ fw.writer.println(" "+getOntologyImport()); fw.writer.println(" for (Statement attribute : attributes) {"); if (complexType != null) { - SchemaObject obj = this.base.complexTypes.get(complexType); + SchemaObject obj = this.base.getComplexType(complexType); this.base.handleElementComplexTypeAttributes(obj); } fw.writer.println(" }"); @@ -717,7 +717,7 @@ public class ExporterGenerator extends JavaGenerator{ fw.writer.println(" super(graph);"); fw.writer.println(" "+getOntologyImport()); if (complexType != null) { - SchemaObject obj = this.base.complexTypes.get(complexType); + SchemaObject obj = this.base.getComplexType(complexType); this.base.handleComplexTypeExtension(obj); } else if (simpleType != null) { 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 2e5fb27..3d137cf 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 @@ -390,7 +390,7 @@ public class ImporterGenerator extends JavaGenerator{ return; } else { if (simpleType == null) { - SchemaObject simpleTypeObj = this.base.simpleTypeName.get(primitiveType.getLocalPart()); + SchemaObject simpleTypeObj = this.base.getSimpleType(primitiveType);//this.base.simpleTypeName.get(primitiveType.getLocalPart()); if (simpleTypeObj != null) simpleType = simpleTypeObj.getSimpleType(); } @@ -458,7 +458,7 @@ public class ImporterGenerator extends JavaGenerator{ public void handle(SchemaObject parent, AttributeGroup attribute) { if (parent != null) { FileWriter fw = getWriter(parent); - NamedAttributeGroup group = this.base.getAttributeGroup(attribute.getRef().getLocalPart()); + 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()) { @@ -470,6 +470,9 @@ public class ImporterGenerator extends JavaGenerator{ handle(obj,(AttributeGroup)annotated); } } + //dd + if(true) + System.out.println(); fw.writer.println(commentTag+" End of AttributeGroup " + group.getName()); } @@ -593,7 +596,7 @@ public class ImporterGenerator extends JavaGenerator{ LocalSimpleType simpleType = element.getSimpleType(); if (complexType != null) { - SchemaObject obj = this.base.complexTypes.get(complexType); + SchemaObject obj = this.base.getComplexType(complexType); this.base.handleElementComplexTypeAttributes(obj); } fw.writer.println(" }"); @@ -620,7 +623,7 @@ public class ImporterGenerator extends JavaGenerator{ fw.writer.println(" super();"); if (complexType != null) { - SchemaObject obj = this.base.complexTypes.get(complexType); + SchemaObject obj = this.base.getComplexType(complexType); this.base.handleComplexTypeExtension(obj); } else if (simpleType != null) { diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java index 126dcf4..72b2956 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java @@ -258,8 +258,8 @@ public abstract class JavaGenerator implements SchemaConversionComponent { String binding = this.base.getBindingFromPrimitiveType(refType); if (binding == null) { - SchemaObject refElement = this.base.elementName.get(refType.getLocalPart()); - SchemaObject refComplexType = this.base.complexTypeName.get(refType.getLocalPart()); + SchemaObject refElement = this.base.getElement(refType); + SchemaObject refComplexType = this.base.getComplexType(refType); // prefer element reference over complex type reference String primaryClassName = null; 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 f98a85f..2de5908 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 @@ -144,7 +144,7 @@ public class OntologyGenerator implements SchemaConversionComponent { type = base.getL0TypeFromPrimitiveType(refType); } if (type == null) { - SchemaObject obj = base.getWithName(parent, refType.getLocalPart()); + SchemaObject obj = base.getWithName(refType); types.add(getName(obj,"has")); } } @@ -197,7 +197,7 @@ public class OntologyGenerator implements SchemaConversionComponent { if (refType == RefType.Type) { referenceType = element.getElement().getType(); //refName = element.getElement().getName() - SchemaObject eObj = base.elements.get(element.getElement()); + SchemaObject eObj = base.getElement(element.getElement());//base.elements.get(element.getElement()); if (refName == null) refName = eObj.getName(); } else { @@ -208,7 +208,7 @@ public class OntologyGenerator implements SchemaConversionComponent { String type = base.getL0TypeFromPrimitiveType(referenceType); SchemaObject obj = null; if (type == null) { - obj = base.getWithName(parent, referenceType.getLocalPart()); + obj = base.getWithName(referenceType); writer.println(getName(parent)+".has"+refName + " " + getName(obj)); @@ -524,11 +524,11 @@ public class OntologyGenerator implements SchemaConversionComponent { LocalSimpleType simpleType = element.getSimpleType(); if (complexType != null) { - SchemaObject complexTypeObj = this.base.complexTypes.get(complexType); + SchemaObject complexTypeObj = this.base.getComplexType(complexType); this.base.handleElementComplexTypeAttributes(complexTypeObj); this.base.handleComplexTypeExtension(complexTypeObj); } else if (simpleType != null) { - SchemaObject simpleTypeObj = this.base.simpleTypes.get(simpleType); + SchemaObject simpleTypeObj = this.base.getSimpleType(simpleType); this.base.handleElementSimpleTypeAttributes(simpleTypeObj); } diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java index 3e6c881..2bca88d 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java @@ -196,41 +196,147 @@ public final class SchemaConversionBase { } } - protected Map elementName = new HashMap<>(); - protected Map complexTypeName = new HashMap<>(); - protected Map simpleTypeName = new HashMap<>(); - protected Map modelGroupName = new HashMap<>(); - protected Map elements = new HashMap<>(); - protected Map complexTypes = new HashMap<>(); - protected Map simpleTypes = new HashMap<>(); - protected Map modelGroups = new HashMap<>(); - - - protected SchemaObject getWithName(SchemaObject referrer, String name) { - SchemaObject obj = elementName.get(name); + private Map elementName = new HashMap<>(); + private Map complexTypeName = new HashMap<>(); + private Map simpleTypeName = new HashMap<>(); + private Map modelGroupName = new HashMap<>(); + private Map elements = new HashMap<>(); + private Map complexTypes = new HashMap<>(); + private Map simpleTypes = new HashMap<>(); + private Map modelGroups = new HashMap<>(); + + private SchemaObject _getWithName(QName name) { + SchemaObject obj = elementName.get(name.getLocalPart()); if (obj == null) - obj = complexTypeName.get(name); + obj = complexTypeName.get(name.getLocalPart()); if (obj == null) - obj = simpleTypeName.get(name); - if (obj == null) { - throw new RuntimeException("Cannot locate referred type " + name + " when handling " + referrer.getName()); - } + obj = simpleTypeName.get(name.getLocalPart()); return obj; } - protected SchemaObject getWithName(String name) { - SchemaObject obj = elementName.get(name); - if (obj == null) - obj = complexTypeName.get(name); - if (obj == null) - obj = simpleTypeName.get(name); - if (obj == null) { - throw new RuntimeException("Cannot locate referred type " + name); + protected SchemaObject getWithName(QName name) { + SchemaObject obj = _getWithName(name); + if (obj != null) + return obj; + if (name.getNamespaceURI() != null) { + for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { + if (sc.base != null) { + obj = sc.base._getWithName(name); + if (obj != null) { + return obj; + } + } + } } - return obj; + return null; + } + + private NamedAttributeGroup _getAttributeGroup(QName name) { + for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { + if (attrs instanceof NamedAttributeGroup) { + NamedAttributeGroup group = (NamedAttributeGroup)attrs; + if (group.getName().equals(name.getLocalPart())) + return group; + } + } + return null; + } + + public NamedAttributeGroup getAttributeGroup(QName name) { + NamedAttributeGroup group = _getAttributeGroup(name); + if (group != null) + return group; + if (name.getNamespaceURI() != null) { + for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { + if (sc.base != null) { + group = sc.base._getAttributeGroup(name); + if (group != null) { + return group; + } + } + } + } + return null; + } + + private SchemaObject _getElement(QName name) { + return elementName.get(name.getLocalPart()); + } + + protected SchemaObject getElement(QName name) { + SchemaObject obj = _getElement(name); + if (obj != null) + return obj; + if (name.getNamespaceURI() != null) { + for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { + if (sc.base != null) { + obj = sc.base._getElement(name); + if (obj != null) { + return obj; + } + } + } + } + return null; + } + + protected SchemaObject getElement(Element element) { + return elements.get(element); + } + + + private SchemaObject _getComplexType(QName name) { + return complexTypeName.get(name.getLocalPart()); + } + + protected SchemaObject getComplexType(QName name) { + SchemaObject obj = _getComplexType(name); + if (obj != null) + return obj; + if (name.getNamespaceURI() != null) { + for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { + if (sc.base != null) { + obj = sc.base._getComplexType(name); + if (obj != null) { + return obj; + } + } + } + } + return null; + } + + protected SchemaObject getComplexType(ComplexType complexType) { + return complexTypes.get(complexType); + } + + private SchemaObject _getSimpleType(QName name) { + return simpleTypeName.get(name.getLocalPart()); + } + + protected SchemaObject getSimpleType(QName name) { + SchemaObject obj = _getSimpleType(name); + if (obj != null) + return obj; + if (name.getNamespaceURI() != null) { + for (SchemaConverter sc : converter.getConverter(name.getNamespaceURI())) { + if (sc.base != null) { + obj = sc.base._getSimpleType(name); + if (obj != null) { + return obj; + } + } + } + } + return null; + } + + protected SchemaObject getSimpleType(SimpleType simpleType) { + return simpleTypes.get(simpleType); } protected SchemaObject getWithObj(SchemaObject referrer, OpenAttrs attrs) { + // FIXME : this method cannot handle references to other schemas. SchemaObject obj = null; if (attrs instanceof Element) obj = elements.get(attrs); @@ -492,13 +598,6 @@ public final class SchemaConversionBase { return null; } - - - - - - - private void handleAttributes(SchemaObject complexType, List attributeOrAttributeGroup) { //name = getComplexTypePrefix()+complexType.getName() @@ -763,6 +862,17 @@ public final class SchemaConversionBase { } } } else { + if (choices.size() == 1 && sequences.size() == 0 && alls.size() == 0 && groups.size() == 0) { + // special case: handle lone choice inside sequence with maxOccurs > 1 + SchemaElement choice = choices.get(0); + // move multiplicity restrictions to choice + if (indicator.getRestriction().max == -1 || (choice.getRestriction().max > 0 && indicator.getRestriction().max > choice.getRestriction().max)) + choice.getRestriction().max = indicator.getRestriction().max; + if (indicator.getRestriction().min == 0 || choice.getRestriction().min > indicator.getRestriction().min) + choice.getRestriction().min = indicator.getRestriction().min; + handle(parent, choice, elements); + return; + } if (sequences.size() > 0 || choices.size() > 0 || alls.size() > 0 || groups.size() > 0) { throw new RuntimeException("Cannot handle Sequence with inner ExplicitGroups"); } @@ -915,17 +1025,6 @@ public final class SchemaConversionBase { return simpleTypeName.containsKey(ref); } - public NamedAttributeGroup getAttributeGroup(String name) { - for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) { - if (attrs instanceof NamedAttributeGroup) { - NamedAttributeGroup group = (NamedAttributeGroup)attrs; - if (group.getName().equals(name)) - return group; - } - } - return null; - } - public IDProvider getIDProvider(Element element) { List idProviders = new ArrayList(2); for (JAXBElement e : configuration.getConversionRule()) { @@ -1279,7 +1378,7 @@ public final class SchemaConversionBase { QName type = getElementBase(topLevelElement); if (type != null) { if (!type.getNamespaceURI().equals(SCHEMA_NS)) { - SchemaObject obj = getWithName(topLevelObj, type.getLocalPart()); + SchemaObject obj = getWithName(type); inheritance.baseClass = getName(obj); inheritance.type = InheritanceType.ComplexType; } else { @@ -1295,7 +1394,7 @@ public final class SchemaConversionBase { QName type = topLevelElement.getSubstitutionGroup(); if (type != null) { if (!type.getNamespaceURI().equals(SCHEMA_NS)) { - SchemaObject obj = getWithName(topLevelObj, type.getLocalPart()); + SchemaObject obj = getWithName(type); inheritance.baseClass = getName(obj); inheritance.type = InheritanceType.ComplexType; } else { @@ -1338,7 +1437,8 @@ public final class SchemaConversionBase { */ public void getAtomicTypeInheritance(QName type, Inheritance inheritance) { if (!type.getNamespaceURI().equals(SCHEMA_NS)) { - SchemaObject obj = getWithName(type.getLocalPart()); + SchemaObject obj = getWithName(type); + System.out.println(); if (obj.getType() != ObjectType.SIMPLE_TYPE) throw new RuntimeException("SimpleContent does not use SimpleType definition"); SimpleType simpleType = obj.getSimpleType(); diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java index c716754..ed11c14 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java @@ -16,6 +16,7 @@ import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; +import org.simantics.utils.datastructures.MapList; import org.simantics.xml.sax.configuration.Configuration; import org.w3._2001.xmlschema.Annotation; import org.w3._2001.xmlschema.Import; @@ -49,8 +50,11 @@ public class SchemaConverter { private List parent = new ArrayList<>(); private List subConverters = new ArrayList<>(); - private Map nameMap; + private Map fileMap; + private MapList schemaNSMap; + private MapList shortNameMap; + String schemaNs; String ontologyUri; String className; String name; @@ -88,9 +92,15 @@ public class SchemaConverter { this.parent.add(parent); parent.subConverters.add(this); } else { - nameMap = new HashMap<>(); + fileMap = new HashMap<>(); + schemaNSMap = new MapList<>(); + shortNameMap = new MapList<>(); } - getRoot().nameMap.put(schemaFile.getAbsolutePath(), this); + getRoot().fileMap.put(schemaFile.getAbsolutePath(), this); + } + + public List getConverter(String schemaNS) { + return getRoot().schemaNSMap.getValues(schemaNS); } public void setFileImport(ManualSchemaFileImport fileImport) { @@ -120,7 +130,7 @@ public class SchemaConverter { if (!schemaFile.exists()) throw new FileNotFoundException(schemaFile.getAbsolutePath()); } - SchemaConverter subConverter = getRoot().nameMap.get((schemaFile.getAbsolutePath())); + SchemaConverter subConverter = getRoot().fileMap.get((schemaFile.getAbsolutePath())); if (subConverter == null) { subConverter = new SchemaConverter(this,schemaFile, conversionFile, outputPlugin); subConverter.createPGraph = this.createPGraph; @@ -147,6 +157,38 @@ public class SchemaConverter { boolean init = false; + protected void assignShortName() { + shortName = name.substring(0, 3).toUpperCase(); + SchemaConverter root = getRoot(); + if (!root.shortNameMap.containsKey(shortName)) { + root.shortNameMap.add(shortName, this); + return; + } else { + SchemaConverter sc = root.shortNameMap.getValues(shortName).get(0); + if (sc.schemaNs.equals(schemaNs)) { + root.shortNameMap.add(shortName, this); + return; + } + } + int i = 1; + while (true) { + String n = shortName+i; + if (!root.shortNameMap.containsKey(n)) { + shortName = n; + root.shortNameMap.add(shortName, this); + return; + } else { + SchemaConverter sc = root.shortNameMap.getValues(n).get(0); + if (sc.schemaNs.equals(schemaNs)) { + shortName = n; + root.shortNameMap.add(shortName, this); + return; + } + } + i++; + } + } + protected void init() throws IOException, JAXBException { if (init) return; @@ -172,8 +214,8 @@ public class SchemaConverter { header[2] = "File " + schemaFile.getAbsolutePath(); header[3] = "Date " + new Date().toString(); - - ontologyUri = schema.getTargetNamespace(); + schemaNs = schema.getTargetNamespace(); + ontologyUri = schemaNs; if (ontologyUri == null) { ontologyUri = getSchemaFile().getName(); @@ -197,7 +239,9 @@ public class SchemaConverter { className = getPluginName() + "." + name; - shortName = name.substring(0, 3).toUpperCase(); + assignShortName(); + if (schemaNs != null) + getRoot().schemaNSMap.add(schemaNs, this); for (OpenAttrs attrs : schema.getIncludeOrImportOrRedefine()) {