1 package org.simantics.xml.sax.base;
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.HashMap;
10 import org.simantics.db.Resource;
11 import org.simantics.db.WriteGraph;
12 import org.simantics.db.common.utils.ListUtils;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.utils.datastructures.MapList;
16 import org.xml.sax.Attributes;
18 public class ParserElement implements Serializable {
19 private static final long serialVersionUID = -5207502156942818875L;
21 private String localName;
23 Map<String, Attribute> attributes;
24 MapList<Resource, ParserElement> lists;
26 XMLElementParser elementParser;
29 Map<String,String> nsMap;
31 private ParserElement parent;
33 public ParserElement(String uri, String localName, String qName, Attributes attributes) {
35 this.localName = localName;
37 int attrCount = attributes.getLength();
38 this.attributes = new HashMap<>(attrCount);
39 for (int i = 0; i < attrCount; i++) {
40 this.attributes.put(attributes.getLocalName(i),new Attribute(attributes.getLocalName(i),attributes.getQName(i),attributes.getURI(i),attributes.getValue(i)));
45 public ParserElement(String uri, String localName, String qName, Collection<Attribute> attributes) {
47 this.localName = localName;
49 this.attributes = new HashMap<>(attributes.size());
50 for (Attribute a : attributes) {
51 this.attributes.put(a.localName,new Attribute(a.localName,a.qName,a.uri,a.value));
56 public ParserElement(ParserElement parent, String uri, String localName, String qName, Attributes attributes) {
59 this.localName = localName;
61 int attrCount = attributes.getLength();
62 this.attributes = new HashMap<>(attrCount);
63 for (int i = 0; i < attrCount; i++) {
64 this.attributes.put(attributes.getLocalName(i),new Attribute(attributes.getLocalName(i),attributes.getQName(i),attributes.getURI(i),attributes.getValue(i)));
69 public ParserElement(ParserElement parent, String uri, String localName, String qName, Collection<Attribute> attributes) {
72 this.localName = localName;
74 this.attributes = new HashMap<>(attributes.size());
75 for (Attribute a : attributes) {
76 this.attributes.put(a.localName,new Attribute(a.localName,a.qName,a.uri,a.value));
81 private void processNS() {
83 nsMap = new HashMap<>();
84 for (Attribute a : attributes.values()) {
85 if (a.localName.startsWith("xmlns")) {
86 String ns[] = a.localName.split(":");
88 nsMap.put(ns[1], a.value);
92 Map<String, Attribute> newAttrs = new HashMap<>();
93 for (Attribute a : attributes.values()) {
94 String ns[] = a.localName.split(":");
96 if ("xmlns".equals(ns[0]))
98 String namespace = getNS(ns[0]);
99 if (namespace != null) {
100 if (newAttrs.put(ns[1], new Attribute(ns[1], a.qName, namespace, a.value)) != null)
101 throw new RuntimeException("XML parser internal error, overlapping attribute names " + ns[1]);
103 throw new RuntimeException("XML parser error, namespace " + ns[0] + " for attribute " + a.localName + " not defined.");
106 newAttrs.put(a.localName, a);
109 attributes = newAttrs;
111 String nameNs[] = qName.split(":");
112 if (nameNs.length == 2) {
113 localName = nameNs[1];
114 if (uri.length() == 0)
115 uri = getNS(nameNs[0]) + "/"+localName;
116 } else if (localName.length() == 0) {
121 public String getNS(String key) {
122 String ns = nsMap.get(key);
123 if (ns == null && parent != null)
124 ns = parent.getNS(key);
128 public String getUri() {
132 public String getLocalName() {
136 public String getQName() {
140 public Collection<Attribute> getAttributes() {
141 return attributes.values();
144 public Attribute getAttribute(String name) {
145 return attributes.get(name);
148 private Resource data;
150 public void setData(Resource data) {
154 public Resource getData() {
158 public void setElementParser(XMLElementParser parser) {
159 this.elementParser = parser;
162 public XMLElementParser getElementParser() {
163 return elementParser;
166 public void setXMLParser(XMLParser parser) {
167 this.xmlParser = parser;
170 public XMLParser getXMLParser() {
174 public void registerListChild(Resource predicate, ParserElement child) {
176 lists = new MapList<>();
177 lists.add(predicate, child);
180 public void createLists(WriteGraph graph) throws DatabaseException {
183 Layer0 L0 = Layer0.getInstance(graph);
184 for (Resource predicate : lists.getKeys()) {
185 List<Resource> children = new ArrayList<>();
186 for (ParserElement child : lists.getValues(predicate))
187 children.add(child.getData());
188 Resource list = ListUtils.create(graph, L0.List, L0.List_Element, null, children);
189 graph.claim(getData(), predicate, list);