1 16 19 package org.apache.xml.serializer; 20 21 import java.util.Vector ; 22 23 import org.xml.sax.Attributes ; 24 import org.xml.sax.ContentHandler ; 25 import org.xml.sax.ErrorHandler ; 26 import org.xml.sax.SAXException ; 27 import org.xml.sax.SAXParseException ; 28 import org.xml.sax.ext.LexicalHandler ; 29 30 34 abstract public class ToSAXHandler extends SerializerBase 35 { 36 public ToSAXHandler() 37 { 38 } 39 40 public ToSAXHandler( 41 ContentHandler hdlr, 42 LexicalHandler lex, 43 String encoding) 44 { 45 setContentHandler(hdlr); 46 setLexHandler(lex); 47 setEncoding(encoding); 48 } 49 public ToSAXHandler(ContentHandler handler, String encoding) 50 { 51 setContentHandler(handler); 52 setEncoding(encoding); 53 } 54 55 58 protected ContentHandler m_saxHandler; 59 60 63 protected LexicalHandler m_lexHandler; 64 65 74 private boolean m_shouldGenerateNSAttribute = true; 75 76 80 protected TransformStateSetter m_state = null; 81 82 85 protected void startDocumentInternal() throws SAXException 86 { 87 if (m_needToCallStartDocument) 88 { 89 super.startDocumentInternal(); 90 91 m_saxHandler.startDocument(); 92 m_needToCallStartDocument = false; 93 } 94 } 95 99 public void startDTD(String arg0, String arg1, String arg2) 100 throws SAXException 101 { 102 } 104 105 114 public void characters(String characters) throws SAXException 115 { 116 final int len = characters.length(); 117 if (len > m_charsBuff.length) 118 { 119 m_charsBuff = new char[len*2 + 1]; 120 } 121 characters.getChars(0,len, m_charsBuff, 0); 122 characters(m_charsBuff, 0, len); 123 } 124 125 130 public void comment(String comment) throws SAXException 131 { 132 133 if (m_elemContext.m_startTagOpen) 135 { 136 closeStartTag(); 137 } 138 else if (m_cdataTagOpen) 139 { 140 closeCDATA(); 141 } 142 143 if (m_lexHandler != null) 145 { 146 final int len = comment.length(); 147 if (len > m_charsBuff.length) 148 { 149 m_charsBuff = new char[len*2 + 1]; 150 } 151 comment.getChars(0,len, m_charsBuff, 0); 152 m_lexHandler.comment(m_charsBuff, 0, len); 153 if (m_tracer != null) 155 super.fireCommentEvent(m_charsBuff, 0, len); 156 } 157 158 } 159 160 165 public void processingInstruction(String target, String data) 166 throws SAXException 167 { 168 } 170 171 protected void closeStartTag() throws SAXException 172 { 173 } 174 175 protected void closeCDATA() throws SAXException 176 { 177 } 179 180 204 public void startElement( 205 String arg0, 206 String arg1, 207 String arg2, 208 Attributes arg3) 209 throws SAXException 210 { 211 if (m_state != null) { 212 m_state.resetState(getTransformer()); 213 } 214 215 if (m_tracer != null) 217 super.fireStartElem(arg2); 218 } 219 220 224 public void setLexHandler(LexicalHandler _lexHandler) 225 { 226 this.m_lexHandler = _lexHandler; 227 } 228 229 233 public void setContentHandler(ContentHandler _saxHandler) 234 { 235 this.m_saxHandler = _saxHandler; 236 if (m_lexHandler == null && _saxHandler instanceof LexicalHandler ) 237 { 238 m_lexHandler = (LexicalHandler ) _saxHandler; 241 } 242 } 243 244 249 public void setCdataSectionElements(Vector URI_and_localNames) 250 { 251 } 253 254 260 public void setShouldOutputNSAttr(boolean doOutputNSAttr) 261 { 262 m_shouldGenerateNSAttribute = doOutputNSAttr; 263 } 264 265 271 boolean getShouldOutputNSAttr() 272 { 273 return m_shouldGenerateNSAttribute; 274 } 275 276 280 public void flushPending() throws SAXException 281 { 282 283 if (m_needToCallStartDocument) 284 { 285 startDocumentInternal(); 286 m_needToCallStartDocument = false; 287 } 288 289 if (m_elemContext.m_startTagOpen) 290 { 291 closeStartTag(); 292 m_elemContext.m_startTagOpen = false; 293 } 294 295 if (m_cdataTagOpen) 296 { 297 closeCDATA(); 298 m_cdataTagOpen = false; 299 } 300 301 } 302 303 311 public void setTransformState(TransformStateSetter ts) { 312 this.m_state = ts; 313 } 314 315 325 public void startElement(String uri, String localName, String qName) 326 throws SAXException { 327 328 if (m_state != null) { 329 m_state.resetState(getTransformer()); 330 } 331 332 if (m_tracer != null) 334 super.fireStartElem(qName); 335 } 336 337 344 public void startElement(String qName) throws SAXException { 345 if (m_state != null) { 346 m_state.resetState(getTransformer()); 347 } 348 if (m_tracer != null) 350 super.fireStartElem(qName); 351 } 352 353 359 public void characters(org.w3c.dom.Node node) 360 throws org.xml.sax.SAXException 361 { 362 if (m_state != null) 364 { 365 m_state.setCurrentNode(node); 366 } 367 368 String data = node.getNodeValue(); 371 if (data != null) { 372 this.characters(data); 373 } 374 } 375 376 379 public void fatalError(SAXParseException exc) throws SAXException { 380 super.fatalError(exc); 381 382 m_needToCallStartDocument = false; 383 384 if (m_saxHandler instanceof ErrorHandler ) { 385 ((ErrorHandler )m_saxHandler).fatalError(exc); 386 } 387 } 388 389 392 public void error(SAXParseException exc) throws SAXException { 393 super.error(exc); 394 395 if (m_saxHandler instanceof ErrorHandler ) 396 ((ErrorHandler )m_saxHandler).error(exc); 397 398 } 399 400 403 public void warning(SAXParseException exc) throws SAXException { 404 super.warning(exc); 405 406 if (m_saxHandler instanceof ErrorHandler ) 407 ((ErrorHandler )m_saxHandler).warning(exc); 408 } 409 410 411 419 public boolean reset() 420 { 421 boolean wasReset = false; 422 if (super.reset()) 423 { 424 resetToSAXHandler(); 425 wasReset = true; 426 } 427 return wasReset; 428 } 429 430 434 private void resetToSAXHandler() 435 { 436 this.m_lexHandler = null; 437 this.m_saxHandler = null; 438 this.m_state = null; 439 this.m_shouldGenerateNSAttribute = false; 440 } 441 442 445 public void addUniqueAttribute(String qName, String value, int flags) 446 throws SAXException 447 { 448 addAttribute(qName, value); 449 } 450 } 451 | Popular Tags |