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;h=23831a2a2efe1ef1d98a767097a6d783b488bd72;hb=f52233c5cab2e5b8839b0de84b13c7d71f3dd571;hp=14a3fca9f327c5dfd6e80f3e26e5ede095d7fba7;hpb=dd3b2c7ecd5f4b60734f2602b16637aa8be2a263;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 index 14a3fca..23831a2 100644 --- 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 @@ -1,8 +1,10 @@ package org.simantics.xml.sax.base; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; @@ -18,11 +20,15 @@ import org.simantics.message.MessageService; public class AbstractExporter { + private static String ENCODING = "UTF-8"; + private static String XML_VERSION = "1.0"; + private Session session; private File file; private ILogger logger; private Resource root; private XMLWriter xmlWriter; + private boolean indentOutput = true; public AbstractExporter(Session session, File file , Resource root, XMLWriter xmlWriter) { this.session = session; @@ -41,6 +47,10 @@ public class AbstractExporter { this.xmlWriter = xmlWriter; } + public void setIndentOutput(boolean indentOutput) { + this.indentOutput = indentOutput; + } + public void doExport() throws DatabaseException { doExport(MessageService.getDefault()); } @@ -50,24 +60,47 @@ public class AbstractExporter { session.syncRequest(req); } + private static XMLStreamWriter createIdentXMLStreamWriter(OutputStream textWriter) throws XMLStreamException { + XMLOutputFactory output = XMLOutputFactory.newInstance(); + return new IndentingXMLStreamWriter(output.createXMLStreamWriter(textWriter, ENCODING)); + //return new com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter(output.createXMLStreamWriter(textWriter, "UTF-8")); + } + + private static XMLStreamWriter createXMLStreamWriter(OutputStream textWriter) throws XMLStreamException { + XMLOutputFactory output = XMLOutputFactory.newInstance(); + return output.createXMLStreamWriter(textWriter, ENCODING); + } + private class ExportRequest extends ReadRequest { @Override public void run(ReadGraph graph) throws DatabaseException { XMLStreamWriter writer = null; + OutputStream os = null; try { - XMLOutputFactory output = XMLOutputFactory.newInstance(); - writer = output.createXMLStreamWriter(new FileOutputStream(file),"UTF-8"); - writer.writeStartDocument("UTF-8", "1.0"); + os = new BufferedOutputStream(new FileOutputStream(file)); + if (indentOutput) + writer =createIdentXMLStreamWriter(os); + else + writer =createXMLStreamWriter(os); + writer.writeStartDocument(ENCODING, XML_VERSION); + xmlWriter.write(root, writer); writer.writeEndDocument(); + writer.flush(); writer.close(); + os.close(); } catch (IOException|XMLStreamException e) { try { writer.close(); } catch (XMLStreamException err) { + } + try { + os.close(); + }catch (IOException err) { + } throw new DatabaseException(e); }