1 16 17 18 23 24 package org.apache.xml.serialize; 25 26 27 import java.io.IOException ; 28 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.Node ; 31 import org.xml.sax.AttributeList ; 32 import org.xml.sax.Attributes ; 33 import org.xml.sax.SAXException ; 34 35 36 59 public class TextSerializer 60 extends BaseMarkupSerializer 61 { 62 63 64 69 public TextSerializer() 70 { 71 super( new OutputFormat( Method.TEXT, null, false ) ); 72 } 73 74 75 public void setOutputFormat( OutputFormat format ) 76 { 77 super.setOutputFormat( format != null ? format : new OutputFormat( Method.TEXT, null, false ) ); 78 } 79 80 81 85 86 public void startElement( String namespaceURI, String localName, 87 String rawName, Attributes attrs ) 88 throws SAXException 89 { 90 startElement( rawName == null ? localName : rawName, null ); 91 } 92 93 94 public void endElement( String namespaceURI, String localName, 95 String rawName ) 96 throws SAXException 97 { 98 endElement( rawName == null ? localName : rawName ); 99 } 100 101 102 106 107 public void startElement( String tagName, AttributeList attrs ) 108 throws SAXException 109 { 110 boolean preserveSpace; 111 ElementState state; 112 113 try { 114 state = getElementState(); 115 if ( isDocumentState() ) { 116 if ( ! _started ) 121 startDocument( tagName ); 122 } 123 preserveSpace = state.preserveSpace; 126 127 130 133 state = enterElementState( null, null, tagName, preserveSpace ); 137 } catch ( IOException except ) { 138 throw new SAXException ( except ); 139 } 140 } 141 142 143 public void endElement( String tagName ) 144 throws SAXException 145 { 146 try { 147 endElementIO( tagName ); 148 } catch ( IOException except ) { 149 throw new SAXException ( except ); 150 } 151 } 152 153 154 public void endElementIO( String tagName ) 155 throws IOException 156 { 157 ElementState state; 158 159 state = getElementState(); 163 state = leaveElementState(); 166 state.afterElement = true; 167 state.empty = false; 168 if ( isDocumentState() ) 169 _printer.flush(); 170 } 171 172 173 public void processingInstructionIO( String target, String code ) throws IOException 174 { 175 } 176 177 178 public void comment( String text ) 179 { 180 } 181 182 183 public void comment( char[] chars, int start, int length ) 184 { 185 } 186 187 188 public void characters( char[] chars, int start, int length ) 189 throws SAXException 190 { 191 ElementState state; 192 193 try { 194 state = content(); 195 state.doCData = state.inCData = false; 196 printText( chars, start, length, true, true ); 197 } catch ( IOException except ) { 198 throw new SAXException ( except ); 199 } 200 } 201 202 203 protected void characters( String text, boolean unescaped ) 204 throws IOException 205 { 206 ElementState state; 207 208 state = content(); 209 state.doCData = state.inCData = false; 210 printText( text, true, true ); 211 } 212 213 214 218 219 228 protected void startDocument( String rootTagName ) 229 throws IOException 230 { 231 _printer.leaveDTD(); 234 235 _started = true; 236 serializePreRoot(); 238 } 239 240 241 246 protected void serializeElement( Element elem ) 247 throws IOException 248 { 249 Node child; 250 ElementState state; 251 boolean preserveSpace; 252 String tagName; 253 254 tagName = elem.getTagName(); 255 state = getElementState(); 256 if ( isDocumentState() ) { 257 if ( ! _started ) 262 startDocument( tagName ); 263 } 264 preserveSpace = state.preserveSpace; 267 268 271 274 if ( elem.hasChildNodes() ) { 277 state = enterElementState( null, null, tagName, preserveSpace ); 280 child = elem.getFirstChild(); 281 while ( child != null ) { 282 serializeNode( child ); 283 child = child.getNextSibling(); 284 } 285 endElementIO( tagName ); 286 } else { 287 if ( ! isDocumentState() ) { 288 state.afterElement = true; 290 state.empty = false; 291 } 292 } 293 } 294 295 296 301 protected void serializeNode( Node node ) 302 throws IOException 303 { 304 switch ( node.getNodeType() ) { 308 case Node.TEXT_NODE : { 309 String text; 310 311 text = node.getNodeValue(); 312 if ( text != null ) 313 characters( node.getNodeValue(), true ); 314 break; 315 } 316 317 case Node.CDATA_SECTION_NODE : { 318 String text; 319 320 text = node.getNodeValue(); 321 if ( text != null ) 322 characters( node.getNodeValue(), true ); 323 break; 324 } 325 326 case Node.COMMENT_NODE : 327 break; 328 329 case Node.ENTITY_REFERENCE_NODE : 330 break; 332 333 case Node.PROCESSING_INSTRUCTION_NODE : 334 break; 335 336 case Node.ELEMENT_NODE : 337 serializeElement( (Element ) node ); 338 break; 339 340 case Node.DOCUMENT_NODE : 341 case Node.DOCUMENT_FRAGMENT_NODE : { 343 Node child; 344 345 child = node.getFirstChild(); 349 while ( child != null ) { 350 serializeNode( child ); 351 child = child.getNextSibling(); 352 } 353 break; 354 } 355 356 default: 357 break; 358 } 359 } 360 361 362 protected ElementState content() 363 { 364 ElementState state; 365 366 state = getElementState(); 367 if ( ! isDocumentState() ) { 368 if ( state.empty ) 371 state.empty = false; 372 state.afterElement = false; 376 } 377 return state; 378 } 379 380 381 protected String getEntityRef( int ch ) 382 { 383 return null; 384 } 385 386 387 } 388 389 390 | Popular Tags |