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: XRTreeFrag.java,v 1.33 2005/01/24 04:04:40 mcnamara Exp $
018: */
019: package org.apache.xpath.objects;
020:
021: import org.apache.xml.dtm.DTM;
022: import org.apache.xml.dtm.DTMIterator;
023: import org.apache.xml.utils.XMLString;
024: import org.apache.xpath.Expression;
025: import org.apache.xpath.ExpressionNode;
026: import org.apache.xpath.XPathContext;
027: import org.apache.xpath.axes.RTFIterator;
028:
029: import org.w3c.dom.NodeList;
030:
031: /**
032: * This class represents an XPath result tree fragment object, and is capable of
033: * converting the RTF to other types, such as a string.
034: * @xsl.usage general
035: */
036: public class XRTreeFrag extends XObject implements Cloneable {
037: static final long serialVersionUID = -3201553822254911567L;
038: private DTMXRTreeFrag m_DTMXRTreeFrag;
039: private int m_dtmRoot = DTM.NULL;
040: protected boolean m_allowRelease = false;
041:
042: /**
043: * Create an XRTreeFrag Object.
044: *
045: */
046: public XRTreeFrag(int root, XPathContext xctxt,
047: ExpressionNode parent) {
048: super (null);
049: exprSetParent(parent);
050: initDTM(root, xctxt);
051: }
052:
053: /**
054: * Create an XRTreeFrag Object.
055: *
056: */
057: public XRTreeFrag(int root, XPathContext xctxt) {
058: super (null);
059: initDTM(root, xctxt);
060: }
061:
062: private final void initDTM(int root, XPathContext xctxt) {
063: m_dtmRoot = root;
064: final DTM dtm = xctxt.getDTM(root);
065: if (dtm != null) {
066: m_DTMXRTreeFrag = xctxt.getDTMXRTreeFrag(xctxt
067: .getDTMIdentity(dtm));
068: }
069: }
070:
071: /**
072: * Return a java object that's closest to the representation
073: * that should be handed to an extension.
074: *
075: * @return The object that this class wraps
076: */
077: public Object object() {
078: if (m_DTMXRTreeFrag.getXPathContext() != null)
079: return new org.apache.xml.dtm.ref.DTMNodeIterator(
080: (DTMIterator) (new org.apache.xpath.NodeSetDTM(
081: m_dtmRoot, m_DTMXRTreeFrag
082: .getXPathContext().getDTMManager())));
083: else
084: return super .object();
085: }
086:
087: /**
088: * Create an XRTreeFrag Object.
089: *
090: */
091: public XRTreeFrag(Expression expr) {
092: super (expr);
093: }
094:
095: /**
096: * Specify if it's OK for detach to release the iterator for reuse.
097: *
098: * @param allowRelease true if it is OK for detach to release this iterator
099: * for pooling.
100: */
101: public void allowDetachToRelease(boolean allowRelease) {
102: m_allowRelease = allowRelease;
103: }
104:
105: /**
106: * Detaches the <code>DTMIterator</code> from the set which it iterated
107: * over, releasing any computational resources and placing the iterator
108: * in the INVALID state. After <code>detach</code> has been invoked,
109: * calls to <code>nextNode</code> or <code>previousNode</code> will
110: * raise a runtime exception.
111: *
112: * In general, detach should only be called once on the object.
113: */
114: public void detach() {
115: if (m_allowRelease) {
116: m_DTMXRTreeFrag.destruct();
117: m_obj = null;
118: }
119: }
120:
121: /**
122: * Tell what kind of class this is.
123: *
124: * @return type CLASS_RTREEFRAG
125: */
126: public int getType() {
127: return CLASS_RTREEFRAG;
128: }
129:
130: /**
131: * Given a request type, return the equivalent string.
132: * For diagnostic purposes.
133: *
134: * @return type string "#RTREEFRAG"
135: */
136: public String getTypeString() {
137: return "#RTREEFRAG";
138: }
139:
140: /**
141: * Cast result object to a number.
142: *
143: * @return The result tree fragment as a number or NaN
144: */
145: public double num() throws javax.xml.transform.TransformerException {
146:
147: XMLString s = xstr();
148:
149: return s.toDouble();
150: }
151:
152: /**
153: * Cast result object to a boolean. This always returns true for a RTreeFrag
154: * because it is treated like a node-set with a single root node.
155: *
156: * @return true
157: */
158: public boolean bool() {
159: return true;
160: }
161:
162: private XMLString m_xmlStr = null;
163:
164: /**
165: * Cast result object to an XMLString.
166: *
167: * @return The document fragment node data or the empty string.
168: */
169: public XMLString xstr() {
170: if (null == m_xmlStr)
171: m_xmlStr = m_DTMXRTreeFrag.getDTM().getStringValue(
172: m_dtmRoot);
173:
174: return m_xmlStr;
175: }
176:
177: /**
178: * Cast result object to a string.
179: *
180: * @return The string this wraps or the empty string if null
181: */
182: public void appendToFsb(org.apache.xml.utils.FastStringBuffer fsb) {
183: XString xstring = (XString) xstr();
184: xstring.appendToFsb(fsb);
185: }
186:
187: /**
188: * Cast result object to a string.
189: *
190: * @return The document fragment node data or the empty string.
191: */
192: public String str() {
193: String str = m_DTMXRTreeFrag.getDTM().getStringValue(m_dtmRoot)
194: .toString();
195:
196: return (null == str) ? "" : str;
197: }
198:
199: /**
200: * Cast result object to a result tree fragment.
201: *
202: * @return The document fragment this wraps
203: */
204: public int rtf() {
205: return m_dtmRoot;
206: }
207:
208: /**
209: * Cast result object to a DTMIterator.
210: * dml - modified to return an RTFIterator for
211: * benefit of EXSLT object-type function in
212: * {@link org.apache.xalan.lib.ExsltCommon}.
213: * @return The document fragment as a DTMIterator
214: */
215: public DTMIterator asNodeIterator() {
216: return new RTFIterator(m_dtmRoot, m_DTMXRTreeFrag
217: .getXPathContext().getDTMManager());
218: }
219:
220: /**
221: * Cast result object to a nodelist. (special function).
222: *
223: * @return The document fragment as a nodelist
224: */
225: public NodeList convertToNodeset() {
226:
227: if (m_obj instanceof NodeList)
228: return (NodeList) m_obj;
229: else
230: return new org.apache.xml.dtm.ref.DTMNodeList(
231: asNodeIterator());
232: }
233:
234: /**
235: * Tell if two objects are functionally equal.
236: *
237: * @param obj2 Object to compare this to
238: *
239: * @return True if the two objects are equal
240: *
241: * @throws javax.xml.transform.TransformerException
242: */
243: public boolean equals(XObject obj2) {
244:
245: try {
246: if (XObject.CLASS_NODESET == obj2.getType()) {
247:
248: // In order to handle the 'all' semantics of
249: // nodeset comparisons, we always call the
250: // nodeset function.
251: return obj2.equals(this );
252: } else if (XObject.CLASS_BOOLEAN == obj2.getType()) {
253: return bool() == obj2.bool();
254: } else if (XObject.CLASS_NUMBER == obj2.getType()) {
255: return num() == obj2.num();
256: } else if (XObject.CLASS_NODESET == obj2.getType()) {
257: return xstr().equals(obj2.xstr());
258: } else if (XObject.CLASS_STRING == obj2.getType()) {
259: return xstr().equals(obj2.xstr());
260: } else if (XObject.CLASS_RTREEFRAG == obj2.getType()) {
261:
262: // Probably not so good. Think about this.
263: return xstr().equals(obj2.xstr());
264: } else {
265: return super .equals(obj2);
266: }
267: } catch (javax.xml.transform.TransformerException te) {
268: throw new org.apache.xml.utils.WrappedRuntimeException(te);
269: }
270: }
271:
272: }
|