View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 1999-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  /*
21   * $Id: DTMManager.java,v 1.2.4.1 2005/09/15 08:14:54 suresh_emailid Exp $
22   */
23  package com.sun.org.apache.xml.internal.dtm;
24  
25  import com.sun.org.apache.xml.internal.res.XMLErrorResources;
26  import com.sun.org.apache.xml.internal.res.XMLMessages;
27  import com.sun.org.apache.xml.internal.utils.PrefixResolver;
28  import com.sun.org.apache.xml.internal.utils.XMLStringFactory;
29  import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
30  import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
31  
32  /**
33   * A DTMManager instance can be used to create DTM and
34   * DTMIterator objects, and manage the DTM objects in the system.
35   *
36   * <p>The system property that determines which Factory implementation
37   * to create is named "com.sun.org.apache.xml.internal.utils.DTMFactory". This
38   * property names a concrete subclass of the DTMFactory abstract
39   *  class. If the property is not defined, a platform default is be used.</p>
40   *
41   * <p>An instance of this class <emph>must</emph> be safe to use across
42   * thread instances.  It is expected that a client will create a single instance
43   * of a DTMManager to use across multiple threads.  This will allow sharing
44   * of DTMs across multiple processes.</p>
45   *
46   * <p>Note: this class is incomplete right now.  It will be pretty much
47   * modeled after javax.xml.transform.TransformerFactory in terms of its
48   * factory support.</p>
49   *
50   * <p>State: In progress!!</p>
51   */
52  public abstract class DTMManager
53  {
54  
55    /**
56     * Factory for creating XMLString objects.
57     *  %TBD% Make this set by the caller.
58     */
59    protected XMLStringFactory m_xsf = null;
60  
61    private boolean _useServicesMechanism;
62    /**
63     * Default constructor is protected on purpose.
64     */
65    protected DTMManager(){}
66  
67    /**
68     * Get the XMLStringFactory used for the DTMs.
69     *
70     *
71     * @return a valid XMLStringFactory object, or null if it hasn't been set yet.
72     */
73    public XMLStringFactory getXMLStringFactory()
74    {
75      return m_xsf;
76    }
77  
78    /**
79     * Set the XMLStringFactory used for the DTMs.
80     *
81     *
82     * @param xsf a valid XMLStringFactory object, should not be null.
83     */
84    public void setXMLStringFactory(XMLStringFactory xsf)
85    {
86      m_xsf = xsf;
87    }
88  
89    /**
90     * Obtain a new instance of a <code>DTMManager</code>.
91     * This static method creates a new factory instance
92     * using the default <code>DTMManager</code> implementation, which is
93     * <code>com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault</code>.
94     * </li>
95     * </ul>
96     *
97     * Once an application has obtained a reference to a <code>
98     * DTMManager</code> it can use the factory to configure
99     * and obtain parser instances.
100    *
101    * @return new DTMManager instance, never null.
102    *
103    * @throws DTMConfigurationException
104    * if the implementation is not available or cannot be instantiated.
105    */
106   public static DTMManager newInstance(XMLStringFactory xsf)
107            throws DTMConfigurationException
108   {
109       final DTMManager factoryImpl = new com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault();
110       factoryImpl.setXMLStringFactory(xsf);
111 
112       return factoryImpl;
113   }
114 
115   /**
116    * Get an instance of a DTM, loaded with the content from the
117    * specified source.  If the unique flag is true, a new instance will
118    * always be returned.  Otherwise it is up to the DTMManager to return a
119    * new instance or an instance that it already created and may be being used
120    * by someone else.
121    *
122    * (More parameters may eventually need to be added for error handling
123    * and entity resolution, and to better control selection of implementations.)
124    *
125    * @param source the specification of the source object, which may be null,
126    *               in which case it is assumed that node construction will take
127    *               by some other means.
128    * @param unique true if the returned DTM must be unique, probably because it
129    * is going to be mutated.
130    * @param whiteSpaceFilter Enables filtering of whitespace nodes, and may
131    *                         be null.
132    * @param incremental true if the DTM should be built incrementally, if
133    *                    possible.
134    * @param doIndexing true if the caller considers it worth it to use
135    *                   indexing schemes.
136    *
137    * @return a non-null DTM reference.
138    */
139   public abstract DTM getDTM(javax.xml.transform.Source source,
140                              boolean unique, DTMWSFilter whiteSpaceFilter,
141                              boolean incremental, boolean doIndexing);
142 
143   /**
144    * Get the instance of DTM that "owns" a node handle.
145    *
146    * @param nodeHandle the nodeHandle.
147    *
148    * @return a non-null DTM reference.
149    */
150   public abstract DTM getDTM(int nodeHandle);
151 
152   /**
153    * Given a W3C DOM node, try and return a DTM handle.
154    * Note: calling this may be non-optimal.
155    *
156    * @param node Non-null reference to a DOM node.
157    *
158    * @return a valid DTM handle.
159    */
160   public abstract int getDTMHandleFromNode(org.w3c.dom.Node node);
161 
162   /**
163    * Creates a DTM representing an empty <code>DocumentFragment</code> object.
164    * @return a non-null DTM reference.
165    */
166   public abstract DTM createDocumentFragment();
167 
168   /**
169    * Release a DTM either to a lru pool, or completely remove reference.
170    * DTMs without system IDs are always hard deleted.
171    * State: experimental.
172    *
173    * @param dtm The DTM to be released.
174    * @param shouldHardDelete True if the DTM should be removed no matter what.
175    * @return true if the DTM was removed, false if it was put back in a lru pool.
176    */
177   public abstract boolean release(DTM dtm, boolean shouldHardDelete);
178 
179   /**
180    * Create a new <code>DTMIterator</code> based on an XPath
181    * <a href="http://www.w3.org/TR/xpath#NT-LocationPath>LocationPath</a> or
182    * a <a href="http://www.w3.org/TR/xpath#NT-UnionExpr">UnionExpr</a>.
183    *
184    * @param xpathCompiler ??? Somehow we need to pass in a subpart of the
185    * expression.  I hate to do this with strings, since the larger expression
186    * has already been parsed.
187    *
188    * @param pos The position in the expression.
189    * @return The newly created <code>DTMIterator</code>.
190    */
191   public abstract DTMIterator createDTMIterator(Object xpathCompiler,
192           int pos);
193 
194   /**
195    * Create a new <code>DTMIterator</code> based on an XPath
196    * <a href="http://www.w3.org/TR/xpath#NT-LocationPath>LocationPath</a> or
197    * a <a href="http://www.w3.org/TR/xpath#NT-UnionExpr">UnionExpr</a>.
198    *
199    * @param xpathString Must be a valid string expressing a
200    * <a href="http://www.w3.org/TR/xpath#NT-LocationPath>LocationPath</a> or
201    * a <a href="http://www.w3.org/TR/xpath#NT-UnionExpr">UnionExpr</a>.
202    *
203    * @param presolver An object that can resolve prefixes to namespace URLs.
204    *
205    * @return The newly created <code>DTMIterator</code>.
206    */
207   public abstract DTMIterator createDTMIterator(String xpathString,
208           PrefixResolver presolver);
209 
210   /**
211    * Create a new <code>DTMIterator</code> based only on a whatToShow
212    * and a DTMFilter.  The traversal semantics are defined as the
213    * descendant access.
214    * <p>
215    * Note that DTMIterators may not be an exact match to DOM
216    * NodeIterators. They are initialized and used in much the same way
217    * as a NodeIterator, but their response to document mutation is not
218    * currently defined.
219    *
220    * @param whatToShow This flag specifies which node types may appear in
221    *   the logical view of the tree presented by the iterator. See the
222    *   description of <code>NodeFilter</code> for the set of possible
223    *   <code>SHOW_</code> values.These flags can be combined using
224    *   <code>OR</code>.
225    * @param filter The <code>NodeFilter</code> to be used with this
226    *   <code>DTMFilter</code>, or <code>null</code> to indicate no filter.
227    * @param entityReferenceExpansion The value of this flag determines
228    *   whether entity reference nodes are expanded.
229    *
230    * @return The newly created <code>DTMIterator</code>.
231    */
232   public abstract DTMIterator createDTMIterator(int whatToShow,
233           DTMFilter filter, boolean entityReferenceExpansion);
234 
235   /**
236    * Create a new <code>DTMIterator</code> that holds exactly one node.
237    *
238    * @param node The node handle that the DTMIterator will iterate to.
239    *
240    * @return The newly created <code>DTMIterator</code>.
241    */
242   public abstract DTMIterator createDTMIterator(int node);
243 
244   /* Flag indicating whether an incremental transform is desired */
245   public boolean m_incremental = false;
246 
247   /*
248    * Flag set by FEATURE_SOURCE_LOCATION.
249    * This feature specifies whether the transformation phase should
250    * keep track of line and column numbers for the input source
251    * document.
252    */
253   public boolean m_source_location = false;
254 
255   /**
256    * Get a flag indicating whether an incremental transform is desired
257    * @return incremental boolean.
258    *
259    */
260   public boolean getIncremental()
261   {
262     return m_incremental;
263   }
264 
265   /**
266    * Set a flag indicating whether an incremental transform is desired
267    * This flag should have the same value as the FEATURE_INCREMENTAL feature
268    * which is set by the TransformerFactory.setAttribut() method before a
269    * DTMManager is created
270    * @param incremental boolean to use to set m_incremental.
271    *
272    */
273   public void setIncremental(boolean incremental)
274   {
275     m_incremental = incremental;
276   }
277 
278   /**
279    * Get a flag indicating whether the transformation phase should
280    * keep track of line and column numbers for the input source
281    * document.
282    * @return source location boolean
283    *
284    */
285   public boolean getSource_location()
286   {
287     return m_source_location;
288   }
289 
290   /**
291    * Set a flag indicating whether the transformation phase should
292    * keep track of line and column numbers for the input source
293    * document.
294    * This flag should have the same value as the FEATURE_SOURCE_LOCATION feature
295    * which is set by the TransformerFactory.setAttribut() method before a
296    * DTMManager is created
297    * @param sourceLocation boolean to use to set m_source_location
298    */
299   public void setSource_location(boolean sourceLocation){
300     m_source_location = sourceLocation;
301   }
302 
303     /**
304      * Return the state of the services mechanism feature.
305      */
306     public boolean useServicesMechnism() {
307         return _useServicesMechanism;
308     }
309 
310     /**
311      * Set the state of the services mechanism feature.
312      */
313     public void setServicesMechnism(boolean flag) {
314         _useServicesMechanism = flag;
315     }
316 
317   // -------------------- private methods --------------------
318 
319    /**
320    * Temp debug code - this will be removed after we test everything
321    */
322   private static boolean debug;
323 
324   static
325   {
326     try
327     {
328       debug = SecuritySupport.getSystemProperty("dtm.debug") != null;
329     }
330     catch (SecurityException ex){}
331   }
332 
333   /** This value, set at compile time, controls how many bits of the
334    * DTM node identifier numbers are used to identify a node within a
335    * document, and thus sets the maximum number of nodes per
336    * document. The remaining bits are used to identify the DTM
337    * document which contains this node.
338    *
339    * If you change IDENT_DTM_NODE_BITS, be sure to rebuild _ALL_ the
340    * files which use it... including the IDKey testcases.
341    *
342    * (FuncGenerateKey currently uses the node identifier directly and
343    * thus is affected when this changes. The IDKEY results will still be
344    * _correct_ (presuming no other breakage), but simple equality
345    * comparison against the previous "golden" files will probably
346    * complain.)
347    * */
348   public static final int IDENT_DTM_NODE_BITS = 16;
349 
350 
351   /** When this bitmask is ANDed with a DTM node handle number, the result
352    * is the low bits of the node's index number within that DTM. To obtain
353    * the high bits, add the DTM ID portion's offset as assigned in the DTM
354    * Manager.
355    */
356   public static final int IDENT_NODE_DEFAULT = (1<<IDENT_DTM_NODE_BITS)-1;
357 
358 
359   /** When this bitmask is ANDed with a DTM node handle number, the result
360    * is the DTM's document identity number.
361    */
362   public static final int IDENT_DTM_DEFAULT = ~IDENT_NODE_DEFAULT;
363 
364   /** This is the maximum number of DTMs available.  The highest DTM is
365     * one less than this.
366    */
367   public static final int IDENT_MAX_DTMS = (IDENT_DTM_DEFAULT >>> IDENT_DTM_NODE_BITS) + 1;
368 
369 
370   /**
371    * %TBD% Doc
372    *
373    * NEEDSDOC @param dtm
374    *
375    * NEEDSDOC ($objectName$) @return
376    */
377   public abstract int getDTMIdentity(DTM dtm);
378 
379   /**
380    * %TBD% Doc
381    *
382    * NEEDSDOC ($objectName$) @return
383    */
384   public int getDTMIdentityMask()
385   {
386     return IDENT_DTM_DEFAULT;
387   }
388 
389   /**
390    * %TBD% Doc
391    *
392    * NEEDSDOC ($objectName$) @return
393    */
394   public int getNodeIdentityMask()
395   {
396     return IDENT_NODE_DEFAULT;
397   }
398 
399     //
400     // Classes
401     //
402 
403     /**
404      * A configuration error.
405      * Originally in ObjectFactory. This is the only portion used in this package
406      */
407     static class ConfigurationError
408         extends Error {
409                 static final long serialVersionUID = 5122054096615067992L;
410         //
411         // Data
412         //
413 
414         /** Exception. */
415         private Exception exception;
416 
417         //
418         // Constructors
419         //
420 
421         /**
422          * Construct a new instance with the specified detail string and
423          * exception.
424          */
425         ConfigurationError(String msg, Exception x) {
426             super(msg);
427             this.exception = x;
428         } // <init>(String,Exception)
429 
430         //
431         // Public methods
432         //
433 
434         /** Returns the exception associated to this error. */
435         Exception getException() {
436             return exception;
437         } // getException():Exception
438 
439     } // class ConfigurationError
440 
441 }