View Javadoc
1   package ca.uhn.fhir.util;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
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  	@CoverageIgnore
49  	public String getPrefix(String theUri) throws XMLStreamException {
50  		return myTarget.getPrefix(theUri);
51  	}
52  
53  	@Override
54  	@CoverageIgnore
55  	public void setPrefix(String thePrefix, String theUri) throws XMLStreamException {
56  		myTarget.setPrefix(thePrefix, theUri);
57  	}
58  
59  	@Override
60  	@CoverageIgnore
61  	public void setDefaultNamespace(String theUri) throws XMLStreamException {
62  		myTarget.setDefaultNamespace(theUri);
63  	}
64  
65  	@Override
66  	@CoverageIgnore
67  	public void setNamespaceContext(NamespaceContext theContext) throws XMLStreamException {
68  		myTarget.setNamespaceContext(theContext);
69  	}
70  
71  	@Override
72  	@CoverageIgnore
73  	public NamespaceContext getNamespaceContext() {
74  		return myTarget.getNamespaceContext();
75  	}
76  
77  	@Override
78  	public void writeStartElement(String theLocalName) throws XMLStreamException {
79  		if (PRE.equals(theLocalName) || myInsidePre > 0) {
80  			myInsidePre++;
81  		}
82  		myTarget.writeStartElement(theLocalName);
83  	}
84  
85  	@Override
86  	public void writeStartElement(String theNamespaceURI, String theLocalName) throws XMLStreamException {
87  		if (PRE.equals(theLocalName) || myInsidePre > 0) {
88  			myInsidePre++;
89  		}
90  		myTarget.writeStartElement(theNamespaceURI, theLocalName);
91  	}
92  
93  	@Override
94  	public void writeStartElement(String thePrefix, String theLocalName, String theNamespaceURI) throws XMLStreamException {
95  		if (PRE.equals(theLocalName) || myInsidePre > 0) {
96  			myInsidePre++;
97  		}
98  		myTarget.writeStartElement(thePrefix, theLocalName, theNamespaceURI);
99  	}
100 
101 	@Override
102 	@CoverageIgnore
103 	public void writeEmptyElement(String theNamespaceURI, String theLocalName) throws XMLStreamException {
104 		myTarget.writeEmptyElement(theNamespaceURI, theLocalName);
105 	}
106 
107 	@Override
108 	@CoverageIgnore
109 	public void writeEmptyElement(String thePrefix, String theLocalName, String theNamespaceURI) throws XMLStreamException {
110 		myTarget.writeEmptyElement(thePrefix, theLocalName, theNamespaceURI);
111 	}
112 
113 	@Override
114 	@CoverageIgnore
115 	public void writeEmptyElement(String theLocalName) throws XMLStreamException {
116 		myTarget.writeEmptyElement(theLocalName);
117 	}
118 
119 	@Override
120 	public void writeEndElement() throws XMLStreamException {
121 		if (myInsidePre > 0) {
122 			myInsidePre--;
123 		}
124 		myTarget.writeEndElement();
125 	}
126 
127 	@Override
128 	public void writeEndDocument() throws XMLStreamException {
129 		myTarget.writeEndDocument();
130 	}
131 
132 	@Override
133 	public void writeAttribute(String theLocalName, String theValue) throws XMLStreamException {
134 		myTarget.writeAttribute(theLocalName, theValue);
135 	}
136 
137 	@Override
138 	@CoverageIgnore
139 	public void writeAttribute(String thePrefix, String theNamespaceURI, String theLocalName, String theValue) throws XMLStreamException {
140 		myTarget.writeAttribute(thePrefix, theNamespaceURI, theLocalName, theValue);
141 	}
142 
143 	@Override
144 	@CoverageIgnore
145 	public void writeAttribute(String theNamespaceURI, String theLocalName, String theValue) throws XMLStreamException {
146 		myTarget.writeAttribute(theNamespaceURI, theLocalName, theValue);
147 	}
148 
149 	@Override
150 	public void writeNamespace(String thePrefix, String theNamespaceURI) throws XMLStreamException {
151 		myTarget.writeNamespace(thePrefix, theNamespaceURI);
152 	}
153 
154 	@Override
155 	public void writeDefaultNamespace(String theNamespaceURI) throws XMLStreamException {
156 		myTarget.writeDefaultNamespace(theNamespaceURI);
157 	}
158 
159 	@Override
160 	public void writeComment(String theData) throws XMLStreamException {
161 		myTarget.writeComment(theData);
162 	}
163 
164 	@Override
165 	@CoverageIgnore
166 	public void writeProcessingInstruction(String theTarget) throws XMLStreamException {
167 		myTarget.writeProcessingInstruction(theTarget);
168 	}
169 
170 	@Override
171 	@CoverageIgnore
172 	public void writeProcessingInstruction(String theTarget, String theData) throws XMLStreamException {
173 		myTarget.writeProcessingInstruction(theTarget, theData);
174 	}
175 
176 	@Override
177 	@CoverageIgnore
178 	public void writeCData(String theData) throws XMLStreamException {
179 		myTarget.writeCData(theData);
180 	}
181 
182 	@Override
183 	@CoverageIgnore
184 	public void writeDTD(String theDtd) throws XMLStreamException {
185 		myTarget.writeDTD(theDtd);
186 	}
187 
188 	@Override
189 	@CoverageIgnore
190 	public void writeEntityRef(String theName) throws XMLStreamException {
191 		myTarget.writeEntityRef(theName);
192 	}
193 
194 	@Override
195 	@CoverageIgnore
196 	public void writeStartDocument() throws XMLStreamException {
197 		myTarget.writeStartDocument();
198 	}
199 
200 	@Override
201 	@CoverageIgnore
202 	public void writeStartDocument(String theVersion) throws XMLStreamException {
203 		myTarget.writeStartDocument(theVersion);
204 	}
205 
206 	@Override
207 	public void writeStartDocument(String theEncoding, String theVersion) throws XMLStreamException {
208 		myTarget.writeStartDocument(theEncoding, theVersion);
209 	}
210 
211 	@Override
212 	public void writeCharacters(String theText) throws XMLStreamException {
213 		if (myInsidePre > 0) {
214 			myTarget.writeCharacters(theText);
215 		} else {
216 			writeCharacters(theText.toCharArray(), 0, theText.length());
217 		}
218 	}
219 
220 	@Override
221 	public void writeCharacters(char[] theText, int theStart, int theLen) throws XMLStreamException {
222 		writeCharacters(theText, theStart, theLen, myTarget, myInsidePre);
223 	}
224 
225 	static void writeCharacters(char[] theText, int theStart, int theLen, XMLStreamWriter target, int insidePre) throws XMLStreamException {
226 		if (theLen > 0) {
227 			if (insidePre > 0) {
228 				target.writeCharacters(theText, theStart, theLen);
229 			} else {
230 				int initialEnd = theStart + (theLen - 1);
231 				int start = theStart;
232 				int end = initialEnd;
233 				while (Character.isWhitespace(theText[start]) && start < end) {
234 					start++;
235 				}
236 				while (Character.isWhitespace(theText[end]) && end > start) {
237 					end--;
238 				}
239 				if (start == end) {
240 					if (Character.isWhitespace(theText[start])) {
241 						target.writeCharacters(" ");
242 						return;
243 					}
244 				}
245 				if (start > theStart) {
246 					target.writeCharacters(" ");
247 				}
248 				target.writeCharacters(theText, start, (end - start) + 1);
249 				if (end < initialEnd) {
250 					target.writeCharacters(" ");
251 				}
252 
253 			}
254 		}
255 	}
256 
257 	@Override
258 	@CoverageIgnore
259 	public Object getProperty(String theName) throws IllegalArgumentException {
260 		return myTarget.getProperty(theName);
261 	}
262 
263 }