1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.server.impl.atompub;
20
21 import java.io.OutputStream;
22
23 import javax.xml.stream.XMLOutputFactory;
24 import javax.xml.stream.XMLStreamException;
25 import javax.xml.stream.XMLStreamWriter;
26
27 import org.apache.chemistry.opencmis.commons.impl.Constants;
28
29
30
31
32 public abstract class XMLDocumentBase {
33
34 public static final String PREFIX_ATOM = "atom";
35 public static final String PREFIX_CMIS = "cmis";
36 public static final String PREFIX_RESTATOM = "cmisra";
37 public static final String PREFIX_APP = "app";
38 public static final String PREFIX_XSI = "xsi";
39
40 private XMLStreamWriter writer;
41
42
43
44
45 public void setNamespaces() throws XMLStreamException {
46 writer.setPrefix(PREFIX_ATOM, Constants.NAMESPACE_ATOM);
47 writer.setPrefix(PREFIX_CMIS, Constants.NAMESPACE_CMIS);
48 writer.setPrefix(PREFIX_RESTATOM, Constants.NAMESPACE_RESTATOM);
49 writer.setPrefix(PREFIX_APP, Constants.NAMESPACE_APP);
50 writer.setPrefix(PREFIX_XSI, Constants.NAMESPACE_XSI);
51 }
52
53
54
55
56 public void writeNamespace(String namespaceUri) throws XMLStreamException {
57 writer.writeNamespace(writer.getPrefix(namespaceUri), namespaceUri);
58 }
59
60
61
62
63 public void startDocument(OutputStream out) throws XMLStreamException {
64
65 XMLOutputFactory factory = XMLOutputFactory.newInstance();
66 writer = factory.createXMLStreamWriter(out, "UTF-8");
67
68
69 writer.writeStartDocument("UTF-8", "1.0");
70 setNamespaces();
71 }
72
73
74
75
76 public void endDocument() throws XMLStreamException {
77 if (writer == null) {
78 return;
79 }
80
81
82 writer.writeEndDocument();
83
84
85 writer.close();
86 }
87
88
89
90
91 public XMLStreamWriter getWriter() {
92 return writer;
93 }
94
95
96
97
98 protected void setWriter(XMLStreamWriter writer) {
99 this.writer = writer;
100 }
101 }