X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=org.simantics.xml.sax.base%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2Fbase%2FAbstractImporter.java;fp=org.simantics.xml.sax.base%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2Fbase%2FAbstractImporter.java;h=af5c2c3da0fc43c1da6ea4f42e53129aa0fd43c3;hb=ada38ab0a1a98dcb413bef3273064da36ce2d273;hp=0000000000000000000000000000000000000000;hpb=db6def39515fd442543064502a9ae7ae07ffc160;p=simantics%2Finterop.git diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractImporter.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractImporter.java new file mode 100644 index 0000000..af5c2c3 --- /dev/null +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractImporter.java @@ -0,0 +1,104 @@ +package org.simantics.xml.sax.base; + +import java.io.File; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.DelayedWriteRequest; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.Layer0Utils; +import org.simantics.message.ILogger; +import org.simantics.message.MessageService; + + +public abstract class AbstractImporter { + + + private Session session; + private File file; + private ILogger logger; + + private static final boolean USE_DELAYED = false; + + public AbstractImporter(Session session, File file) { + this.session = session; + this.file = file; + } + public Resource doImport() throws DatabaseException { + return doImport(MessageService.getDefault()); + } + + public Resource doImport(ILogger logger) throws DatabaseException { + this.logger = logger; + if (USE_DELAYED) { + ImportDelayedRequest req = new ImportDelayedRequest(); + session.syncRequest(req); + return req.getResult(); + } else { + ImportRequest req = new ImportRequest(); + session.syncRequest(req); + return req.getResult(); + } + } + + public File getFile() { + return file; + } + + public abstract void configure(XMLParser parser); + + private class ImportRequest extends WriteRequest { + private Resource result; + public ImportRequest() { + super(); + } + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + result = doImport(graph); + } + + public Resource getResult() { + return result; + } + } + + private class ImportDelayedRequest extends DelayedWriteRequest { + private Resource result; + public ImportDelayedRequest() { + super(); + } + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + result = doImport(graph); + } + + public Resource getResult() { + return result; + } + } + + private Resource doImport(WriteGraph graph) throws DatabaseException { + Layer0Utils.setDependenciesIndexingDisabled(graph, true); + try { + XMLParser parser = new XMLParser(graph); + configure(parser); + parser.parse(file, logger); + System.out.println(file.getAbsolutePath() + " imported"); + Resource root = parser.getRoot(); + if (root == null) { + throw new DatabaseException(file.getAbsolutePath() + " import failed. File is corrupted, or is not given type"); + } + return root; + } catch (DatabaseException e) { + throw e; + } catch (Exception e) { + throw new DatabaseException(e); + } + } +}