001package ca.uhn.fhir.util;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import javax.xml.namespace.NamespaceContext;
024import javax.xml.stream.XMLStreamException;
025import javax.xml.stream.XMLStreamWriter;
026
027public class NonPrettyPrintWriterWrapper implements XMLStreamWriter {
028
029        private static final String PRE = "pre";
030        private XMLStreamWriter myTarget;
031        private int myInsidePre = 0;
032
033        public NonPrettyPrintWriterWrapper(XMLStreamWriter target) {
034                myTarget = target;
035        }
036
037        @Override
038        public void flush() throws XMLStreamException {
039                myTarget.flush();
040        }
041
042        @Override
043        public void close() throws XMLStreamException {
044                myTarget.close();
045        }
046
047        @Override
048        @CoverageIgnore
049        public String getPrefix(String theUri) throws XMLStreamException {
050                return myTarget.getPrefix(theUri);
051        }
052
053        @Override
054        @CoverageIgnore
055        public void setPrefix(String thePrefix, String theUri) throws XMLStreamException {
056                myTarget.setPrefix(thePrefix, theUri);
057        }
058
059        @Override
060        @CoverageIgnore
061        public void setDefaultNamespace(String theUri) throws XMLStreamException {
062                myTarget.setDefaultNamespace(theUri);
063        }
064
065        @Override
066        @CoverageIgnore
067        public void setNamespaceContext(NamespaceContext theContext) throws XMLStreamException {
068                myTarget.setNamespaceContext(theContext);
069        }
070
071        @Override
072        @CoverageIgnore
073        public NamespaceContext getNamespaceContext() {
074                return myTarget.getNamespaceContext();
075        }
076
077        @Override
078        public void writeStartElement(String theLocalName) throws XMLStreamException {
079                if (PRE.equals(theLocalName) || myInsidePre > 0) {
080                        myInsidePre++;
081                }
082                myTarget.writeStartElement(theLocalName);
083        }
084
085        @Override
086        public void writeStartElement(String theNamespaceURI, String theLocalName) throws XMLStreamException {
087                if (PRE.equals(theLocalName) || myInsidePre > 0) {
088                        myInsidePre++;
089                }
090                myTarget.writeStartElement(theNamespaceURI, theLocalName);
091        }
092
093        @Override
094        public void writeStartElement(String thePrefix, String theLocalName, String theNamespaceURI) throws XMLStreamException {
095                if (PRE.equals(theLocalName) || myInsidePre > 0) {
096                        myInsidePre++;
097                }
098                myTarget.writeStartElement(thePrefix, theLocalName, theNamespaceURI);
099        }
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}