001/*
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.util;
021
022import javax.xml.namespace.NamespaceContext;
023import javax.xml.stream.XMLStreamException;
024import javax.xml.stream.XMLStreamWriter;
025
026public class NonPrettyPrintWriterWrapper implements XMLStreamWriter {
027
028        private static final String PRE = "pre";
029        private XMLStreamWriter myTarget;
030        private int myInsidePre = 0;
031
032        public NonPrettyPrintWriterWrapper(XMLStreamWriter target) {
033                myTarget = target;
034        }
035
036        @Override
037        public void flush() throws XMLStreamException {
038                myTarget.flush();
039        }
040
041        @Override
042        public void close() throws XMLStreamException {
043                myTarget.close();
044        }
045
046        @Override
047        @CoverageIgnore
048        public String getPrefix(String theUri) throws XMLStreamException {
049                return myTarget.getPrefix(theUri);
050        }
051
052        @Override
053        @CoverageIgnore
054        public void setPrefix(String thePrefix, String theUri) throws XMLStreamException {
055                myTarget.setPrefix(thePrefix, theUri);
056        }
057
058        @Override
059        @CoverageIgnore
060        public void setDefaultNamespace(String theUri) throws XMLStreamException {
061                myTarget.setDefaultNamespace(theUri);
062        }
063
064        @Override
065        @CoverageIgnore
066        public void setNamespaceContext(NamespaceContext theContext) throws XMLStreamException {
067                myTarget.setNamespaceContext(theContext);
068        }
069
070        @Override
071        @CoverageIgnore
072        public NamespaceContext getNamespaceContext() {
073                return myTarget.getNamespaceContext();
074        }
075
076        @Override
077        public void writeStartElement(String theLocalName) throws XMLStreamException {
078                if (PRE.equals(theLocalName) || myInsidePre > 0) {
079                        myInsidePre++;
080                }
081                myTarget.writeStartElement(theLocalName);
082        }
083
084        @Override
085        public void writeStartElement(String theNamespaceURI, String theLocalName) throws XMLStreamException {
086                if (PRE.equals(theLocalName) || myInsidePre > 0) {
087                        myInsidePre++;
088                }
089                myTarget.writeStartElement(theNamespaceURI, theLocalName);
090        }
091
092        @Override
093        public void writeStartElement(String thePrefix, String theLocalName, String theNamespaceURI) throws XMLStreamException {
094                if (PRE.equals(theLocalName) || myInsidePre > 0) {
095                        myInsidePre++;
096                }
097                myTarget.writeStartElement(thePrefix, theLocalName, theNamespaceURI);
098        }
099
100        @Override
101        @CoverageIgnore
102        public void writeEmptyElement(String theNamespaceURI, String theLocalName) throws XMLStreamException {
103                myTarget.writeEmptyElement(theNamespaceURI, theLocalName);
104        }
105
106        @Override
107        @CoverageIgnore
108        public void writeEmptyElement(String thePrefix, String theLocalName, String theNamespaceURI) throws XMLStreamException {
109                myTarget.writeEmptyElement(thePrefix, theLocalName, theNamespaceURI);
110        }
111
112        @Override
113        @CoverageIgnore
114        public void writeEmptyElement(String theLocalName) throws XMLStreamException {
115                myTarget.writeEmptyElement(theLocalName);
116        }
117
118        @Override
119        public void writeEndElement() throws XMLStreamException {
120                if (myInsidePre > 0) {
121                        myInsidePre--;
122                }
123                myTarget.writeEndElement();
124        }
125
126        @Override
127        public void writeEndDocument() throws XMLStreamException {
128                myTarget.writeEndDocument();
129        }
130
131        @Override
132        public void writeAttribute(String theLocalName, String theValue) throws XMLStreamException {
133                myTarget.writeAttribute(theLocalName, theValue);
134        }
135
136        @Override
137        @CoverageIgnore
138        public void writeAttribute(String thePrefix, String theNamespaceURI, String theLocalName, String theValue) throws XMLStreamException {
139                myTarget.writeAttribute(thePrefix, theNamespaceURI, theLocalName, theValue);
140        }
141
142        @Override
143        @CoverageIgnore
144        public void writeAttribute(String theNamespaceURI, String theLocalName, String theValue) throws XMLStreamException {
145                myTarget.writeAttribute(theNamespaceURI, theLocalName, theValue);
146        }
147
148        @Override
149        public void writeNamespace(String thePrefix, String theNamespaceURI) throws XMLStreamException {
150                myTarget.writeNamespace(thePrefix, theNamespaceURI);
151        }
152
153        @Override
154        public void writeDefaultNamespace(String theNamespaceURI) throws XMLStreamException {
155                myTarget.writeDefaultNamespace(theNamespaceURI);
156        }
157
158        @Override
159        public void writeComment(String theData) throws XMLStreamException {
160                myTarget.writeComment(theData);
161        }
162
163        @Override
164        @CoverageIgnore
165        public void writeProcessingInstruction(String theTarget) throws XMLStreamException {
166                myTarget.writeProcessingInstruction(theTarget);
167        }
168
169        @Override
170        @CoverageIgnore
171        public void writeProcessingInstruction(String theTarget, String theData) throws XMLStreamException {
172                myTarget.writeProcessingInstruction(theTarget, theData);
173        }
174
175        @Override
176        @CoverageIgnore
177        public void writeCData(String theData) throws XMLStreamException {
178                myTarget.writeCData(theData);
179        }
180
181        @Override
182        @CoverageIgnore
183        public void writeDTD(String theDtd) throws XMLStreamException {
184                myTarget.writeDTD(theDtd);
185        }
186
187        @Override
188        @CoverageIgnore
189        public void writeEntityRef(String theName) throws XMLStreamException {
190                myTarget.writeEntityRef(theName);
191        }
192
193        @Override
194        @CoverageIgnore
195        public void writeStartDocument() throws XMLStreamException {
196                myTarget.writeStartDocument();
197        }
198
199        @Override
200        @CoverageIgnore
201        public void writeStartDocument(String theVersion) throws XMLStreamException {
202                myTarget.writeStartDocument(theVersion);
203        }
204
205        @Override
206        public void writeStartDocument(String theEncoding, String theVersion) throws XMLStreamException {
207                myTarget.writeStartDocument(theEncoding, theVersion);
208        }
209
210        @Override
211        public void writeCharacters(String theText) throws XMLStreamException {
212                if (myInsidePre > 0) {
213                        myTarget.writeCharacters(theText);
214                } else {
215                        writeCharacters(theText.toCharArray(), 0, theText.length());
216                }
217        }
218
219        @Override
220        public void writeCharacters(char[] theText, int theStart, int theLen) throws XMLStreamException {
221                writeCharacters(theText, theStart, theLen, myTarget, myInsidePre);
222        }
223
224        static void writeCharacters(char[] theText, int theStart, int theLen, XMLStreamWriter target, int insidePre) throws XMLStreamException {
225                if (theLen > 0) {
226                        if (insidePre > 0) {
227                                target.writeCharacters(theText, theStart, theLen);
228                        } else {
229                                int initialEnd = theStart + (theLen - 1);
230                                int start = theStart;
231                                int end = initialEnd;
232                                while (Character.isWhitespace(theText[start]) && start < end) {
233                                        start++;
234                                }
235                                while (Character.isWhitespace(theText[end]) && end > start) {
236                                        end--;
237                                }
238                                if (start == end) {
239                                        if (Character.isWhitespace(theText[start])) {
240                                                target.writeCharacters(" ");
241                                                return;
242                                        }
243                                }
244                                if (start > theStart) {
245                                        target.writeCharacters(" ");
246                                }
247                                target.writeCharacters(theText, start, (end - start) + 1);
248                                if (end < initialEnd) {
249                                        target.writeCharacters(" ");
250                                }
251
252                        }
253                }
254        }
255
256        @Override
257        @CoverageIgnore
258        public Object getProperty(String theName) throws IllegalArgumentException {
259                return myTarget.getProperty(theName);
260        }
261
262}