1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.trax; 21 22 import javax.xml.transform.Result ; 23 import javax.xml.transform.Transformer ; 24 import javax.xml.transform.TransformerException ; 25 import javax.xml.transform.dom.DOMResult ; 26 import javax.xml.transform.sax.TransformerHandler ; 27 28 import com.sun.org.apache.xalan.internal.xsltc.StripFilter; 29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 30 import com.sun.org.apache.xalan.internal.xsltc.dom.DOMWSFilter; 31 import com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl; 32 import com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager; 33 import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet; 34 import com.sun.org.apache.xml.internal.dtm.DTMWSFilter; 35 import com.sun.org.apache.xml.internal.serializer.SerializationHandler; 36 37 import org.xml.sax.Attributes ; 38 import org.xml.sax.ContentHandler ; 39 import org.xml.sax.DTDHandler ; 40 import org.xml.sax.Locator ; 41 import org.xml.sax.SAXException ; 42 import org.xml.sax.ext.DeclHandler ; 43 import org.xml.sax.ext.LexicalHandler ; 44 import org.xml.sax.helpers.DefaultHandler ; 45 46 50 public class TransformerHandlerImpl implements TransformerHandler , DeclHandler { 51 52 private TransformerImpl _transformer; 53 private AbstractTranslet _translet = null; 54 private String _systemId; 55 private SAXImpl _dom = null; 56 private ContentHandler _handler = null; 57 private LexicalHandler _lexHandler = null; 58 private DTDHandler _dtdHandler = null; 59 private DeclHandler _declHandler = null; 60 private Result _result = null; 61 private Locator _locator = null; 62 63 private boolean _done = false; 65 69 private boolean _isIdentity = false; 70 71 74 public TransformerHandlerImpl(TransformerImpl transformer) { 75 _transformer = transformer; 77 78 if (transformer.isIdentity()) { 79 _handler = new DefaultHandler (); 81 _isIdentity = true; 82 } 83 else { 84 _translet = _transformer.getTranslet(); 86 } 87 } 88 89 95 public String getSystemId() { 96 return _systemId; 97 } 98 99 105 public void setSystemId(String id) { 106 _systemId = id; 107 } 108 109 115 public Transformer getTransformer() { 116 return _transformer; 117 } 118 119 126 public void setResult(Result result) throws IllegalArgumentException { 127 _result = result; 128 129 if (null == result) { 130 ErrorMsg err = new ErrorMsg(ErrorMsg.ER_RESULT_NULL); 131 throw new IllegalArgumentException (err.toString()); } 133 134 if (_isIdentity) { 135 try { 136 SerializationHandler outputHandler = 138 _transformer.getOutputHandler(result); 139 _transformer.transferOutputProperties(outputHandler); 140 141 _handler = outputHandler; 142 _lexHandler = outputHandler; 143 } 144 catch (TransformerException e) { 145 _result = null; 146 } 147 } 148 else if (_done) { 149 try { 151 _transformer.setDOM(_dom); 152 _transformer.transform(null, _result); 153 } 154 catch (TransformerException e) { 155 throw new IllegalArgumentException (e.getMessage()); 157 } 158 } 159 } 160 161 165 public void characters(char[] ch, int start, int length) 166 throws SAXException 167 { 168 _handler.characters(ch, start, length); 169 } 170 171 175 public void startDocument() throws SAXException { 176 if (_result == null) { 178 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_RESULT_ERR); 179 throw new SAXException (err.toString()); 180 } 181 182 if (!_isIdentity) { 183 boolean hasIdCall = (_translet != null) ? _translet.hasIdCall() : false; 184 XSLTCDTMManager dtmManager = null; 185 186 try { 188 dtmManager = 189 (XSLTCDTMManager)_transformer.getTransformerFactory() 190 .getDTMManagerClass() 191 .newInstance(); 192 } catch (Exception e) { 193 throw new SAXException (e); 194 } 195 196 DTMWSFilter wsFilter; 197 if (_translet != null && _translet instanceof StripFilter) { 198 wsFilter = new DOMWSFilter(_translet); 199 } else { 200 wsFilter = null; 201 } 202 203 _dom = (SAXImpl)dtmManager.getDTM(null, false, wsFilter, true, 205 false, hasIdCall); 206 207 _handler = _dom.getBuilder(); 208 _lexHandler = (LexicalHandler ) _handler; 209 _dtdHandler = (DTDHandler ) _handler; 210 _declHandler = (DeclHandler ) _handler; 211 212 213 _dom.setDocumentURI(_systemId); 215 216 if (_locator != null) { 217 _handler.setDocumentLocator(_locator); 218 } 219 } 220 221 _handler.startDocument(); 223 } 224 225 229 public void endDocument() throws SAXException { 230 _handler.endDocument(); 232 233 if (!_isIdentity) { 234 if (_result != null) { 236 try { 237 _transformer.setDOM(_dom); 238 _transformer.transform(null, _result); 239 } 240 catch (TransformerException e) { 241 throw new SAXException (e); 242 } 243 } 244 _done = true; 246 247 _transformer.setDOM(_dom); 249 } 250 if (_isIdentity && _result instanceof DOMResult ) { 251 ((DOMResult )_result).setNode(_transformer.getTransletOutputHandlerFactory().getNode()); 252 } 253 } 254 255 259 public void startElement(String uri, String localName, 260 String qname, Attributes attributes) 261 throws SAXException 262 { 263 _handler.startElement(uri, localName, qname, attributes); 264 } 265 266 270 public void endElement(String namespaceURI, String localName, String qname) 271 throws SAXException 272 { 273 _handler.endElement(namespaceURI, localName, qname); 274 } 275 276 280 public void processingInstruction(String target, String data) 281 throws SAXException 282 { 283 _handler.processingInstruction(target, data); 284 } 285 286 289 public void startCDATA() throws SAXException { 290 if (_lexHandler != null) { 291 _lexHandler.startCDATA(); 292 } 293 } 294 295 298 public void endCDATA() throws SAXException { 299 if (_lexHandler != null) { 300 _lexHandler.endCDATA(); 301 } 302 } 303 304 308 public void comment(char[] ch, int start, int length) 309 throws SAXException 310 { 311 if (_lexHandler != null) { 312 _lexHandler.comment(ch, start, length); 313 } 314 } 315 316 321 public void ignorableWhitespace(char[] ch, int start, int length) 322 throws SAXException 323 { 324 _handler.ignorableWhitespace(ch, start, length); 325 } 326 327 331 public void setDocumentLocator(Locator locator) { 332 _locator = locator; 333 334 if (_handler != null) { 335 _handler.setDocumentLocator(locator); 336 } 337 } 338 339 343 public void skippedEntity(String name) throws SAXException { 344 _handler.skippedEntity(name); 345 } 346 347 351 public void startPrefixMapping(String prefix, String uri) 352 throws SAXException { 353 _handler.startPrefixMapping(prefix, uri); 354 } 355 356 360 public void endPrefixMapping(String prefix) throws SAXException { 361 _handler.endPrefixMapping(prefix); 362 } 363 364 367 public void startDTD(String name, String publicId, String systemId) 368 throws SAXException 369 { 370 if (_lexHandler != null) { 371 _lexHandler.startDTD(name, publicId, systemId); 372 } 373 } 374 375 378 public void endDTD() throws SAXException { 379 if (_lexHandler != null) { 380 _lexHandler.endDTD(); 381 } 382 } 383 384 387 public void startEntity(String name) throws SAXException { 388 if (_lexHandler != null) { 389 _lexHandler.startEntity(name); 390 } 391 } 392 393 396 public void endEntity(String name) throws SAXException { 397 if (_lexHandler != null) { 398 _lexHandler.endEntity(name); 399 } 400 } 401 402 405 public void unparsedEntityDecl(String name, String publicId, 406 String systemId, String notationName) throws SAXException 407 { 408 if (_dtdHandler != null) { 409 _dtdHandler.unparsedEntityDecl(name, publicId, systemId, 410 notationName); 411 } 412 } 413 414 417 public void notationDecl(String name, String publicId, String systemId) 418 throws SAXException 419 { 420 if (_dtdHandler != null) { 421 _dtdHandler.notationDecl(name, publicId, systemId); 422 } 423 } 424 425 428 public void attributeDecl(String eName, String aName, String type, 429 String valueDefault, String value) throws SAXException 430 { 431 if (_declHandler != null) { 432 _declHandler.attributeDecl(eName, aName, type, valueDefault, value); 433 } 434 } 435 436 439 public void elementDecl(String name, String model) 440 throws SAXException 441 { 442 if (_declHandler != null) { 443 _declHandler.elementDecl(name, model); 444 } 445 } 446 447 450 public void externalEntityDecl(String name, String publicId, String systemId) 451 throws SAXException 452 { 453 if (_declHandler != null) { 454 _declHandler.externalEntityDecl(name, publicId, systemId); 455 } 456 } 457 458 461 public void internalEntityDecl(String name, String value) 462 throws SAXException 463 { 464 if (_declHandler != null) { 465 _declHandler.internalEntityDecl(name, value); 466 } 467 } 468 469 470 473 public void reset() { 474 _systemId = null; 475 _dom = null; 476 _handler = null; 477 _lexHandler = null; 478 _dtdHandler = null; 479 _declHandler = null; 480 _result = null; 481 _locator = null; 482 } 483 } 484 | Popular Tags |