package org.simantics.xml.sax.base;\r
\r
+import java.io.BufferedOutputStream;\r
import java.io.File;\r
import java.io.FileOutputStream;\r
import java.io.IOException;\r
+import java.io.OutputStream;\r
\r
import javax.xml.stream.XMLOutputFactory;\r
import javax.xml.stream.XMLStreamException;\r
private ILogger logger;\r
private Resource root;\r
private XMLWriter xmlWriter;\r
+ private boolean indentOutput = true;\r
\r
public AbstractExporter(Session session, File file , Resource root, XMLWriter xmlWriter) {\r
this.session = session;\r
this.xmlWriter = xmlWriter;\r
}\r
\r
+ public void setIndentOutput(boolean indentOutput) {\r
+ this.indentOutput = indentOutput;\r
+ }\r
+ \r
public void doExport() throws DatabaseException {\r
doExport(MessageService.getDefault()); \r
}\r
session.syncRequest(req);\r
}\r
\r
+ private static XMLStreamWriter createIdentXMLStreamWriter(OutputStream textWriter) throws XMLStreamException {\r
+ XMLOutputFactory output = XMLOutputFactory.newInstance();\r
+ return new IndentingXMLStreamWriter(output.createXMLStreamWriter(textWriter, "UTF-8"));\r
+ //return new com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter(output.createXMLStreamWriter(textWriter, "UTF-8"));\r
+ }\r
+ \r
+ private static XMLStreamWriter createXMLStreamWriter(OutputStream textWriter) throws XMLStreamException {\r
+ XMLOutputFactory output = XMLOutputFactory.newInstance();\r
+ return output.createXMLStreamWriter(textWriter, "UTF-8");\r
+ }\r
+ \r
\r
private class ExportRequest extends ReadRequest {\r
@Override\r
public void run(ReadGraph graph) throws DatabaseException {\r
XMLStreamWriter writer = null;\r
+ OutputStream os = null;\r
try {\r
- XMLOutputFactory output = XMLOutputFactory.newInstance();\r
- writer = output.createXMLStreamWriter(new FileOutputStream(file),"UTF-8");\r
- writer.writeStartDocument("UTF-8", "1.0");\r
+ os = new BufferedOutputStream(new FileOutputStream(file));\r
+ if (indentOutput)\r
+ writer =createIdentXMLStreamWriter(os);\r
+ else\r
+ writer =createXMLStreamWriter(os);\r
+ writer.writeStartDocument("UTF-8", "1.1");\r
+ \r
xmlWriter.write(root, writer);\r
\r
writer.writeEndDocument();\r
+ writer.flush();\r
writer.close();\r
+ os.close();\r
} catch (IOException|XMLStreamException e) {\r
try {\r
writer.close();\r
} catch (XMLStreamException err) {\r
\r
+ }\r
+ try {\r
+ os.close();\r
+ }catch (IOException err) {\r
+ \r
}\r
throw new DatabaseException(e);\r
} \r