]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ParserElement.java
Switch to delayed write for more performance.
[simantics/interop.git] / org.simantics.xml.sax.base / src / org / simantics / xml / sax / base / ParserElement.java
index e3298e3677c8c54916bf4d80cb030f447ea29cac..2ab58f773ae54d2d154b944bc3d0f89e8926ff17 100644 (file)
@@ -1,11 +1,18 @@
 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
@@ -13,16 +20,19 @@ public class ParserElement implements Serializable {
        String uri;\r
        String localName;\r
        String qName;\r
-       Map<String,Attribute> attributes = new HashMap<String, Attribute>();\r
-       \r
+       Map<String, Attribute> attributes;\r
+       MapList<Resource, ParserElement> lists;\r
+\r
        XMLElementParser elementParser;\r
        XMLParser xmlParser;\r
-       \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
-               for (int i = 0; i < attributes.getLength(); i++) {\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
@@ -31,6 +41,7 @@ public class ParserElement implements Serializable {
                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
@@ -82,4 +93,23 @@ public class ParserElement implements Serializable {
                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
 }
\ No newline at end of file