1 package org.simantics.xml.sax.base;
\r
3 import java.io.BufferedOutputStream;
\r
5 import java.io.FileOutputStream;
\r
6 import java.io.IOException;
\r
7 import java.io.OutputStream;
\r
9 import javax.xml.stream.XMLOutputFactory;
\r
10 import javax.xml.stream.XMLStreamException;
\r
11 import javax.xml.stream.XMLStreamWriter;
\r
13 import org.simantics.db.ReadGraph;
\r
14 import org.simantics.db.Resource;
\r
15 import org.simantics.db.Session;
\r
16 import org.simantics.db.common.request.ReadRequest;
\r
17 import org.simantics.db.exception.DatabaseException;
\r
18 import org.simantics.message.ILogger;
\r
19 import org.simantics.message.MessageService;
\r
21 public class AbstractExporter {
\r
23 private static String ENCODING = "UTF-8";
\r
24 private static String XML_VERSION = "1.0";
\r
26 private Session session;
\r
28 private ILogger logger;
\r
29 private Resource root;
\r
30 private XMLWriter xmlWriter;
\r
31 private boolean indentOutput = true;
\r
33 public AbstractExporter(Session session, File file , Resource root, XMLWriter xmlWriter) {
\r
34 this.session = session;
\r
37 this.xmlWriter = xmlWriter;
\r
40 public AbstractExporter(Session session, File file , Resource root) {
\r
41 this.session = session;
\r
46 public void setXmlWriter(XMLWriter xmlWriter) {
\r
47 this.xmlWriter = xmlWriter;
\r
50 public void setIndentOutput(boolean indentOutput) {
\r
51 this.indentOutput = indentOutput;
\r
54 public void doExport() throws DatabaseException {
\r
55 doExport(MessageService.getDefault());
\r
58 public void doExport(ILogger logger) throws DatabaseException {
\r
59 ExportRequest req = new ExportRequest();
\r
60 session.syncRequest(req);
\r
63 private static XMLStreamWriter createIdentXMLStreamWriter(OutputStream textWriter) throws XMLStreamException {
\r
64 XMLOutputFactory output = XMLOutputFactory.newInstance();
\r
65 return new IndentingXMLStreamWriter(output.createXMLStreamWriter(textWriter, ENCODING));
\r
66 //return new com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter(output.createXMLStreamWriter(textWriter, "UTF-8"));
\r
69 private static XMLStreamWriter createXMLStreamWriter(OutputStream textWriter) throws XMLStreamException {
\r
70 XMLOutputFactory output = XMLOutputFactory.newInstance();
\r
71 return output.createXMLStreamWriter(textWriter, ENCODING);
\r
75 private class ExportRequest extends ReadRequest {
\r
77 public void run(ReadGraph graph) throws DatabaseException {
\r
78 XMLStreamWriter writer = null;
\r
79 OutputStream os = null;
\r
81 os = new BufferedOutputStream(new FileOutputStream(file));
\r
83 writer =createIdentXMLStreamWriter(os);
\r
85 writer =createXMLStreamWriter(os);
\r
86 writer.writeStartDocument(ENCODING, XML_VERSION);
\r
88 xmlWriter.write(root, writer);
\r
90 writer.writeEndDocument();
\r
94 } catch (IOException|XMLStreamException e) {
\r
97 } catch (XMLStreamException err) {
\r
102 }catch (IOException err) {
\r
105 throw new DatabaseException(e);
\r