1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package eu.ehri.project.exporters.xml;
21
22 import javax.xml.namespace.NamespaceContext;
23 import javax.xml.stream.XMLStreamException;
24 import javax.xml.stream.XMLStreamWriter;
25 import java.util.Stack;
26
27
28
29
30 public class IndentingXMLStreamWriter implements XMLStreamWriter, AutoCloseable {
31
32 private enum State {
33 SEEN_NOTHING, SEEN_ELEMENT, SEEN_DATA
34 }
35
36 private State state = State.SEEN_NOTHING;
37 private Stack<State> stateStack = new Stack<>();
38
39 private final String indentStep;
40 private int depth = 0;
41
42 private final XMLStreamWriter writer;
43
44 public IndentingXMLStreamWriter(XMLStreamWriter writer, String indentStep) {
45 this.writer = writer;
46 this.indentStep = indentStep;
47 }
48
49 public IndentingXMLStreamWriter(XMLStreamWriter writer) {
50 this(writer, " ");
51 }
52
53 private void onStartElement() throws XMLStreamException {
54 stateStack.push(State.SEEN_ELEMENT);
55 state = State.SEEN_NOTHING;
56 if (depth > 0) {
57 writer.writeCharacters("\n");
58 }
59 doIndent();
60 depth++;
61 }
62
63 private void onEndElement() throws XMLStreamException {
64 depth--;
65 if (state == State.SEEN_ELEMENT) {
66 writer.writeCharacters("\n");
67 doIndent();
68 }
69 state = stateStack.pop();
70 }
71
72 private void onEmptyElement() throws XMLStreamException {
73 state = State.SEEN_ELEMENT;
74 if (depth > 0) {
75 writer.writeCharacters("\n");
76 }
77 doIndent();
78 }
79
80 private void doIndent() throws XMLStreamException {
81 if (depth > 0) {
82 for (int i = 0; i < depth; i++)
83 writer.writeCharacters(indentStep);
84 }
85 }
86
87 @Override
88 public void writeStartDocument() throws XMLStreamException {
89 writer.writeStartDocument();
90 writer.writeCharacters("\n");
91 }
92
93 @Override
94 public void writeStartDocument(String version) throws XMLStreamException {
95 writer.writeStartDocument(version);
96 writer.writeCharacters("\n");
97 }
98
99 @Override
100 public void writeStartDocument(String encoding, String version) throws XMLStreamException {
101 writer.writeStartDocument(encoding, version);
102 writer.writeCharacters("\n");
103 }
104
105 @Override
106 public void writeStartElement(String localName) throws XMLStreamException {
107 onStartElement();
108 writer.writeStartElement(localName);
109 }
110
111 @Override
112 public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
113 onStartElement();
114 writer.writeStartElement(namespaceURI, localName);
115 }
116
117 @Override
118 public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
119 onStartElement();
120 writer.writeStartElement(prefix, localName, namespaceURI);
121 }
122
123 @Override
124 public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
125 onEmptyElement();
126 writer.writeEmptyElement(namespaceURI, localName);
127 }
128
129 @Override
130 public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
131 onEmptyElement();
132 writer.writeEmptyElement(prefix, localName, namespaceURI);
133 }
134
135 @Override
136 public void writeEmptyElement(String localName) throws XMLStreamException {
137 onEmptyElement();
138 writer.writeEmptyElement(localName);
139 }
140
141 @Override
142 public void writeEndElement() throws XMLStreamException {
143 onEndElement();
144 writer.writeEndElement();
145 }
146
147 @Override
148 public void writeEndDocument() throws XMLStreamException {
149 writer.writeEndDocument();
150 }
151
152 @Override
153 public void close() throws XMLStreamException {
154 writer.close();
155 }
156
157 @Override
158 public void flush() throws XMLStreamException {
159 writer.flush();
160 }
161
162 @Override
163 public void writeAttribute(String localName, String value) throws XMLStreamException {
164 writer.writeAttribute(localName, value);
165 }
166
167 @Override
168 public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException {
169 writer.writeAttribute(prefix, namespaceURI, localName, value);
170 }
171
172 @Override
173 public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException {
174 writer.writeAttribute(namespaceURI, localName, value);
175 }
176
177 @Override
178 public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException {
179 writer.writeNamespace(prefix, namespaceURI);
180 }
181
182 @Override
183 public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException {
184 writer.writeDefaultNamespace(namespaceURI);
185 }
186
187 @Override
188 public void writeComment(String data) throws XMLStreamException {
189 writer.writeComment(data);
190 }
191
192 @Override
193 public void writeProcessingInstruction(String target) throws XMLStreamException {
194 writer.writeProcessingInstruction(target);
195 }
196
197 @Override
198 public void writeProcessingInstruction(String target, String data) throws XMLStreamException {
199 writer.writeProcessingInstruction(target, data);
200 }
201
202 @Override
203 public void writeCharacters(String text) throws XMLStreamException {
204 state = State.SEEN_DATA;
205 writer.writeCharacters(text);
206 }
207
208 @Override
209 public void writeCharacters(char[] text, int start, int len) throws XMLStreamException {
210 state = State.SEEN_DATA;
211 writer.writeCharacters(text, start, len);
212 }
213
214 @Override
215 public String getPrefix(String uri) throws XMLStreamException {
216 return null;
217 }
218
219 @Override
220 public void setPrefix(String prefix, String uri) throws XMLStreamException {
221
222 }
223
224 @Override
225 public void setDefaultNamespace(String uri) throws XMLStreamException {
226
227 }
228
229 @Override
230 public void setNamespaceContext(NamespaceContext context) throws XMLStreamException {
231
232 }
233
234 @Override
235 public NamespaceContext getNamespaceContext() {
236 return null;
237 }
238
239 @Override
240 public Object getProperty(String name) throws IllegalArgumentException {
241 return null;
242 }
243
244 @Override
245 public void writeCData(String data) throws XMLStreamException {
246 state = State.SEEN_DATA;
247 writer.writeCData(data);
248 }
249
250 @Override
251 public void writeDTD(String dtd) throws XMLStreamException {
252
253 }
254
255 @Override
256 public void writeEntityRef(String name) throws XMLStreamException {
257
258 }
259 }