2
* Licensed to the Apache Software Foundation (ASF) under one
3
* or more contributor license agreements. See the NOTICE file
4
* distributed with this work for additional information
5
* regarding copyright ownership. The ASF licenses this file
6
* to you under the Apache License, Version 2.0 (the "License");
7
* you may not use this file except in compliance with the License.
8
* You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
19
* $Id: XRTreeFrag.java,v 1.2 2009/12/10 03:18:30 matthewoliver Exp $
21
package org.apache.xpath.objects;
23
import org.apache.xml.dtm.DTM;
24
import org.apache.xml.dtm.DTMIterator;
25
import org.apache.xml.utils.XMLString;
26
import org.apache.xpath.Expression;
27
import org.apache.xpath.ExpressionNode;
28
import org.apache.xpath.XPathContext;
29
import org.apache.xpath.axes.RTFIterator;
31
import org.w3c.dom.NodeList;
34
* This class represents an XPath result tree fragment object, and is capable of
35
* converting the RTF to other types, such as a string.
38
public class XRTreeFrag extends XObject implements Cloneable
40
static final long serialVersionUID = -3201553822254911567L;
41
private DTMXRTreeFrag m_DTMXRTreeFrag;
42
private int m_dtmRoot = DTM.NULL;
43
protected boolean m_allowRelease = false;
47
* Create an XRTreeFrag Object.
50
public XRTreeFrag(int root, XPathContext xctxt, ExpressionNode parent)
53
exprSetParent(parent);
58
* Create an XRTreeFrag Object.
61
public XRTreeFrag(int root, XPathContext xctxt)
67
private final void initDTM(int root, XPathContext xctxt){
69
final DTM dtm = xctxt.getDTM(root);
71
m_DTMXRTreeFrag = xctxt.getDTMXRTreeFrag(xctxt.getDTMIdentity(dtm));
76
* Return a java object that's closest to the representation
77
* that should be handed to an extension.
79
* @return The object that this class wraps
81
public Object object()
83
if (m_DTMXRTreeFrag.getXPathContext() != null)
84
return new org.apache.xml.dtm.ref.DTMNodeIterator((DTMIterator)(new org.apache.xpath.NodeSetDTM(m_dtmRoot, m_DTMXRTreeFrag.getXPathContext().getDTMManager())));
86
return super.object();
90
* Create an XRTreeFrag Object.
93
public XRTreeFrag(Expression expr)
99
* Specify if it's OK for detach to release the iterator for reuse.
101
* @param allowRelease true if it is OK for detach to release this iterator
104
public void allowDetachToRelease(boolean allowRelease)
106
m_allowRelease = allowRelease;
110
* Detaches the <code>DTMIterator</code> from the set which it iterated
111
* over, releasing any computational resources and placing the iterator
112
* in the INVALID state. After <code>detach</code> has been invoked,
113
* calls to <code>nextNode</code> or <code>previousNode</code> will
114
* raise a runtime exception.
116
* In general, detach should only be called once on the object.
118
public void detach(){
120
m_DTMXRTreeFrag.destruct();
126
* Tell what kind of class this is.
128
* @return type CLASS_RTREEFRAG
132
return CLASS_RTREEFRAG;
136
* Given a request type, return the equivalent string.
137
* For diagnostic purposes.
139
* @return type string "#RTREEFRAG"
141
public String getTypeString()
147
* Cast result object to a number.
149
* @return The result tree fragment as a number or NaN
152
throws javax.xml.transform.TransformerException
155
XMLString s = xstr();
161
* Cast result object to a boolean. This always returns true for a RTreeFrag
162
* because it is treated like a node-set with a single root node.
166
public boolean bool()
171
private XMLString m_xmlStr = null;
174
* Cast result object to an XMLString.
176
* @return The document fragment node data or the empty string.
178
public XMLString xstr()
181
m_xmlStr = m_DTMXRTreeFrag.getDTM().getStringValue(m_dtmRoot);
187
* Cast result object to a string.
189
* @return The string this wraps or the empty string if null
191
public void appendToFsb(org.apache.xml.utils.FastStringBuffer fsb)
193
XString xstring = (XString)xstr();
194
xstring.appendToFsb(fsb);
199
* Cast result object to a string.
201
* @return The document fragment node data or the empty string.
205
String str = m_DTMXRTreeFrag.getDTM().getStringValue(m_dtmRoot).toString();
207
return (null == str) ? "" : str;
211
* Cast result object to a result tree fragment.
213
* @return The document fragment this wraps
221
* Cast result object to a DTMIterator.
222
* dml - modified to return an RTFIterator for
223
* benefit of EXSLT object-type function in
224
* {@link org.apache.xalan.lib.ExsltCommon}.
225
* @return The document fragment as a DTMIterator
227
public DTMIterator asNodeIterator()
229
return new RTFIterator(m_dtmRoot, m_DTMXRTreeFrag.getXPathContext().getDTMManager());
233
* Cast result object to a nodelist. (special function).
235
* @return The document fragment as a nodelist
237
public NodeList convertToNodeset()
240
if (m_obj instanceof NodeList)
241
return (NodeList) m_obj;
243
return new org.apache.xml.dtm.ref.DTMNodeList(asNodeIterator());
247
* Tell if two objects are functionally equal.
249
* @param obj2 Object to compare this to
251
* @return True if the two objects are equal
253
* @throws javax.xml.transform.TransformerException
255
public boolean equals(XObject obj2)
260
if (XObject.CLASS_NODESET == obj2.getType())
263
// In order to handle the 'all' semantics of
264
// nodeset comparisons, we always call the
266
return obj2.equals(this);
268
else if (XObject.CLASS_BOOLEAN == obj2.getType())
270
return bool() == obj2.bool();
272
else if (XObject.CLASS_NUMBER == obj2.getType())
274
return num() == obj2.num();
276
else if (XObject.CLASS_NODESET == obj2.getType())
278
return xstr().equals(obj2.xstr());
280
else if (XObject.CLASS_STRING == obj2.getType())
282
return xstr().equals(obj2.xstr());
284
else if (XObject.CLASS_RTREEFRAG == obj2.getType())
287
// Probably not so good. Think about this.
288
return xstr().equals(obj2.xstr());
292
return super.equals(obj2);
295
catch(javax.xml.transform.TransformerException te)
297
throw new org.apache.xml.utils.WrappedRuntimeException(te);