1 package org.simantics.interop.xmlio;
\r
3 import java.io.BufferedReader;
\r
5 import java.io.FileReader;
\r
6 import java.io.Reader;
\r
7 import java.io.Serializable;
\r
8 import java.util.ArrayList;
\r
9 import java.util.List;
\r
10 import java.util.Stack;
\r
12 import javax.xml.parsers.SAXParser;
\r
13 import javax.xml.parsers.SAXParserFactory;
\r
15 import org.cojen.util.IntHashMap;
\r
16 import org.simantics.db.ReadGraph;
\r
17 import org.simantics.db.Resource;
\r
18 import org.simantics.db.Session;
\r
19 import org.simantics.db.WriteGraph;
\r
20 import org.simantics.db.common.request.DelayedWriteRequest;
\r
21 import org.simantics.db.common.request.WriteRequest;
\r
22 import org.simantics.db.exception.DatabaseException;
\r
23 import org.simantics.db.layer0.util.Layer0Utils;
\r
24 import org.simantics.db.layer0.util.SessionGarbageCollection;
\r
25 import org.simantics.layer0.Layer0;
\r
26 import org.xml.sax.Attributes;
\r
27 import org.xml.sax.InputSource;
\r
28 import org.xml.sax.SAXException;
\r
29 import org.xml.sax.XMLReader;
\r
30 import org.xml.sax.helpers.DefaultHandler;
\r
34 public class LoadXML {
\r
36 private Session session;
\r
37 private Resource resultRoot;
\r
42 //private Map<Integer,Resource> idMap = new HashMap<Integer, Resource>();
\r
43 private IntHashMap<Resource> idMap = new IntHashMap<Resource>();
\r
45 //private Map<String,Resource> uriMap = new HashMap<String, Resource>();
\r
47 private WriteGraph graph;
\r
49 private LoadRule rule;
\r
51 private int resources;
\r
52 private int statements;
\r
54 private static final boolean USE_DELAYED = true;
\r
56 public LoadXML(Session session, File file) {
\r
57 this.session = session;
\r
59 this.rule = new DefaultLoadRule();
\r
62 public void setRule(LoadRule rule) {
\r
66 public void load(Resource subj, Resource pred) throws Exception {
\r
71 session.syncRequest(new ImportDelayedRequest(subj,pred));
\r
73 session.syncRequest(new ImportRequest(subj,pred));
\r
75 //session.syncRequest(new DelayedWriteRequest() {
\r
78 System.out.println("Import done.");
\r
81 private class ImportRequest extends WriteRequest {
\r
82 Resource subj;Resource pred;
\r
85 public ImportRequest(Resource subj, Resource pred) {
\r
92 public void perform(WriteGraph graph) throws DatabaseException {
\r
93 doImport(graph, subj, pred);
\r
97 private class ImportDelayedRequest extends DelayedWriteRequest {
\r
98 Resource subj;Resource pred;
\r
101 public ImportDelayedRequest(Resource subj, Resource pred) {
\r
108 public void perform(WriteGraph graph) throws DatabaseException {
\r
109 doImport(graph, subj, pred);
\r
113 private void doImport(WriteGraph graph, Resource subj, Resource pred) throws DatabaseException {
\r
114 LoadXML.this.graph = graph;
\r
115 l0 = Layer0.getInstance(graph);
\r
116 Layer0Utils.setDependenciesIndexingDisabled(graph, true);
\r
118 XMLParser parser = new XMLParser();
\r
119 parser.parse(file.getAbsolutePath());
\r
120 System.out.println("Imported " + statements + " Statements, " + resources + " Resources.");
\r
121 } catch (Exception e) {
\r
122 throw new DatabaseException(e);
\r
124 resultRoot = idMap.get(0);
\r
125 graph.claim(subj, pred, resultRoot);
\r
131 // public Resource getResult() {
\r
132 // return resultRoot;
\r
135 protected Resource getResource(ReadGraph graph, String uri) throws DatabaseException {
\r
136 return rule.getResource(graph, uri);
\r
139 protected Object createValue(ReadGraph graph, List<String> value, List<String> types) throws DatabaseException {
\r
140 return rule.createValue(graph, value, types);
\r
143 protected void initializeWithBundles(ReadGraph graph, List<Bundle> bundles) throws DatabaseException {
\r
144 rule.initializeWithBundles(graph, bundles);
\r
148 public class XMLParser extends DefaultHandler implements Serializable {
\r
150 private static final long serialVersionUID = -4995836637014958966L;
\r
152 private Stack<Element> current = new Stack<Element>();
\r
154 private void loadElement(Element element) throws SAXException{
\r
155 String name = element.qName;
\r
156 if ("graphexport".equals(name))
\r
158 if ("graphbundles".equals(name)) {
\r
159 element.setData(new ArrayList<Bundle>());
\r
162 if ("bundle".equals(name))
\r
164 if ("resource".equals(name)) {
\r
165 element.setData(new Res());
\r
168 if ("type".equals(name)) {
\r
171 if ("statement".equals(name))
\r
176 private void handleElement(Stack<Element> parents, Element element) throws SAXException{
\r
178 String elementName = element.qName;
\r
179 if ("graphexport".equals(elementName))
\r
181 if ("graphbundles".equals(elementName)) {
\r
182 List<Bundle> list = (List<Bundle>)element.getData();
\r
183 initializeWithBundles(graph, list);
\r
184 element.setData(null);
\r
187 if ("bundle".equals(elementName)) {
\r
188 List<Bundle> list = (List<Bundle>)parents.peek().getData();
\r
189 String name = null;
\r
190 String versionId = null;
\r
191 for (Attribute a : element.getAttributes()) {
\r
192 if ("name".equals(a.qName))
\r
194 if ("versionid".equals(a.qName))
\r
195 versionId = a.value;
\r
197 list.add(new Bundle(name,versionId));
\r
200 if ("resource".equals(elementName)) {
\r
201 Res res = (Res)element.getData();
\r
202 boolean rel = false;
\r
206 for (Attribute a : element.getAttributes()) {
\r
207 if ("id".equals(a.qName))
\r
208 id = Integer.parseInt(a.value);
\r
209 if ("rel".equals(a.qName))
\r
210 rel = "true".equals(a.value);
\r
211 if ("uri".equals(a.qName))
\r
215 r = getResource(graph, uri);
\r
218 r = graph.newResource();
\r
219 for (String typeUri : res.types) {
\r
220 Resource type =getResource(graph, typeUri);
\r
221 graph.claim(r, l0.InstanceOf, type);
\r
224 if (res.values.size() > 0 ) {
\r
225 Object valObj = createValue(graph, res.values, res.types);
\r
226 graph.claimValue(r, valObj);
\r
230 throw new SAXException("Resource id missing");
\r
234 res.values.clear();
\r
235 element.setData(null);
\r
239 if ("type".equals(elementName)) {
\r
240 Element parent = parents.peek();
\r
241 Res res = (Res)parent.getData();
\r
243 for (Attribute a : element.getAttributes()) {
\r
244 if ("uri".equals(a.qName))
\r
248 res.types.add(uri);
\r
252 if ("value".equals(elementName)) {
\r
253 Element parent = parents.peek();
\r
254 Res res = (Res)parent.getData();
\r
255 String value = null;
\r
256 for (Attribute a : element.getAttributes()) {
\r
257 if ("value".equals(a.qName))
\r
261 res.values.add(value);
\r
265 if ("statement".equals(elementName)) {
\r
269 for (Attribute a : element.getAttributes()) {
\r
270 if ("subject".equals(a.qName))
\r
271 subjId = Integer.parseInt(a.value);
\r
272 if ("predicate".equals(a.qName))
\r
273 predId = Integer.parseInt(a.value);
\r
274 if ("object".equals(a.qName))
\r
275 objId = Integer.parseInt(a.value);
\r
278 graph.claim(idMap.get(subjId), idMap.get(predId), idMap.get(objId));
\r
280 if (statements % 10000 == 0) {
\r
281 System.out.println("Imported " + statements + " Statements, " + resources + " Resources..");
\r
282 //SessionGarbageCollection.gc(graph, 0, -1);
\r
284 if (!USE_DELAYED && statements % 100000 == 0) {
\r
285 SessionGarbageCollection.gc(graph, 0, -1);
\r
292 } catch (DatabaseException e) {
\r
293 throw new SAXException(e);
\r
297 public void done() {
\r
301 public void parse(String filename) throws Exception {
\r
302 SAXParserFactory spf = SAXParserFactory.newInstance();
\r
303 SAXParser saxParser = spf.newSAXParser();
\r
305 XMLReader reader = saxParser.getXMLReader();
\r
306 Reader file = new BufferedReader(new FileReader(filename));
\r
307 reader.setContentHandler(this);
\r
308 reader.parse(new InputSource(file));
\r
313 public void startElement(String uri, String localName, String name,
\r
314 Attributes attributes) throws SAXException {
\r
315 Element e = new Element(uri,localName,name,attributes);
\r
317 //texts.push(new String());
\r
322 public void endElement(String uri, String localName, String name)
\r
323 throws SAXException {
\r
325 if (!current.empty()) {
\r
329 handleElement(current,e);
\r
335 public class Attribute {
\r
336 public String localName;
\r
337 public String qName;
\r
339 public String value;
\r
341 public Attribute(String localName, String qName, String uri, String value) {
\r
342 this.localName = localName;
\r
343 this.qName = qName;
\r
345 this.value = value;
\r
349 public class Element implements Serializable {
\r
350 private static final long serialVersionUID = -5207502156942818875L;
\r
354 List<Attribute> attributes = new ArrayList<Attribute>();
\r
356 public Element(String uri, String localName, String qName, Attributes attributes) {
\r
358 this.localName = localName;
\r
359 this.qName = qName;
\r
360 for (int i = 0; i < attributes.getLength(); i++) {
\r
361 this.attributes.add(new Attribute(attributes.getLocalName(i),attributes.getQName(i),attributes.getURI(i),attributes.getValue(i)));
\r
365 public String getUri() {
\r
369 public String getLocalName() {
\r
373 public String getQName() {
\r
377 public List<Attribute> getAttributes() {
\r
381 private Object data;
\r
383 public void setData(Object data) {
\r
387 public Object getData() {
\r
394 List<String> types = new ArrayList<String>();
\r
395 List<String> values = new ArrayList<String>();
\r
398 public static class Bundle {
\r
401 public Bundle(String name, String versionId) {
\r
404 this.versionId = versionId;
\r
407 public String getName() {
\r
411 public String getVersionId() {
\r