From 070e46f1fba569c5f5cb70fba5681ffafcb88088 Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Thu, 6 Sep 2018 13:42:52 +0300 Subject: [PATCH] Handle multi-namespace XML files. gitlab #3 Change-Id: I7b067e47a415116b127ecd707b54090c8d277446 --- .../simantics/xml/sax/base/ParserElement.java | 306 +++++++++++------- .../base/XMLElementNamedChildParserBase.java | 124 +++---- .../org/simantics/xml/sax/base/XMLParser.java | 67 +++- .../simantics/xml/data/XmlDataConverter.java | 271 ++++++++-------- .../simantics/xml/sax/ImporterGenerator.java | 24 +- 5 files changed, 469 insertions(+), 323 deletions(-) diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ParserElement.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ParserElement.java index 2ab58f7..ca4a91a 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ParserElement.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ParserElement.java @@ -1,115 +1,193 @@ -package org.simantics.xml.sax.base; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.utils.ListUtils; -import org.simantics.db.exception.DatabaseException; -import org.simantics.layer0.Layer0; -import org.simantics.utils.datastructures.MapList; -import org.xml.sax.Attributes; - -public class ParserElement implements Serializable { - private static final long serialVersionUID = -5207502156942818875L; - String uri; - String localName; - String qName; - Map attributes; - MapList lists; - - XMLElementParser elementParser; - XMLParser xmlParser; - - public ParserElement(String uri, String localName, String qName, Attributes attributes) { - this.uri = uri; - this.localName = localName; - this.qName = qName; - int attrCount = attributes.getLength(); - this.attributes = new HashMap<>(attrCount); - for (int i = 0; i < attrCount; i++) { - this.attributes.put(attributes.getLocalName(i),new Attribute(attributes.getLocalName(i),attributes.getQName(i),attributes.getURI(i),attributes.getValue(i))); - } - } - - public ParserElement(String uri, String localName, String qName, Collection attributes) { - this.uri = uri; - this.localName = localName; - this.qName = qName; - this.attributes = new HashMap<>(attributes.size()); - for (Attribute a : attributes) { - this.attributes.put(a.localName,new Attribute(a.localName,a.qName,a.uri,a.value)); - } - } - - public String getUri() { - return uri; - } - - public String getLocalName() { - return localName; - } - - public String getQName() { - return qName; - } - - public Collection getAttributes() { - return attributes.values(); - } - - public Attribute getAttribute(String name) { - return attributes.get(name); - } - - private Resource data; - - public void setData(Resource data) { - this.data = data; - } - - public Resource getData() { - return data; - } - - public void setElementParser(XMLElementParser parser) { - this.elementParser = parser; - } - - public XMLElementParser getElementParser() { - return elementParser; - } - - public void setXMLParser(XMLParser parser) { - this.xmlParser = parser; - } - - public XMLParser getXMLParser() { - return xmlParser; - } - - public void registerListChild(Resource predicate, ParserElement child) { - if (lists == null) - lists = new MapList<>(); - lists.add(predicate, child); - } - - public void createLists(WriteGraph graph) throws DatabaseException { - if (lists == null) - return; - Layer0 L0 = Layer0.getInstance(graph); - for (Resource predicate : lists.getKeys()) { - List children = new ArrayList<>(); - for (ParserElement child : lists.getValues(predicate)) - children.add(child.getData()); - Resource list = ListUtils.create(graph, L0.List, L0.List_Element, null, children); - graph.claim(getData(), predicate, list); - } - } - +package org.simantics.xml.sax.base; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.ListUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.utils.datastructures.MapList; +import org.xml.sax.Attributes; + +public class ParserElement implements Serializable { + private static final long serialVersionUID = -5207502156942818875L; + private String uri; + private String localName; + private String qName; + Map attributes; + MapList lists; + + XMLElementParser elementParser; + XMLParser xmlParser; + + Map nsMap; + + private ParserElement parent; + + public ParserElement(String uri, String localName, String qName, Attributes attributes) { + this.uri = uri; + this.localName = localName; + this.qName = qName; + int attrCount = attributes.getLength(); + this.attributes = new HashMap<>(attrCount); + for (int i = 0; i < attrCount; i++) { + this.attributes.put(attributes.getLocalName(i),new Attribute(attributes.getLocalName(i),attributes.getQName(i),attributes.getURI(i),attributes.getValue(i))); + } + processNS(); + } + + public ParserElement(String uri, String localName, String qName, Collection attributes) { + this.uri = uri; + this.localName = localName; + this.qName = qName; + this.attributes = new HashMap<>(attributes.size()); + for (Attribute a : attributes) { + this.attributes.put(a.localName,new Attribute(a.localName,a.qName,a.uri,a.value)); + } + processNS(); + } + + public ParserElement(ParserElement parent, String uri, String localName, String qName, Attributes attributes) { + this.parent = parent; + this.uri = uri; + this.localName = localName; + this.qName = qName; + int attrCount = attributes.getLength(); + this.attributes = new HashMap<>(attrCount); + for (int i = 0; i < attrCount; i++) { + this.attributes.put(attributes.getLocalName(i),new Attribute(attributes.getLocalName(i),attributes.getQName(i),attributes.getURI(i),attributes.getValue(i))); + } + processNS(); + } + + public ParserElement(ParserElement parent, String uri, String localName, String qName, Collection attributes) { + this.parent = parent; + this.uri = uri; + this.localName = localName; + this.qName = qName; + this.attributes = new HashMap<>(attributes.size()); + for (Attribute a : attributes) { + this.attributes.put(a.localName,new Attribute(a.localName,a.qName,a.uri,a.value)); + } + processNS(); + } + + private void processNS() { + + nsMap = new HashMap<>(); + for (Attribute a : attributes.values()) { + if (a.localName.startsWith("xmlns")) { + String ns[] = a.localName.split(":"); + if (ns.length == 2) { + nsMap.put(ns[1], a.value); + } + } + } + Map newAttrs = new HashMap<>(); + for (Attribute a : attributes.values()) { + String ns[] = a.localName.split(":"); + if (ns.length == 2) { + if ("xmlns".equals(ns[0])) + continue; + String namespace = getNS(ns[0]); + if (namespace != null) { + if (newAttrs.put(ns[1], new Attribute(ns[1], a.qName, namespace, a.value)) != null) + throw new RuntimeException("XML parser internal error, overlapping attribute names " + ns[1]); + } else { + throw new RuntimeException("XML parser error, namespace " + ns[0] + " for attribute " + a.localName + " not defined."); + } + } else { + newAttrs.put(a.localName, a); + } + } + attributes = newAttrs; + + String nameNs[] = qName.split(":"); + if (nameNs.length == 2) { + localName = nameNs[1]; + if (uri.length() == 0) + uri = getNS(nameNs[0]) + "/"+localName; + } else if (localName.length() == 0) { + localName = qName; + } + } + + public String getNS(String key) { + String ns = nsMap.get(key); + if (ns == null && parent != null) + ns = parent.getNS(key); + return ns; + } + + public String getUri() { + return uri; + } + + public String getLocalName() { + return localName; + } + + public String getQName() { + return qName; + } + + public Collection getAttributes() { + return attributes.values(); + } + + public Attribute getAttribute(String name) { + return attributes.get(name); + } + + private Resource data; + + public void setData(Resource data) { + this.data = data; + } + + public Resource getData() { + return data; + } + + public void setElementParser(XMLElementParser parser) { + this.elementParser = parser; + } + + public XMLElementParser getElementParser() { + return elementParser; + } + + public void setXMLParser(XMLParser parser) { + this.xmlParser = parser; + } + + public XMLParser getXMLParser() { + return xmlParser; + } + + public void registerListChild(Resource predicate, ParserElement child) { + if (lists == null) + lists = new MapList<>(); + lists.add(predicate, child); + } + + public void createLists(WriteGraph graph) throws DatabaseException { + if (lists == null) + return; + Layer0 L0 = Layer0.getInstance(graph); + for (Resource predicate : lists.getKeys()) { + List children = new ArrayList<>(); + for (ParserElement child : lists.getValues(predicate)) + children.add(child.getData()); + Resource list = ListUtils.create(graph, L0.List, L0.List_Element, null, children); + graph.claim(getData(), predicate, list); + } + } + } \ No newline at end of file diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementNamedChildParserBase.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementNamedChildParserBase.java index f1bbdf3..af1853d 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementNamedChildParserBase.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementNamedChildParserBase.java @@ -1,62 +1,62 @@ -package org.simantics.xml.sax.base; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import org.simantics.db.WriteGraph; -import org.simantics.db.exception.DatabaseException; - -public abstract class XMLElementNamedChildParserBase implements XMLElementNamedChildParser{ - - private Map> namedParsers = new HashMap>(); - private Set> parsers = new HashSet>(); - - @Override - public Class getParser(Map parsers, ParserElement element, ParserElement child) { - Class parserClass = namedParsers.get(child.qName); - if (parserClass != null) - return parserClass; - XMLElementParser parser = parsers.get(child.qName); - if (parser == null) - return null; - parserClass = parser.getClass(); - if (this.parsers.contains(parserClass)) - return parserClass; - for (Class c : this.parsers) - if (c.isAssignableFrom(parserClass)) - return parserClass; - return null; - } - - public void addParser(String name, Class parser) { - namedParsers.put(name, parser); - } - - public void addParser(Class parser) { - parsers.add(parser); - } - -// @Override -// public void configureChild(WriteGraph graph, Stack parents, Element element, Element child) throws DatabaseException { -// -// } - - @Override - public void configure(WriteGraph graph, ParserElement element, String string) throws DatabaseException { - - } - - @Override - public String getID() { - return null; - } - - @Override - public int idPriority() { - return 0; - } - - -} +package org.simantics.xml.sax.base; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; + +public abstract class XMLElementNamedChildParserBase implements XMLElementNamedChildParser{ + + private Map> namedParsers = new HashMap>(); + private Set> parsers = new HashSet>(); + + @Override + public Class getParser(Map parsers, ParserElement element, ParserElement child) { + Class parserClass = namedParsers.get(child.getLocalName()); + if (parserClass != null) + return parserClass; + XMLElementParser parser = parsers.get(child.getLocalName()); + if (parser == null) + return null; + parserClass = parser.getClass(); + if (this.parsers.contains(parserClass)) + return parserClass; + for (Class c : this.parsers) + if (c.isAssignableFrom(parserClass)) + return parserClass; + return null; + } + + public void addParser(String name, Class parser) { + namedParsers.put(name, parser); + } + + public void addParser(Class parser) { + parsers.add(parser); + } + +// @Override +// public void configureChild(WriteGraph graph, Stack parents, Element element, Element child) throws DatabaseException { +// +// } + + @Override + public void configure(WriteGraph graph, ParserElement element, String string) throws DatabaseException { + + } + + @Override + public String getID() { + return null; + } + + @Override + public int idPriority() { + return 0; + } + + +} diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLParser.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLParser.java index 87bb477..3d708f4 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLParser.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLParser.java @@ -52,11 +52,21 @@ public class XMLParser extends DefaultHandler implements Serializable { } public void setGraph(WriteGraph graph) { + if (graph == this.graph) + return; this.graph = graph; for (XMLParser p : subParsers.values()) p.setGraph(graph); } + public void setLogger(ILogger logger) { + if (logger == this.logger) + return; + this.logger = logger; + for (XMLParser p : subParsers.values()) + p.setLogger(logger); + } + public String getSchemaURI() { return schemaURI; } @@ -73,16 +83,35 @@ public class XMLParser extends DefaultHandler implements Serializable { public void add(XMLParser parser) { subParsers.put(parser.getSchemaURI(), parser); + + } + + public XMLParser resolveDependencies() { + + java.util.Map map = new java.util.HashMap<>(); + map.put(this.getSchemaURI(), this); + addDependencies(map); + return this; + } + + public void addDependencies(java.util.Map map) { + } private List idReferenceElements = new ArrayList(); + private void loadElement(Deque parents, ParserElement element) throws SAXException{ + loadElement(parents, element, true); + } + private void loadElement(Deque parents, ParserElement element, boolean checkParent) throws SAXException{ XMLElementParser parser = null; ParserElement parent = null; if (parents.size() > 0) { + // process a child element parent = parents.peek(); // check for assigned subparser - if (parent.getXMLParser() != null && parent.getXMLParser() != this) { + if (checkParent && parent.getXMLParser() != null && parent.getXMLParser() != this) { + //element.setXMLParser(parent.getXMLParser()); element.setXMLParser(parent.getXMLParser()); parent.getXMLParser().loadElement(parents, element); return; @@ -107,11 +136,11 @@ public class XMLParser extends DefaultHandler implements Serializable { // parser = parsers.get(element.qName); } else { // otherwise use globally configured element parser - parser = parsers.get(element.qName); + parser = parsers.get(element.getLocalName()); } } else { - // use globally configured element parser - parser = parsers.get(element.qName); + // use globally configured element parser for a root element + parser = parsers.get(element.getLocalName()); } if (parser != null) { @@ -127,18 +156,23 @@ public class XMLParser extends DefaultHandler implements Serializable { } else { // check for schema reference attempt to locate subparser for it. Attribute schemaRef = element.getAttribute(XML_NAMESPACE_REF); + String nsRef[] = element.getQName().split(":"); if (schemaRef != null && subParsers.containsKey(schemaRef.value)) { XMLParser subParser = subParsers.get(schemaRef.value); subParser.loadElement(parents, element); element.setXMLParser(subParser); + } else if (nsRef.length == 2 && element.getNS(nsRef[0]) != null && subParsers.containsKey(element.getNS(nsRef[0]))) { + XMLParser subParser = subParsers.get(element.getNS(nsRef[0])); + subParser.loadElement(parents, element, false); + element.setXMLParser(subParser); } else { if (parent == null && parents.size() > 0) parent = parents.peek(); String err = null; if (parent != null) - err = "Unknown element " + element.qName + ", parent " + (parent != null ? parent.getQName() : "None"); + err = "Unknown element " + element.getLocalName() + ", parent " + (parent != null ? parent.getLocalName() : "None"); else { - err = "Unknown root element " + element.qName + ", cannot import the file"; + err = "Unknown root element " + element.getLocalName() + ", cannot import the file"; throw new SAXException(err); } @@ -160,12 +194,12 @@ public class XMLParser extends DefaultHandler implements Serializable { if (parent.getElementParser() != null) { if (!parent.getElementParser().connectChild(graph, parent, element)) if (!parser.connectParent(graph, parent, element)) { - String err = "Did not connect element " + element.qName + ", parent " + (parent != null ? parent.getQName() : "None"); + String err = "Did not connect element " + element.getLocalName() + ", parent " + (parent != null ? parent.getLocalName() : "None"); logger.log(new Status(IStatus.ERROR, PLUGIN_ID, err)); if (debug) System.err.println(err); } } else { - String err = "Did not connect element " + element.qName + ", parent " + (parent != null ? parent.getQName() : "None") + " was not imported"; + String err = "Did not connect element " + element.getLocalName() + ", parent " + (parent != null ? parent.getLocalName() : "None") + " was not imported"; logger.log(new Status(IStatus.ERROR, PLUGIN_ID, err)); if (debug) System.err.println(err); } @@ -178,7 +212,7 @@ public class XMLParser extends DefaultHandler implements Serializable { boolean useExt = existing.elementParser.idPriority() > element.elementParser.idPriority(); if (useExt) idMap.put(id, existing); - String err = "Duplicate XML element id: " + id + " for " + element.getQName() + " and " + existing.getQName() + ", using " + (useExt ? existing.getQName() : element.getQName()); + String err = "Duplicate XML element id: " + id + " for " + element.getLocalName() + " and " + existing.getLocalName() + ", using " + (useExt ? existing.getLocalName() : element.getLocalName()); logger.log(new Status(IStatus.ERROR, PLUGIN_ID, err)); if (debug) System.err.println(err); } @@ -223,9 +257,9 @@ public class XMLParser extends DefaultHandler implements Serializable { public void done() throws SAXException{ try { for (ParserElement e : idReferenceElements) { - IDReferenceParser parser = (IDReferenceParser)parsers.get(e.qName); + IDReferenceParser parser = (IDReferenceParser)parsers.get(e.getLocalName()); if (!parser.connectReferences(graph, e, idMap)) { - String err ="Could not resolve ID references for " + e.getQName() + " " + e.getUri(); + String err ="Could not resolve ID references for " + e.getLocalName() + " " + e.getUri(); logger.log(new Status(IStatus.ERROR, PLUGIN_ID, err)); if (debug) System.err.println(err); } @@ -243,7 +277,7 @@ public class XMLParser extends DefaultHandler implements Serializable { @Override public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { - ParserElement e = new ParserElement(uri,localName,name,attributes); + ParserElement e = new ParserElement(current.peek(),uri,localName,name,attributes); loadElement(current,e); current.push(e); @@ -261,11 +295,12 @@ public class XMLParser extends DefaultHandler implements Serializable { if (e != null) { handleCharacters(e, charactersValue.toString()); handleElement(current,e); - } - if (current.isEmpty()) { - root = e.getData(); + if (current.isEmpty()) { + root = e.getData(); + } } + charactersValue = new StringBuilder(); } @@ -282,7 +317,7 @@ public class XMLParser extends DefaultHandler implements Serializable { } public void parse(File file, ILogger logger) throws Exception { - this.logger = logger; + setLogger(logger); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxParser = spf.newSAXParser(); diff --git a/org.simantics.xml.sax/src/org/simantics/xml/data/XmlDataConverter.java b/org.simantics.xml.sax/src/org/simantics/xml/data/XmlDataConverter.java index b4e7c21..dfd1d6e 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/data/XmlDataConverter.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/data/XmlDataConverter.java @@ -149,152 +149,159 @@ public class XmlDataConverter { protected void doConvert() throws IOException, XMLStreamException, JAXBException { XMLInputFactory input = XMLInputFactory.newInstance(); - Deque elementStack = new ArrayDeque<>(); + for (File inputFile : inputFiles) { XMLEventReader reader = input.createXMLEventReader(new FileInputStream(inputFile)); - while (reader.hasNext()) { - XMLEvent event = reader.nextEvent(); - if (event.isStartElement()) { - StartElement parseElement = event.asStartElement(); -// System.out.println("Start " + parseElement.getName()); - Element schemaElement = null; - String currentNS = parseElement.getName().getNamespaceURI(); - Schema s = schemaMap.get(currentNS); - String elementName = parseElement.getName().getLocalPart(); - if ("GroupComponent".equals(elementName)) - System.out.println(); - if (s == null) { - s = getOrCreateSchema(parseElement); - } else { - schemaElement = elementMap.get(s).get(elementName); - } - Element parentElement = elementStack.peek(); - - boolean newElement = false; - boolean sameNameSpace = true; - - if (parentElement != null) { - //QName parentType = parentElement.getType(); - String parentNs = elementNsMap.get(parentElement); - sameNameSpace =currentNS.equals(parentNs); - if (!sameNameSpace) { - Schema ps = getOrCreateSchema(parentNs); - addSchemaDependency(ps, s); - } - + convertFile(reader); + } + + } + + private void convertFile(XMLEventReader reader) throws XMLStreamException { + Deque elementStack = new ArrayDeque<>(); + while (reader.hasNext()) { + XMLEvent event = reader.nextEvent(); + if (event.isStartElement()) { + StartElement parseElement = event.asStartElement(); +// System.out.println("Start " + parseElement.getName()); + Element schemaElement = null; + String currentNS = parseElement.getName().getNamespaceURI(); + Schema s = schemaMap.get(currentNS); + String elementName = parseElement.getName().getLocalPart(); + if (s == null) { + s = getOrCreateSchema(parseElement); + } else { + schemaElement = elementMap.get(s).get(elementName); + } + Element parentElement = elementStack.peek(); + + boolean newElement = false; + boolean sameNameSpace = true; + + if (parentElement != null) { + //QName parentType = parentElement.getType(); + String parentNs = elementNsMap.get(parentElement); + sameNameSpace =currentNS.equals(parentNs); + if (!sameNameSpace) { + Schema ps = getOrCreateSchema(parentNs); + addSchemaDependency(ps, s); } - if (schemaElement == null) { - LocalElement localElement = null; - //QName type = null; - if (elementStack.isEmpty()) { - schemaElement = new TopLevelElement(); - s.getSimpleTypeOrComplexTypeOrGroup().add(schemaElement); - } else { - - -// if (sameNameSpace) { -// localElement = new LocalElement(); -// schemaElement = localElement; -// //type = new QName(elementName); -// -// } else { - schemaElement = new TopLevelElement(); - s.getSimpleTypeOrComplexTypeOrGroup().add(schemaElement); - //type = new QName(SchemaConversionBase.SCHEMA_NS,"element"); - localElement = new LocalElement(); - localElement.setRef(new QName(parseElement.getName().getNamespaceURI(), elementName)); - -// } - } - schemaElement.setName(elementName); - elementNsMap.put(schemaElement, currentNS); + + } + if (schemaElement == null) { + LocalElement localElement = null; + //QName type = null; + if (elementStack.isEmpty()) { + schemaElement = new TopLevelElement(); + s.getSimpleTypeOrComplexTypeOrGroup().add(schemaElement); + } else { + + // if (sameNameSpace) { -// schemaElement.setType(new QName(parseElement.getName().getNamespaceURI(),elementName)); +// localElement = new LocalElement(); +// schemaElement = localElement; +// //type = new QName(elementName); +// // } else { -// schemaElement.setType(new QName(parseElement.getName().getNamespaceURI(), elementName)); + schemaElement = new TopLevelElement(); + s.getSimpleTypeOrComplexTypeOrGroup().add(schemaElement); + //type = new QName(SchemaConversionBase.SCHEMA_NS,"element"); + localElement = new LocalElement(); + localElement.setRef(new QName(parseElement.getName().getNamespaceURI(), elementName)); + // } - if (!elementStack.isEmpty()) { - ComplexType complexType = parentElement.getComplexType(); - ExplicitGroup choice = complexType.getChoice(); - if (choice == null) { - choice = new ExplicitGroup(); - complexType.setChoice(choice); - choice.setMaxOccurs("unbounded"); - } - addElement(choice, new QName(SchemaConversionBase.SCHEMA_NS,"element"), localElement); - } - - - elementMap.get(s).put(elementName, schemaElement); - newElement = true; } - elementStack.push(schemaElement); - - Iterator attributeIterator = parseElement.getAttributes(); - -// while (attributeIterator.hasNext()) { -// Attribute attribute = attributeIterator.next(); -// System.out.println("Attribute " + attribute.getName() + " " + attribute.getValue()); + schemaElement.setName(elementName); + elementNsMap.put(schemaElement, currentNS); +// if (sameNameSpace) { +// schemaElement.setType(new QName(parseElement.getName().getNamespaceURI(),elementName)); +// } else { +// schemaElement.setType(new QName(parseElement.getName().getNamespaceURI(), elementName)); // } - if (newElement) { - LocalComplexType complexType = new LocalComplexType(); - schemaElement.setComplexType(complexType); - attributeIterator = parseElement.getAttributes(); - while (attributeIterator.hasNext()) { - Attribute attribute = attributeIterator.next(); - addAttribute(attribute, complexType, currentNS); - } - - } else { - LocalComplexType complexType = schemaElement.getComplexType(); - attributeIterator = parseElement.getAttributes(); - Map currentAttributes = new HashMap<>(); - Iterator currentAttributeIterator = complexType.getAttributeOrAttributeGroup().iterator(); - while (currentAttributeIterator.hasNext()) { - Annotated annotated = currentAttributeIterator.next(); - if (annotated instanceof org.w3._2001.xmlschema.Attribute) { - org.w3._2001.xmlschema.Attribute schemaAttribute = (org.w3._2001.xmlschema.Attribute)annotated; - String n = schemaAttribute.getName(); - if (n != null) - currentAttributes.put(n, schemaAttribute); - } - } - while (attributeIterator.hasNext()) { - Attribute attribute = attributeIterator.next(); - org.w3._2001.xmlschema.Attribute schemaAttribute = currentAttributes.get(attribute.getName().getLocalPart()); - if (schemaAttribute == null) { - addAttribute(attribute, complexType, currentNS); - } else { - QName newType = getType(attribute.getValue()); - updateAttributeType(schemaAttribute, newType); - } - + if (!elementStack.isEmpty()) { + ComplexType complexType = parentElement.getComplexType(); + ExplicitGroup choice = complexType.getChoice(); + if (choice == null) { + choice = new ExplicitGroup(); + complexType.setChoice(choice); + choice.setMaxOccurs("unbounded"); } + addElement(choice, new QName(SchemaConversionBase.SCHEMA_NS,"element"), localElement); } - } else if (event.isEndElement()) { - EndElement element = event.asEndElement(); -// System.out.println("End " + element.getName()); - elementStack.pop(); - } else if (event.isAttribute()) { - } else if (event.isStartDocument()) { + elementMap.get(s).put(elementName, schemaElement); + newElement = true; + } + elementStack.push(schemaElement); + + Iterator attributeIterator = parseElement.getAttributes(); - } else if (event.isEndDocument()) { +// while (attributeIterator.hasNext()) { +// Attribute attribute = attributeIterator.next(); +// System.out.println("Attribute " + attribute.getName() + " " + attribute.getValue()); +// } + if (newElement) { + LocalComplexType complexType = new LocalComplexType(); + schemaElement.setComplexType(complexType); + attributeIterator = parseElement.getAttributes(); + while (attributeIterator.hasNext()) { + Attribute attribute = attributeIterator.next(); + if ("http://www.w3.org/XML/1998/namespace".equals(attribute.getName().getNamespaceURI())) + continue; + addAttribute(attribute, complexType, parseElement.getNamespaceURI(attribute.getName().getPrefix())); + } - } else if (event.isEntityReference()) { + } else { + LocalComplexType complexType = schemaElement.getComplexType(); + attributeIterator = parseElement.getAttributes(); + Map currentAttributes = new HashMap<>(); + Iterator currentAttributeIterator = complexType.getAttributeOrAttributeGroup().iterator(); + while (currentAttributeIterator.hasNext()) { + Annotated annotated = currentAttributeIterator.next(); + if (annotated instanceof org.w3._2001.xmlschema.Attribute) { + org.w3._2001.xmlschema.Attribute schemaAttribute = (org.w3._2001.xmlschema.Attribute)annotated; + String n = schemaAttribute.getName(); + if (n != null) + currentAttributes.put(n, schemaAttribute); + } + } + while (attributeIterator.hasNext()) { + Attribute attribute = attributeIterator.next(); + if ("http://www.w3.org/XML/1998/namespace".equals(attribute.getName().getNamespaceURI())) + continue; + org.w3._2001.xmlschema.Attribute schemaAttribute = currentAttributes.get(attribute.getName().getLocalPart()); + if (schemaAttribute == null) { + addAttribute(attribute, complexType, parseElement.getNamespaceURI(attribute.getName().getPrefix())); + } else { + QName newType = getType(attribute.getValue()); + updateAttributeType(schemaAttribute, newType); + } + + } + } - } else if (event.isCharacters()) { - Characters characters = event.asCharacters(); -// if (!characters.isWhiteSpace()) -// System.out.println(characters.getData()); - } else if (event.isNamespace()) { - - } - } + } else if (event.isEndElement()) { + EndElement element = event.asEndElement(); +// System.out.println("End " + element.getName()); + elementStack.pop(); + } else if (event.isAttribute()) { + System.out.println(event); + } else if (event.isStartDocument()) { + System.out.println(event); + } else if (event.isEndDocument()) { + + } else if (event.isEntityReference()) { + + } else if (event.isCharacters()) { + Characters characters = event.asCharacters(); +// if (!characters.isWhiteSpace()) +// System.out.println(characters.getData()); + } else if (event.isNamespace()) { + System.out.println(event); + } } - } private void updateAttributeType(org.w3._2001.xmlschema.Attribute schemaAttribute, QName newType) { @@ -345,7 +352,7 @@ public class XmlDataConverter { } private void addAttribute(Attribute attribute, ComplexType complexType, String currentNS) { - if (attribute.getName().getLocalPart().equals("GridOptions.GridVisibility")) + if (attribute.getName().getLocalPart().equals("Panel.ZIndex")) System.out.println(); if (attribute.getName().getNamespaceURI().length() == 0 || attribute.getName().getNamespaceURI().equals(currentNS)) { org.w3._2001.xmlschema.Attribute schemaAttribute = new org.w3._2001.xmlschema.Attribute(); @@ -399,6 +406,8 @@ public class XmlDataConverter { } } } + if ("Panel.ZIndex".equals(schemaAttribute.getName())) + System.out.println(); complexType.getAttributeOrAttributeGroup().add(schemaAttribute); } @@ -428,6 +437,8 @@ public class XmlDataConverter { } private Schema getOrCreateSchema(String ns) { + if (ns == null) + throw new IllegalArgumentException("Schema NS cannot be null."); Schema s = schemaMap.get(ns); if (s == null) { s = new Schema(); 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 58f5748..4b0d1b5 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 @@ -60,7 +60,7 @@ public class ImporterGenerator extends JavaGenerator{ mainWriter.println("public class " + name + importerClassPostfix+" extends AbstractImporter {"); mainWriter.println(); mainWriter.println(" public " + name + importerClassPostfix+"(Session session, File file) {"); - mainWriter.println(" super(session, file, new "+name + parserClassPostfix+"());"); + mainWriter.println(" super(session, file, new "+name + parserClassPostfix+"().resolveDependencies());"); mainWriter.println(" }"); mainWriter.println(); mainWriter.println("}"); @@ -84,7 +84,29 @@ public class ImporterGenerator extends JavaGenerator{ for (String s : ruleClassNames) { mainWriter.println(" add(new "+s+"());"); } + mainWriter.println(); mainWriter.println(" }"); + mainWriter.println(); + if (converter.getSubConverters().size() > 0) { + mainWriter.println(" public void addDependencies(java.util.Map map) {"); + for (SchemaConverter sc : converter.getSubConverters()) { + String s = sc.className; + if (s.endsWith("Ontology")) + s = s.substring(0,s.length()-"Ontology".length()); + s +="Parser"; + mainWriter.println(" {"); + mainWriter.println(" XMLParser parser = new "+s+"();"); + mainWriter.println(" if (!map.containsKey(parser.getSchemaURI())) {"); + mainWriter.println(" map.put(parser.getSchemaURI(), parser);"); + mainWriter.println(" parser.addDependencies(map);"); + mainWriter.println(" } else {"); + mainWriter.println(" parser = map.get(parser.getSchemaURI());"); + mainWriter.println(" }"); + mainWriter.println(" add(parser);"); + mainWriter.println(" }"); + } + mainWriter.println(" }"); + } mainWriter.println("}"); mainWriter.println(); -- 2.47.1