6
|
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;
|
12458
|
29 |
import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
|
16953
|
30 |
import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
|
6
|
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 |
/** The default property name to load the manager. */
|
|
56 |
private static final String defaultPropName =
|
|
57 |
"com.sun.org.apache.xml.internal.dtm.DTMManager";
|
|
58 |
|
|
59 |
/** The default class name to use as the manager. */
|
|
60 |
private static String defaultClassName =
|
|
61 |
"com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault";
|
|
62 |
|
|
63 |
/**
|
|
64 |
* Factory for creating XMLString objects.
|
|
65 |
* %TBD% Make this set by the caller.
|
|
66 |
*/
|
|
67 |
protected XMLStringFactory m_xsf = null;
|
|
68 |
|
12458
|
69 |
private boolean _useServicesMechanism;
|
6
|
70 |
/**
|
|
71 |
* Default constructor is protected on purpose.
|
|
72 |
*/
|
|
73 |
protected DTMManager(){}
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Get the XMLStringFactory used for the DTMs.
|
|
77 |
*
|
|
78 |
*
|
|
79 |
* @return a valid XMLStringFactory object, or null if it hasn't been set yet.
|
|
80 |
*/
|
|
81 |
public XMLStringFactory getXMLStringFactory()
|
|
82 |
{
|
|
83 |
return m_xsf;
|
|
84 |
}
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Set the XMLStringFactory used for the DTMs.
|
|
88 |
*
|
|
89 |
*
|
|
90 |
* @param xsf a valid XMLStringFactory object, should not be null.
|
|
91 |
*/
|
|
92 |
public void setXMLStringFactory(XMLStringFactory xsf)
|
|
93 |
{
|
|
94 |
m_xsf = xsf;
|
|
95 |
}
|
|
96 |
|
|
97 |
/**
|
|
98 |
* Obtain a new instance of a <code>DTMManager</code>.
|
|
99 |
* This static method creates a new factory instance
|
|
100 |
* This method uses the following ordered lookup procedure to determine
|
|
101 |
* the <code>DTMManager</code> implementation class to
|
|
102 |
* load:
|
|
103 |
* <ul>
|
|
104 |
* <li>
|
|
105 |
* Use the <code>com.sun.org.apache.xml.internal.dtm.DTMManager</code> system
|
|
106 |
* property.
|
|
107 |
* </li>
|
|
108 |
* <li>
|
|
109 |
* Use the JAVA_HOME(the parent directory where jdk is
|
|
110 |
* installed)/lib/xalan.properties for a property file that contains the
|
|
111 |
* name of the implementation class keyed on the same value as the
|
|
112 |
* system property defined above.
|
|
113 |
* </li>
|
|
114 |
* <li>
|
|
115 |
* Use the Services API (as detailed in the JAR specification), if
|
|
116 |
* available, to determine the classname. The Services API will look
|
|
117 |
* for a classname in the file
|
|
118 |
* <code>META-INF/services/com.sun.org.apache.xml.internal.dtm.DTMManager</code>
|
|
119 |
* in jars available to the runtime.
|
|
120 |
* </li>
|
|
121 |
* <li>
|
|
122 |
* Use the default <code>DTMManager</code> classname, which is
|
|
123 |
* <code>com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault</code>.
|
|
124 |
* </li>
|
|
125 |
* </ul>
|
|
126 |
*
|
|
127 |
* Once an application has obtained a reference to a <code>
|
|
128 |
* DTMManager</code> it can use the factory to configure
|
|
129 |
* and obtain parser instances.
|
|
130 |
*
|
|
131 |
* @return new DTMManager instance, never null.
|
|
132 |
*
|
|
133 |
* @throws DTMConfigurationException
|
|
134 |
* if the implementation is not available or cannot be instantiated.
|
|
135 |
*/
|
|
136 |
public static DTMManager newInstance(XMLStringFactory xsf)
|
|
137 |
throws DTMConfigurationException
|
|
138 |
{
|
12458
|
139 |
return newInstance(xsf, true);
|
|
140 |
}
|
|
141 |
|
|
142 |
public static DTMManager newInstance(XMLStringFactory xsf, boolean useServicesMechanism)
|
|
143 |
throws DTMConfigurationException
|
|
144 |
{
|
6
|
145 |
DTMManager factoryImpl = null;
|
|
146 |
try
|
|
147 |
{
|
12458
|
148 |
if (useServicesMechanism) {
|
|
149 |
factoryImpl = (DTMManager) ObjectFactory
|
|
150 |
.createObject(defaultPropName, defaultClassName);
|
|
151 |
} else {
|
|
152 |
factoryImpl = new com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault();
|
|
153 |
}
|
6
|
154 |
}
|
12458
|
155 |
catch (ConfigurationError e)
|
6
|
156 |
{
|
|
157 |
throw new DTMConfigurationException(XMLMessages.createXMLMessage(
|
|
158 |
XMLErrorResources.ER_NO_DEFAULT_IMPL, null), e.getException());
|
|
159 |
//"No default implementation found");
|
|
160 |
}
|
|
161 |
|
|
162 |
if (factoryImpl == null)
|
|
163 |
{
|
|
164 |
throw new DTMConfigurationException(XMLMessages.createXMLMessage(
|
|
165 |
XMLErrorResources.ER_NO_DEFAULT_IMPL, null));
|
|
166 |
//"No default implementation found");
|
|
167 |
}
|
|
168 |
|
|
169 |
factoryImpl.setXMLStringFactory(xsf);
|
|
170 |
|
|
171 |
return factoryImpl;
|
|
172 |
}
|
|
173 |
|
|
174 |
/**
|
|
175 |
* Get an instance of a DTM, loaded with the content from the
|
|
176 |
* specified source. If the unique flag is true, a new instance will
|
|
177 |
* always be returned. Otherwise it is up to the DTMManager to return a
|
|
178 |
* new instance or an instance that it already created and may be being used
|
|
179 |
* by someone else.
|
|
180 |
*
|
|
181 |
* (More parameters may eventually need to be added for error handling
|
|
182 |
* and entity resolution, and to better control selection of implementations.)
|
|
183 |
*
|
|
184 |
* @param source the specification of the source object, which may be null,
|
|
185 |
* in which case it is assumed that node construction will take
|
|
186 |
* by some other means.
|
|
187 |
* @param unique true if the returned DTM must be unique, probably because it
|
|
188 |
* is going to be mutated.
|
|
189 |
* @param whiteSpaceFilter Enables filtering of whitespace nodes, and may
|
|
190 |
* be null.
|
|
191 |
* @param incremental true if the DTM should be built incrementally, if
|
|
192 |
* possible.
|
|
193 |
* @param doIndexing true if the caller considers it worth it to use
|
|
194 |
* indexing schemes.
|
|
195 |
*
|
|
196 |
* @return a non-null DTM reference.
|
|
197 |
*/
|
|
198 |
public abstract DTM getDTM(javax.xml.transform.Source source,
|
|
199 |
boolean unique, DTMWSFilter whiteSpaceFilter,
|
|
200 |
boolean incremental, boolean doIndexing);
|
|
201 |
|
|
202 |
/**
|
|
203 |
* Get the instance of DTM that "owns" a node handle.
|
|
204 |
*
|
|
205 |
* @param nodeHandle the nodeHandle.
|
|
206 |
*
|
|
207 |
* @return a non-null DTM reference.
|
|
208 |
*/
|
|
209 |
public abstract DTM getDTM(int nodeHandle);
|
|
210 |
|
|
211 |
/**
|
|
212 |
* Given a W3C DOM node, try and return a DTM handle.
|
|
213 |
* Note: calling this may be non-optimal.
|
|
214 |
*
|
|
215 |
* @param node Non-null reference to a DOM node.
|
|
216 |
*
|
|
217 |
* @return a valid DTM handle.
|
|
218 |
*/
|
|
219 |
public abstract int getDTMHandleFromNode(org.w3c.dom.Node node);
|
|
220 |
|
|
221 |
/**
|
|
222 |
* Creates a DTM representing an empty <code>DocumentFragment</code> object.
|
|
223 |
* @return a non-null DTM reference.
|
|
224 |
*/
|
|
225 |
public abstract DTM createDocumentFragment();
|
|
226 |
|
|
227 |
/**
|
|
228 |
* Release a DTM either to a lru pool, or completely remove reference.
|
|
229 |
* DTMs without system IDs are always hard deleted.
|
|
230 |
* State: experimental.
|
|
231 |
*
|
|
232 |
* @param dtm The DTM to be released.
|
|
233 |
* @param shouldHardDelete True if the DTM should be removed no matter what.
|
|
234 |
* @return true if the DTM was removed, false if it was put back in a lru pool.
|
|
235 |
*/
|
|
236 |
public abstract boolean release(DTM dtm, boolean shouldHardDelete);
|
|
237 |
|
|
238 |
/**
|
|
239 |
* Create a new <code>DTMIterator</code> based on an XPath
|
|
240 |
* <a href="http://www.w3.org/TR/xpath#NT-LocationPath>LocationPath</a> or
|
|
241 |
* a <a href="http://www.w3.org/TR/xpath#NT-UnionExpr">UnionExpr</a>.
|
|
242 |
*
|
|
243 |
* @param xpathCompiler ??? Somehow we need to pass in a subpart of the
|
|
244 |
* expression. I hate to do this with strings, since the larger expression
|
|
245 |
* has already been parsed.
|
|
246 |
*
|
|
247 |
* @param pos The position in the expression.
|
|
248 |
* @return The newly created <code>DTMIterator</code>.
|
|
249 |
*/
|
|
250 |
public abstract DTMIterator createDTMIterator(Object xpathCompiler,
|
|
251 |
int pos);
|
|
252 |
|
|
253 |
/**
|
|
254 |
* Create a new <code>DTMIterator</code> based on an XPath
|
|
255 |
* <a href="http://www.w3.org/TR/xpath#NT-LocationPath>LocationPath</a> or
|
|
256 |
* a <a href="http://www.w3.org/TR/xpath#NT-UnionExpr">UnionExpr</a>.
|
|
257 |
*
|
|
258 |
* @param xpathString Must be a valid string expressing a
|
|
259 |
* <a href="http://www.w3.org/TR/xpath#NT-LocationPath>LocationPath</a> or
|
|
260 |
* a <a href="http://www.w3.org/TR/xpath#NT-UnionExpr">UnionExpr</a>.
|
|
261 |
*
|
|
262 |
* @param presolver An object that can resolve prefixes to namespace URLs.
|
|
263 |
*
|
|
264 |
* @return The newly created <code>DTMIterator</code>.
|
|
265 |
*/
|
|
266 |
public abstract DTMIterator createDTMIterator(String xpathString,
|
|
267 |
PrefixResolver presolver);
|
|
268 |
|
|
269 |
/**
|
|
270 |
* Create a new <code>DTMIterator</code> based only on a whatToShow
|
|
271 |
* and a DTMFilter. The traversal semantics are defined as the
|
|
272 |
* descendant access.
|
|
273 |
* <p>
|
|
274 |
* Note that DTMIterators may not be an exact match to DOM
|
|
275 |
* NodeIterators. They are initialized and used in much the same way
|
|
276 |
* as a NodeIterator, but their response to document mutation is not
|
|
277 |
* currently defined.
|
|
278 |
*
|
|
279 |
* @param whatToShow This flag specifies which node types may appear in
|
|
280 |
* the logical view of the tree presented by the iterator. See the
|
|
281 |
* description of <code>NodeFilter</code> for the set of possible
|
|
282 |
* <code>SHOW_</code> values.These flags can be combined using
|
|
283 |
* <code>OR</code>.
|
|
284 |
* @param filter The <code>NodeFilter</code> to be used with this
|
|
285 |
* <code>DTMFilter</code>, or <code>null</code> to indicate no filter.
|
|
286 |
* @param entityReferenceExpansion The value of this flag determines
|
|
287 |
* whether entity reference nodes are expanded.
|
|
288 |
*
|
|
289 |
* @return The newly created <code>DTMIterator</code>.
|
|
290 |
*/
|
|
291 |
public abstract DTMIterator createDTMIterator(int whatToShow,
|
|
292 |
DTMFilter filter, boolean entityReferenceExpansion);
|
|
293 |
|
|
294 |
/**
|
|
295 |
* Create a new <code>DTMIterator</code> that holds exactly one node.
|
|
296 |
*
|
|
297 |
* @param node The node handle that the DTMIterator will iterate to.
|
|
298 |
*
|
|
299 |
* @return The newly created <code>DTMIterator</code>.
|
|
300 |
*/
|
|
301 |
public abstract DTMIterator createDTMIterator(int node);
|
|
302 |
|
|
303 |
/* Flag indicating whether an incremental transform is desired */
|
|
304 |
public boolean m_incremental = false;
|
|
305 |
|
|
306 |
/*
|
|
307 |
* Flag set by FEATURE_SOURCE_LOCATION.
|
|
308 |
* This feature specifies whether the transformation phase should
|
|
309 |
* keep track of line and column numbers for the input source
|
|
310 |
* document.
|
|
311 |
*/
|
|
312 |
public boolean m_source_location = false;
|
|
313 |
|
|
314 |
/**
|
|
315 |
* Get a flag indicating whether an incremental transform is desired
|
|
316 |
* @return incremental boolean.
|
|
317 |
*
|
|
318 |
*/
|
|
319 |
public boolean getIncremental()
|
|
320 |
{
|
|
321 |
return m_incremental;
|
|
322 |
}
|
|
323 |
|
|
324 |
/**
|
|
325 |
* Set a flag indicating whether an incremental transform is desired
|
|
326 |
* This flag should have the same value as the FEATURE_INCREMENTAL feature
|
|
327 |
* which is set by the TransformerFactory.setAttribut() method before a
|
|
328 |
* DTMManager is created
|
|
329 |
* @param incremental boolean to use to set m_incremental.
|
|
330 |
*
|
|
331 |
*/
|
|
332 |
public void setIncremental(boolean incremental)
|
|
333 |
{
|
|
334 |
m_incremental = incremental;
|
|
335 |
}
|
|
336 |
|
|
337 |
/**
|
|
338 |
* Get a flag indicating whether the transformation phase should
|
|
339 |
* keep track of line and column numbers for the input source
|
|
340 |
* document.
|
|
341 |
* @return source location boolean
|
|
342 |
*
|
|
343 |
*/
|
|
344 |
public boolean getSource_location()
|
|
345 |
{
|
|
346 |
return m_source_location;
|
|
347 |
}
|
|
348 |
|
|
349 |
/**
|
|
350 |
* Set a flag indicating whether the transformation phase should
|
|
351 |
* keep track of line and column numbers for the input source
|
|
352 |
* document.
|
|
353 |
* This flag should have the same value as the FEATURE_SOURCE_LOCATION feature
|
|
354 |
* which is set by the TransformerFactory.setAttribut() method before a
|
|
355 |
* DTMManager is created
|
|
356 |
* @param sourceLocation boolean to use to set m_source_location
|
|
357 |
*/
|
|
358 |
public void setSource_location(boolean sourceLocation){
|
|
359 |
m_source_location = sourceLocation;
|
|
360 |
}
|
|
361 |
|
12458
|
362 |
/**
|
|
363 |
* Return the state of the services mechanism feature.
|
|
364 |
*/
|
|
365 |
public boolean useServicesMechnism() {
|
|
366 |
return _useServicesMechanism;
|
|
367 |
}
|
|
368 |
|
|
369 |
/**
|
|
370 |
* Set the state of the services mechanism feature.
|
|
371 |
*/
|
|
372 |
public void setServicesMechnism(boolean flag) {
|
|
373 |
_useServicesMechanism = flag;
|
|
374 |
}
|
6
|
375 |
|
|
376 |
// -------------------- private methods --------------------
|
|
377 |
|
|
378 |
/**
|
|
379 |
* Temp debug code - this will be removed after we test everything
|
|
380 |
*/
|
|
381 |
private static boolean debug;
|
|
382 |
|
|
383 |
static
|
|
384 |
{
|
|
385 |
try
|
|
386 |
{
|
16953
|
387 |
debug = SecuritySupport.getSystemProperty("dtm.debug") != null;
|
6
|
388 |
}
|
|
389 |
catch (SecurityException ex){}
|
|
390 |
}
|
|
391 |
|
|
392 |
/** This value, set at compile time, controls how many bits of the
|
|
393 |
* DTM node identifier numbers are used to identify a node within a
|
|
394 |
* document, and thus sets the maximum number of nodes per
|
|
395 |
* document. The remaining bits are used to identify the DTM
|
|
396 |
* document which contains this node.
|
|
397 |
*
|
|
398 |
* If you change IDENT_DTM_NODE_BITS, be sure to rebuild _ALL_ the
|
|
399 |
* files which use it... including the IDKey testcases.
|
|
400 |
*
|
|
401 |
* (FuncGenerateKey currently uses the node identifier directly and
|
|
402 |
* thus is affected when this changes. The IDKEY results will still be
|
|
403 |
* _correct_ (presuming no other breakage), but simple equality
|
|
404 |
* comparison against the previous "golden" files will probably
|
|
405 |
* complain.)
|
|
406 |
* */
|
|
407 |
public static final int IDENT_DTM_NODE_BITS = 16;
|
|
408 |
|
|
409 |
|
|
410 |
/** When this bitmask is ANDed with a DTM node handle number, the result
|
|
411 |
* is the low bits of the node's index number within that DTM. To obtain
|
|
412 |
* the high bits, add the DTM ID portion's offset as assigned in the DTM
|
|
413 |
* Manager.
|
|
414 |
*/
|
|
415 |
public static final int IDENT_NODE_DEFAULT = (1<<IDENT_DTM_NODE_BITS)-1;
|
|
416 |
|
|
417 |
|
|
418 |
/** When this bitmask is ANDed with a DTM node handle number, the result
|
|
419 |
* is the DTM's document identity number.
|
|
420 |
*/
|
|
421 |
public static final int IDENT_DTM_DEFAULT = ~IDENT_NODE_DEFAULT;
|
|
422 |
|
|
423 |
/** This is the maximum number of DTMs available. The highest DTM is
|
|
424 |
* one less than this.
|
|
425 |
*/
|
|
426 |
public static final int IDENT_MAX_DTMS = (IDENT_DTM_DEFAULT >>> IDENT_DTM_NODE_BITS) + 1;
|
|
427 |
|
|
428 |
|
|
429 |
/**
|
|
430 |
* %TBD% Doc
|
|
431 |
*
|
|
432 |
* NEEDSDOC @param dtm
|
|
433 |
*
|
|
434 |
* NEEDSDOC ($objectName$) @return
|
|
435 |
*/
|
|
436 |
public abstract int getDTMIdentity(DTM dtm);
|
|
437 |
|
|
438 |
/**
|
|
439 |
* %TBD% Doc
|
|
440 |
*
|
|
441 |
* NEEDSDOC ($objectName$) @return
|
|
442 |
*/
|
|
443 |
public int getDTMIdentityMask()
|
|
444 |
{
|
|
445 |
return IDENT_DTM_DEFAULT;
|
|
446 |
}
|
|
447 |
|
|
448 |
/**
|
|
449 |
* %TBD% Doc
|
|
450 |
*
|
|
451 |
* NEEDSDOC ($objectName$) @return
|
|
452 |
*/
|
|
453 |
public int getNodeIdentityMask()
|
|
454 |
{
|
|
455 |
return IDENT_NODE_DEFAULT;
|
|
456 |
}
|
|
457 |
|
12458
|
458 |
//
|
|
459 |
// Classes
|
|
460 |
//
|
|
461 |
|
|
462 |
/**
|
|
463 |
* A configuration error.
|
|
464 |
* Originally in ObjectFactory. This is the only portion used in this package
|
|
465 |
*/
|
|
466 |
static class ConfigurationError
|
|
467 |
extends Error {
|
|
468 |
static final long serialVersionUID = 5122054096615067992L;
|
|
469 |
//
|
|
470 |
// Data
|
|
471 |
//
|
|
472 |
|
|
473 |
/** Exception. */
|
|
474 |
private Exception exception;
|
|
475 |
|
|
476 |
//
|
|
477 |
// Constructors
|
|
478 |
//
|
|
479 |
|
|
480 |
/**
|
|
481 |
* Construct a new instance with the specified detail string and
|
|
482 |
* exception.
|
|
483 |
*/
|
|
484 |
ConfigurationError(String msg, Exception x) {
|
|
485 |
super(msg);
|
|
486 |
this.exception = x;
|
|
487 |
} // <init>(String,Exception)
|
|
488 |
|
|
489 |
//
|
|
490 |
// Public methods
|
|
491 |
//
|
|
492 |
|
|
493 |
/** Returns the exception associated to this error. */
|
|
494 |
Exception getException() {
|
|
495 |
return exception;
|
|
496 |
} // getException():Exception
|
|
497 |
|
|
498 |
} // class ConfigurationError
|
|
499 |
|
6
|
500 |
}
|