1 package org.jahia.utils.xml; 2 3 56 57 import java.io.IOException ; 58 import java.io.Writer ; 59 import java.util.Stack ; 60 61 70 public class XmlWriter { 71 72 private Writer writer; private Stack stack; private StringBuffer attrs; private boolean empty; private boolean closed; 78 private String namespace; 80 private boolean pretty; private boolean wroteText; private String indent; private String newline; 85 88 public XmlWriter(Writer writer) { 89 this.writer = writer; 90 this.closed = true; 91 this.stack = new Stack (); 92 this.pretty = true; 93 this.wroteText = false; 94 this.newline = "\n"; 95 this.indent = " "; 96 } 97 98 105 public void enablePrettyPrint(boolean enable) { 106 this.pretty = enable; 107 } 108 109 117 public void setIndent(String indent) { 118 this.indent = indent; 119 } 120 121 131 public void setNewline(String newline) { 132 this.newline = newline; 133 } 134 135 141 public void setDefaultNamespace(String namespace) { 142 this.namespace = namespace; 143 } 144 145 151 public XmlWriter writeEntityWithText(String name, String text) throws IOException { 152 writeEntity(name); 153 writeText(text); 154 return endEntity(); 155 } 156 157 162 public XmlWriter writeEmptyEntity(String name) throws IOException { 163 writeEntity(name); 164 return endEntity(); 165 } 166 167 173 public XmlWriter writeEntity(String name) throws IOException { 174 if(this.namespace == null) { 175 return openEntity(name); 176 } else { 177 return openEntity(this.namespace+":"+name); 178 } 179 } 180 181 186 private XmlWriter openEntity(String name) throws IOException { 187 boolean wasClosed = this.closed; 188 closeOpeningTag(); 189 this.closed = false; 190 if (this.pretty) { 191 if (! wasClosed || this.wroteText) { 197 this.writer.write(newline); 198 } 199 for (int i = 0; i < this.stack.size(); i++) { 200 this.writer.write(indent); } 202 } 203 this.writer.write("<"); 204 this.writer.write(name); 205 stack.add(name); 206 this.empty = true; 207 this.wroteText = false; 208 return this; 209 } 210 211 private void closeOpeningTag() throws IOException { 213 if (!this.closed) { 214 writeAttributes(); 215 this.closed = true; 216 this.writer.write(">"); 217 } 218 } 219 220 private void writeAttributes() throws IOException { 222 if (this.attrs != null) { 223 this.writer.write(this.attrs.toString()); 224 this.attrs.setLength(0); 225 this.empty = false; 226 } 227 } 228 229 238 public XmlWriter writeAttribute(String attr, String value) throws IOException { 239 240 if (false) throw new IOException (); 242 243 if (this.attrs == null) { 244 this.attrs = new StringBuffer (); 245 } 246 this.attrs.append(" "); 247 this.attrs.append(attr); 248 this.attrs.append("=\""); 249 this.attrs.append(XmlUtils.escapeXml(value)); 250 this.attrs.append("\""); 251 return this; 252 } 253 254 259 public XmlWriter endEntity() throws IOException { 260 if(this.stack.empty()) { 261 throw new IOException ("Called endEntity too many times. "); 262 } 263 String name = (String )this.stack.pop(); 264 if (name != null) { 265 if (this.empty) { 266 writeAttributes(); 267 this.writer.write("/>"); 268 } else { 269 if (this.pretty && ! this.wroteText) { 270 for (int i = 0; i < this.stack.size(); i++) { 271 this.writer.write(indent); } 273 } 274 this.writer.write("</"); 275 this.writer.write(name); 276 this.writer.write(">"); 277 } 278 if (this.pretty) 279 this.writer.write(newline); this.empty = false; 281 this.closed = true; 282 this.wroteText = false; 283 } 284 return this; 285 } 286 287 292 public void close() throws IOException { 293 if(!this.stack.empty()) { 294 throw new IOException ("Tags are not all closed. "+ 295 "Possibly, "+this.stack.pop()+" is unclosed. "); 296 } 297 } 298 299 302 public XmlWriter writeText(String text) throws IOException { 303 closeOpeningTag(); 304 this.empty = false; 305 this.wroteText = true; 306 this.writer.write(XmlUtils.escapeXml(text)); 307 return this; 308 } 309 310 316 public XmlWriter writeCData(String cdata) throws IOException { 317 writeChunk("<![CDATA[ "+cdata+" ]]>"); 318 return this; 319 } 320 321 327 public XmlWriter writeComment(String comment) throws IOException { 328 writeChunk("<!-- "+comment+" -->"); 329 return this; 330 } 331 private void writeChunk(String data) throws IOException { 332 closeOpeningTag(); 333 this.empty = false; 334 if (this.pretty && ! this.wroteText) { 335 for (int i = 0; i < this.stack.size(); i++) { 336 this.writer.write(indent); 337 } 338 } 339 this.writer.write(data); 340 if (this.pretty) { 341 this.writer.write(newline); 342 } 343 } 344 345 } 346 | Popular Tags |