001package ca.uhn.fhir.util; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2016 University Health Network 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 public String getPrefix(String theUri) throws XMLStreamException { 049 return myTarget.getPrefix(theUri); 050 } 051 052 @Override 053 public void setPrefix(String thePrefix, String theUri) throws XMLStreamException { 054 myTarget.setPrefix(thePrefix, theUri); 055 } 056 057 @Override 058 public void setDefaultNamespace(String theUri) throws XMLStreamException { 059 myTarget.setDefaultNamespace(theUri); 060 } 061 062 @Override 063 public void setNamespaceContext(NamespaceContext theContext) throws XMLStreamException { 064 myTarget.setNamespaceContext(theContext); 065 } 066 067 @Override 068 public NamespaceContext getNamespaceContext() { 069 return myTarget.getNamespaceContext(); 070 } 071 072 @Override 073 public void writeStartElement(String theLocalName) throws XMLStreamException { 074 if (PRE.equals(theLocalName) || myInsidePre > 0) { 075 myInsidePre++; 076 } 077 myTarget.writeStartElement(theLocalName); 078 } 079 080 @Override 081 public void writeStartElement(String theNamespaceURI, String theLocalName) throws XMLStreamException { 082 if (PRE.equals(theLocalName) || myInsidePre > 0) { 083 myInsidePre++; 084 } 085 myTarget.writeStartElement(theNamespaceURI, theLocalName); 086 } 087 088 @Override 089 public void writeStartElement(String thePrefix, String theLocalName, String theNamespaceURI) throws XMLStreamException { 090 if (PRE.equals(theLocalName) || myInsidePre > 0) { 091 myInsidePre++; 092 } 093 myTarget.writeStartElement(thePrefix, theLocalName, theNamespaceURI); 094 } 095 096 @Override 097 public void writeEmptyElement(String theNamespaceURI, String theLocalName) throws XMLStreamException { 098 myTarget.writeEmptyElement(theNamespaceURI, theLocalName); 099 } 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}