1 57 58 59 64 65 package com.sun.org.apache.xml.internal.serialize; 66 67 68 import java.io.IOException ; 69 70 import org.w3c.dom.Element ; 71 import org.w3c.dom.Node ; 72 import org.xml.sax.AttributeList ; 73 import org.xml.sax.Attributes ; 74 import org.xml.sax.SAXException ; 75 76 77 100 public class TextSerializer 101 extends BaseMarkupSerializer 102 { 103 104 105 110 public TextSerializer() 111 { 112 super( new OutputFormat( Method.TEXT, null, false ) ); 113 } 114 115 116 public void setOutputFormat( OutputFormat format ) 117 { 118 super.setOutputFormat( format != null ? format : new OutputFormat( Method.TEXT, null, false ) ); 119 } 120 121 122 126 127 public void startElement( String namespaceURI, String localName, 128 String rawName, Attributes attrs ) 129 throws SAXException 130 { 131 startElement( rawName == null ? localName : rawName, null ); 132 } 133 134 135 public void endElement( String namespaceURI, String localName, 136 String rawName ) 137 throws SAXException 138 { 139 endElement( rawName == null ? localName : rawName ); 140 } 141 142 143 147 148 public void startElement( String tagName, AttributeList attrs ) 149 throws SAXException 150 { 151 boolean preserveSpace; 152 ElementState state; 153 154 try { 155 state = getElementState(); 156 if ( isDocumentState() ) { 157 if ( ! _started ) 162 startDocument( tagName ); 163 } 164 preserveSpace = state.preserveSpace; 167 168 171 174 state = enterElementState( null, null, tagName, preserveSpace ); 178 } catch ( IOException except ) { 179 throw new SAXException ( except ); 180 } 181 } 182 183 184 public void endElement( String tagName ) 185 throws SAXException 186 { 187 try { 188 endElementIO( tagName ); 189 } catch ( IOException except ) { 190 throw new SAXException ( except ); 191 } 192 } 193 194 195 public void endElementIO( String tagName ) 196 throws IOException 197 { 198 ElementState state; 199 200 state = getElementState(); 204 state = leaveElementState(); 207 state.afterElement = true; 208 state.empty = false; 209 if ( isDocumentState() ) 210 _printer.flush(); 211 } 212 213 214 public void processingInstructionIO( String target, String code ) throws IOException 215 { 216 } 217 218 219 public void comment( String text ) 220 { 221 } 222 223 224 public void comment( char[] chars, int start, int length ) 225 { 226 } 227 228 229 public void characters( char[] chars, int start, int length ) 230 throws SAXException 231 { 232 ElementState state; 233 234 try { 235 state = content(); 236 state.doCData = state.inCData = false; 237 printText( chars, start, length, true, true ); 238 } catch ( IOException except ) { 239 throw new SAXException ( except ); 240 } 241 } 242 243 244 protected void characters( String text, boolean unescaped ) 245 throws IOException 246 { 247 ElementState state; 248 249 state = content(); 250 state.doCData = state.inCData = false; 251 printText( text, true, true ); 252 } 253 254 255 259 260 269 protected void startDocument( String rootTagName ) 270 throws IOException 271 { 272 _printer.leaveDTD(); 275 276 _started = true; 277 serializePreRoot(); 279 } 280 281 282 287 protected void serializeElement( Element elem ) 288 throws IOException 289 { 290 Node child; 291 ElementState state; 292 boolean preserveSpace; 293 String tagName; 294 295 tagName = elem.getTagName(); 296 state = getElementState(); 297 if ( isDocumentState() ) { 298 if ( ! _started ) 303 startDocument( tagName ); 304 } 305 preserveSpace = state.preserveSpace; 308 309 312 315 if ( elem.hasChildNodes() ) { 318 state = enterElementState( null, null, tagName, preserveSpace ); 321 child = elem.getFirstChild(); 322 while ( child != null ) { 323 serializeNode( child ); 324 child = child.getNextSibling(); 325 } 326 endElementIO( tagName ); 327 } else { 328 if ( ! isDocumentState() ) { 329 state.afterElement = true; 331 state.empty = false; 332 } 333 } 334 } 335 336 337 342 protected void serializeNode( Node node ) 343 throws IOException 344 { 345 switch ( node.getNodeType() ) { 349 case Node.TEXT_NODE : { 350 String text; 351 352 text = node.getNodeValue(); 353 if ( text != null ) 354 characters( node.getNodeValue(), true ); 355 break; 356 } 357 358 case Node.CDATA_SECTION_NODE : { 359 String text; 360 361 text = node.getNodeValue(); 362 if ( text != null ) 363 characters( node.getNodeValue(), true ); 364 break; 365 } 366 367 case Node.COMMENT_NODE : 368 break; 369 370 case Node.ENTITY_REFERENCE_NODE : 371 break; 373 374 case Node.PROCESSING_INSTRUCTION_NODE : 375 break; 376 377 case Node.ELEMENT_NODE : 378 serializeElement( (Element ) node ); 379 break; 380 381 case Node.DOCUMENT_NODE : 382 case Node.DOCUMENT_FRAGMENT_NODE : { 384 Node child; 385 386 child = node.getFirstChild(); 390 while ( child != null ) { 391 serializeNode( child ); 392 child = child.getNextSibling(); 393 } 394 break; 395 } 396 397 default: 398 break; 399 } 400 } 401 402 403 protected ElementState content(boolean ignorable) 404 { 405 ElementState state; 406 407 state = getElementState(); 408 if ( ! isDocumentState() ) { 409 if ( state.empty ) 412 state.empty = false; 413 state.afterElement = false; 417 } 418 return state; 419 } 420 421 422 protected String getEntityRef( int ch ) 423 { 424 return null; 425 } 426 427 428 } 429 430 431 | Popular Tags |