]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
Handle multi-namespace XML files. 26/2126/2
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 6 Sep 2018 10:42:52 +0000 (13:42 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 6 Sep 2018 10:59:45 +0000 (10:59 +0000)
gitlab #3

Change-Id: I7b067e47a415116b127ecd707b54090c8d277446

org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ParserElement.java
org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementNamedChildParserBase.java
org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLParser.java
org.simantics.xml.sax/src/org/simantics/xml/data/XmlDataConverter.java
org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java

index 2ab58f773ae54d2d154b944bc3d0f89e8926ff17..ca4a91a39cab329739ecbc32b039767de93e14a6 100644 (file)
-package org.simantics.xml.sax.base;\r
-\r
-import java.io.Serializable;\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.utils.ListUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.utils.datastructures.MapList;\r
-import org.xml.sax.Attributes;\r
-\r
-public class ParserElement implements Serializable {\r
-       private static final long serialVersionUID = -5207502156942818875L;\r
-       String uri;\r
-       String localName;\r
-       String qName;\r
-       Map<String, Attribute> attributes;\r
-       MapList<Resource, ParserElement> lists;\r
-\r
-       XMLElementParser elementParser;\r
-       XMLParser xmlParser;\r
-\r
-       public ParserElement(String uri, String localName, String qName, Attributes attributes) {\r
-               this.uri = uri;\r
-               this.localName = localName;\r
-               this.qName = qName;\r
-               int attrCount = attributes.getLength();\r
-               this.attributes = new HashMap<>(attrCount);\r
-               for (int i = 0; i < attrCount; i++) {\r
-                       this.attributes.put(attributes.getLocalName(i),new Attribute(attributes.getLocalName(i),attributes.getQName(i),attributes.getURI(i),attributes.getValue(i)));\r
-               }\r
-       }\r
-       \r
-       public ParserElement(String uri, String localName, String qName, Collection<Attribute> attributes) {\r
-               this.uri = uri;\r
-               this.localName = localName;\r
-               this.qName = qName;\r
-               this.attributes = new HashMap<>(attributes.size());\r
-               for (Attribute a : attributes) {\r
-                       this.attributes.put(a.localName,new Attribute(a.localName,a.qName,a.uri,a.value));\r
-               }\r
-       }\r
-\r
-       public String getUri() {\r
-               return uri;\r
-       }\r
-\r
-       public String getLocalName() {\r
-               return localName;\r
-       }\r
-\r
-       public String getQName() {\r
-               return qName;\r
-       }\r
-\r
-       public Collection<Attribute> getAttributes() {\r
-               return attributes.values();\r
-       }\r
-       \r
-       public Attribute getAttribute(String name) {\r
-               return attributes.get(name);\r
-       }\r
-\r
-       private Resource data;\r
-       \r
-       public void setData(Resource data) {\r
-               this.data = data;\r
-       }\r
-       \r
-       public Resource getData() {\r
-               return data;\r
-       }\r
-       \r
-       public void setElementParser(XMLElementParser parser) {\r
-               this.elementParser = parser;\r
-       }\r
-       \r
-       public XMLElementParser getElementParser() {\r
-               return elementParser;\r
-       }\r
-       \r
-       public void setXMLParser(XMLParser parser) {\r
-               this.xmlParser = parser;\r
-       }\r
-       \r
-       public XMLParser getXMLParser() {\r
-               return xmlParser;\r
-       }\r
-\r
-       public void registerListChild(Resource predicate, ParserElement child) {\r
-               if (lists == null)\r
-                       lists = new MapList<>();\r
-               lists.add(predicate, child);\r
-       }\r
-\r
-       public void createLists(WriteGraph graph) throws DatabaseException {\r
-               if (lists == null)\r
-                       return;\r
-               Layer0 L0 = Layer0.getInstance(graph);\r
-               for (Resource predicate : lists.getKeys()) {\r
-                       List<Resource> children = new ArrayList<>();\r
-                       for (ParserElement child : lists.getValues(predicate))\r
-                               children.add(child.getData());\r
-                       Resource list = ListUtils.create(graph, L0.List, L0.List_Element, null, children);\r
-                       graph.claim(getData(), predicate, list);\r
-               }\r
-       }\r
-\r
+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<String, Attribute> attributes;
+       MapList<Resource, ParserElement> lists;
+
+       XMLElementParser elementParser;
+       XMLParser xmlParser;
+
+       Map<String,String> 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<Attribute> 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<Attribute> 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<String, Attribute> 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<Attribute> 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<Resource> 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
index f1bbdf3f0b0effc34c784b41aad8b1e0977c9bbe..af1853d366bba7c1ffd3d2a00b736d7825119bc6 100644 (file)
@@ -1,62 +1,62 @@
-package org.simantics.xml.sax.base;\r
-\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
-import java.util.Map;\r
-import java.util.Set;\r
-\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
-\r
-public abstract class XMLElementNamedChildParserBase implements XMLElementNamedChildParser{\r
-       \r
-       private Map<String,Class<? extends XMLElementParser>> namedParsers = new HashMap<String, Class<? extends XMLElementParser>>();\r
-       private Set<Class<? extends XMLElementParser>> parsers = new HashSet<Class<? extends XMLElementParser>>();\r
-       \r
-       @Override\r
-       public Class<? extends XMLElementParser> getParser(Map<String, XMLElementParser> parsers, ParserElement element, ParserElement child) {\r
-               Class<? extends XMLElementParser> parserClass =  namedParsers.get(child.qName);\r
-               if (parserClass != null)\r
-                       return parserClass;\r
-               XMLElementParser parser = parsers.get(child.qName);\r
-               if (parser == null)\r
-                       return null;\r
-               parserClass = parser.getClass();\r
-               if (this.parsers.contains(parserClass))\r
-                       return parserClass;\r
-               for (Class<?> c : this.parsers)\r
-                       if (c.isAssignableFrom(parserClass))\r
-                               return parserClass;\r
-               return null;\r
-       }\r
-       \r
-       public void addParser(String name, Class<? extends XMLElementParser> parser) {\r
-               namedParsers.put(name, parser);\r
-       }\r
-       \r
-       public void addParser(Class<? extends XMLElementParser> parser) {\r
-               parsers.add(parser);\r
-       }\r
-       \r
-//     @Override\r
-//     public void configureChild(WriteGraph graph, Stack<Element> parents, Element element, Element child) throws DatabaseException {\r
-//\r
-//     }\r
-       \r
-       @Override\r
-       public void configure(WriteGraph graph, ParserElement element, String string) throws DatabaseException {\r
-               \r
-       }\r
-       \r
-       @Override\r
-       public String getID() {\r
-               return null;\r
-       }\r
-       \r
-       @Override\r
-       public int idPriority() {\r
-               return 0;\r
-       }\r
-       \r
-\r
-}\r
+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<String,Class<? extends XMLElementParser>> namedParsers = new HashMap<String, Class<? extends XMLElementParser>>();
+       private Set<Class<? extends XMLElementParser>> parsers = new HashSet<Class<? extends XMLElementParser>>();
+       
+       @Override
+       public Class<? extends XMLElementParser> getParser(Map<String, XMLElementParser> parsers, ParserElement element, ParserElement child) {
+               Class<? extends XMLElementParser> 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<? extends XMLElementParser> parser) {
+               namedParsers.put(name, parser);
+       }
+       
+       public void addParser(Class<? extends XMLElementParser> parser) {
+               parsers.add(parser);
+       }
+       
+//     @Override
+//     public void configureChild(WriteGraph graph, Stack<Element> 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;
+       }
+       
+
+}
index 87bb477b788902adc54ffd5f7d38fd21861d726b..3d708f4842f729ea4f58bf5a76041524a3420242 100644 (file)
@@ -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<String, XMLParser> map = new java.util.HashMap<>();
+               map.put(this.getSchemaURI(), this);
+               addDependencies(map);
+               return this;
+       }
+       
+       public void addDependencies(java.util.Map<String,XMLParser> map) {
+               
        }
        
        private List<ParserElement> idReferenceElements = new ArrayList<ParserElement>();
+       
        private void loadElement(Deque<ParserElement> parents, ParserElement element) throws SAXException{
+               loadElement(parents, element, true);
+       }
+       private void loadElement(Deque<ParserElement> 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();
                
index b4e7c21cd49e1c0918d89fdc4a66f279a7d465fd..dfd1d6e059d2b2ad3d63ee02e5f8a064183f505e 100644 (file)
@@ -149,152 +149,159 @@ public class XmlDataConverter {
        
        protected void doConvert() throws IOException, XMLStreamException, JAXBException {
                XMLInputFactory input = XMLInputFactory.newInstance();
-               Deque<Element> 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<Element> 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<Attribute> 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<String,org.w3._2001.xmlschema.Attribute> currentAttributes = new HashMap<>();
-                                               Iterator<Annotated> 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<Attribute> 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<String,org.w3._2001.xmlschema.Attribute> currentAttributes = new HashMap<>();
+                                       Iterator<Annotated> 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();
index 58f5748e05e1d4bef1f9af01611c02bcc6dc3128..4b0d1b55154f3e172d0679288fd8f6597454b1b5 100644 (file)
@@ -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<String,XMLParser> 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();