X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.xml.sax.base%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2Fbase%2FAbstractExporter.java;fp=org.simantics.xml.sax.base%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2Fbase%2FAbstractExporter.java;h=14a3fca9f327c5dfd6e80f3e26e5ede095d7fba7;hb=dd3b2c7ecd5f4b60734f2602b16637aa8be2a263;hp=0000000000000000000000000000000000000000;hpb=47791aa9453c9d90786bc8ca7de102fb3ee90f3b;p=simantics%2Finterop.git diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractExporter.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractExporter.java new file mode 100644 index 0000000..14a3fca --- /dev/null +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractExporter.java @@ -0,0 +1,79 @@ +package org.simantics.xml.sax.base; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.message.ILogger; +import org.simantics.message.MessageService; + +public class AbstractExporter { + + private Session session; + private File file; + private ILogger logger; + private Resource root; + private XMLWriter xmlWriter; + + public AbstractExporter(Session session, File file , Resource root, XMLWriter xmlWriter) { + this.session = session; + this.file = file; + this.root = root; + this.xmlWriter = xmlWriter; + } + + public AbstractExporter(Session session, File file , Resource root) { + this.session = session; + this.file = file; + this.root = root; + } + + public void setXmlWriter(XMLWriter xmlWriter) { + this.xmlWriter = xmlWriter; + } + + public void doExport() throws DatabaseException { + doExport(MessageService.getDefault()); + } + + public void doExport(ILogger logger) throws DatabaseException { + ExportRequest req = new ExportRequest(); + session.syncRequest(req); + } + + + private class ExportRequest extends ReadRequest { + @Override + public void run(ReadGraph graph) throws DatabaseException { + XMLStreamWriter writer = null; + try { + XMLOutputFactory output = XMLOutputFactory.newInstance(); + writer = output.createXMLStreamWriter(new FileOutputStream(file),"UTF-8"); + writer.writeStartDocument("UTF-8", "1.0"); + xmlWriter.write(root, writer); + + writer.writeEndDocument(); + writer.close(); + } catch (IOException|XMLStreamException e) { + try { + writer.close(); + } catch (XMLStreamException err) { + + } + throw new DatabaseException(e); + } + + + } + } + +}