1 31 32 package org.opencms.util; 33 34 import org.opencms.i18n.CmsEncoder; 35 import org.opencms.main.OpenCms; 36 37 import java.io.IOException ; 38 import java.io.StringWriter ; 39 import java.io.Writer ; 40 41 import org.xml.sax.Attributes ; 42 import org.xml.sax.SAXException ; 43 import org.xml.sax.ext.LexicalHandler ; 44 import org.xml.sax.helpers.DefaultHandler ; 45 46 62 public class CmsXmlSaxWriter extends DefaultHandler implements LexicalHandler { 63 64 65 private static final String INDENT_STR = "\t"; 66 67 68 private String m_encoding; 69 70 75 private boolean m_escapeUnknownChars; 76 77 78 private boolean m_escapeXml; 79 80 81 private int m_indentLevel; 82 83 84 private boolean m_isCdata; 85 86 87 private String m_lastElementName; 88 89 90 private boolean m_openCdata; 91 92 93 private boolean m_openElement; 94 95 96 private Writer m_writer; 97 98 102 public CmsXmlSaxWriter() { 103 104 this(new StringWriter (), OpenCms.getSystemInfo().getDefaultEncoding()); 105 } 106 107 113 public CmsXmlSaxWriter(String encoding) { 114 115 this(new StringWriter (), encoding); 116 } 117 118 124 public CmsXmlSaxWriter(Writer writer) { 125 126 this(writer, OpenCms.getSystemInfo().getDefaultEncoding()); 127 } 128 129 136 public CmsXmlSaxWriter(Writer writer, String encoding) { 137 138 m_writer = writer; 139 m_encoding = encoding; 140 m_indentLevel = 0; 141 m_escapeXml = true; 142 m_escapeUnknownChars = false; 143 } 144 145 148 public void characters(char[] buf, int offset, int len) throws SAXException { 149 150 if (len == 0) { 151 return; 152 } 153 if (m_openElement) { 154 write(">"); 155 m_openElement = false; 156 } 157 if (m_openCdata) { 158 write("<![CDATA["); 159 m_openCdata = false; 160 } 161 if (m_escapeXml && !m_isCdata) { 162 String escaped = new String (buf, offset, len); 164 escaped = CmsEncoder.escapeXml(escaped, true); 166 if (m_escapeUnknownChars) { 167 escaped = CmsEncoder.adjustHtmlEncoding(escaped, getEncoding()); 169 } 170 write(escaped); 171 } else { 172 write(new String (buf, offset, len)); 174 } 175 } 176 177 180 public void comment(char[] ch, int start, int length) { 181 182 } 184 185 188 public void endCDATA() throws SAXException { 189 190 if (!m_openCdata) { 191 write("]]>"); 192 } 193 m_openCdata = false; 194 m_isCdata = false; 195 } 196 197 200 public void endDocument() throws SAXException { 201 202 try { 203 if (m_openElement) { 204 write("/>"); 205 m_openElement = false; 206 } 207 writeNewLine(); 208 m_writer.flush(); 209 } catch (IOException e) { 210 throw new SAXException (Messages.get().getBundle().key(Messages.ERR_IOERROR_0), e); 211 } 212 } 213 214 217 public void endDTD() { 218 219 } 221 222 225 public void endElement(String namespaceURI, String localName, String qualifiedName) throws SAXException { 226 227 String elementName = resolveName(localName, qualifiedName); 228 if (m_openElement) { 229 write("/>"); 230 } else { 231 if (!elementName.equals(m_lastElementName)) { 232 writeNewLine(); 233 } 234 write("</"); 235 write(elementName); 236 write(">"); 237 } 238 m_openElement = false; 239 m_indentLevel--; 240 } 241 242 245 public void endEntity(String name) { 246 247 } 249 250 255 public String getEncoding() { 256 257 return m_encoding; 258 } 259 260 265 public Writer getWriter() { 266 267 return m_writer; 268 } 269 270 278 public boolean isEscapeUnknownChars() { 279 280 return m_escapeUnknownChars; 281 } 282 283 288 public boolean isEscapeXml() { 289 290 return m_escapeXml; 291 } 292 293 298 public void setEncoding(String value) { 299 300 m_encoding = value; 301 } 302 303 310 public void setEscapeUnknownChars(boolean value) { 311 312 m_escapeUnknownChars = value; 313 } 314 315 321 public void setEscapeXml(boolean value) { 322 323 m_escapeXml = value; 324 } 325 326 329 public void startCDATA() { 330 331 m_openCdata = true; 332 m_isCdata = true; 333 } 334 335 338 public void startDocument() throws SAXException { 339 340 write("<?xml version=\"1.0\" encoding=\""); 341 write(m_encoding); 342 write("\"?>"); 343 writeNewLine(); 344 } 345 346 349 public void startDTD(String name, String publicId, String systemId) { 350 351 } 353 354 357 public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes attributes) 358 throws SAXException { 359 360 if (m_openElement) { 361 write(">"); 362 m_openElement = false; 363 } 364 m_indentLevel++; 366 writeNewLine(); 367 m_lastElementName = resolveName(localName, qualifiedName); 369 write("<"); 370 write(m_lastElementName); 371 if (attributes != null) { 372 for (int i = 0; i < attributes.getLength(); i++) { 373 write(" "); 374 write(resolveName(attributes.getLocalName(i), attributes.getQName(i))); 375 write("=\""); 376 write(attributes.getValue(i)); 377 write("\""); 378 } 379 } 380 m_openElement = true; 381 } 382 383 386 public void startEntity(String name) { 387 388 } 390 391 400 private String resolveName(String localName, String qualifiedName) { 401 402 if ((localName == null) || (localName.length() == 0)) { 403 return qualifiedName; 404 } else { 405 return localName; 406 } 407 } 408 409 415 private void write(String s) throws SAXException { 416 417 try { 418 m_writer.write(s); 419 } catch (IOException e) { 420 throw new SAXException (Messages.get().getBundle().key(Messages.ERR_IOERROR_0), e); 421 } 422 } 423 424 429 private void writeNewLine() throws SAXException { 430 431 try { 432 m_writer.write("\r\n"); 434 for (int i = 1; i < m_indentLevel; i++) { 436 m_writer.write(INDENT_STR); 437 } 438 m_writer.flush(); 440 } catch (IOException e) { 441 throw new SAXException (Messages.get().getBundle().key(Messages.ERR_IOERROR_0), e); 442 } 443 } 444 } | Popular Tags |