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: DTMManager.java,v 1.20 2005/01/24 04:04:40 mcnamara Exp $
018: */
019: package org.apache.xml.dtm;
020:
021: import org.apache.xml.res.XMLErrorResources;
022: import org.apache.xml.res.XMLMessages;
023: import org.apache.xml.utils.PrefixResolver;
024: import org.apache.xml.utils.XMLStringFactory;
025:
026: /**
027: * A DTMManager instance can be used to create DTM and
028: * DTMIterator objects, and manage the DTM objects in the system.
029: *
030: * <p>The system property that determines which Factory implementation
031: * to create is named "org.apache.xml.utils.DTMFactory". This
032: * property names a concrete subclass of the DTMFactory abstract
033: * class. If the property is not defined, a platform default is be used.</p>
034: *
035: * <p>An instance of this class <emph>must</emph> be safe to use across
036: * thread instances. It is expected that a client will create a single instance
037: * of a DTMManager to use across multiple threads. This will allow sharing
038: * of DTMs across multiple processes.</p>
039: *
040: * <p>Note: this class is incomplete right now. It will be pretty much
041: * modeled after javax.xml.transform.TransformerFactory in terms of its
042: * factory support.</p>
043: *
044: * <p>State: In progress!!</p>
045: */
046: public abstract class DTMManager {
047:
048: /** The default property name to load the manager. */
049: private static final String defaultPropName = "org.apache.xml.dtm.DTMManager";
050:
051: /** The default class name to use as the manager. */
052: private static String defaultClassName = "org.apache.xml.dtm.ref.DTMManagerDefault";
053:
054: /**
055: * Factory for creating XMLString objects.
056: * %TBD% Make this set by the caller.
057: */
058: protected XMLStringFactory m_xsf = null;
059:
060: /**
061: * Default constructor is protected on purpose.
062: */
063: protected DTMManager() {
064: }
065:
066: /**
067: * Get the XMLStringFactory used for the DTMs.
068: *
069: *
070: * @return a valid XMLStringFactory object, or null if it hasn't been set yet.
071: */
072: public XMLStringFactory getXMLStringFactory() {
073: return m_xsf;
074: }
075:
076: /**
077: * Set the XMLStringFactory used for the DTMs.
078: *
079: *
080: * @param xsf a valid XMLStringFactory object, should not be null.
081: */
082: public void setXMLStringFactory(XMLStringFactory xsf) {
083: m_xsf = xsf;
084: }
085:
086: /**
087: * Obtain a new instance of a <code>DTMManager</code>.
088: * This static method creates a new factory instance
089: * This method uses the following ordered lookup procedure to determine
090: * the <code>DTMManager</code> implementation class to
091: * load:
092: * <ul>
093: * <li>
094: * Use the <code>org.apache.xml.dtm.DTMManager</code> system
095: * property.
096: * </li>
097: * <li>
098: * Use the JAVA_HOME(the parent directory where jdk is
099: * installed)/lib/xalan.properties for a property file that contains the
100: * name of the implementation class keyed on the same value as the
101: * system property defined above.
102: * </li>
103: * <li>
104: * Use the Services API (as detailed in the JAR specification), if
105: * available, to determine the classname. The Services API will look
106: * for a classname in the file
107: * <code>META-INF/services/org.apache.xml.dtm.DTMManager</code>
108: * in jars available to the runtime.
109: * </li>
110: * <li>
111: * Use the default <code>DTMManager</code> classname, which is
112: * <code>org.apache.xml.dtm.ref.DTMManagerDefault</code>.
113: * </li>
114: * </ul>
115: *
116: * Once an application has obtained a reference to a <code>
117: * DTMManager</code> it can use the factory to configure
118: * and obtain parser instances.
119: *
120: * @return new DTMManager instance, never null.
121: *
122: * @throws DTMConfigurationException
123: * if the implementation is not available or cannot be instantiated.
124: */
125: public static DTMManager newInstance(XMLStringFactory xsf)
126: throws DTMConfigurationException {
127: DTMManager factoryImpl = null;
128: try {
129: factoryImpl = (DTMManager) ObjectFactory.createObject(
130: defaultPropName, defaultClassName);
131: } catch (ObjectFactory.ConfigurationError e) {
132: throw new DTMConfigurationException(
133: XMLMessages.createXMLMessage(
134: XMLErrorResources.ER_NO_DEFAULT_IMPL, null),
135: e.getException());
136: //"No default implementation found");
137: }
138:
139: if (factoryImpl == null) {
140: throw new DTMConfigurationException(XMLMessages
141: .createXMLMessage(
142: XMLErrorResources.ER_NO_DEFAULT_IMPL, null));
143: //"No default implementation found");
144: }
145:
146: factoryImpl.setXMLStringFactory(xsf);
147:
148: return factoryImpl;
149: }
150:
151: /**
152: * Get an instance of a DTM, loaded with the content from the
153: * specified source. If the unique flag is true, a new instance will
154: * always be returned. Otherwise it is up to the DTMManager to return a
155: * new instance or an instance that it already created and may be being used
156: * by someone else.
157: *
158: * (More parameters may eventually need to be added for error handling
159: * and entity resolution, and to better control selection of implementations.)
160: *
161: * @param source the specification of the source object, which may be null,
162: * in which case it is assumed that node construction will take
163: * by some other means.
164: * @param unique true if the returned DTM must be unique, probably because it
165: * is going to be mutated.
166: * @param whiteSpaceFilter Enables filtering of whitespace nodes, and may
167: * be null.
168: * @param incremental true if the DTM should be built incrementally, if
169: * possible.
170: * @param doIndexing true if the caller considers it worth it to use
171: * indexing schemes.
172: *
173: * @return a non-null DTM reference.
174: */
175: public abstract DTM getDTM(javax.xml.transform.Source source,
176: boolean unique, DTMWSFilter whiteSpaceFilter,
177: boolean incremental, boolean doIndexing);
178:
179: /**
180: * Get the instance of DTM that "owns" a node handle.
181: *
182: * @param nodeHandle the nodeHandle.
183: *
184: * @return a non-null DTM reference.
185: */
186: public abstract DTM getDTM(int nodeHandle);
187:
188: /**
189: * Given a W3C DOM node, try and return a DTM handle.
190: * Note: calling this may be non-optimal.
191: *
192: * @param node Non-null reference to a DOM node.
193: *
194: * @return a valid DTM handle.
195: */
196: public abstract int getDTMHandleFromNode(org.w3c.dom.Node node);
197:
198: /**
199: * Creates a DTM representing an empty <code>DocumentFragment</code> object.
200: * @return a non-null DTM reference.
201: */
202: public abstract DTM createDocumentFragment();
203:
204: /**
205: * Release a DTM either to a lru pool, or completely remove reference.
206: * DTMs without system IDs are always hard deleted.
207: * State: experimental.
208: *
209: * @param dtm The DTM to be released.
210: * @param shouldHardDelete True if the DTM should be removed no matter what.
211: * @return true if the DTM was removed, false if it was put back in a lru pool.
212: */
213: public abstract boolean release(DTM dtm, boolean shouldHardDelete);
214:
215: /**
216: * Create a new <code>DTMIterator</code> based on an XPath
217: * <a href="http://www.w3.org/TR/xpath#NT-LocationPath>LocationPath</a> or
218: * a <a href="http://www.w3.org/TR/xpath#NT-UnionExpr">UnionExpr</a>.
219: *
220: * @param xpathCompiler ??? Somehow we need to pass in a subpart of the
221: * expression. I hate to do this with strings, since the larger expression
222: * has already been parsed.
223: *
224: * @param pos The position in the expression.
225: * @return The newly created <code>DTMIterator</code>.
226: */
227: public abstract DTMIterator createDTMIterator(Object xpathCompiler,
228: int pos);
229:
230: /**
231: * Create a new <code>DTMIterator</code> based on an XPath
232: * <a href="http://www.w3.org/TR/xpath#NT-LocationPath>LocationPath</a> or
233: * a <a href="http://www.w3.org/TR/xpath#NT-UnionExpr">UnionExpr</a>.
234: *
235: * @param xpathString Must be a valid string expressing a
236: * <a href="http://www.w3.org/TR/xpath#NT-LocationPath>LocationPath</a> or
237: * a <a href="http://www.w3.org/TR/xpath#NT-UnionExpr">UnionExpr</a>.
238: *
239: * @param presolver An object that can resolve prefixes to namespace URLs.
240: *
241: * @return The newly created <code>DTMIterator</code>.
242: */
243: public abstract DTMIterator createDTMIterator(String xpathString,
244: PrefixResolver presolver);
245:
246: /**
247: * Create a new <code>DTMIterator</code> based only on a whatToShow
248: * and a DTMFilter. The traversal semantics are defined as the
249: * descendant access.
250: * <p>
251: * Note that DTMIterators may not be an exact match to DOM
252: * NodeIterators. They are initialized and used in much the same way
253: * as a NodeIterator, but their response to document mutation is not
254: * currently defined.
255: *
256: * @param whatToShow This flag specifies which node types may appear in
257: * the logical view of the tree presented by the iterator. See the
258: * description of <code>NodeFilter</code> for the set of possible
259: * <code>SHOW_</code> values.These flags can be combined using
260: * <code>OR</code>.
261: * @param filter The <code>NodeFilter</code> to be used with this
262: * <code>DTMFilter</code>, or <code>null</code> to indicate no filter.
263: * @param entityReferenceExpansion The value of this flag determines
264: * whether entity reference nodes are expanded.
265: *
266: * @return The newly created <code>DTMIterator</code>.
267: */
268: public abstract DTMIterator createDTMIterator(int whatToShow,
269: DTMFilter filter, boolean entityReferenceExpansion);
270:
271: /**
272: * Create a new <code>DTMIterator</code> that holds exactly one node.
273: *
274: * @param node The node handle that the DTMIterator will iterate to.
275: *
276: * @return The newly created <code>DTMIterator</code>.
277: */
278: public abstract DTMIterator createDTMIterator(int node);
279:
280: /* Flag indicating whether an incremental transform is desired */
281: public boolean m_incremental = false;
282:
283: /*
284: * Flag set by FEATURE_SOURCE_LOCATION.
285: * This feature specifies whether the transformation phase should
286: * keep track of line and column numbers for the input source
287: * document.
288: */
289: public boolean m_source_location = false;
290:
291: /**
292: * Get a flag indicating whether an incremental transform is desired
293: * @return incremental boolean.
294: *
295: */
296: public boolean getIncremental() {
297: return m_incremental;
298: }
299:
300: /**
301: * Set a flag indicating whether an incremental transform is desired
302: * This flag should have the same value as the FEATURE_INCREMENTAL feature
303: * which is set by the TransformerFactory.setAttribut() method before a
304: * DTMManager is created
305: * @param incremental boolean to use to set m_incremental.
306: *
307: */
308: public void setIncremental(boolean incremental) {
309: m_incremental = incremental;
310: }
311:
312: /**
313: * Get a flag indicating whether the transformation phase should
314: * keep track of line and column numbers for the input source
315: * document.
316: * @return source location boolean
317: *
318: */
319: public boolean getSource_location() {
320: return m_source_location;
321: }
322:
323: /**
324: * Set a flag indicating whether the transformation phase should
325: * keep track of line and column numbers for the input source
326: * document.
327: * This flag should have the same value as the FEATURE_SOURCE_LOCATION feature
328: * which is set by the TransformerFactory.setAttribut() method before a
329: * DTMManager is created
330: * @param sourceLocation boolean to use to set m_source_location
331: */
332: public void setSource_location(boolean sourceLocation) {
333: m_source_location = sourceLocation;
334: }
335:
336: // -------------------- private methods --------------------
337:
338: /**
339: * Temp debug code - this will be removed after we test everything
340: */
341: private static boolean debug;
342:
343: static {
344: try {
345: debug = System.getProperty("dtm.debug") != null;
346: } catch (SecurityException ex) {
347: }
348: }
349:
350: /** This value, set at compile time, controls how many bits of the
351: * DTM node identifier numbers are used to identify a node within a
352: * document, and thus sets the maximum number of nodes per
353: * document. The remaining bits are used to identify the DTM
354: * document which contains this node.
355: *
356: * If you change IDENT_DTM_NODE_BITS, be sure to rebuild _ALL_ the
357: * files which use it... including the IDKey testcases.
358: *
359: * (FuncGenerateKey currently uses the node identifier directly and
360: * thus is affected when this changes. The IDKEY results will still be
361: * _correct_ (presuming no other breakage), but simple equality
362: * comparison against the previous "golden" files will probably
363: * complain.)
364: * */
365: public static final int IDENT_DTM_NODE_BITS = 16;
366:
367: /** When this bitmask is ANDed with a DTM node handle number, the result
368: * is the low bits of the node's index number within that DTM. To obtain
369: * the high bits, add the DTM ID portion's offset as assigned in the DTM
370: * Manager.
371: */
372: public static final int IDENT_NODE_DEFAULT = (1 << IDENT_DTM_NODE_BITS) - 1;
373:
374: /** When this bitmask is ANDed with a DTM node handle number, the result
375: * is the DTM's document identity number.
376: */
377: public static final int IDENT_DTM_DEFAULT = ~IDENT_NODE_DEFAULT;
378:
379: /** This is the maximum number of DTMs available. The highest DTM is
380: * one less than this.
381: */
382: public static final int IDENT_MAX_DTMS = (IDENT_DTM_DEFAULT >>> IDENT_DTM_NODE_BITS) + 1;
383:
384: /**
385: * %TBD% Doc
386: *
387: * NEEDSDOC @param dtm
388: *
389: * NEEDSDOC ($objectName$) @return
390: */
391: public abstract int getDTMIdentity(DTM dtm);
392:
393: /**
394: * %TBD% Doc
395: *
396: * NEEDSDOC ($objectName$) @return
397: */
398: public int getDTMIdentityMask() {
399: return IDENT_DTM_DEFAULT;
400: }
401:
402: /**
403: * %TBD% Doc
404: *
405: * NEEDSDOC ($objectName$) @return
406: */
407: public int getNodeIdentityMask() {
408: return IDENT_NODE_DEFAULT;
409: }
410:
411: }
|