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); } } }