]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.xml.sax/src/org/simantics/xml/data/XmlDataConverter.java
Handle multi-namespace XML files.
[simantics/interop.git] / org.simantics.xml.sax / src / org / simantics / xml / data / XmlDataConverter.java
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();