1 16 19 package com.sun.org.apache.xml.internal.serializer; 20 21 import java.io.IOException ; 22 23 import com.sun.org.apache.xml.internal.res.XMLErrorResources; 24 import com.sun.org.apache.xml.internal.res.XMLMessages; 25 import org.xml.sax.Attributes ; 26 import org.xml.sax.SAXException ; 27 28 31 public class ToTextStream extends ToStream 32 { 33 34 35 38 public ToTextStream() 39 { 40 super(); 41 } 42 43 44 45 57 protected void startDocumentInternal() throws org.xml.sax.SAXException 58 { 59 super.startDocumentInternal(); 60 61 m_needToCallStartDocument = false; 62 63 } 65 66 80 public void endDocument() throws org.xml.sax.SAXException 81 { 82 flushPending(); 83 flushWriter(); 84 if (m_tracer != null) 85 super.fireEndDoc(); 86 } 87 88 120 public void startElement( 121 String namespaceURI, String localName, String name, Attributes atts) 122 throws org.xml.sax.SAXException 123 { 124 if (m_tracer != null) { 126 super.fireStartElem(name); 127 this.firePseudoAttributes(); 128 } 129 return; 130 } 131 132 158 public void endElement(String namespaceURI, String localName, String name) 159 throws org.xml.sax.SAXException 160 { 161 if (m_tracer != null) 162 super.fireEndElem(name); 163 } 164 165 190 public void characters(char ch[], int start, int length) 191 throws org.xml.sax.SAXException 192 { 193 194 flushPending(); 196 197 try 198 { 199 writeNormalizedChars(ch, start, length, false, m_lineSepUse); 200 if (m_tracer != null) 201 super.fireCharEvent(ch, start, length); 202 } 203 catch(IOException ioe) 204 { 205 throw new SAXException (ioe); 206 } 207 } 208 209 220 public void charactersRaw(char ch[], int start, int length) 221 throws org.xml.sax.SAXException 222 { 223 224 try 225 { 226 writeNormalizedChars(ch, start, length, false, m_lineSepUse); 227 } 228 catch(IOException ioe) 229 { 230 throw new SAXException (ioe); 231 } 232 } 233 234 249 void writeNormalizedChars( 250 final char ch[], 251 final int start, 252 final int length, 253 final boolean isCData, 254 final boolean useLineSep) 255 throws IOException , org.xml.sax.SAXException 256 { 257 final java.io.Writer writer = m_writer; 258 final int end = start + length; 259 260 261 final char S_LINEFEED = CharInfo.S_LINEFEED; 262 final int M_MAXCHARACTER = this.m_maxCharacter; 263 264 if (isCData) 265 { 266 for (int i = start; i < end; i++) 270 { 271 final char c = ch[i]; 272 273 if (S_LINEFEED == c && useLineSep) 274 { 275 writer.write(m_lineSep, 0, m_lineSepLen); 276 } 277 else if (c > M_MAXCHARACTER) 278 { 279 if (i != 0) 280 closeCDATA(); 281 282 if (isUTF16Surrogate(c)) 284 { 285 writeUTF16Surrogate(c, ch, i, end); 286 i++; } 288 else 289 { 290 writer.write(c); 291 } 292 293 if ((i != 0) && (i < (end - 1))) 294 { 295 writer.write(CDATA_DELIMITER_OPEN); 296 m_cdataTagOpen = true; 297 } 298 } 299 else if ( 300 ((i < (end - 2)) 301 && (']' == c) 302 && (']' == ch[i + 1]) 303 && ('>' == ch[i + 2]))) 304 { 305 writer.write(CDATA_CONTINUE); 306 i += 2; 307 } 308 else 309 { 310 if (c <= M_MAXCHARACTER) 311 { 312 writer.write(c); 313 } 314 315 else if (isUTF16Surrogate(c)) 316 { 317 writeUTF16Surrogate(c, ch, i, end); 318 i++; } 320 else 321 { 322 326 String encoding = getEncoding(); 327 if (encoding != null) 328 { 329 332 String integralValue = Integer.toString(c); 333 throw new SAXException (XMLMessages.createXMLMessage( 334 XMLErrorResources.ER_ILLEGAL_CHARACTER, 335 new Object []{ integralValue, encoding})); 336 } 337 else 338 { 339 342 writer.write(c); 343 } 344 } 345 } 346 } 347 } 348 else 349 { 350 for (int i = start; i < end; i++) 352 { 353 final char c = ch[i]; 354 355 if (S_LINEFEED == c && useLineSep) 356 { 357 writer.write(m_lineSep, 0, m_lineSepLen); 358 } 359 else if (c <= M_MAXCHARACTER) 360 { 361 writer.write(c); 362 } 363 else if (isUTF16Surrogate(c)) 364 { 365 writeUTF16Surrogate(c, ch, i, end); 366 i++; } 368 else 369 { 370 374 String encoding = getEncoding(); 375 if (encoding != null) 376 { 377 380 String integralValue = Integer.toString(c); 381 throw new SAXException (XMLMessages.createXMLMessage( 382 XMLErrorResources.ER_ILLEGAL_CHARACTER, 383 new Object []{ integralValue, encoding})); 384 } 385 else 386 { 387 390 writer.write(c); 391 } 392 } 393 } 394 } 395 } 396 397 422 public void cdata(char ch[], int start, int length) 423 throws org.xml.sax.SAXException 424 { 425 try 426 { 427 writeNormalizedChars(ch, start, length, false, m_lineSepUse); 428 if (m_tracer != null) 429 super.fireCDATAEvent(ch, start, length); 430 } 431 catch(IOException ioe) 432 { 433 throw new SAXException (ioe); 434 } 435 } 436 437 463 public void ignorableWhitespace(char ch[], int start, int length) 464 throws org.xml.sax.SAXException 465 { 466 467 try 468 { 469 writeNormalizedChars(ch, start, length, false, m_lineSepUse); 470 } 471 catch(IOException ioe) 472 { 473 throw new SAXException (ioe); 474 } 475 } 476 477 496 public void processingInstruction(String target, String data) 497 throws org.xml.sax.SAXException 498 { 499 flushPending(); 501 502 if (m_tracer != null) 503 super.fireEscapingEvent(target, data); 504 } 505 506 515 public void comment(String data) throws org.xml.sax.SAXException 516 { 517 final int length = data.length(); 518 if (length > m_charsBuff.length) 519 { 520 m_charsBuff = new char[length*2 + 1]; 521 } 522 data.getChars(0, length, m_charsBuff, 0); 523 comment(m_charsBuff, 0, length); 524 } 525 526 538 public void comment(char ch[], int start, int length) 539 throws org.xml.sax.SAXException 540 { 541 542 flushPending(); 543 if (m_tracer != null) 544 super.fireCommentEvent(ch, start, length); 545 } 546 547 554 public void entityReference(String name) throws org.xml.sax.SAXException 555 { 556 if (m_tracer != null) 557 super.fireEntityReference(name); 558 } 559 560 563 public void addAttribute( 564 String uri, 565 String localName, 566 String rawName, 567 String type, 568 String value) 569 { 570 } 572 573 576 public void endCDATA() throws SAXException 577 { 578 } 580 581 584 public void endElement(String elemName) throws SAXException 585 { 586 if (m_tracer != null) 587 super.fireEndElem(elemName); 588 } 589 590 593 public void startElement( 594 String elementNamespaceURI, 595 String elementLocalName, 596 String elementName) 597 throws SAXException 598 { 599 if (m_needToCallStartDocument) 600 startDocumentInternal(); 601 if (m_tracer != null) { 603 super.fireStartElem(elementName); 604 this.firePseudoAttributes(); 605 } 606 607 return; 608 } 609 610 611 614 public void characters(String characters) 615 throws SAXException 616 { 617 final int length = characters.length(); 618 if (length > m_charsBuff.length) 619 { 620 m_charsBuff = new char[length*2 + 1]; 621 } 622 characters.getChars(0, length, m_charsBuff, 0); 623 characters(m_charsBuff, 0, length); 624 } 625 626 627 630 public void addAttribute(String name, String value) 631 { 632 } 634 635 638 public void addUniqueAttribute(String qName, String value, int flags) 639 throws SAXException 640 { 641 } 643 644 public boolean startPrefixMapping( 645 String prefix, 646 String uri, 647 boolean shouldFlush) 648 throws SAXException 649 { 650 return false; 652 } 653 654 655 public void startPrefixMapping(String prefix, String uri) 656 throws org.xml.sax.SAXException 657 { 658 } 660 661 662 public void namespaceAfterStartElement( 663 final String prefix, 664 final String uri) 665 throws SAXException 666 { 667 } 669 670 public void flushPending() throws org.xml.sax.SAXException 671 { 672 if (m_needToCallStartDocument) 673 { 674 startDocumentInternal(); 675 m_needToCallStartDocument = false; 676 } 677 } 678 } 679 | Popular Tags |