1 package org.simantics.interop.xmlio;
3 import java.io.BufferedReader;
5 import java.io.FileReader;
7 import java.io.Serializable;
8 import java.util.ArrayList;
10 import java.util.Stack;
12 import javax.xml.parsers.SAXParser;
13 import javax.xml.parsers.SAXParserFactory;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.Session;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.DelayedWriteRequest;
20 import org.simantics.db.common.request.WriteRequest;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.util.Layer0Utils;
23 import org.simantics.db.layer0.util.SessionGarbageCollection;
24 import org.simantics.layer0.Layer0;
25 import org.xml.sax.Attributes;
26 import org.xml.sax.InputSource;
27 import org.xml.sax.SAXException;
28 import org.xml.sax.XMLReader;
29 import org.xml.sax.helpers.DefaultHandler;
31 import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
35 public class LoadXML {
37 private Session session;
38 private Resource resultRoot;
43 //private Map<Integer,Resource> idMap = new HashMap<Integer, Resource>();
44 Int2ObjectOpenHashMap<Resource> idMap = new Int2ObjectOpenHashMap<Resource>();
46 //private Map<String,Resource> uriMap = new HashMap<String, Resource>();
48 private WriteGraph graph;
50 private LoadRule rule;
52 private int resources;
53 private int statements;
55 private static final boolean USE_DELAYED = true;
57 public LoadXML(Session session, File file) {
58 this.session = session;
60 this.rule = new DefaultLoadRule();
63 public void setRule(LoadRule rule) {
67 public void load(Resource subj, Resource pred) throws Exception {
72 session.syncRequest(new ImportDelayedRequest(subj,pred));
74 session.syncRequest(new ImportRequest(subj,pred));
76 //session.syncRequest(new DelayedWriteRequest() {
79 System.out.println("Import done.");
82 private class ImportRequest extends WriteRequest {
83 Resource subj;Resource pred;
86 public ImportRequest(Resource subj, Resource pred) {
93 public void perform(WriteGraph graph) throws DatabaseException {
94 doImport(graph, subj, pred);
98 private class ImportDelayedRequest extends DelayedWriteRequest {
99 Resource subj;Resource pred;
102 public ImportDelayedRequest(Resource subj, Resource pred) {
109 public void perform(WriteGraph graph) throws DatabaseException {
110 doImport(graph, subj, pred);
114 private void doImport(WriteGraph graph, Resource subj, Resource pred) throws DatabaseException {
115 LoadXML.this.graph = graph;
116 l0 = Layer0.getInstance(graph);
117 Layer0Utils.setDependenciesIndexingDisabled(graph, true);
119 XMLParser parser = new XMLParser();
120 parser.parse(file.getAbsolutePath());
121 System.out.println("Imported " + statements + " Statements, " + resources + " Resources.");
122 } catch (Exception e) {
123 throw new DatabaseException(e);
125 resultRoot = idMap.get(0);
126 graph.claim(subj, pred, resultRoot);
132 // public Resource getResult() {
133 // return resultRoot;
136 protected Resource getResource(ReadGraph graph, String uri) throws DatabaseException {
137 return rule.getResource(graph, uri);
140 protected Object createValue(ReadGraph graph, List<String> value, List<String> types) throws DatabaseException {
141 return rule.createValue(graph, value, types);
144 protected void initializeWithBundles(ReadGraph graph, List<Bundle> bundles) throws DatabaseException {
145 rule.initializeWithBundles(graph, bundles);
149 public class XMLParser extends DefaultHandler implements Serializable {
151 private static final long serialVersionUID = -4995836637014958966L;
153 private Stack<Element> current = new Stack<Element>();
155 private void loadElement(Element element) throws SAXException{
156 String name = element.qName;
157 if ("graphexport".equals(name))
159 if ("graphbundles".equals(name)) {
160 element.setData(new ArrayList<Bundle>());
163 if ("bundle".equals(name))
165 if ("resource".equals(name)) {
166 element.setData(new Res());
169 if ("type".equals(name)) {
172 if ("statement".equals(name))
177 private void handleElement(Stack<Element> parents, Element element) throws SAXException{
179 String elementName = element.qName;
180 if ("graphexport".equals(elementName))
182 if ("graphbundles".equals(elementName)) {
183 List<Bundle> list = (List<Bundle>)element.getData();
184 initializeWithBundles(graph, list);
185 element.setData(null);
188 if ("bundle".equals(elementName)) {
189 List<Bundle> list = (List<Bundle>)parents.peek().getData();
191 String versionId = null;
192 for (Attribute a : element.getAttributes()) {
193 if ("name".equals(a.qName))
195 if ("versionid".equals(a.qName))
198 list.add(new Bundle(name,versionId));
201 if ("resource".equals(elementName)) {
202 Res res = (Res)element.getData();
207 for (Attribute a : element.getAttributes()) {
208 if ("id".equals(a.qName))
209 id = Integer.parseInt(a.value);
210 if ("rel".equals(a.qName))
211 rel = "true".equals(a.value);
212 if ("uri".equals(a.qName))
216 r = getResource(graph, uri);
219 r = graph.newResource();
220 for (String typeUri : res.types) {
221 Resource type =getResource(graph, typeUri);
222 graph.claim(r, l0.InstanceOf, type);
225 if (res.values.size() > 0 ) {
226 Object valObj = createValue(graph, res.values, res.types);
227 graph.claimValue(r, valObj);
231 throw new SAXException("Resource id missing");
236 element.setData(null);
240 if ("type".equals(elementName)) {
241 Element parent = parents.peek();
242 Res res = (Res)parent.getData();
244 for (Attribute a : element.getAttributes()) {
245 if ("uri".equals(a.qName))
253 if ("value".equals(elementName)) {
254 Element parent = parents.peek();
255 Res res = (Res)parent.getData();
257 for (Attribute a : element.getAttributes()) {
258 if ("value".equals(a.qName))
262 res.values.add(value);
266 if ("statement".equals(elementName)) {
270 for (Attribute a : element.getAttributes()) {
271 if ("subject".equals(a.qName))
272 subjId = Integer.parseInt(a.value);
273 if ("predicate".equals(a.qName))
274 predId = Integer.parseInt(a.value);
275 if ("object".equals(a.qName))
276 objId = Integer.parseInt(a.value);
279 graph.claim(idMap.get(subjId), idMap.get(predId), idMap.get(objId));
281 if (statements % 10000 == 0) {
282 System.out.println("Imported " + statements + " Statements, " + resources + " Resources..");
283 //SessionGarbageCollection.gc(graph, 0, -1);
285 if (!USE_DELAYED && statements % 100000 == 0) {
286 SessionGarbageCollection.gc(graph, 0, -1);
293 } catch (DatabaseException e) {
294 throw new SAXException(e);
302 public void parse(String filename) throws Exception {
303 SAXParserFactory spf = SAXParserFactory.newInstance();
304 SAXParser saxParser = spf.newSAXParser();
306 XMLReader reader = saxParser.getXMLReader();
307 Reader file = new BufferedReader(new FileReader(filename));
308 reader.setContentHandler(this);
309 reader.parse(new InputSource(file));
314 public void startElement(String uri, String localName, String name,
315 Attributes attributes) throws SAXException {
316 Element e = new Element(uri,localName,name,attributes);
318 //texts.push(new String());
323 public void endElement(String uri, String localName, String name)
324 throws SAXException {
326 if (!current.empty()) {
330 handleElement(current,e);
336 public class Attribute {
337 public String localName;
342 public Attribute(String localName, String qName, String uri, String value) {
343 this.localName = localName;
350 public class Element implements Serializable {
351 private static final long serialVersionUID = -5207502156942818875L;
355 List<Attribute> attributes = new ArrayList<Attribute>();
357 public Element(String uri, String localName, String qName, Attributes attributes) {
359 this.localName = localName;
361 for (int i = 0; i < attributes.getLength(); i++) {
362 this.attributes.add(new Attribute(attributes.getLocalName(i),attributes.getQName(i),attributes.getURI(i),attributes.getValue(i)));
366 public String getUri() {
370 public String getLocalName() {
374 public String getQName() {
378 public List<Attribute> getAttributes() {
384 public void setData(Object data) {
388 public Object getData() {
395 List<String> types = new ArrayList<String>();
396 List<String> values = new ArrayList<String>();
399 public static class Bundle {
402 public Bundle(String name, String versionId) {
405 this.versionId = versionId;
408 public String getName() {
412 public String getVersionId() {