1 16 19 package com.sun.org.apache.xalan.internal.xsltc.dom; 20 21 import javax.xml.transform.Source ; 22 import javax.xml.transform.dom.DOMSource ; 23 import javax.xml.transform.sax.SAXSource ; 24 import javax.xml.transform.stream.StreamSource ; 25 26 import com.sun.org.apache.xml.internal.dtm.DTM; 27 import com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase; 28 import com.sun.org.apache.xml.internal.dtm.DTMException; 29 import com.sun.org.apache.xml.internal.dtm.DTMWSFilter; 30 import com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault; 31 import com.sun.org.apache.xml.internal.res.XMLErrorResources; 32 import com.sun.org.apache.xml.internal.res.XMLMessages; 33 import com.sun.org.apache.xml.internal.utils.SystemIDResolver; 34 import com.sun.org.apache.xalan.internal.xsltc.trax.DOM2SAX; 35 36 import org.xml.sax.InputSource ; 37 import org.xml.sax.SAXNotRecognizedException ; 38 import org.xml.sax.SAXNotSupportedException ; 39 import org.xml.sax.XMLReader ; 40 41 44 public class XSLTCDTMManager extends DTMManagerDefault 45 { 46 47 48 private static final String DEFAULT_CLASS_NAME = 49 "com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager"; 50 51 private static final String DEFAULT_PROP_NAME = 52 "com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager"; 53 54 private static final String NAMESPACE_PREFIXES_FEATURE = 55 "http://xml.org/sax/features/namespace-prefixes"; 56 57 58 private static final boolean DUMPTREE = false; 59 60 61 private static final boolean DEBUG = false; 62 63 67 public XSLTCDTMManager() 68 { 69 super(); 70 } 71 72 77 public static XSLTCDTMManager newInstance() 78 { 79 return new XSLTCDTMManager(); 80 } 81 82 97 public static Class getDTMManagerClass() { 98 Class mgrClass = ObjectFactory.lookUpFactoryClass(DEFAULT_PROP_NAME, 99 null, 100 DEFAULT_CLASS_NAME); 101 return (mgrClass != null) ? mgrClass : XSLTCDTMManager.class; 105 } 106 107 128 public DTM getDTM(Source source, boolean unique, 129 DTMWSFilter whiteSpaceFilter, boolean incremental, 130 boolean doIndexing) 131 { 132 return getDTM(source, unique, whiteSpaceFilter, incremental, 133 doIndexing, false, 0, true, false); 134 } 135 136 158 public DTM getDTM(Source source, boolean unique, 159 DTMWSFilter whiteSpaceFilter, boolean incremental, 160 boolean doIndexing, boolean buildIdIndex) 161 { 162 return getDTM(source, unique, whiteSpaceFilter, incremental, 163 doIndexing, false, 0, buildIdIndex, false); 164 } 165 166 190 public DTM getDTM(Source source, boolean unique, 191 DTMWSFilter whiteSpaceFilter, boolean incremental, 192 boolean doIndexing, boolean buildIdIndex, 193 boolean newNameTable) 194 { 195 return getDTM(source, unique, whiteSpaceFilter, incremental, 196 doIndexing, false, 0, buildIdIndex, newNameTable); 197 } 198 199 226 public DTM getDTM(Source source, boolean unique, 227 DTMWSFilter whiteSpaceFilter, boolean incremental, 228 boolean doIndexing, boolean hasUserReader, int size, 229 boolean buildIdIndex) 230 { 231 return getDTM(source, unique, whiteSpaceFilter, incremental, 232 doIndexing, hasUserReader, size, 233 buildIdIndex, false); 234 } 235 236 265 public DTM getDTM(Source source, boolean unique, 266 DTMWSFilter whiteSpaceFilter, boolean incremental, 267 boolean doIndexing, boolean hasUserReader, int size, 268 boolean buildIdIndex, boolean newNameTable) 269 { 270 if(DEBUG && null != source) { 271 System.out.println("Starting "+ 272 (unique ? "UNIQUE" : "shared")+ 273 " source: "+source.getSystemId()); 274 } 275 276 int dtmPos = getFirstFreeDTMID(); 277 int documentID = dtmPos << IDENT_DTM_NODE_BITS; 278 279 if ((null != source) && source instanceof DOMSource ) 280 { 281 final DOMSource domsrc = (DOMSource ) source; 282 final org.w3c.dom.Node node = domsrc.getNode(); 283 final DOM2SAX dom2sax = new DOM2SAX(node); 284 285 SAXImpl dtm; 286 287 if (size <= 0) { 288 dtm = new SAXImpl(this, source, documentID, 289 whiteSpaceFilter, null, doIndexing, 290 DTMDefaultBase.DEFAULT_BLOCKSIZE, 291 buildIdIndex, newNameTable); 292 } else { 293 dtm = new SAXImpl(this, source, documentID, 294 whiteSpaceFilter, null, doIndexing, 295 size, buildIdIndex, newNameTable); 296 } 297 298 dtm.setDocumentURI(source.getSystemId()); 299 300 addDTM(dtm, dtmPos, 0); 301 302 dom2sax.setContentHandler(dtm); 303 304 try { 305 dom2sax.parse(); 306 } 307 catch (RuntimeException re) { 308 throw re; 309 } 310 catch (Exception e) { 311 throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e); 312 } 313 314 return dtm; 315 } 316 else 317 { 318 boolean isSAXSource = (null != source) 319 ? (source instanceof SAXSource ) : true; 320 boolean isStreamSource = (null != source) 321 ? (source instanceof StreamSource ) : false; 322 323 if (isSAXSource || isStreamSource) { 324 XMLReader reader; 325 InputSource xmlSource; 326 327 if (null == source) { 328 xmlSource = null; 329 reader = null; 330 hasUserReader = false; } 332 else { 333 reader = getXMLReader(source); 334 xmlSource = SAXSource.sourceToInputSource(source); 335 336 String urlOfSource = xmlSource.getSystemId(); 337 338 if (null != urlOfSource) { 339 try { 340 urlOfSource = SystemIDResolver.getAbsoluteURI(urlOfSource); 341 } 342 catch (Exception e) { 343 System.err.println("Can not absolutize URL: " + urlOfSource); 345 } 346 347 xmlSource.setSystemId(urlOfSource); 348 } 349 } 350 351 SAXImpl dtm; 353 if (size <= 0) { 354 dtm = new SAXImpl(this, source, documentID, whiteSpaceFilter, 355 null, doIndexing, 356 DTMDefaultBase.DEFAULT_BLOCKSIZE, 357 buildIdIndex, newNameTable); 358 } else { 359 dtm = new SAXImpl(this, source, documentID, whiteSpaceFilter, 360 null, doIndexing, size, buildIdIndex, newNameTable); 361 } 362 363 addDTM(dtm, dtmPos, 0); 367 368 if (null == reader) { 369 return dtm; 371 } 372 373 reader.setContentHandler(dtm.getBuilder()); 374 375 if (!hasUserReader || null == reader.getDTDHandler()) { 376 reader.setDTDHandler(dtm); 377 } 378 379 if(!hasUserReader || null == reader.getErrorHandler()) { 380 reader.setErrorHandler(dtm); 381 } 382 383 try { 384 reader.setProperty("http://xml.org/sax/properties/lexical-handler", dtm); 385 reader.setFeature(NAMESPACE_PREFIXES_FEATURE, true); 386 } 387 catch (SAXNotRecognizedException e){} 388 catch (SAXNotSupportedException e){} 389 390 try { 391 reader.parse(xmlSource); 392 } 393 catch (RuntimeException re) { 394 throw re; 395 } 396 catch (Exception e) { 397 throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e); 398 } finally { 399 if (!hasUserReader) { 400 releaseXMLReader(reader); 401 } 402 } 403 404 if (DUMPTREE) { 405 System.out.println("Dumping SAX2DOM"); 406 dtm.dumpDTM(System.err); 407 } 408 409 return dtm; 410 } 411 else { 412 throw new DTMException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NOT_SUPPORTED, new Object []{source})); 415 } 416 } 417 } 418 } 419 | Popular Tags |