1 16 19 package org.apache.xalan.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 org.apache.xml.dtm.DTM; 27 import org.apache.xml.dtm.ref.DTMDefaultBase; 28 import org.apache.xml.dtm.DTMException; 29 import org.apache.xml.dtm.DTMWSFilter; 30 import org.apache.xml.dtm.ref.DTMManagerDefault; 31 import org.apache.xml.res.XMLErrorResources; 32 import org.apache.xml.res.XMLMessages; 33 import org.apache.xml.utils.SystemIDResolver; 34 import org.apache.xalan.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 "org.apache.xalan.xsltc.dom.XSLTCDTMManager"; 50 51 private static final String DEFAULT_PROP_NAME = 52 "org.apache.xalan.xsltc.dom.XSLTCDTMManager"; 53 54 55 private static final boolean DUMPTREE = false; 56 57 58 private static final boolean DEBUG = false; 59 60 64 public XSLTCDTMManager() 65 { 66 super(); 67 } 68 69 74 public static XSLTCDTMManager newInstance() 75 { 76 return new XSLTCDTMManager(); 77 } 78 79 94 public static Class getDTMManagerClass() { 95 Class mgrClass = ObjectFactory.lookUpFactoryClass(DEFAULT_PROP_NAME, 96 null, 97 DEFAULT_CLASS_NAME); 98 return (mgrClass != null) ? mgrClass : XSLTCDTMManager.class; 102 } 103 104 125 public DTM getDTM(Source source, boolean unique, 126 DTMWSFilter whiteSpaceFilter, boolean incremental, 127 boolean doIndexing) 128 { 129 return getDTM(source, unique, whiteSpaceFilter, incremental, 130 doIndexing, false, 0, true, false); 131 } 132 133 155 public DTM getDTM(Source source, boolean unique, 156 DTMWSFilter whiteSpaceFilter, boolean incremental, 157 boolean doIndexing, boolean buildIdIndex) 158 { 159 return getDTM(source, unique, whiteSpaceFilter, incremental, 160 doIndexing, false, 0, buildIdIndex, false); 161 } 162 163 187 public DTM getDTM(Source source, boolean unique, 188 DTMWSFilter whiteSpaceFilter, boolean incremental, 189 boolean doIndexing, boolean buildIdIndex, 190 boolean newNameTable) 191 { 192 return getDTM(source, unique, whiteSpaceFilter, incremental, 193 doIndexing, false, 0, buildIdIndex, newNameTable); 194 } 195 196 223 public DTM getDTM(Source source, boolean unique, 224 DTMWSFilter whiteSpaceFilter, boolean incremental, 225 boolean doIndexing, boolean hasUserReader, int size, 226 boolean buildIdIndex) 227 { 228 return getDTM(source, unique, whiteSpaceFilter, incremental, 229 doIndexing, hasUserReader, size, 230 buildIdIndex, false); 231 } 232 233 262 public DTM getDTM(Source source, boolean unique, 263 DTMWSFilter whiteSpaceFilter, boolean incremental, 264 boolean doIndexing, boolean hasUserReader, int size, 265 boolean buildIdIndex, boolean newNameTable) 266 { 267 if(DEBUG && null != source) { 268 System.out.println("Starting "+ 269 (unique ? "UNIQUE" : "shared")+ 270 " source: "+source.getSystemId()); 271 } 272 273 int dtmPos = getFirstFreeDTMID(); 274 int documentID = dtmPos << IDENT_DTM_NODE_BITS; 275 276 if ((null != source) && source instanceof DOMSource ) 277 { 278 final DOMSource domsrc = (DOMSource ) source; 279 final org.w3c.dom.Node node = domsrc.getNode(); 280 final DOM2SAX dom2sax = new DOM2SAX(node); 281 282 SAXImpl dtm; 283 284 if (size <= 0) { 285 dtm = new SAXImpl(this, source, documentID, 286 whiteSpaceFilter, null, doIndexing, 287 DTMDefaultBase.DEFAULT_BLOCKSIZE, 288 buildIdIndex, newNameTable); 289 } else { 290 dtm = new SAXImpl(this, source, documentID, 291 whiteSpaceFilter, null, doIndexing, 292 size, buildIdIndex, newNameTable); 293 } 294 295 dtm.setDocumentURI(source.getSystemId()); 296 297 addDTM(dtm, dtmPos, 0); 298 299 dom2sax.setContentHandler(dtm); 300 301 try { 302 dom2sax.parse(); 303 } 304 catch (RuntimeException re) { 305 throw re; 306 } 307 catch (Exception e) { 308 throw new org.apache.xml.utils.WrappedRuntimeException(e); 309 } 310 311 return dtm; 312 } 313 else 314 { 315 boolean isSAXSource = (null != source) 316 ? (source instanceof SAXSource ) : true; 317 boolean isStreamSource = (null != source) 318 ? (source instanceof StreamSource ) : false; 319 320 if (isSAXSource || isStreamSource) { 321 XMLReader reader; 322 InputSource xmlSource; 323 324 if (null == source) { 325 xmlSource = null; 326 reader = null; 327 hasUserReader = false; } 329 else { 330 reader = getXMLReader(source); 331 xmlSource = SAXSource.sourceToInputSource(source); 332 333 String urlOfSource = xmlSource.getSystemId(); 334 335 if (null != urlOfSource) { 336 try { 337 urlOfSource = SystemIDResolver.getAbsoluteURI(urlOfSource); 338 } 339 catch (Exception e) { 340 System.err.println("Can not absolutize URL: " + urlOfSource); 342 } 343 344 xmlSource.setSystemId(urlOfSource); 345 } 346 } 347 348 SAXImpl dtm; 350 if (size <= 0) { 351 dtm = new SAXImpl(this, source, documentID, whiteSpaceFilter, 352 null, doIndexing, 353 DTMDefaultBase.DEFAULT_BLOCKSIZE, 354 buildIdIndex, newNameTable); 355 } else { 356 dtm = new SAXImpl(this, source, documentID, whiteSpaceFilter, 357 null, doIndexing, size, buildIdIndex, newNameTable); 358 } 359 360 addDTM(dtm, dtmPos, 0); 364 365 if (null == reader) { 366 return dtm; 368 } 369 370 reader.setContentHandler(dtm.getBuilder()); 371 372 if (!hasUserReader || null == reader.getDTDHandler()) { 373 reader.setDTDHandler(dtm); 374 } 375 376 if(!hasUserReader || null == reader.getErrorHandler()) { 377 reader.setErrorHandler(dtm); 378 } 379 380 try { 381 reader.setProperty("http://xml.org/sax/properties/lexical-handler", dtm); 382 } 383 catch (SAXNotRecognizedException e){} 384 catch (SAXNotSupportedException e){} 385 386 try { 387 reader.parse(xmlSource); 388 } 389 catch (RuntimeException re) { 390 throw re; 391 } 392 catch (Exception e) { 393 throw new org.apache.xml.utils.WrappedRuntimeException(e); 394 } finally { 395 if (!hasUserReader) { 396 releaseXMLReader(reader); 397 } 398 } 399 400 if (DUMPTREE) { 401 System.out.println("Dumping SAX2DOM"); 402 dtm.dumpDTM(System.err); 403 } 404 405 return dtm; 406 } 407 else { 408 throw new DTMException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NOT_SUPPORTED, new Object []{source})); 411 } 412 } 413 } 414 } 415 | Popular Tags |