1 16 17 18 19 32 33 package org.apache.xml.serialize; 34 35 36 import java.io.IOException ; 37 import java.io.OutputStream ; 38 import java.io.Writer ; 39 40 import org.apache.xerces.dom.DOMMessageFormatter; 41 import org.apache.xerces.impl.Constants; 42 import org.apache.xerces.util.NamespaceSupport; 43 import org.apache.xerces.util.SymbolTable; 44 import org.apache.xerces.util.XML11Char; 45 import org.apache.xerces.util.XMLChar; 46 import org.xml.sax.SAXException ; 47 import org.w3c.dom.DOMError ; 48 49 81 public class XML11Serializer 82 extends XMLSerializer { 83 84 88 protected static final boolean DEBUG = false; 89 90 94 98 99 protected NamespaceSupport fNSBinder; 100 101 102 protected NamespaceSupport fLocalNSBinder; 103 104 105 protected SymbolTable fSymbolTable; 106 107 protected boolean fDOML1 = false; 109 protected int fNamespaceCounter = 1; 111 protected final static String PREFIX = "NS"; 112 113 120 protected boolean fNamespaces = false; 121 122 123 private boolean fPreserveSpace; 124 125 126 131 public XML11Serializer() { 132 super( ); 133 _format.setVersion("1.1"); 134 } 135 136 137 142 public XML11Serializer( OutputFormat format ) { 143 super( format ); 144 _format.setVersion("1.1"); 145 } 146 147 148 156 public XML11Serializer( Writer writer, OutputFormat format ) { 157 super( writer, format ); 158 _format.setVersion("1.1"); 159 } 160 161 162 170 public XML11Serializer( OutputStream output, OutputFormat format ) { 171 super( output, format != null ? format : new OutputFormat( Method.XML, null, false ) ); 172 _format.setVersion("1.1"); 173 } 174 175 179 180 public void characters( char[] chars, int start, int length ) 181 throws SAXException 182 { 183 ElementState state; 184 185 try { 186 state = content(); 187 188 192 if ( state.inCData || state.doCData ) { 193 int saveIndent; 194 195 if ( ! state.inCData ) { 199 _printer.printText( "<![CDATA[" ); 200 state.inCData = true; 201 } 202 saveIndent = _printer.getNextIndent(); 203 _printer.setNextIndent( 0 ); 204 char ch; 205 final int end = start + length; 206 for ( int index = start; index < end; ++index ) { 207 ch = chars[index]; 208 if ( ch == ']' && index + 2 < end && 209 chars[ index + 1 ] == ']' && chars[ index + 2 ] == '>' ) { 210 _printer.printText("]]]]><![CDATA[>"); 211 index +=2; 212 continue; 213 } 214 if (!XML11Char.isXML11Valid(ch)) { 215 if (++index < end) { 217 surrogates(ch, chars[index]); 218 } 219 else { 220 fatalError("The character '"+(char)ch+"' is an invalid XML character"); 221 } 222 continue; 223 } else { 224 if ( _encodingInfo.isPrintable((char)ch) && XML11Char.isXML11ValidLiteral(ch)) { 225 _printer.printText((char)ch); 226 } else { 227 _printer.printText("]]>&#x"); 229 _printer.printText(Integer.toHexString(ch)); 230 _printer.printText(";<![CDATA["); 231 } 232 } 233 } 234 _printer.setNextIndent( saveIndent ); 235 236 } else { 237 238 int saveIndent; 239 240 if ( state.preserveSpace ) { 241 saveIndent = _printer.getNextIndent(); 246 _printer.setNextIndent( 0 ); 247 printText( chars, start, length, true, state.unescaped ); 248 _printer.setNextIndent( saveIndent ); 249 } else { 250 printText( chars, start, length, false, state.unescaped ); 251 } 252 } 253 } catch ( IOException except ) { 254 throw new SAXException ( except ); 255 } 256 } 257 258 259 protected void printEscaped( String source ) throws IOException { 263 int length = source.length(); 264 for ( int i = 0 ; i < length ; ++i ) { 265 int ch = source.charAt(i); 266 if (!XML11Char.isXML11Valid(ch)) { 267 if (++i <length) { 268 surrogates(ch, source.charAt(i)); 269 } else { 270 fatalError("The character '"+(char)ch+"' is an invalid XML character"); 271 } 272 continue; 273 } 274 if (ch == '\n' || ch == '\r' || ch == '\t' || ch == 0x0085 || ch == 0x2028){ 275 printHex(ch); 276 } else if (ch == '<') { 277 _printer.printText("<"); 278 } else if (ch == '&') { 279 _printer.printText("&"); 280 } else if (ch == '"') { 281 _printer.printText("""); 282 } else if ((ch >= ' ' && _encodingInfo.isPrintable((char) ch))) { 283 _printer.printText((char) ch); 284 } else { 285 printHex(ch); 286 } 287 } 288 } 289 290 protected final void printCDATAText(String text) throws IOException { 291 int length = text.length(); 292 char ch; 293 294 for (int index = 0; index < length; ++index) { 295 ch = text.charAt(index); 296 297 if (ch == ']' 298 && index + 2 < length 299 && text.charAt(index + 1) == ']' 300 && text.charAt(index + 2) == '>') { if (fDOMErrorHandler != null){ 302 if ((features & DOMSerializerImpl.SPLITCDATA) == 0 305 && (features & DOMSerializerImpl.WELLFORMED) == 0) { 306 String msg = 308 DOMMessageFormatter.formatMessage( 309 DOMMessageFormatter.SERIALIZER_DOMAIN, 310 "EndingCDATA", 311 null); 312 modifyDOMError( 313 msg, 314 DOMError.SEVERITY_FATAL_ERROR, 315 null, fCurrentNode); 316 boolean continueProcess = 317 fDOMErrorHandler.handleError(fDOMError); 318 if (!continueProcess) { 319 throw new IOException (); 320 } 321 } else { 322 String msg = 324 DOMMessageFormatter.formatMessage( 325 DOMMessageFormatter.SERIALIZER_DOMAIN, 326 "SplittingCDATA", 327 null); 328 modifyDOMError( 329 msg, 330 DOMError.SEVERITY_WARNING, 331 null, fCurrentNode); 332 fDOMErrorHandler.handleError(fDOMError); 333 } 334 } 335 _printer.printText("]]]]><![CDATA[>"); 337 index += 2; 338 continue; 339 } 340 341 if (!XML11Char.isXML11Valid(ch)) { 342 if (++index < length) { 344 surrogates(ch, text.charAt(index)); 345 } else { 346 fatalError( 347 "The character '" 348 + (char) ch 349 + "' is an invalid XML character"); 350 } 351 continue; 352 } else { 353 if (_encodingInfo.isPrintable((char) ch) 354 && XML11Char.isXML11ValidLiteral(ch)) { 355 _printer.printText((char) ch); 356 } else { 357 358 _printer.printText("]]>&#x"); 360 _printer.printText(Integer.toHexString(ch)); 361 _printer.printText(";<![CDATA["); 362 } 363 } 364 } 365 } 366 367 368 protected final void printXMLChar( int ch ) throws IOException { 371 372 if (ch == '\r' || ch == 0x0085 || ch == 0x2028) { 373 printHex(ch); 374 } else if ( ch == '<') { 375 _printer.printText("<"); 376 } else if (ch == '&') { 377 _printer.printText("&"); 378 } else if (ch == '>'){ 379 _printer.printText(">"); 382 } else if ( _encodingInfo.isPrintable((char)ch) && XML11Char.isXML11ValidLiteral(ch)) { 383 _printer.printText((char)ch); 384 } else { 385 printHex(ch); 386 } 387 } 388 389 390 391 protected final void surrogates(int high, int low) throws IOException { 392 if (XMLChar.isHighSurrogate(high)) { 393 if (!XMLChar.isLowSurrogate(low)) { 394 fatalError("The character '"+(char)low+"' is an invalid XML character"); 396 } 397 else { 398 int supplemental = XMLChar.supplemental((char)high, (char)low); 399 if (!XML11Char.isXML11Valid(supplemental)) { 400 fatalError("The character '"+(char)supplemental+"' is an invalid XML character"); 402 } 403 else { 404 if (content().inCData ) { 405 _printer.printText("]]>&#x"); 406 _printer.printText(Integer.toHexString(supplemental)); 407 _printer.printText(";<![CDATA["); 408 } 409 else { 410 printHex(supplemental); 411 } 412 } 413 } 414 } else { 415 fatalError("The character '"+(char)high+"' is an invalid XML character"); 416 } 417 418 } 419 420 421 protected void printText( String text, boolean preserveSpace, boolean unescaped ) 422 throws IOException { 423 int index; 424 char ch; 425 int length = text.length(); 426 if ( preserveSpace ) { 427 for ( index = 0 ; index < length ; ++index ) { 432 ch = text.charAt( index ); 433 if (!XML11Char.isXML11Valid(ch)) { 434 if (++index <length) { 436 surrogates(ch, text.charAt(index)); 437 } else { 438 fatalError("The character '"+(char)ch+"' is an invalid XML character"); 439 } 440 continue; 441 } 442 if ( unescaped && XML11Char.isXML11ValidLiteral(ch)) { 443 _printer.printText( ch ); 444 } else 445 printXMLChar( ch ); 446 } 447 } else { 448 for ( index = 0 ; index < length ; ++index ) { 454 ch = text.charAt( index ); 455 if (!XML11Char.isXML11Valid(ch)) { 456 if (++index <length) { 458 surrogates(ch, text.charAt(index)); 459 } else { 460 fatalError("The character '"+(char)ch+"' is an invalid XML character"); 461 } 462 continue; 463 } 464 465 if ( unescaped && XML11Char.isXML11ValidLiteral(ch) ) 466 _printer.printText( ch ); 467 else 468 printXMLChar( ch); 469 } 470 } 471 } 472 473 474 475 protected void printText( char[] chars, int start, int length, 476 boolean preserveSpace, boolean unescaped ) throws IOException { 477 int index; 478 char ch; 479 480 if ( preserveSpace ) { 481 while ( length-- > 0 ) { 486 ch = chars[start++]; 487 if (!XML11Char.isXML11Valid(ch)) { 488 if ( length-- > 0) { 490 surrogates(ch, chars[start++]); 491 } else { 492 fatalError("The character '"+(char)ch+"' is an invalid XML character"); 493 } 494 continue; 495 } 496 if ( unescaped && XML11Char.isXML11ValidLiteral(ch)) 497 _printer.printText( ch ); 498 else 499 printXMLChar( ch ); 500 } 501 } else { 502 while ( length-- > 0 ) { 508 ch = chars[start++]; 509 if (!XML11Char.isXML11Valid(ch)) { 510 if ( length-- > 0) { 512 surrogates(ch, chars[start++]); 513 } else { 514 fatalError("The character '"+(char)ch+"' is an invalid XML character"); 515 } 516 continue; 517 } 518 519 if ( unescaped && XML11Char.isXML11ValidLiteral(ch)) 520 _printer.printText( ch ); 521 else 522 printXMLChar( ch ); 523 } 524 } 525 } 526 527 528 public boolean reset() { 529 super.reset(); 530 return true; 531 532 } 533 534 } 535 536 537 538 539 | Popular Tags |