1 package ca.uhn.fhir.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import javax.xml.namespace.NamespaceContext;
24 import javax.xml.stream.XMLStreamException;
25 import javax.xml.stream.XMLStreamWriter;
26
27 public class NonPrettyPrintWriterWrapper implements XMLStreamWriter {
28
29 private static final String PRE = "pre";
30 private XMLStreamWriter myTarget;
31 private int myInsidePre = 0;
32
33 public NonPrettyPrintWriterWrapper(XMLStreamWriter target) {
34 myTarget = target;
35 }
36
37 @Override
38 public void flush() throws XMLStreamException {
39 myTarget.flush();
40 }
41
42 @Override
43 public void close() throws XMLStreamException {
44 myTarget.close();
45 }
46
47 @Override
48 public String getPrefix(String theUri) throws XMLStreamException {
49 return myTarget.getPrefix(theUri);
50 }
51
52 @Override
53 public void setPrefix(String thePrefix, String theUri) throws XMLStreamException {
54 myTarget.setPrefix(thePrefix, theUri);
55 }
56
57 @Override
58 public void setDefaultNamespace(String theUri) throws XMLStreamException {
59 myTarget.setDefaultNamespace(theUri);
60 }
61
62 @Override
63 public void setNamespaceContext(NamespaceContext theContext) throws XMLStreamException {
64 myTarget.setNamespaceContext(theContext);
65 }
66
67 @Override
68 public NamespaceContext getNamespaceContext() {
69 return myTarget.getNamespaceContext();
70 }
71
72 @Override
73 public void writeStartElement(String theLocalName) throws XMLStreamException {
74 if (PRE.equals(theLocalName) || myInsidePre > 0) {
75 myInsidePre++;
76 }
77 myTarget.writeStartElement(theLocalName);
78 }
79
80 @Override
81 public void writeStartElement(String theNamespaceURI, String theLocalName) throws XMLStreamException {
82 if (PRE.equals(theLocalName) || myInsidePre > 0) {
83 myInsidePre++;
84 }
85 myTarget.writeStartElement(theNamespaceURI, theLocalName);
86 }
87
88 @Override
89 public void writeStartElement(String thePrefix, String theLocalName, String theNamespaceURI) throws XMLStreamException {
90 if (PRE.equals(theLocalName) || myInsidePre > 0) {
91 myInsidePre++;
92 }
93 myTarget.writeStartElement(thePrefix, theLocalName, theNamespaceURI);
94 }
95
96 @Override
97 public void writeEmptyElement(String theNamespaceURI, String theLocalName) throws XMLStreamException {
98 myTarget.writeEmptyElement(theNamespaceURI, theLocalName);
99 }
100
101 @Override
102 public void writeEmptyElement(String thePrefix, String theLocalName, String theNamespaceURI) throws XMLStreamException {
103 myTarget.writeEmptyElement(thePrefix, theLocalName, theNamespaceURI);
104 }
105
106 @Override
107 public void writeEmptyElement(String theLocalName) throws XMLStreamException {
108 myTarget.writeEmptyElement(theLocalName);
109 }
110
111 @Override
112 public void writeEndElement() throws XMLStreamException {
113 if (myInsidePre > 0) {
114 myInsidePre--;
115 }
116 myTarget.writeEndElement();
117 }
118
119 @Override
120 public void writeEndDocument() throws XMLStreamException {
121 myTarget.writeEndDocument();
122 }
123
124 @Override
125 public void writeAttribute(String theLocalName, String theValue) throws XMLStreamException {
126 myTarget.writeAttribute(theLocalName, theValue);
127 }
128
129 @Override
130 public void writeAttribute(String thePrefix, String theNamespaceURI, String theLocalName, String theValue) throws XMLStreamException {
131 myTarget.writeAttribute(thePrefix, theNamespaceURI, theLocalName, theValue);
132 }
133
134 @Override
135 public void writeAttribute(String theNamespaceURI, String theLocalName, String theValue) throws XMLStreamException {
136 myTarget.writeAttribute(theNamespaceURI, theLocalName, theValue);
137 }
138
139 @Override
140 public void writeNamespace(String thePrefix, String theNamespaceURI) throws XMLStreamException {
141 myTarget.writeNamespace(thePrefix, theNamespaceURI);
142 }
143
144 @Override
145 public void writeDefaultNamespace(String theNamespaceURI) throws XMLStreamException {
146 myTarget.writeDefaultNamespace(theNamespaceURI);
147 }
148
149 @Override
150 public void writeComment(String theData) throws XMLStreamException {
151 myTarget.writeComment(theData);
152 }
153
154 @Override
155 public void writeProcessingInstruction(String theTarget) throws XMLStreamException {
156 myTarget.writeProcessingInstruction(theTarget);
157 }
158
159 @Override
160 public void writeProcessingInstruction(String theTarget, String theData) throws XMLStreamException {
161 myTarget.writeProcessingInstruction(theTarget, theData);
162 }
163
164 @Override
165 public void writeCData(String theData) throws XMLStreamException {
166 myTarget.writeCData(theData);
167 }
168
169 @Override
170 public void writeDTD(String theDtd) throws XMLStreamException {
171 myTarget.writeDTD(theDtd);
172 }
173
174 @Override
175 public void writeEntityRef(String theName) throws XMLStreamException {
176 myTarget.writeEntityRef(theName);
177 }
178
179 @Override
180 public void writeStartDocument() throws XMLStreamException {
181 myTarget.writeStartDocument();
182 }
183
184 @Override
185 public void writeStartDocument(String theVersion) throws XMLStreamException {
186 myTarget.writeStartDocument(theVersion);
187 }
188
189 @Override
190 public void writeStartDocument(String theEncoding, String theVersion) throws XMLStreamException {
191 myTarget.writeStartDocument(theEncoding, theVersion);
192 }
193
194 @Override
195 public void writeCharacters(String theText) throws XMLStreamException {
196 if (myInsidePre > 0) {
197 myTarget.writeCharacters(theText);
198 } else {
199 writeCharacters(theText.toCharArray(), 0, theText.length());
200 }
201 }
202
203 @Override
204 public void writeCharacters(char[] theText, int theStart, int theLen) throws XMLStreamException {
205 writeCharacters(theText, theStart, theLen, myTarget, myInsidePre);
206 }
207
208 static void writeCharacters(char[] theText, int theStart, int theLen, XMLStreamWriter target, int insidePre) throws XMLStreamException {
209 if (theLen == 0) {
210 return;
211 } else {
212 if (insidePre > 0) {
213 target.writeCharacters(theText, theStart, theLen);
214 } else {
215 int initialEnd = theStart + (theLen - 1);
216 int start = theStart;
217 int end = initialEnd;
218 while (Character.isWhitespace(theText[start]) && start < end) {
219 start++;
220 }
221 while (Character.isWhitespace(theText[end]) && end > start) {
222 end--;
223 }
224 if (start == end) {
225 if (Character.isWhitespace(theText[start])) {
226 target.writeCharacters(" ");
227 return;
228 }
229 }
230 if (start > theStart) {
231 target.writeCharacters(" ");
232 }
233 target.writeCharacters(theText, start, (end - start) + 1);
234 if (end < initialEnd) {
235 target.writeCharacters(" ");
236 }
237
238 }
239 }
240 }
241
242 @Override
243 public Object getProperty(String theName) throws IllegalArgumentException {
244 return myTarget.getProperty(theName);
245 }
246
247 }