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 if ("xml".equals(ns[0]))
100 String namespace = getNS(ns[0]);
101 if (namespace != null) {
102 if (newAttrs.put(ns[1], new Attribute(ns[1], a.qName, namespace, a.value)) != null)
103 throw new RuntimeException("XML parser internal error, overlapping attribute names " + ns[1]);
105 throw new RuntimeException("XML parser error, namespace " + ns[0] + " for attribute " + a.localName + " not defined.");
108 newAttrs.put(a.localName, a);
111 attributes = newAttrs;
113 String nameNs[] = qName.split(":");
114 if (nameNs.length == 2) {
115 localName = nameNs[1];
116 if (uri.length() == 0)
117 uri = getNS(nameNs[0]) + "/"+localName;
118 } else if (localName.length() == 0) {
123 public String getNS(String key) {
124 String ns = nsMap.get(key);
125 if (ns == null && parent != null)
126 ns = parent.getNS(key);
130 public String getUri() {
134 public String getLocalName() {
138 public String getQName() {
142 public Collection<Attribute> getAttributes() {
143 return attributes.values();
146 public Attribute getAttribute(String name) {
147 return attributes.get(name);
150 private Resource data;
152 public void setData(Resource data) {
156 public Resource getData() {
160 public void setElementParser(XMLElementParser parser) {
161 this.elementParser = parser;
164 public XMLElementParser getElementParser() {
165 return elementParser;
168 public void setXMLParser(XMLParser parser) {
169 this.xmlParser = parser;
172 public XMLParser getXMLParser() {
176 public void registerListChild(Resource predicate, ParserElement child) {
178 lists = new MapList<>();
179 lists.add(predicate, child);
182 public void createLists(WriteGraph graph) throws DatabaseException {
185 Layer0 L0 = Layer0.getInstance(graph);
186 for (Resource predicate : lists.getKeys()) {
187 List<Resource> children = new ArrayList<>();
188 for (ParserElement child : lists.getValues(predicate))
189 children.add(child.getData());
190 Resource list = ListUtils.create(graph, L0.List, L0.List_Element, null, children);
191 graph.claim(getData(), predicate, list);