KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > dom > XSLTCDTMManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: XSLTCDTMManager.java,v 1.6 2004/02/23 10:29:36 aruny Exp $
18  */

19 package org.apache.xalan.xsltc.dom;
20
21 import javax.xml.transform.Source JavaDoc;
22 import javax.xml.transform.dom.DOMSource JavaDoc;
23 import javax.xml.transform.sax.SAXSource JavaDoc;
24 import javax.xml.transform.stream.StreamSource JavaDoc;
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 JavaDoc;
37 import org.xml.sax.SAXNotRecognizedException JavaDoc;
38 import org.xml.sax.SAXNotSupportedException JavaDoc;
39 import org.xml.sax.XMLReader JavaDoc;
40
41 /**
42  * The default implementation for the DTMManager.
43  */

44 public class XSLTCDTMManager extends DTMManagerDefault
45 {
46     
47     /** The default class name to use as the manager. */
48     private static final String JavaDoc DEFAULT_CLASS_NAME =
49         "org.apache.xalan.xsltc.dom.XSLTCDTMManager";
50
51     private static final String JavaDoc DEFAULT_PROP_NAME =
52         "org.apache.xalan.xsltc.dom.XSLTCDTMManager";
53
54     /** Set this to true if you want a dump of the DTM after creation */
55     private static final boolean DUMPTREE = false;
56   
57     /** Set this to true if you want basic diagnostics */
58     private static final boolean DEBUG = false;
59
60     /**
61      * Constructor DTMManagerDefault
62      *
63      */

64     public XSLTCDTMManager()
65     {
66         super();
67     }
68
69     /**
70      * Obtain a new instance of a <code>DTMManager</code>.
71      * This static method creates a new factory instance.
72      * The current implementation just returns a new XSLTCDTMManager instance.
73      */

74     public static XSLTCDTMManager newInstance()
75     {
76         return new XSLTCDTMManager();
77     }
78
79     /**
80      * Look up the class that provides the XSLTC DTM Manager service.
81      * The following lookup procedure is used to find the service provider.
82      * <ol>
83      * <li>The value of the
84      * <code>org.apache.xalan.xsltc.dom.XSLTCDTMManager</code> property, is
85      * checked.</li>
86      * <li>The <code>xalan.propeties</code> file is checked for a property
87      * of the same name.</li>
88      * <li>The
89      * <code>META-INF/services/org.apache.xalan.xsltc.dom.XSLTCDTMManager</code>
90      * file is checked.
91      * </ol>
92      * The default is <code>org.apache.xalan.xsltc.dom.XSLTCDTMManager</code>.
93      */

94     public static Class JavaDoc getDTMManagerClass() {
95         Class JavaDoc mgrClass = ObjectFactory.lookUpFactoryClass(DEFAULT_PROP_NAME,
96                                                           null,
97                                                           DEFAULT_CLASS_NAME);
98         // If no class found, default to this one. (This should never happen -
99
// the ObjectFactory has already been told that the current class is
100
// the default).
101
return (mgrClass != null) ? mgrClass : XSLTCDTMManager.class;
102     }
103
104     /**
105      * Get an instance of a DTM, loaded with the content from the
106      * specified source. If the unique flag is true, a new instance will
107      * always be returned. Otherwise it is up to the DTMManager to return a
108      * new instance or an instance that it already created and may be being used
109      * by someone else.
110      * (I think more parameters will need to be added for error handling, and
111      * entity resolution).
112      *
113      * @param source the specification of the source object.
114      * @param unique true if the returned DTM must be unique, probably because it
115      * is going to be mutated.
116      * @param whiteSpaceFilter Enables filtering of whitespace nodes, and may
117      * be null.
118      * @param incremental true if the DTM should be built incrementally, if
119      * possible.
120      * @param doIndexing true if the caller considers it worth it to use
121      * indexing schemes.
122      *
123      * @return a non-null DTM reference.
124      */

125     public DTM getDTM(Source JavaDoc 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     /**
134      * Get an instance of a DTM, loaded with the content from the
135      * specified source. If the unique flag is true, a new instance will
136      * always be returned. Otherwise it is up to the DTMManager to return a
137      * new instance or an instance that it already created and may be being used
138      * by someone else.
139      * (I think more parameters will need to be added for error handling, and
140      * entity resolution).
141      *
142      * @param source the specification of the source object.
143      * @param unique true if the returned DTM must be unique, probably because it
144      * is going to be mutated.
145      * @param whiteSpaceFilter Enables filtering of whitespace nodes, and may
146      * be null.
147      * @param incremental true if the DTM should be built incrementally, if
148      * possible.
149      * @param doIndexing true if the caller considers it worth it to use
150      * indexing schemes.
151      * @param buildIdIndex true if the id index table should be built.
152      *
153      * @return a non-null DTM reference.
154      */

155     public DTM getDTM(Source JavaDoc 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     /**
164      * Get an instance of a DTM, loaded with the content from the
165      * specified source. If the unique flag is true, a new instance will
166      * always be returned. Otherwise it is up to the DTMManager to return a
167      * new instance or an instance that it already created and may be being used
168      * by someone else.
169      * (I think more parameters will need to be added for error handling, and
170      * entity resolution).
171      *
172      * @param source the specification of the source object.
173      * @param unique true if the returned DTM must be unique, probably because it
174      * is going to be mutated.
175      * @param whiteSpaceFilter Enables filtering of whitespace nodes, and may
176      * be null.
177      * @param incremental true if the DTM should be built incrementally, if
178      * possible.
179      * @param doIndexing true if the caller considers it worth it to use
180      * indexing schemes.
181      * @param buildIdIndex true if the id index table should be built.
182      * @param newNameTable true if we want to use a separate ExpandedNameTable
183      * for this DTM.
184      *
185      * @return a non-null DTM reference.
186      */

187   public DTM getDTM(Source JavaDoc 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   /**
197      * Get an instance of a DTM, loaded with the content from the
198      * specified source. If the unique flag is true, a new instance will
199      * always be returned. Otherwise it is up to the DTMManager to return a
200      * new instance or an instance that it already created and may be being used
201      * by someone else.
202      * (I think more parameters will need to be added for error handling, and
203      * entity resolution).
204      *
205      * @param source the specification of the source object.
206      * @param unique true if the returned DTM must be unique, probably because it
207      * is going to be mutated.
208      * @param whiteSpaceFilter Enables filtering of whitespace nodes, and may
209      * be null.
210      * @param incremental true if the DTM should be built incrementally, if
211      * possible.
212      * @param doIndexing true if the caller considers it worth it to use
213      * indexing schemes.
214      * @param hasUserReader true if <code>source</code> is a
215      * <code>SAXSource</code> object that has an
216      * <code>XMLReader</code>, that was specified by the
217      * user.
218      * @param size Specifies initial size of tables that represent the DTM
219      * @param buildIdIndex true if the id index table should be built.
220      *
221      * @return a non-null DTM reference.
222      */

223     public DTM getDTM(Source JavaDoc 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   /**
234      * Get an instance of a DTM, loaded with the content from the
235      * specified source. If the unique flag is true, a new instance will
236      * always be returned. Otherwise it is up to the DTMManager to return a
237      * new instance or an instance that it already created and may be being used
238      * by someone else.
239      * (I think more parameters will need to be added for error handling, and
240      * entity resolution).
241      *
242      * @param source the specification of the source object.
243      * @param unique true if the returned DTM must be unique, probably because it
244      * is going to be mutated.
245      * @param whiteSpaceFilter Enables filtering of whitespace nodes, and may
246      * be null.
247      * @param incremental true if the DTM should be built incrementally, if
248      * possible.
249      * @param doIndexing true if the caller considers it worth it to use
250      * indexing schemes.
251      * @param hasUserReader true if <code>source</code> is a
252      * <code>SAXSource</code> object that has an
253      * <code>XMLReader</code>, that was specified by the
254      * user.
255      * @param size Specifies initial size of tables that represent the DTM
256      * @param buildIdIndex true if the id index table should be built.
257      * @param newNameTable true if we want to use a separate ExpandedNameTable
258      * for this DTM.
259      *
260      * @return a non-null DTM reference.
261      */

262   public DTM getDTM(Source JavaDoc 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 JavaDoc)
277         {
278             final DOMSource JavaDoc domsrc = (DOMSource JavaDoc) source;
279             final org.w3c.dom.Node JavaDoc 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 JavaDoc re) {
305                 throw re;
306             }
307             catch (Exception JavaDoc 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 JavaDoc) : true;
317             boolean isStreamSource = (null != source)
318                                   ? (source instanceof StreamSource JavaDoc) : false;
319
320             if (isSAXSource || isStreamSource) {
321                 XMLReader JavaDoc reader;
322                 InputSource JavaDoc xmlSource;
323
324                 if (null == source) {
325                     xmlSource = null;
326                     reader = null;
327                     hasUserReader = false; // Make sure the user didn't lie
328
}
329                 else {
330                     reader = getXMLReader(source);
331                     xmlSource = SAXSource.sourceToInputSource(source);
332
333                     String JavaDoc urlOfSource = xmlSource.getSystemId();
334
335                     if (null != urlOfSource) {
336                         try {
337                             urlOfSource = SystemIDResolver.getAbsoluteURI(urlOfSource);
338                         }
339                         catch (Exception JavaDoc e) {
340                             // %REVIEW% Is there a better way to send a warning?
341
System.err.println("Can not absolutize URL: " + urlOfSource);
342                         }
343
344                         xmlSource.setSystemId(urlOfSource);
345                     }
346                 }
347
348                 // Create the basic SAX2DTM.
349
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                 // Go ahead and add the DTM to the lookup table. This needs to be
361
// done before any parsing occurs. Note offset 0, since we've just
362
// created a new DTM.
363
addDTM(dtm, dtmPos, 0);
364
365                 if (null == reader) {
366                     // Then the user will construct it themselves.
367
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 JavaDoc e){}
384                 catch (SAXNotSupportedException JavaDoc e){}
385
386                 try {
387                     reader.parse(xmlSource);
388                 }
389                 catch (RuntimeException JavaDoc re) {
390                     throw re;
391                 }
392                 catch (Exception JavaDoc 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                 // It should have been handled by a derived class or the caller
409
// made a mistake.
410
throw new DTMException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NOT_SUPPORTED, new Object JavaDoc[]{source}));
411             }
412         }
413     }
414 }
415
Popular Tags