KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > 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.1.2.2 2006/10/03 13:52:11 spericas Exp $
18  */

19 package com.sun.org.apache.xalan.internal.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 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 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         "com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager";
50
51     private static final String JavaDoc DEFAULT_PROP_NAME =
52         "com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager";
53
54     private static final String JavaDoc NAMESPACE_PREFIXES_FEATURE =
55         "http://xml.org/sax/features/namespace-prefixes";
56     
57     /** Set this to true if you want a dump of the DTM after creation */
58     private static final boolean DUMPTREE = false;
59   
60     /** Set this to true if you want basic diagnostics */
61     private static final boolean DEBUG = false;
62
63     /**
64      * Constructor DTMManagerDefault
65      *
66      */

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

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

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

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

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

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

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

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