1 package org.simantics.xml.sax.base;
\r
3 import java.io.Serializable;
\r
4 import java.util.ArrayList;
\r
5 import java.util.Collection;
\r
6 import java.util.HashMap;
\r
7 import java.util.List;
\r
8 import java.util.Map;
\r
10 import org.simantics.db.Resource;
\r
11 import org.simantics.db.WriteGraph;
\r
12 import org.simantics.db.common.utils.ListUtils;
\r
13 import org.simantics.db.exception.DatabaseException;
\r
14 import org.simantics.layer0.Layer0;
\r
15 import org.simantics.utils.datastructures.MapList;
\r
16 import org.xml.sax.Attributes;
\r
18 public class ParserElement implements Serializable {
\r
19 private static final long serialVersionUID = -5207502156942818875L;
\r
23 Map<String, Attribute> attributes;
\r
24 MapList<Resource, ParserElement> lists;
\r
26 XMLElementParser elementParser;
\r
27 XMLParser xmlParser;
\r
29 public ParserElement(String uri, String localName, String qName, Attributes attributes) {
\r
31 this.localName = localName;
\r
33 int attrCount = attributes.getLength();
\r
34 this.attributes = new HashMap<>(attrCount);
\r
35 for (int i = 0; i < attrCount; i++) {
\r
36 this.attributes.put(attributes.getLocalName(i),new Attribute(attributes.getLocalName(i),attributes.getQName(i),attributes.getURI(i),attributes.getValue(i)));
\r
40 public ParserElement(String uri, String localName, String qName, Collection<Attribute> attributes) {
\r
42 this.localName = localName;
\r
44 this.attributes = new HashMap<>(attributes.size());
\r
45 for (Attribute a : attributes) {
\r
46 this.attributes.put(a.localName,new Attribute(a.localName,a.qName,a.uri,a.value));
\r
50 public String getUri() {
\r
54 public String getLocalName() {
\r
58 public String getQName() {
\r
62 public Collection<Attribute> getAttributes() {
\r
63 return attributes.values();
\r
66 public Attribute getAttribute(String name) {
\r
67 return attributes.get(name);
\r
70 private Resource data;
\r
72 public void setData(Resource data) {
\r
76 public Resource getData() {
\r
80 public void setElementParser(XMLElementParser parser) {
\r
81 this.elementParser = parser;
\r
84 public XMLElementParser getElementParser() {
\r
85 return elementParser;
\r
88 public void setXMLParser(XMLParser parser) {
\r
89 this.xmlParser = parser;
\r
92 public XMLParser getXMLParser() {
\r
96 public void registerListChild(Resource predicate, ParserElement child) {
\r
98 lists = new MapList<>();
\r
99 lists.add(predicate, child);
\r
102 public void createLists(WriteGraph graph) throws DatabaseException {
\r
105 Layer0 L0 = Layer0.getInstance(graph);
\r
106 for (Resource predicate : lists.getKeys()) {
\r
107 List<Resource> children = new ArrayList<>();
\r
108 for (ParserElement child : lists.getValues(predicate))
\r
109 children.add(child.getData());
\r
110 Resource list = ListUtils.create(graph, L0.List, L0.List_Element, null, children);
\r
111 graph.claim(getData(), predicate, list);
\r