1 57 58 59 60 73 74 package com.sun.org.apache.xml.internal.serialize; 75 76 77 import java.io.IOException ; 78 import java.io.OutputStream ; 79 import java.io.Writer ; 80 81 import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter; 82 import org.w3c.dom.DOMError ; 83 import com.sun.org.apache.xerces.internal.impl.Constants; 84 import com.sun.org.apache.xerces.internal.util.NamespaceSupport; 85 import com.sun.org.apache.xerces.internal.util.SymbolTable; 86 import com.sun.org.apache.xerces.internal.util.XML11Char; 87 import com.sun.org.apache.xerces.internal.util.XMLChar; 88 import org.xml.sax.SAXException ; 89 90 122 public class XML11Serializer 123 extends XMLSerializer { 124 125 129 protected static final boolean DEBUG = false; 130 131 135 139 140 protected NamespaceSupport fNSBinder; 141 142 143 protected NamespaceSupport fLocalNSBinder; 144 145 146 protected SymbolTable fSymbolTable; 147 148 protected boolean fDOML1 = false; 150 protected int fNamespaceCounter = 1; 152 protected final static String PREFIX = "NS"; 153 154 161 protected boolean fNamespaces = false; 162 163 164 private boolean fPreserveSpace; 165 166 167 172 public XML11Serializer() { 173 super( ); 174 _format.setVersion("1.1"); 175 } 176 177 178 183 public XML11Serializer( OutputFormat format ) { 184 super( format ); 185 _format.setVersion("1.1"); 186 } 187 188 189 197 public XML11Serializer( Writer writer, OutputFormat format ) { 198 super( writer, format ); 199 _format.setVersion("1.1"); 200 } 201 202 203 211 public XML11Serializer( OutputStream output, OutputFormat format ) { 212 super( output, format != null ? format : new OutputFormat( Method.XML, null, false ) ); 213 _format.setVersion("1.1"); 214 } 215 216 220 221 public void characters( char[] chars, int start, int length ) 222 throws SAXException 223 { 224 ElementState state; 225 226 try { 227 state = content(); 228 229 233 if ( state.inCData || state.doCData ) { 234 int saveIndent; 235 236 if ( ! state.inCData ) { 240 _printer.printText( "<![CDATA[" ); 241 state.inCData = true; 242 } 243 saveIndent = _printer.getNextIndent(); 244 _printer.setNextIndent( 0 ); 245 char ch; 246 for ( int index = start ; index < length ; ++index ) { 247 ch = chars[index]; 248 if ( ch == ']' && index + 2 < length && 249 chars[ index + 1 ] == ']' && chars[ index + 2 ] == '>' ) { 250 _printer.printText("]]]]><![CDATA[>"); 251 index +=2; 252 continue; 253 } 254 if (!XML11Char.isXML11Valid(ch)) { 255 if (++index <length) { 257 surrogates(ch, chars[index]); 258 } 259 else { 260 fatalError("The character '"+(char)ch+"' is an invalid XML character"); 261 } 262 continue; 263 } else { 264 if ( _encodingInfo.isPrintable((char)ch) && XML11Char.isXML11ValidLiteral(ch)) { 265 _printer.printText((char)ch); 266 } else { 267 _printer.printText("]]>&#x"); 269 _printer.printText(Integer.toHexString(ch)); 270 _printer.printText(";<![CDATA["); 271 } 272 } 273 } 274 _printer.setNextIndent( saveIndent ); 275 276 } else { 277 278 int saveIndent; 279 280 if ( state.preserveSpace ) { 281 saveIndent = _printer.getNextIndent(); 286 _printer.setNextIndent( 0 ); 287 printText( chars, start, length, true, state.unescaped ); 288 _printer.setNextIndent( saveIndent ); 289 } else { 290 printText( chars, start, length, false, state.unescaped ); 291 } 292 } 293 } catch ( IOException except ) { 294 throw new SAXException ( except ); 295 } 296 } 297 298 299 protected void printEscaped( String source ) throws IOException { 303 int length = source.length(); 304 for ( int i = 0 ; i < length ; ++i ) { 305 int ch = source.charAt(i); 306 if (!XML11Char.isXML11Valid(ch)) { 307 if (++i <length) { 308 surrogates(ch, source.charAt(i)); 309 } else { 310 fatalError("The character '"+(char)ch+"' is an invalid XML character"); 311 } 312 continue; 313 } 314 if (ch == '\n' || ch == '\r' || ch == '\t' || ch == 0x0085 || ch == 0x2028){ 315 printHex(ch); 316 } else if (ch == '<') { 317 _printer.printText("<"); 318 } else if (ch == '&') { 319 _printer.printText("&"); 320 } else if (ch == '"') { 321 _printer.printText("""); 322 } else if ((ch >= ' ' && _encodingInfo.isPrintable((char) ch))) { 323 _printer.printText((char) ch); 324 } else { 325 printHex(ch); 326 } 327 } 328 } 329 330 protected final void printCDATAText(String text) throws IOException { 331 int length = text.length(); 332 char ch; 333 334 for (int index = 0; index < length; ++index) { 335 ch = text.charAt(index); 336 337 if (ch == ']' 338 && index + 2 < length 339 && text.charAt(index + 1) == ']' 340 && text.charAt(index + 2) == '>') { if (fDOMErrorHandler != null){ 342 if ((features & DOMSerializerImpl.SPLITCDATA) == 0 345 && (features & DOMSerializerImpl.WELLFORMED) == 0) { 346 String msg = 348 DOMMessageFormatter.formatMessage( 349 DOMMessageFormatter.SERIALIZER_DOMAIN, 350 "EndingCDATA", 351 null); 352 modifyDOMError( 353 msg, 354 DOMError.SEVERITY_FATAL_ERROR, 355 fCurrentNode); 356 boolean continueProcess = 357 fDOMErrorHandler.handleError(fDOMError); 358 if (!continueProcess) { 359 throw new IOException (); 360 } 361 } else { 362 String msg = 364 DOMMessageFormatter.formatMessage( 365 DOMMessageFormatter.SERIALIZER_DOMAIN, 366 "SplittingCDATA", 367 null); 368 modifyDOMError( 369 msg, 370 DOMError.SEVERITY_WARNING, 371 fCurrentNode); 372 fDOMErrorHandler.handleError(fDOMError); 373 } 374 } 375 _printer.printText("]]]]><![CDATA[>"); 377 index += 2; 378 continue; 379 } 380 381 if (!XML11Char.isXML11Valid(ch)) { 382 if (++index < length) { 384 surrogates(ch, text.charAt(index)); 385 } else { 386 fatalError( 387 "The character '" 388 + (char) ch 389 + "' is an invalid XML character"); 390 } 391 continue; 392 } else { 393 if (_encodingInfo.isPrintable((char) ch) 394 && XML11Char.isXML11ValidLiteral(ch)) { 395 _printer.printText((char) ch); 396 } else { 397 398 _printer.printText("]]>&#x"); 400 _printer.printText(Integer.toHexString(ch)); 401 _printer.printText(";<![CDATA["); 402 } 403 } 404 } 405 } 406 407 408 protected final void printXMLChar( int ch ) throws IOException { 411 412 if (ch == '\r' || ch == 0x0085 || ch == 0x2028) { 413 printHex(ch); 414 } else if ( ch == '<') { 415 _printer.printText("<"); 416 } else if (ch == '&') { 417 _printer.printText("&"); 418 } else if (ch == '>'){ 419 _printer.printText(">"); 422 } else if ( _encodingInfo.isPrintable((char)ch) && XML11Char.isXML11ValidLiteral(ch)) { 423 _printer.printText((char)ch); 424 } else { 425 printHex(ch); 426 } 427 } 428 429 430 431 protected final void surrogates(int high, int low) throws IOException { 432 if (XMLChar.isHighSurrogate(high)) { 433 if (!XMLChar.isLowSurrogate(low)) { 434 fatalError("The character '"+(char)low+"' is an invalid XML character"); 436 } 437 else { 438 int supplemental = XMLChar.supplemental((char)high, (char)low); 439 if (!XML11Char.isXML11Valid(supplemental)) { 440 fatalError("The character '"+(char)supplemental+"' is an invalid XML character"); 442 } 443 else { 444 if (content().inCData ) { 445 _printer.printText("]]>&#x"); 446 _printer.printText(Integer.toHexString(supplemental)); 447 _printer.printText(";<![CDATA["); 448 } 449 else { 450 printHex(supplemental); 451 } 452 } 453 } 454 } else { 455 fatalError("The character '"+(char)high+"' is an invalid XML character"); 456 } 457 458 } 459 460 461 protected void printText( String text, boolean preserveSpace, boolean unescaped ) 462 throws IOException { 463 int index; 464 char ch; 465 int length = text.length(); 466 if ( preserveSpace ) { 467 for ( index = 0 ; index < length ; ++index ) { 472 ch = text.charAt( index ); 473 if (!XML11Char.isXML11Valid(ch)) { 474 if (++index <length) { 476 surrogates(ch, text.charAt(index)); 477 } else { 478 fatalError("The character '"+(char)ch+"' is an invalid XML character"); 479 } 480 continue; 481 } 482 if ( unescaped && XML11Char.isXML11ValidLiteral(ch)) { 483 _printer.printText( ch ); 484 } else 485 printXMLChar( ch ); 486 } 487 } else { 488 for ( index = 0 ; index < length ; ++index ) { 494 ch = text.charAt( index ); 495 if (!XML11Char.isXML11Valid(ch)) { 496 if (++index <length) { 498 surrogates(ch, text.charAt(index)); 499 } else { 500 fatalError("The character '"+(char)ch+"' is an invalid XML character"); 501 } 502 continue; 503 } 504 505 if ( unescaped && XML11Char.isXML11ValidLiteral(ch) ) 506 _printer.printText( ch ); 507 else 508 printXMLChar( ch); 509 } 510 } 511 } 512 513 514 515 protected void printText( char[] chars, int start, int length, 516 boolean preserveSpace, boolean unescaped ) throws IOException { 517 int index; 518 char ch; 519 520 if ( preserveSpace ) { 521 while ( length-- > 0 ) { 526 ch = chars[ start ]; 527 ++start; 528 if (!XML11Char.isXML11Valid(ch)) { 529 if (++start <length) { 531 surrogates(ch, chars[start]); 532 } else { 533 fatalError("The character '"+(char)ch+"' is an invalid XML character"); 534 } 535 continue; 536 } 537 if ( unescaped && XML11Char.isXML11ValidLiteral(ch)) 538 _printer.printText( ch ); 539 else 540 printXMLChar( ch ); 541 } 542 } else { 543 while ( length-- > 0 ) { 549 ch = chars[ start ]; 550 ++start; 551 552 if (!XML11Char.isXML11Valid(ch)) { 553 if (++start <length) { 555 surrogates(ch, chars[start]); 556 } else { 557 fatalError("The character '"+(char)ch+"' is an invalid XML character"); 558 } 559 continue; 560 } 561 562 if ( unescaped && XML11Char.isXML11ValidLiteral(ch)) 563 _printer.printText( ch ); 564 else 565 printXMLChar( ch ); 566 } 567 } 568 } 569 570 571 public boolean reset() { 572 super.reset(); 573 return true; 574 575 } 576 577 } 578 579 580 581 582 | Popular Tags |