12005
|
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: XRTreeFrag.java,v 1.2.4.1 2005/09/14 20:44:48 jeffsuttor Exp $
|
|
22 |
*/
|
|
23 |
package com.sun.org.apache.xpath.internal.objects;
|
|
24 |
|
|
25 |
import com.sun.org.apache.xml.internal.dtm.DTM;
|
|
26 |
import com.sun.org.apache.xml.internal.dtm.DTMIterator;
|
|
27 |
import com.sun.org.apache.xml.internal.utils.XMLString;
|
|
28 |
import com.sun.org.apache.xpath.internal.Expression;
|
|
29 |
import com.sun.org.apache.xpath.internal.ExpressionNode;
|
|
30 |
import com.sun.org.apache.xpath.internal.XPathContext;
|
|
31 |
import com.sun.org.apache.xpath.internal.axes.RTFIterator;
|
|
32 |
|
|
33 |
import org.w3c.dom.NodeList;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* This class represents an XPath result tree fragment object, and is capable of
|
|
37 |
* converting the RTF to other types, such as a string.
|
|
38 |
* @xsl.usage general
|
|
39 |
*/
|
|
40 |
public class XRTreeFrag extends XObject implements Cloneable
|
|
41 |
{
|
|
42 |
static final long serialVersionUID = -3201553822254911567L;
|
|
43 |
private DTMXRTreeFrag m_DTMXRTreeFrag;
|
|
44 |
private int m_dtmRoot = DTM.NULL;
|
|
45 |
protected boolean m_allowRelease = false;
|
|
46 |
|
|
47 |
|
|
48 |
/**
|
|
49 |
* Create an XRTreeFrag Object.
|
|
50 |
*
|
|
51 |
*/
|
|
52 |
public XRTreeFrag(int root, XPathContext xctxt, ExpressionNode parent)
|
|
53 |
{
|
|
54 |
super(null);
|
|
55 |
exprSetParent(parent);
|
|
56 |
initDTM(root, xctxt);
|
|
57 |
}
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Create an XRTreeFrag Object.
|
|
61 |
*
|
|
62 |
*/
|
|
63 |
public XRTreeFrag(int root, XPathContext xctxt)
|
|
64 |
{
|
|
65 |
super(null);
|
|
66 |
initDTM(root, xctxt);
|
|
67 |
}
|
|
68 |
|
|
69 |
private final void initDTM(int root, XPathContext xctxt){
|
|
70 |
m_dtmRoot = root;
|
|
71 |
final DTM dtm = xctxt.getDTM(root);
|
|
72 |
if(dtm != null){
|
|
73 |
m_DTMXRTreeFrag = xctxt.getDTMXRTreeFrag(xctxt.getDTMIdentity(dtm));
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
/**
|
|
78 |
* Return a java object that's closest to the representation
|
|
79 |
* that should be handed to an extension.
|
|
80 |
*
|
|
81 |
* @return The object that this class wraps
|
|
82 |
*/
|
|
83 |
public Object object()
|
|
84 |
{
|
|
85 |
if (m_DTMXRTreeFrag.getXPathContext() != null)
|
|
86 |
return new com.sun.org.apache.xml.internal.dtm.ref.DTMNodeIterator((DTMIterator)(new com.sun.org.apache.xpath.internal.NodeSetDTM(m_dtmRoot, m_DTMXRTreeFrag.getXPathContext().getDTMManager())));
|
|
87 |
else
|
|
88 |
return super.object();
|
|
89 |
}
|
|
90 |
|
|
91 |
/**
|
|
92 |
* Create an XRTreeFrag Object.
|
|
93 |
*
|
|
94 |
*/
|
|
95 |
public XRTreeFrag(Expression expr)
|
|
96 |
{
|
|
97 |
super(expr);
|
|
98 |
}
|
|
99 |
|
|
100 |
/**
|
|
101 |
* Specify if it's OK for detach to release the iterator for reuse.
|
|
102 |
*
|
|
103 |
* @param allowRelease true if it is OK for detach to release this iterator
|
|
104 |
* for pooling.
|
|
105 |
*/
|
|
106 |
public void allowDetachToRelease(boolean allowRelease)
|
|
107 |
{
|
|
108 |
m_allowRelease = allowRelease;
|
|
109 |
}
|
|
110 |
|
|
111 |
/**
|
|
112 |
* Detaches the <code>DTMIterator</code> from the set which it iterated
|
|
113 |
* over, releasing any computational resources and placing the iterator
|
|
114 |
* in the INVALID state. After <code>detach</code> has been invoked,
|
|
115 |
* calls to <code>nextNode</code> or <code>previousNode</code> will
|
|
116 |
* raise a runtime exception.
|
|
117 |
*
|
|
118 |
* In general, detach should only be called once on the object.
|
|
119 |
*/
|
|
120 |
public void detach(){
|
|
121 |
if(m_allowRelease){
|
|
122 |
m_DTMXRTreeFrag.destruct();
|
|
123 |
setObject(null);
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
/**
|
|
128 |
* Tell what kind of class this is.
|
|
129 |
*
|
|
130 |
* @return type CLASS_RTREEFRAG
|
|
131 |
*/
|
|
132 |
public int getType()
|
|
133 |
{
|
|
134 |
return CLASS_RTREEFRAG;
|
|
135 |
}
|
|
136 |
|
|
137 |
/**
|
|
138 |
* Given a request type, return the equivalent string.
|
|
139 |
* For diagnostic purposes.
|
|
140 |
*
|
|
141 |
* @return type string "#RTREEFRAG"
|
|
142 |
*/
|
|
143 |
public String getTypeString()
|
|
144 |
{
|
|
145 |
return "#RTREEFRAG";
|
|
146 |
}
|
|
147 |
|
|
148 |
/**
|
|
149 |
* Cast result object to a number.
|
|
150 |
*
|
|
151 |
* @return The result tree fragment as a number or NaN
|
|
152 |
*/
|
|
153 |
public double num()
|
|
154 |
throws javax.xml.transform.TransformerException
|
|
155 |
{
|
|
156 |
|
|
157 |
XMLString s = xstr();
|
|
158 |
|
|
159 |
return s.toDouble();
|
|
160 |
}
|
|
161 |
|
|
162 |
/**
|
|
163 |
* Cast result object to a boolean. This always returns true for a RTreeFrag
|
|
164 |
* because it is treated like a node-set with a single root node.
|
|
165 |
*
|
|
166 |
* @return true
|
|
167 |
*/
|
|
168 |
public boolean bool()
|
|
169 |
{
|
|
170 |
return true;
|
|
171 |
}
|
|
172 |
|
|
173 |
private XMLString m_xmlStr = null;
|
|
174 |
|
|
175 |
/**
|
|
176 |
* Cast result object to an XMLString.
|
|
177 |
*
|
|
178 |
* @return The document fragment node data or the empty string.
|
|
179 |
*/
|
|
180 |
public XMLString xstr()
|
|
181 |
{
|
|
182 |
if(null == m_xmlStr)
|
|
183 |
m_xmlStr = m_DTMXRTreeFrag.getDTM().getStringValue(m_dtmRoot);
|
|
184 |
|
|
185 |
return m_xmlStr;
|
|
186 |
}
|
|
187 |
|
|
188 |
/**
|
|
189 |
* Cast result object to a string.
|
|
190 |
*
|
|
191 |
* @return The string this wraps or the empty string if null
|
|
192 |
*/
|
|
193 |
public void appendToFsb(com.sun.org.apache.xml.internal.utils.FastStringBuffer fsb)
|
|
194 |
{
|
|
195 |
XString xstring = (XString)xstr();
|
|
196 |
xstring.appendToFsb(fsb);
|
|
197 |
}
|
|
198 |
|
|
199 |
|
|
200 |
/**
|
|
201 |
* Cast result object to a string.
|
|
202 |
*
|
|
203 |
* @return The document fragment node data or the empty string.
|
|
204 |
*/
|
|
205 |
public String str()
|
|
206 |
{
|
|
207 |
String str = m_DTMXRTreeFrag.getDTM().getStringValue(m_dtmRoot).toString();
|
|
208 |
|
|
209 |
return (null == str) ? "" : str;
|
|
210 |
}
|
|
211 |
|
|
212 |
/**
|
|
213 |
* Cast result object to a result tree fragment.
|
|
214 |
*
|
|
215 |
* @return The document fragment this wraps
|
|
216 |
*/
|
|
217 |
public int rtf()
|
|
218 |
{
|
|
219 |
return m_dtmRoot;
|
|
220 |
}
|
|
221 |
|
|
222 |
/**
|
|
223 |
* Cast result object to a DTMIterator.
|
|
224 |
* dml - modified to return an RTFIterator for
|
|
225 |
* benefit of EXSLT object-type function in
|
|
226 |
* {@link com.sun.org.apache.xalan.internal.lib.ExsltCommon}.
|
|
227 |
* @return The document fragment as a DTMIterator
|
|
228 |
*/
|
|
229 |
public DTMIterator asNodeIterator()
|
|
230 |
{
|
|
231 |
return new RTFIterator(m_dtmRoot, m_DTMXRTreeFrag.getXPathContext().getDTMManager());
|
|
232 |
}
|
|
233 |
|
|
234 |
/**
|
|
235 |
* Cast result object to a nodelist. (special function).
|
|
236 |
*
|
|
237 |
* @return The document fragment as a nodelist
|
|
238 |
*/
|
|
239 |
public NodeList convertToNodeset()
|
|
240 |
{
|
|
241 |
|
|
242 |
if (m_obj instanceof NodeList)
|
|
243 |
return (NodeList) m_obj;
|
|
244 |
else
|
|
245 |
return new com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList(asNodeIterator());
|
|
246 |
}
|
|
247 |
|
|
248 |
/**
|
|
249 |
* Tell if two objects are functionally equal.
|
|
250 |
*
|
|
251 |
* @param obj2 Object to compare this to
|
|
252 |
*
|
|
253 |
* @return True if the two objects are equal
|
|
254 |
*
|
|
255 |
* @throws javax.xml.transform.TransformerException
|
|
256 |
*/
|
|
257 |
public boolean equals(XObject obj2)
|
|
258 |
{
|
|
259 |
|
|
260 |
try
|
|
261 |
{
|
|
262 |
if (XObject.CLASS_NODESET == obj2.getType())
|
|
263 |
{
|
|
264 |
|
|
265 |
// In order to handle the 'all' semantics of
|
|
266 |
// nodeset comparisons, we always call the
|
|
267 |
// nodeset function.
|
|
268 |
return obj2.equals(this);
|
|
269 |
}
|
|
270 |
else if (XObject.CLASS_BOOLEAN == obj2.getType())
|
|
271 |
{
|
|
272 |
return bool() == obj2.bool();
|
|
273 |
}
|
|
274 |
else if (XObject.CLASS_NUMBER == obj2.getType())
|
|
275 |
{
|
|
276 |
return num() == obj2.num();
|
|
277 |
}
|
|
278 |
else if (XObject.CLASS_NODESET == obj2.getType())
|
|
279 |
{
|
|
280 |
return xstr().equals(obj2.xstr());
|
|
281 |
}
|
|
282 |
else if (XObject.CLASS_STRING == obj2.getType())
|
|
283 |
{
|
|
284 |
return xstr().equals(obj2.xstr());
|
|
285 |
}
|
|
286 |
else if (XObject.CLASS_RTREEFRAG == obj2.getType())
|
|
287 |
{
|
|
288 |
|
|
289 |
// Probably not so good. Think about this.
|
|
290 |
return xstr().equals(obj2.xstr());
|
|
291 |
}
|
|
292 |
else
|
|
293 |
{
|
|
294 |
return super.equals(obj2);
|
|
295 |
}
|
|
296 |
}
|
|
297 |
catch(javax.xml.transform.TransformerException te)
|
|
298 |
{
|
|
299 |
throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(te);
|
|
300 |
}
|
|
301 |
}
|
|
302 |
|
|
303 |
}
|