]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
Initial support for multi-schema parsers
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 20 Jan 2017 09:16:06 +0000 (09:16 +0000)
committerMarko Luukkainen <marko.luukkainen@vtt.fi>
Thu, 2 Feb 2017 12:01:35 +0000 (14:01 +0200)
Parsing multiline Base64 data did not work

refs #6985

git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@33433 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractImporter.java
org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/Element.java
org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLParser.java
org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java
org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java

index f175fc4b5a204dd658bb5f20f475f71b019f6ed5..f47dd9395c90e2338236fb0508ac9572a856327c 100644 (file)
@@ -19,6 +19,7 @@ public abstract class AbstractImporter {
        private Session session;\r
        private File file;\r
        private ILogger logger;\r
+       private XMLParser parser;\r
        \r
        private static final boolean USE_DELAYED = false;\r
        \r
@@ -26,6 +27,12 @@ public abstract class AbstractImporter {
                this.session = session;\r
                this.file = file;\r
        }\r
+       \r
+       public AbstractImporter(Session session, File file, XMLParser parser)  {\r
+               this.session = session;\r
+               this.file = file;\r
+               this.parser = parser;\r
+       }\r
        public Resource doImport() throws DatabaseException {\r
                return doImport(MessageService.getDefault());   \r
        }\r
@@ -47,7 +54,8 @@ public abstract class AbstractImporter {
                return file;\r
        }\r
        \r
-       public abstract void configure(XMLParser parser);\r
+       public void configure(XMLParser parser){};\r
+       \r
        \r
        private class ImportRequest extends WriteRequest {\r
                private Resource result;\r
@@ -86,8 +94,15 @@ public abstract class AbstractImporter {
        private Resource doImport(WriteGraph graph) throws DatabaseException {\r
                Layer0Utils.setDependenciesIndexingDisabled(graph, true);\r
                try {\r
-                       XMLParser parser = new XMLParser(graph);\r
-                       configure(parser);\r
+                       \r
+                       if (parser == null) {\r
+                               parser = new XMLParser();\r
+                               parser.setGraph(graph);\r
+                               configure(parser);\r
+                       } else {\r
+                               parser.setGraph(graph);\r
+                       }\r
+                       \r
                        parser.parse(file, logger);\r
                        System.out.println(file.getAbsolutePath() + " imported");\r
                        Resource root = parser.getRoot();\r
index 3aba943d15c657a35c9299abc5e4c11da4f7b139..62f1597eacd06718976480070cab754e4c5d8277 100644 (file)
@@ -15,7 +15,8 @@ public class Element implements Serializable {
        String qName;\r
        Map<String,Attribute> attributes = new HashMap<String, Attribute>();\r
        \r
-       XMLElementParser parser;\r
+       XMLElementParser elementParser;\r
+       XMLParser xmlParser;\r
        \r
        public Element(String uri, String localName, String qName, Attributes attributes) {\r
                this.uri = uri;\r
@@ -65,12 +66,20 @@ public class Element implements Serializable {
                return data;\r
        }\r
        \r
-       public void setParser(XMLElementParser parser) {\r
-               this.parser = parser;\r
+       public void setElementParser(XMLElementParser parser) {\r
+               this.elementParser = parser;\r
        }\r
        \r
-       public XMLElementParser getParser() {\r
-               return parser;\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
 }
\ No newline at end of file
index be8fef042f4cb882d97d73aac759c5661ef63bb9..43a07c560c3e5b86f2eacc1b8540cb25c2f27ea1 100644 (file)
@@ -30,16 +30,37 @@ public class XMLParser extends DefaultHandler implements Serializable {
 \r
        private static final long serialVersionUID = 7360740940824360338L;\r
        private static final boolean debug = false;\r
+       private static String XML_SCHEMA_REF = "xmlns";\r
        private ILogger logger;\r
        private Resource root;\r
+       private String schemaURI;\r
        private Deque<Element> current = new ArrayDeque<Element>();\r
        private Map<String, XMLElementParser> parsers = new HashMap<String, XMLElementParser>();\r
        private WriteGraph graph;\r
-       \r
        private Map<Class<? extends XMLElementParser>, XMLElementParser> namedParsers = new HashMap<Class<? extends XMLElementParser>, XMLElementParser>();\r
        \r
-       public XMLParser(WriteGraph graph) {\r
+       private Map<String, XMLParser> subParsers = new HashMap<String, XMLParser>();\r
+       \r
+       public XMLParser() {\r
+       \r
+       }\r
+       \r
+       public WriteGraph getGraph() {\r
+               return graph;\r
+       }\r
+       \r
+       public void setGraph(WriteGraph graph) {\r
                this.graph = graph;\r
+               for (XMLParser p : subParsers.values())\r
+                       p.setGraph(graph);\r
+       }\r
+       \r
+       public String getSchemaURI() {\r
+               return schemaURI;\r
+       }\r
+       \r
+       public void setSchemaURI(String schemaURI) {\r
+               this.schemaURI = schemaURI;\r
        }\r
        \r
        public void add(XMLElementParser parser) {\r
@@ -48,6 +69,9 @@ public class XMLParser extends DefaultHandler implements Serializable {
                namedParsers.put(parser.getClass(), parser);\r
        }\r
        \r
+       public void add(XMLParser parser) {\r
+               subParsers.put(parser.getSchemaURI(), parser);\r
+       }\r
        \r
        private List<Element> idReferenceElements = new ArrayList<Element>();\r
        private void loadElement(Deque<Element> parents, Element element) throws SAXException{\r
@@ -55,9 +79,15 @@ public class XMLParser extends DefaultHandler implements Serializable {
                Element parent = null;\r
                if (parents.size() > 0) {\r
                        parent = parents.peek();\r
-                       if (parent.getParser() instanceof XMLElementNamedChildParser) {\r
+                       // check for assigned subparser\r
+                       if (parent.getXMLParser() != null && parent.getXMLParser() != this) {\r
+                               element.setXMLParser(parent.getXMLParser());\r
+                               parent.getXMLParser().loadElement(parents, element);\r
+                               return;\r
+                       }\r
+                       if (parent.getElementParser() instanceof XMLElementNamedChildParser) {\r
                                // use parent's named child parser if it is supported\r
-                               Class<? extends XMLElementParser> parserClass = ((XMLElementNamedChildParser)parent.getParser()).getParser(parsers,parent,element);\r
+                               Class<? extends XMLElementParser> parserClass = ((XMLElementNamedChildParser)parent.getElementParser()).getParser(parsers,parent,element);\r
                                if (parserClass != null) {\r
                                        parser = namedParsers.get(parserClass);\r
                                        if (parser == null) {\r
@@ -65,7 +95,7 @@ public class XMLParser extends DefaultHandler implements Serializable {
                                                        parser = parserClass.newInstance();\r
                                                        namedParsers.put(parserClass, parser);\r
                                                } catch (IllegalAccessException | InstantiationException e) {\r
-                                                       String err = "Element parsers must have accessible default constructor";\r
+                                                       String err = "Error processing " + parserClass.getName() + " : element parsers must have accessible default constructor";\r
                                                        logger.log(new Status(IStatus.ERROR, PLUGIN_ID, err));\r
                                                        throw new SAXException(err, e);\r
                                                }\r
@@ -88,30 +118,38 @@ public class XMLParser extends DefaultHandler implements Serializable {
                                element.setData(parser.create(graph, element));\r
                                if (parser instanceof IDReferenceParser)\r
                                        idReferenceElements.add(element);\r
-                               element.setParser(parser);\r
+                               element.setElementParser(parser);\r
                        } catch (DatabaseException e) {\r
                                throw new SAXException(e);\r
                        }\r
                } else {\r
-                       if (parent == null && parents.size() > 0)\r
-                               parent = parents.peek();\r
-                       String err = "Unknown element " + element.qName + ", parent " + (parent != null ? parent.getQName() : "None");\r
-                       logger.log(new Status(IStatus.ERROR, PLUGIN_ID, err));\r
-                       if (debug) System.err.println(err);\r
+                       // check for schema reference attempt to locate subparser for it.\r
+                       Attribute schemaRef = element.getAttribute(XML_SCHEMA_REF);\r
+                       if (schemaRef != null && subParsers.containsKey(schemaRef.value)) {\r
+                               XMLParser subParser = subParsers.get(schemaRef.value);\r
+                               subParser.loadElement(parents, element);\r
+                               element.setXMLParser(subParser);\r
+                       } else {\r
+                               if (parent == null && parents.size() > 0)\r
+                                       parent = parents.peek();\r
+                               String err = "Unknown element " + element.qName + ", parent " + (parent != null ? parent.getQName() : "None");\r
+                               logger.log(new Status(IStatus.ERROR, PLUGIN_ID, err));\r
+                               if (debug) System.err.println(err);\r
+                       }\r
                }\r
        }\r
        \r
        private Map<String, Element> idMap = new HashMap<String, Element>();\r
        \r
        private void handleElement(Deque<Element> parents, Element element) throws SAXException{\r
-               XMLElementParser parser = element.getParser();\r
+               XMLElementParser parser = element.getElementParser();\r
                if (parser != null) {\r
                        try {\r
                                parser.configure(graph, parents, element);\r
                                if (parents.size() > 0) {\r
                                        Element parent = parents.peek();\r
-                                       if (parent.getParser() != null) {\r
-                                               if (!parent.getParser().connectChild(graph, parent, element))\r
+                                       if (parent.getElementParser() != null) {\r
+                                               if (!parent.getElementParser().connectChild(graph, parent, element))\r
                                                        if (!parser.connectParent(graph, parent, element)) {\r
                                                                String err = "Did not connect element " + element.qName + ", parent " + (parent != null ? parent.getQName() : "None");\r
                                                                logger.log(new Status(IStatus.ERROR, PLUGIN_ID, err));\r
@@ -128,7 +166,7 @@ public class XMLParser extends DefaultHandler implements Serializable {
                                        Element existing = idMap.put(id, element);\r
                                        if (existing != null) {\r
                                                // report error + use id priorities to select the kept element.\r
-                                               boolean useExt = existing.parser.idPriority() > element.parser.idPriority();\r
+                                               boolean useExt = existing.elementParser.idPriority() > element.elementParser.idPriority();\r
                                                if (useExt)\r
                                                        idMap.put(id, existing);\r
                                                String err = "Duplicate XML element id: " + id + " for " + element.getQName() + " and " + existing.getQName() + ", using " + (useExt ? existing.getQName() : element.getQName());\r
@@ -142,7 +180,7 @@ public class XMLParser extends DefaultHandler implements Serializable {
                } else {\r
                        Element parent = parents.peek();\r
                        if (parent != null) { \r
-                               parser = parent.getParser();\r
+                               parser = parent.getElementParser();\r
                                if (parser != null && parser instanceof UnrecognizedElementParser) {\r
                                        try {\r
                                                ((UnrecognizedElementParser)parser).configureChild(graph, parents, parent, element);\r
@@ -155,7 +193,7 @@ public class XMLParser extends DefaultHandler implements Serializable {
        }\r
        \r
        private void handleCharacters(Element element, String string) throws SAXException{\r
-               XMLElementParser parser = element.getParser();\r
+               XMLElementParser parser = element.getElementParser();\r
                if (parser != null) {\r
                        try {\r
                                parser.configure(graph, element, string);\r
@@ -187,12 +225,16 @@ public class XMLParser extends DefaultHandler implements Serializable {
                return root;\r
        }\r
        \r
+       StringBuilder charactersValue;\r
+       \r
        @Override\r
        public void startElement(String uri, String localName, String name,     Attributes attributes) throws SAXException {\r
                Element e = new Element(uri,localName,name,attributes);\r
                \r
                loadElement(current,e);\r
                current.push(e);\r
+               \r
+               charactersValue = new StringBuilder();\r
        }\r
        \r
        @Override\r
@@ -203,22 +245,26 @@ public class XMLParser extends DefaultHandler implements Serializable {
                        e = current.pop();\r
                }\r
                if (e != null) {\r
+                       if (charactersValue.length() > 0)\r
+                               handleCharacters(e, charactersValue.toString());\r
                        handleElement(current,e);\r
                }\r
                if (current.isEmpty()) {\r
                        root = e.getData();\r
                }\r
+               \r
+               charactersValue = new StringBuilder();\r
        }\r
        \r
        @Override\r
        public void characters(char[] ch, int start, int length)\r
                        throws SAXException {\r
-               \r
-               if (!current.isEmpty()) {\r
-                       String s = new String(ch,start,length);\r
-                       Element e = current.peek();\r
-                       handleCharacters(e, s);\r
-               }\r
+               charactersValue.append(new String(ch,start,length));\r
+//             if (!current.isEmpty()) {\r
+//                     String s = new String(ch,start,length);\r
+//                     Element e = current.peek();\r
+//                     handleCharacters(e, s);\r
+//             }\r
                \r
        }\r
        \r
index d192ad77000aa3f60e4fe25bdf9209364b93b1c0..4245e3e1b4d471cd19e233e757bd0ae977554578 100644 (file)
@@ -65,6 +65,7 @@ public class ImporterGenerator extends SchemaConversionBase{
                ontShort +=".";\r
                String parserPackagePostfix = "_elem";\r
                String importerClassPostfix = "Importer";\r
+               String parserClassPostfix = "Parser";\r
                elementPackageName = name+parserPackagePostfix;\r
                \r
                importParserDir= new File(converter.getParserDir().getAbsolutePath()+"/"+elementPackageName);\r
@@ -72,26 +73,47 @@ public class ImporterGenerator extends SchemaConversionBase{
                        importParserDir.mkdirs();\r
                \r
                handle(schema);\r
-               \r
-               File importParserFile = new File(converter.getParserDir().getAbsolutePath()+"/"+name+importerClassPostfix+".java");\r
-               PrintWriter mainWriter = createFile(importParserFile);\r
+               // Create Importer class\r
+               File importerFile = new File(converter.getParserDir().getAbsolutePath()+"/"+name+importerClassPostfix+".java");\r
+               PrintWriter mainWriter = createFile(importerFile);\r
                mainWriter.println("package " + converter.getPluginName() +";");\r
                mainWriter.println();\r
                mainWriter.println("import java.io.File;");\r
                mainWriter.println("import org.simantics.db.Session;");\r
                mainWriter.println("import org.simantics.xml.sax.base.AbstractImporter;");\r
-               mainWriter.println("import org.simantics.xml.sax.base.XMLParser;");\r
                mainWriter.println();\r
                mainWriter.println("public class " + name + importerClassPostfix+" extends AbstractImporter {");\r
                mainWriter.println();\r
                mainWriter.println("   public " + name + importerClassPostfix+"(Session session, File file)  {");\r
-               mainWriter.println("      super(session,file);");\r
+               mainWriter.println("      super(session, file, new "+name + parserClassPostfix+"());");\r
                mainWriter.println("   }");\r
                mainWriter.println();\r
-               mainWriter.println("   @Override");\r
-               mainWriter.println("   public void configure(XMLParser parser) {");\r
+//             mainWriter.println("   @Override");\r
+//             mainWriter.println("   public void configure(XMLParser parser) {");\r
+//             for (String s : ruleClassNames) {\r
+//             mainWriter.println("      parser.add(new "+s+"());");\r
+//             }\r
+//             mainWriter.println("   }");\r
+               mainWriter.println("}");\r
+               \r
+               mainWriter.println();\r
+               mainWriter.flush();\r
+               mainWriter.close();\r
+               \r
+               // Create Parser class\r
+               File parserFile = new File(converter.getParserDir().getAbsolutePath()+"/"+name+parserClassPostfix+".java");\r
+               mainWriter = createFile(parserFile);\r
+               mainWriter.println("package " + converter.getPluginName() +";");\r
+               mainWriter.println();\r
+               mainWriter.println("import org.simantics.xml.sax.base.XMLParser;");\r
+               mainWriter.println();\r
+               mainWriter.println("public class " + name + parserClassPostfix+" extends XMLParser {");\r
+               mainWriter.println();\r
+               mainWriter.println("   public " + name + parserClassPostfix+"()  {");\r
+               if (schema.getTargetNamespace() != null)\r
+               mainWriter.println("      setSchemaURI(\""+schema.getTargetNamespace()+"\");");\r
                for (String s : ruleClassNames) {\r
-               mainWriter.println("      parser.add(new "+s+"());");\r
+               mainWriter.println("      add(new "+s+"());");\r
                }\r
                mainWriter.println("   }");\r
                mainWriter.println("}");\r
@@ -359,7 +381,7 @@ public class ImporterGenerator extends SchemaConversionBase{
                        if (!className.equals(refClassName))\r
                        fw.writer.println("        addParser("+refClassName+".class);");\r
                        \r
-                       fw.delayedWriter.println("         if (child.getParser() instanceof "+refClassName+"){");\r
+                       fw.delayedWriter.println("         if (child.getElementParser() instanceof "+refClassName+"){");\r
                        fw.delayedWriter.println("            graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+ref + ", child.getData());");\r
                        if (useElementList(parent, indicator,element, reference, ref, refType)) {\r
        \r
@@ -397,7 +419,7 @@ public class ImporterGenerator extends SchemaConversionBase{
                        fw.writer.println("        addParser(\""+ ref +"\", "+getName(parent) +"_" +ref+".class);");\r
                        \r
                        fw.delayedWriter2.println("   public static class " + getName(parent) +"_" +ref+" extends org.simantics.xml.sax.base.ValueElementParser {");\r
-                       fw.delayedWriter2.println("      "+ getName(parent) +"_" +ref +"(){");\r
+                       fw.delayedWriter2.println("      public "+ getName(parent) +"_" +ref +"(){");\r
                        fw.delayedWriter2.println("           super(\""+ref+"\"," +this.ontologyClassName+".URIs."+getName(parent) + "_has"+ref+", "+binding+");");\r
                        fw.delayedWriter2.println("       }");\r
                        fw.delayedWriter2.println("   }");\r
@@ -426,7 +448,7 @@ public class ImporterGenerator extends SchemaConversionBase{
                else\r
                fw.writer.println("        addParser("+className+".class);");\r
                \r
-               fw.delayedWriter.println("         if (child.getParser() instanceof "+className+"){");\r
+               fw.delayedWriter.println("         if (child.getElementParser() instanceof "+className+"){");\r
                fw.delayedWriter.println("            graph.claim(element.getData(), "+ontShort+getName(parent)+"_has"+ref + ", child.getData());");\r
                if (useElementList(parent, indicator,element, reference, ref, new QName(obj.getName()))) {\r
 \r
@@ -835,8 +857,6 @@ public class ImporterGenerator extends SchemaConversionBase{
        }\r
        \r
        private Inheritance getElementInheritance(SchemaObject topLevelElementObj) {\r
-               if (topLevelElementObj.getElement() != null && topLevelElementObj.getElement().getName().equals("ByteString"))\r
-                       System.out.println();\r
                Element topLevelElement = topLevelElementObj.getElement();\r
                Inheritance inheritance = new Inheritance();\r
                if (topLevelElement.getType() != null) {\r
index 38c934aafa060043725306a06d06b80dc9ccfbb2..bcff6982821b680ebf230e9c4840c5a30dea5901 100644 (file)
@@ -89,7 +89,7 @@ public abstract class SchemaConversionBase {
                schemaTypes.put("unsignedByte", new TypeEntry("L0.Byte",                        "Bindings.BYTE", "byte", "0","java.lang.Byte.parseByte(",")"));\r
                schemaTypes.put("long",                 new TypeEntry("L0.Long",                        "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")"));\r
                schemaTypes.put("unsignedLong", new TypeEntry("L0.Long",                        "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")"));\r
-               schemaTypes.put("base64Binary", new TypeEntry("L0.ByteArray",           "Bindings.BYTE_ARRAY", "byte[]", "new byte[0]","java.util.Base64.getDecoder().decode(",")"));\r
+               schemaTypes.put("base64Binary", new TypeEntry("L0.ByteArray",           "Bindings.BYTE_ARRAY", "byte[]", "new byte[0]","",".getBytes(org.simantics.databoard.util.binary.UTF8.CHARSET)"));\r
                \r
                \r
                \r