1 16 19 package com.sun.org.apache.xml.internal.serializer; 20 21 import java.io.IOException ; 22 23 import javax.xml.transform.ErrorListener ; 24 import javax.xml.transform.Result ; 25 import javax.xml.transform.Transformer ; 26 import javax.xml.transform.TransformerException ; 27 28 import com.sun.org.apache.xml.internal.res.XMLErrorResources; 29 import com.sun.org.apache.xml.internal.res.XMLMessages; 30 import org.xml.sax.SAXException ; 31 32 36 public class ToXMLStream extends ToStream 37 { 38 39 42 boolean m_cdataTagOpen = false; 43 44 45 49 protected static CharInfo m_xmlcharInfo = 50 CharInfo.getCharInfo(CharInfo.XML_ENTITIES_RESOURCE, Method.XML); 52 53 56 public ToXMLStream() 57 { 58 m_charInfo = m_xmlcharInfo; 59 60 initCDATA(); 61 m_prefixMap = new NamespaceMappings(); 63 64 } 65 66 71 public void CopyFrom(ToXMLStream xmlListener) 72 { 73 74 m_writer = xmlListener.m_writer; 75 76 77 String encoding = xmlListener.getEncoding(); 79 setEncoding(encoding); 80 81 setOmitXMLDeclaration(xmlListener.getOmitXMLDeclaration()); 82 83 m_ispreserve = xmlListener.m_ispreserve; 84 m_preserves = xmlListener.m_preserves; 85 m_isprevtext = xmlListener.m_isprevtext; 86 m_doIndent = xmlListener.m_doIndent; 87 setIndentAmount(xmlListener.getIndentAmount()); 88 m_startNewLine = xmlListener.m_startNewLine; 89 m_needToOutputDocTypeDecl = xmlListener.m_needToOutputDocTypeDecl; 90 setDoctypeSystem(xmlListener.getDoctypeSystem()); 91 setDoctypePublic(xmlListener.getDoctypePublic()); 92 setStandalone(xmlListener.getStandalone()); 93 setMediaType(xmlListener.getMediaType()); 94 m_maxCharacter = xmlListener.m_maxCharacter; 95 m_spaceBeforeClose = xmlListener.m_spaceBeforeClose; 96 m_cdataStartCalled = xmlListener.m_cdataStartCalled; 97 98 } 99 100 108 public void startDocumentInternal() throws org.xml.sax.SAXException 109 { 110 111 if (m_needToCallStartDocument) 112 { 113 super.startDocumentInternal(); 114 m_needToCallStartDocument = false; 115 116 if (m_inEntityRef) 117 return; 118 119 m_needToOutputDocTypeDecl = true; 120 m_startNewLine = false; 121 122 if (getOmitXMLDeclaration() == false) 123 { 124 String encoding = Encodings.getMimeEncoding(getEncoding()); 125 String version = getVersion(); 126 if (version == null) 127 version = "1.0"; 128 String standalone; 129 130 if (m_standaloneWasSpecified) 131 { 132 standalone = " standalone=\"" + getStandalone() + "\""; 133 } 134 else 135 { 136 standalone = ""; 137 } 138 139 try 140 { 141 final java.io.Writer writer = m_writer; 142 writer.write("<?xml version=\""); 143 writer.write(version); 144 writer.write("\" encoding=\""); 145 writer.write(encoding); 146 writer.write('\"'); 147 writer.write(standalone); 148 writer.write("?>"); 149 if (m_doIndent) 150 writer.write(m_lineSep, 0, m_lineSepLen); 151 } 152 catch(IOException e) 153 { 154 throw new SAXException (e); 155 } 156 157 } 158 } 159 } 160 161 169 public void endDocument() throws org.xml.sax.SAXException 170 { 171 flushPending(); 172 if (m_doIndent && !m_isprevtext) 173 { 174 try 175 { 176 outputLineSep(); 177 } 178 catch(IOException e) 179 { 180 throw new SAXException (e); 181 } 182 } 183 184 flushWriter(); 185 186 if (m_tracer != null) 187 super.fireEndDoc(); 188 } 189 190 202 public void startPreserving() throws org.xml.sax.SAXException 203 { 204 205 m_preserves.push(true); 207 208 m_ispreserve = true; 209 } 210 211 218 public void endPreserving() throws org.xml.sax.SAXException 219 { 220 221 m_ispreserve = m_preserves.isEmpty() ? false : m_preserves.pop(); 223 } 224 225 236 public void processingInstruction(String target, String data) 237 throws org.xml.sax.SAXException 238 { 239 if (m_inEntityRef) 240 return; 241 242 flushPending(); 243 244 if (target.equals(Result.PI_DISABLE_OUTPUT_ESCAPING)) 245 { 246 startNonEscaping(); 247 } 248 else if (target.equals(Result.PI_ENABLE_OUTPUT_ESCAPING)) 249 { 250 endNonEscaping(); 251 } 252 else 253 { 254 try 255 { 256 if (m_elemContext.m_startTagOpen) 257 { 258 closeStartTag(); 259 m_elemContext.m_startTagOpen = false; 260 } 261 262 if (shouldIndent()) 263 indent(); 264 265 final java.io.Writer writer = m_writer; 266 writer.write("<?"); 267 writer.write(target); 268 269 if (data.length() > 0 270 && !Character.isSpaceChar(data.charAt(0))) 271 writer.write(' '); 272 273 int indexOfQLT = data.indexOf("?>"); 274 275 if (indexOfQLT >= 0) 276 { 277 278 if (indexOfQLT > 0) 280 { 281 writer.write(data.substring(0, indexOfQLT)); 282 } 283 284 writer.write("? >"); 286 if ((indexOfQLT + 2) < data.length()) 287 { 288 writer.write(data.substring(indexOfQLT + 2)); 289 } 290 } 291 else 292 { 293 writer.write(data); 294 } 295 296 writer.write('?'); 297 writer.write('>'); 298 299 if (m_elemContext.m_currentElemDepth <= 0) 303 writer.write(m_lineSep, 0, m_lineSepLen); 304 305 m_startNewLine = true; 306 } 307 catch(IOException e) 308 { 309 throw new SAXException (e); 310 } 311 } 312 313 if (m_tracer != null) 314 super.fireEscapingEvent(target, data); 315 } 316 317 324 public void entityReference(String name) throws org.xml.sax.SAXException 325 { 326 if (m_elemContext.m_startTagOpen) 327 { 328 closeStartTag(); 329 m_elemContext.m_startTagOpen = false; 330 } 331 332 try 333 { 334 if (shouldIndent()) 335 indent(); 336 337 final java.io.Writer writer = m_writer; 338 writer.write('&'); 339 writer.write(name); 340 writer.write(';'); 341 } 342 catch(IOException e) 343 { 344 throw new SAXException (e); 345 } 346 347 if (m_tracer != null) 348 super.fireEntityReference(name); 349 } 350 351 361 public void addUniqueAttribute(String name, String value, int flags) 362 throws SAXException 363 { 364 if (m_elemContext.m_startTagOpen) 365 { 366 367 try 368 { 369 final String patchedName = patchName(name); 370 final java.io.Writer writer = m_writer; 371 if ((flags & NO_BAD_CHARS) > 0 && m_xmlcharInfo.onlyQuotAmpLtGt) 372 { 373 379 writer.write(' '); 380 writer.write(patchedName); 381 writer.write("=\""); 382 writer.write(value); 383 writer.write('"'); 384 } 385 else 386 { 387 writer.write(' '); 388 writer.write(patchedName); 389 writer.write("=\""); 390 writeAttrString(writer, value, this.getEncoding()); 391 writer.write('"'); 392 } 393 } catch (IOException e) { 394 throw new SAXException (e); 395 } 396 } 397 } 398 399 public void addAttribute( 400 String uri, 401 String localName, 402 String rawName, 403 String type, 404 String value) 405 throws SAXException 406 { 407 if (m_elemContext.m_startTagOpen) 408 { 409 if (!rawName.startsWith("xmlns")) 410 { 411 String prefixUsed = 412 ensureAttributesNamespaceIsDeclared( 413 uri, 414 localName, 415 rawName); 416 if (prefixUsed != null 417 && rawName != null 418 && !rawName.startsWith(prefixUsed)) 419 { 420 rawName = prefixUsed + ":" + localName; 423 424 } 425 } 426 addAttributeAlways(uri, localName, rawName, type, value); 427 } 428 else 429 { 430 441 443 String msg = XMLMessages.createXMLMessage( 445 XMLErrorResources.ER_ILLEGAL_ATTRIBUTE_POSITION,new Object []{ localName }); 446 447 try { 448 Transformer tran = super.getTransformer(); 450 ErrorListener errHandler = tran.getErrorListener(); 451 452 453 if (null != errHandler && m_sourceLocator != null) 455 errHandler.warning(new TransformerException (msg, m_sourceLocator)); 456 else 457 System.out.println(msg); 458 } 459 catch (Exception e){} 460 } 461 } 462 463 466 public void endElement(String elemName) throws SAXException 467 { 468 endElement(null, null, elemName); 469 } 470 471 475 public void namespaceAfterStartElement( 476 final String prefix, 477 final String uri) 478 throws SAXException 479 { 480 481 if (m_elemContext.m_elementURI == null) 483 { 484 String prefix1 = getPrefixPart(m_elemContext.m_elementName); 485 if (prefix1 == null && EMPTYSTRING.equals(prefix)) 486 { 487 m_elemContext.m_elementURI = uri; 492 } 493 } 494 startPrefixMapping(prefix,uri,false); 495 return; 496 497 } 498 499 504 protected boolean pushNamespace(String prefix, String uri) 505 { 506 try 507 { 508 if (m_prefixMap.pushNamespace( 509 prefix, uri, m_elemContext.m_currentElemDepth)) 510 { 511 startPrefixMapping(prefix, uri); 512 return true; 513 } 514 } 515 catch (SAXException e) 516 { 517 } 519 return false; 520 } 521 528 public boolean reset() 529 { 530 boolean wasReset = false; 531 if (super.reset()) 532 { 533 resetToXMLStream(); 534 wasReset = true; 535 } 536 return wasReset; 537 } 538 539 543 private void resetToXMLStream() 544 { 545 this.m_cdataTagOpen = false; 546 547 } 548 549 } 550 | Popular Tags |