Source Home >> Java Source 1.6.0 >> com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager V 0.09
  • 001/*
  • 002 * Copyright 1999-2004 The Apache Software Foundation.
  • 003 *
  • 004 * Licensed under the Apache License, Version 2.0 (the "License");
  • 005 * you may not use this file except in compliance with the License.
  • 006 * You may obtain a copy of the License at
  • 007 *
  • 008 * http://www.apache.org/licenses/LICENSE-2.0
  • 009 *
  • 010 * Unless required by applicable law or agreed to in writing, software
  • 011 * distributed under the License is distributed on an "AS IS" BASIS,
  • 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • 013 * See the License for the specific language governing permissions and
  • 014 * limitations under the License.
  • 015 */
  • 016/*
  • 017 * $Id: XSLTCDTMManager.java,v 1.2 2005/08/16 22:32:54 jeffsuttor Exp $
  • 018 */
  • 019package com.sun.org.apache.xalan.internal.xsltc.dom;
  • 020
  • 021import javax.xml.stream.XMLEventReader;
  • 022import javax.xml.stream.XMLStreamReader;
  • 023import javax.xml.transform.Source;
  • 024import javax.xml.transform.dom.DOMSource;
  • 025import javax.xml.transform.sax.SAXSource;
  • 026import javax.xml.transform.stream.StreamSource;
  • 027import javax.xml.transform.stax.StAXSource;
  • 028
  • 029
  • 030import com.sun.org.apache.xml.internal.dtm.DTM;
  • 031import com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase;
  • 032import com.sun.org.apache.xml.internal.dtm.DTMException;
  • 033import com.sun.org.apache.xml.internal.dtm.DTMWSFilter;
  • 034import com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault;
  • 035import com.sun.org.apache.xml.internal.res.XMLErrorResources;
  • 036import com.sun.org.apache.xml.internal.res.XMLMessages;
  • 037import com.sun.org.apache.xml.internal.utils.SystemIDResolver;
  • 038import com.sun.org.apache.xalan.internal.xsltc.trax.DOM2SAX;
  • 039import com.sun.org.apache.xalan.internal.xsltc.trax.StAXEvent2SAX;
  • 040import com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX;
  • 041
  • 042import org.xml.sax.InputSource;
  • 043import org.xml.sax.SAXNotRecognizedException;
  • 044import org.xml.sax.SAXNotSupportedException;
  • 045import org.xml.sax.XMLReader;
  • 046
  • 047/**
  • 048 * The default implementation for the DTMManager.
  • 049 */
  • 050public class XSLTCDTMManager extends DTMManagerDefault
  • 051{
  • 052
  • 053 /** The default class name to use as the manager. */
  • 054 private static final String DEFAULT_CLASS_NAME =
  • 055 "com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager";
  • 056
  • 057 private static final String DEFAULT_PROP_NAME =
  • 058 "com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager";
  • 059
  • 060 /** Set this to true if you want a dump of the DTM after creation */
  • 061 private static final boolean DUMPTREE = false;
  • 062
  • 063 /** Set this to true if you want basic diagnostics */
  • 064 private static final boolean DEBUG = false;
  • 065
  • 066 /**
  • 067 * Constructor DTMManagerDefault
  • 068 *
  • 069 */
  • 070 public XSLTCDTMManager()
  • 071 {
  • 072 super();
  • 073 }
  • 074
  • 075 /**
  • 076 * Obtain a new instance of a <code>DTMManager</code>.
  • 077 * This static method creates a new factory instance.
  • 078 * The current implementation just returns a new XSLTCDTMManager instance.
  • 079 */
  • 080 public static XSLTCDTMManager newInstance()
  • 081 {
  • 082 return new XSLTCDTMManager();
  • 083 }
  • 084
  • 085 /**
  • 086 * Look up the class that provides the XSLTC DTM Manager service.
  • 087 * The following lookup procedure is used to find the service provider.
  • 088 * <ol>
  • 089 * <li>The value of the
  • 090 * <code>com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager</code> property, is
  • 091 * checked.</li>
  • 092 * <li>The <code>xalan.propeties</code> file is checked for a property
  • 093 * of the same name.</li>
  • 094 * <li>The
  • 095 * <code>META-INF/services/com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager</code>
  • 096 * file is checked.
  • 097 * </ol>
  • 098 * The default is <code>com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager</code>.
  • 099 */
  • 100 public static Class getDTMManagerClass() {
  • 101 Class mgrClass = ObjectFactory.lookUpFactoryClass(DEFAULT_PROP_NAME,
  • 102 null,
  • 103 DEFAULT_CLASS_NAME);
  • 104 // If no class found, default to this one. (This should never happen -
  • 105 // the ObjectFactory has already been told that the current class is
  • 106 // the default).
  • 107 return (mgrClass != null) ? mgrClass : XSLTCDTMManager.class;
  • 108 }
  • 109
  • 110 /**
  • 111 * Get an instance of a DTM, loaded with the content from the
  • 112 * specified source. If the unique flag is true, a new instance will
  • 113 * always be returned. Otherwise it is up to the DTMManager to return a
  • 114 * new instance or an instance that it already created and may be being used
  • 115 * by someone else.
  • 116 * (I think more parameters will need to be added for error handling, and
  • 117 * entity resolution).
  • 118 *
  • 119 * @param source the specification of the source object.
  • 120 * @param unique true if the returned DTM must be unique, probably because it
  • 121 * is going to be mutated.
  • 122 * @param whiteSpaceFilter Enables filtering of whitespace nodes, and may
  • 123 * be null.
  • 124 * @param incremental true if the DTM should be built incrementally, if
  • 125 * possible.
  • 126 * @param doIndexing true if the caller considers it worth it to use
  • 127 * indexing schemes.
  • 128 *
  • 129 * @return a non-null DTM reference.
  • 130 */
  • 131 public DTM getDTM(Source source, boolean unique,
  • 132 DTMWSFilter whiteSpaceFilter, boolean incremental,
  • 133 boolean doIndexing)
  • 134 {
  • 135 return getDTM(source, unique, whiteSpaceFilter, incremental,
  • 136 doIndexing, false, 0, true, false);
  • 137 }
  • 138
  • 139 /**
  • 140 * Get an instance of a DTM, loaded with the content from the
  • 141 * specified source. If the unique flag is true, a new instance will
  • 142 * always be returned. Otherwise it is up to the DTMManager to return a
  • 143 * new instance or an instance that it already created and may be being used
  • 144 * by someone else.
  • 145 * (I think more parameters will need to be added for error handling, and
  • 146 * entity resolution).
  • 147 *
  • 148 * @param source the specification of the source object.
  • 149 * @param unique true if the returned DTM must be unique, probably because it
  • 150 * is going to be mutated.
  • 151 * @param whiteSpaceFilter Enables filtering of whitespace nodes, and may
  • 152 * be null.
  • 153 * @param incremental true if the DTM should be built incrementally, if
  • 154 * possible.
  • 155 * @param doIndexing true if the caller considers it worth it to use
  • 156 * indexing schemes.
  • 157 * @param buildIdIndex true if the id index table should be built.
  • 158 *
  • 159 * @return a non-null DTM reference.
  • 160 */
  • 161 public DTM getDTM(Source source, boolean unique,
  • 162 DTMWSFilter whiteSpaceFilter, boolean incremental,
  • 163 boolean doIndexing, boolean buildIdIndex)
  • 164 {
  • 165 return getDTM(source, unique, whiteSpaceFilter, incremental,
  • 166 doIndexing, false, 0, buildIdIndex, false);
  • 167 }
  • 168
  • 169 /**
  • 170 * Get an instance of a DTM, loaded with the content from the
  • 171 * specified source. If the unique flag is true, a new instance will
  • 172 * always be returned. Otherwise it is up to the DTMManager to return a
  • 173 * new instance or an instance that it already created and may be being used
  • 174 * by someone else.
  • 175 * (I think more parameters will need to be added for error handling, and
  • 176 * entity resolution).
  • 177 *
  • 178 * @param source the specification of the source object.
  • 179 * @param unique true if the returned DTM must be unique, probably because it
  • 180 * is going to be mutated.
  • 181 * @param whiteSpaceFilter Enables filtering of whitespace nodes, and may
  • 182 * be null.
  • 183 * @param incremental true if the DTM should be built incrementally, if
  • 184 * possible.
  • 185 * @param doIndexing true if the caller considers it worth it to use
  • 186 * indexing schemes.
  • 187 * @param buildIdIndex true if the id index table should be built.
  • 188 * @param newNameTable true if we want to use a separate ExpandedNameTable
  • 189 * for this DTM.
  • 190 *
  • 191 * @return a non-null DTM reference.
  • 192 */
  • 193 public DTM getDTM(Source source, boolean unique,
  • 194 DTMWSFilter whiteSpaceFilter, boolean incremental,
  • 195 boolean doIndexing, boolean buildIdIndex,
  • 196 boolean newNameTable)
  • 197 {
  • 198 return getDTM(source, unique, whiteSpaceFilter, incremental,
  • 199 doIndexing, false, 0, buildIdIndex, newNameTable);
  • 200 }
  • 201
  • 202 /**
  • 203 * Get an instance of a DTM, loaded with the content from the
  • 204 * specified source. If the unique flag is true, a new instance will
  • 205 * always be returned. Otherwise it is up to the DTMManager to return a
  • 206 * new instance or an instance that it already created and may be being used
  • 207 * by someone else.
  • 208 * (I think more parameters will need to be added for error handling, and
  • 209 * entity resolution).
  • 210 *
  • 211 * @param source the specification of the source object.
  • 212 * @param unique true if the returned DTM must be unique, probably because it
  • 213 * is going to be mutated.
  • 214 * @param whiteSpaceFilter Enables filtering of whitespace nodes, and may
  • 215 * be null.
  • 216 * @param incremental true if the DTM should be built incrementally, if
  • 217 * possible.
  • 218 * @param doIndexing true if the caller considers it worth it to use
  • 219 * indexing schemes.
  • 220 * @param hasUserReader true if <code>source</code> is a
  • 221 * <code>SAXSource</code> object that has an
  • 222 * <code>XMLReader</code>, that was specified by the
  • 223 * user.
  • 224 * @param size Specifies initial size of tables that represent the DTM
  • 225 * @param buildIdIndex true if the id index table should be built.
  • 226 *
  • 227 * @return a non-null DTM reference.
  • 228 */
  • 229 public DTM getDTM(Source source, boolean unique,
  • 230 DTMWSFilter whiteSpaceFilter, boolean incremental,
  • 231 boolean doIndexing, boolean hasUserReader, int size,
  • 232 boolean buildIdIndex)
  • 233 {
  • 234 return getDTM(source, unique, whiteSpaceFilter, incremental,
  • 235 doIndexing, hasUserReader, size,
  • 236 buildIdIndex, false);
  • 237 }
  • 238
  • 239 /**
  • 240 * Get an instance of a DTM, loaded with the content from the
  • 241 * specified source. If the unique flag is true, a new instance will
  • 242 * always be returned. Otherwise it is up to the DTMManager to return a
  • 243 * new instance or an instance that it already created and may be being used
  • 244 * by someone else.
  • 245 * (I think more parameters will need to be added for error handling, and
  • 246 * entity resolution).
  • 247 *
  • 248 * @param source the specification of the source object.
  • 249 * @param unique true if the returned DTM must be unique, probably because it
  • 250 * is going to be mutated.
  • 251 * @param whiteSpaceFilter Enables filtering of whitespace nodes, and may
  • 252 * be null.
  • 253 * @param incremental true if the DTM should be built incrementally, if
  • 254 * possible.
  • 255 * @param doIndexing true if the caller considers it worth it to use
  • 256 * indexing schemes.
  • 257 * @param hasUserReader true if <code>source</code> is a
  • 258 * <code>SAXSource</code> object that has an
  • 259 * <code>XMLReader</code>, that was specified by the
  • 260 * user.
  • 261 * @param size Specifies initial size of tables that represent the DTM
  • 262 * @param buildIdIndex true if the id index table should be built.
  • 263 * @param newNameTable true if we want to use a separate ExpandedNameTable
  • 264 * for this DTM.
  • 265 *
  • 266 * @return a non-null DTM reference.
  • 267 */
  • 268 public DTM getDTM(Source source, boolean unique,
  • 269 DTMWSFilter whiteSpaceFilter, boolean incremental,
  • 270 boolean doIndexing, boolean hasUserReader, int size,
  • 271 boolean buildIdIndex, boolean newNameTable)
  • 272 {
  • 273 if(DEBUG && null != source) {
  • 274 System.out.println("Starting "+
  • 275 (unique ? "UNIQUE" : "shared")+
  • 276 " source: "+source.getSystemId());
  • 277 }
  • 278
  • 279 int dtmPos = getFirstFreeDTMID();
  • 280 int documentID = dtmPos << IDENT_DTM_NODE_BITS;
  • 281
  • 282 if ((null != source) && source instanceof StAXSource) {
  • 283 final StAXSource staxSource = (StAXSource)source;
  • 284 StAXEvent2SAX staxevent2sax = null;
  • 285 StAXStream2SAX staxStream2SAX = null;
  • 286 if (staxSource.getXMLEventReader() != null) {
  • 287 final XMLEventReader xmlEventReader = staxSource.getXMLEventReader();
  • 288 staxevent2sax = new StAXEvent2SAX(xmlEventReader);
  • 289 } else if (staxSource.getXMLStreamReader() != null) {
  • 290 final XMLStreamReader xmlStreamReader = staxSource.getXMLStreamReader();
  • 291 staxStream2SAX = new StAXStream2SAX(xmlStreamReader);
  • 292 }
  • 293
  • 294 SAXImpl dtm;
  • 295
  • 296 if (size <= 0) {
  • 297 dtm = new SAXImpl(this, source, documentID,
  • 298 whiteSpaceFilter, null, doIndexing,
  • 299 DTMDefaultBase.DEFAULT_BLOCKSIZE,
  • 300 buildIdIndex, newNameTable);
  • 301 } else {
  • 302 dtm = new SAXImpl(this, source, documentID,
  • 303 whiteSpaceFilter, null, doIndexing,
  • 304 size, buildIdIndex, newNameTable);
  • 305 }
  • 306
  • 307 dtm.setDocumentURI(source.getSystemId());
  • 308
  • 309 addDTM(dtm, dtmPos, 0);
  • 310
  • 311 try {
  • 312 if (staxevent2sax != null) {
  • 313 staxevent2sax.setContentHandler(dtm);
  • 314 staxevent2sax.parse();
  • 315 }
  • 316 else if (staxStream2SAX != null) {
  • 317 staxStream2SAX.setContentHandler(dtm);
  • 318 staxStream2SAX.parse();
  • 319 }
  • 320
  • 321 }
  • 322 catch (RuntimeException re) {
  • 323 throw re;
  • 324 }
  • 325 catch (Exception e) {
  • 326 throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e);
  • 327 }
  • 328
  • 329 return dtm;
  • 330 }else if ((null != source) && source instanceof DOMSource) {
  • 331 final DOMSource domsrc = (DOMSource) source;
  • 332 final org.w3c.dom.Node node = domsrc.getNode();
  • 333 final DOM2SAX dom2sax = new DOM2SAX(node);
  • 334
  • 335 SAXImpl dtm;
  • 336
  • 337 if (size <= 0) {
  • 338 dtm = new SAXImpl(this, source, documentID,
  • 339 whiteSpaceFilter, null, doIndexing,
  • 340 DTMDefaultBase.DEFAULT_BLOCKSIZE,
  • 341 buildIdIndex, newNameTable);
  • 342 } else {
  • 343 dtm = new SAXImpl(this, source, documentID,
  • 344 whiteSpaceFilter, null, doIndexing,
  • 345 size, buildIdIndex, newNameTable);
  • 346 }
  • 347
  • 348 dtm.setDocumentURI(source.getSystemId());
  • 349
  • 350 addDTM(dtm, dtmPos, 0);
  • 351
  • 352 dom2sax.setContentHandler(dtm);
  • 353
  • 354 try {
  • 355 dom2sax.parse();
  • 356 }
  • 357 catch (RuntimeException re) {
  • 358 throw re;
  • 359 }
  • 360 catch (Exception e) {
  • 361 throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e);
  • 362 }
  • 363
  • 364 return dtm;
  • 365 }
  • 366 else
  • 367 {
  • 368 boolean isSAXSource = (null != source)
  • 369 ? (source instanceof SAXSource) : true;
  • 370 boolean isStreamSource = (null != source)
  • 371 ? (source instanceof StreamSource) : false;
  • 372
  • 373 if (isSAXSource || isStreamSource) {
  • 374 XMLReader reader;
  • 375 InputSource xmlSource;
  • 376
  • 377 if (null == source) {
  • 378 xmlSource = null;
  • 379 reader = null;
  • 380 hasUserReader = false; // Make sure the user didn't lie
  • 381 }
  • 382 else {
  • 383 reader = getXMLReader(source);
  • 384 xmlSource = SAXSource.sourceToInputSource(source);
  • 385
  • 386 String urlOfSource = xmlSource.getSystemId();
  • 387
  • 388 if (null != urlOfSource) {
  • 389 try {
  • 390 urlOfSource = SystemIDResolver.getAbsoluteURI(urlOfSource);
  • 391 }
  • 392 catch (Exception e) {
  • 393 // %REVIEW% Is there a better way to send a warning?
  • 394 System.err.println("Can not absolutize URL: " + urlOfSource);
  • 395 }
  • 396
  • 397 xmlSource.setSystemId(urlOfSource);
  • 398 }
  • 399 }
  • 400
  • 401 // Create the basic SAX2DTM.
  • 402 SAXImpl dtm;
  • 403 if (size <= 0) {
  • 404 dtm = new SAXImpl(this, source, documentID, whiteSpaceFilter,
  • 405 null, doIndexing,
  • 406 DTMDefaultBase.DEFAULT_BLOCKSIZE,
  • 407 buildIdIndex, newNameTable);
  • 408 } else {
  • 409 dtm = new SAXImpl(this, source, documentID, whiteSpaceFilter,
  • 410 null, doIndexing, size, buildIdIndex, newNameTable);
  • 411 }
  • 412
  • 413 // Go ahead and add the DTM to the lookup table. This needs to be
  • 414 // done before any parsing occurs. Note offset 0, since we've just
  • 415 // created a new DTM.
  • 416 addDTM(dtm, dtmPos, 0);
  • 417
  • 418 if (null == reader) {
  • 419 // Then the user will construct it themselves.
  • 420 return dtm;
  • 421 }
  • 422
  • 423 reader.setContentHandler(dtm.getBuilder());
  • 424
  • 425 if (!hasUserReader || null == reader.getDTDHandler()) {
  • 426 reader.setDTDHandler(dtm);
  • 427 }
  • 428
  • 429 if(!hasUserReader || null == reader.getErrorHandler()) {
  • 430 reader.setErrorHandler(dtm);
  • 431 }
  • 432
  • 433 try {
  • 434 reader.setProperty("http://xml.org/sax/properties/lexical-handler", dtm);
  • 435 }
  • 436 catch (SAXNotRecognizedException e){}
  • 437 catch (SAXNotSupportedException e){}
  • 438
  • 439 try {
  • 440 reader.parse(xmlSource);
  • 441 }
  • 442 catch (RuntimeException re) {
  • 443 throw re;
  • 444 }
  • 445 catch (Exception e) {
  • 446 throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e);
  • 447 } finally {
  • 448 if (!hasUserReader) {
  • 449 releaseXMLReader(reader);
  • 450 }
  • 451 }
  • 452
  • 453 if (DUMPTREE) {
  • 454 System.out.println("Dumping SAX2DOM");
  • 455 dtm.dumpDTM(System.err);
  • 456 }
  • 457
  • 458 return dtm;
  • 459 }
  • 460 else {
  • 461 // It should have been handled by a derived class or the caller
  • 462 // made a mistake.
  • 463 throw new DTMException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NOT_SUPPORTED, new Object[]{source}));
  • 464 }
  • 465 }
  • 466 }
  • 467}

文件:XSLTCDTMManager.java
包名:com.sun.org.apache.xalan.internal.xsltc.dom
类名:XSLTCDTMManager
继承:DTMManagerDefault
接口: