X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.xml.sax.base%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2Fbase%2FParserElement.java;fp=org.simantics.xml.sax.base%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2Fbase%2FParserElement.java;h=ca4a91a39cab329739ecbc32b039767de93e14a6;hb=070e46f1fba569c5f5cb70fba5681ffafcb88088;hp=2ab58f773ae54d2d154b944bc3d0f89e8926ff17;hpb=99e240f9ca43aa78fbc6d71d7905c38c00bc679a;p=simantics%2Finterop.git diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ParserElement.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ParserElement.java index 2ab58f7..ca4a91a 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ParserElement.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/ParserElement.java @@ -1,115 +1,193 @@ -package org.simantics.xml.sax.base; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.utils.ListUtils; -import org.simantics.db.exception.DatabaseException; -import org.simantics.layer0.Layer0; -import org.simantics.utils.datastructures.MapList; -import org.xml.sax.Attributes; - -public class ParserElement implements Serializable { - private static final long serialVersionUID = -5207502156942818875L; - String uri; - String localName; - String qName; - Map attributes; - MapList lists; - - XMLElementParser elementParser; - XMLParser xmlParser; - - public ParserElement(String uri, String localName, String qName, Attributes attributes) { - this.uri = uri; - this.localName = localName; - this.qName = qName; - int attrCount = attributes.getLength(); - this.attributes = new HashMap<>(attrCount); - for (int i = 0; i < attrCount; i++) { - this.attributes.put(attributes.getLocalName(i),new Attribute(attributes.getLocalName(i),attributes.getQName(i),attributes.getURI(i),attributes.getValue(i))); - } - } - - public ParserElement(String uri, String localName, String qName, Collection attributes) { - this.uri = uri; - this.localName = localName; - this.qName = qName; - this.attributes = new HashMap<>(attributes.size()); - for (Attribute a : attributes) { - this.attributes.put(a.localName,new Attribute(a.localName,a.qName,a.uri,a.value)); - } - } - - public String getUri() { - return uri; - } - - public String getLocalName() { - return localName; - } - - public String getQName() { - return qName; - } - - public Collection getAttributes() { - return attributes.values(); - } - - public Attribute getAttribute(String name) { - return attributes.get(name); - } - - private Resource data; - - public void setData(Resource data) { - this.data = data; - } - - public Resource getData() { - return data; - } - - public void setElementParser(XMLElementParser parser) { - this.elementParser = parser; - } - - public XMLElementParser getElementParser() { - return elementParser; - } - - public void setXMLParser(XMLParser parser) { - this.xmlParser = parser; - } - - public XMLParser getXMLParser() { - return xmlParser; - } - - public void registerListChild(Resource predicate, ParserElement child) { - if (lists == null) - lists = new MapList<>(); - lists.add(predicate, child); - } - - public void createLists(WriteGraph graph) throws DatabaseException { - if (lists == null) - return; - Layer0 L0 = Layer0.getInstance(graph); - for (Resource predicate : lists.getKeys()) { - List children = new ArrayList<>(); - for (ParserElement child : lists.getValues(predicate)) - children.add(child.getData()); - Resource list = ListUtils.create(graph, L0.List, L0.List_Element, null, children); - graph.claim(getData(), predicate, list); - } - } - +package org.simantics.xml.sax.base; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.ListUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.utils.datastructures.MapList; +import org.xml.sax.Attributes; + +public class ParserElement implements Serializable { + private static final long serialVersionUID = -5207502156942818875L; + private String uri; + private String localName; + private String qName; + Map attributes; + MapList lists; + + XMLElementParser elementParser; + XMLParser xmlParser; + + Map nsMap; + + private ParserElement parent; + + public ParserElement(String uri, String localName, String qName, Attributes attributes) { + this.uri = uri; + this.localName = localName; + this.qName = qName; + int attrCount = attributes.getLength(); + this.attributes = new HashMap<>(attrCount); + for (int i = 0; i < attrCount; i++) { + this.attributes.put(attributes.getLocalName(i),new Attribute(attributes.getLocalName(i),attributes.getQName(i),attributes.getURI(i),attributes.getValue(i))); + } + processNS(); + } + + public ParserElement(String uri, String localName, String qName, Collection attributes) { + this.uri = uri; + this.localName = localName; + this.qName = qName; + this.attributes = new HashMap<>(attributes.size()); + for (Attribute a : attributes) { + this.attributes.put(a.localName,new Attribute(a.localName,a.qName,a.uri,a.value)); + } + processNS(); + } + + public ParserElement(ParserElement parent, String uri, String localName, String qName, Attributes attributes) { + this.parent = parent; + this.uri = uri; + this.localName = localName; + this.qName = qName; + int attrCount = attributes.getLength(); + this.attributes = new HashMap<>(attrCount); + for (int i = 0; i < attrCount; i++) { + this.attributes.put(attributes.getLocalName(i),new Attribute(attributes.getLocalName(i),attributes.getQName(i),attributes.getURI(i),attributes.getValue(i))); + } + processNS(); + } + + public ParserElement(ParserElement parent, String uri, String localName, String qName, Collection attributes) { + this.parent = parent; + this.uri = uri; + this.localName = localName; + this.qName = qName; + this.attributes = new HashMap<>(attributes.size()); + for (Attribute a : attributes) { + this.attributes.put(a.localName,new Attribute(a.localName,a.qName,a.uri,a.value)); + } + processNS(); + } + + private void processNS() { + + nsMap = new HashMap<>(); + for (Attribute a : attributes.values()) { + if (a.localName.startsWith("xmlns")) { + String ns[] = a.localName.split(":"); + if (ns.length == 2) { + nsMap.put(ns[1], a.value); + } + } + } + Map newAttrs = new HashMap<>(); + for (Attribute a : attributes.values()) { + String ns[] = a.localName.split(":"); + if (ns.length == 2) { + if ("xmlns".equals(ns[0])) + continue; + String namespace = getNS(ns[0]); + if (namespace != null) { + if (newAttrs.put(ns[1], new Attribute(ns[1], a.qName, namespace, a.value)) != null) + throw new RuntimeException("XML parser internal error, overlapping attribute names " + ns[1]); + } else { + throw new RuntimeException("XML parser error, namespace " + ns[0] + " for attribute " + a.localName + " not defined."); + } + } else { + newAttrs.put(a.localName, a); + } + } + attributes = newAttrs; + + String nameNs[] = qName.split(":"); + if (nameNs.length == 2) { + localName = nameNs[1]; + if (uri.length() == 0) + uri = getNS(nameNs[0]) + "/"+localName; + } else if (localName.length() == 0) { + localName = qName; + } + } + + public String getNS(String key) { + String ns = nsMap.get(key); + if (ns == null && parent != null) + ns = parent.getNS(key); + return ns; + } + + public String getUri() { + return uri; + } + + public String getLocalName() { + return localName; + } + + public String getQName() { + return qName; + } + + public Collection getAttributes() { + return attributes.values(); + } + + public Attribute getAttribute(String name) { + return attributes.get(name); + } + + private Resource data; + + public void setData(Resource data) { + this.data = data; + } + + public Resource getData() { + return data; + } + + public void setElementParser(XMLElementParser parser) { + this.elementParser = parser; + } + + public XMLElementParser getElementParser() { + return elementParser; + } + + public void setXMLParser(XMLParser parser) { + this.xmlParser = parser; + } + + public XMLParser getXMLParser() { + return xmlParser; + } + + public void registerListChild(Resource predicate, ParserElement child) { + if (lists == null) + lists = new MapList<>(); + lists.add(predicate, child); + } + + public void createLists(WriteGraph graph) throws DatabaseException { + if (lists == null) + return; + Layer0 L0 = Layer0.getInstance(graph); + for (Resource predicate : lists.getKeys()) { + List children = new ArrayList<>(); + for (ParserElement child : lists.getValues(predicate)) + children.add(child.getData()); + Resource list = ListUtils.create(graph, L0.List, L0.List_Element, null, children); + graph.claim(getData(), predicate, list); + } + } + } \ No newline at end of file