KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xpath > internal > operations > VariableSafeAbsRef


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: VariableSafeAbsRef.java,v 1.5 2004/02/17 04:35:12 minchau Exp $
18  */

19 package com.sun.org.apache.xpath.internal.operations;
20
21 import com.sun.org.apache.xml.internal.dtm.DTMManager;
22 import com.sun.org.apache.xpath.internal.Expression;
23 import com.sun.org.apache.xpath.internal.XPathContext;
24 import com.sun.org.apache.xpath.internal.objects.XNodeSet;
25 import com.sun.org.apache.xpath.internal.objects.XObject;
26
27
28 /**
29  * This is a "smart" variable reference that is used in situations where
30  * an absolute path is optimized into a variable reference, but may
31  * be used in some situations where the document context may have changed.
32  * For instance, in select="document(doc/@href)//name[//salary > 7250]", the
33  * root in the predicate will be different for each node in the set. While
34  * this is easy to detect statically in this case, in other cases static
35  * detection would be very hard or impossible. So, this class does a dynamic check
36  * to make sure the document context of the referenced variable is the same as
37  * the current document context, and, if it is not, execute the referenced variable's
38  * expression with the current context instead.
39  */

40 public class VariableSafeAbsRef extends Variable
41 {
42     
43   /**
44    * Dereference the variable, and return the reference value. Note that lazy
45    * evaluation will occur. If a variable within scope is not found, a warning
46    * will be sent to the error listener, and an empty nodeset will be returned.
47    *
48    *
49    * @param xctxt The runtime execution context.
50    *
51    * @return The evaluated variable, or an empty nodeset if not found.
52    *
53    * @throws javax.xml.transform.TransformerException
54    */

55   public XObject execute(XPathContext xctxt, boolean destructiveOK)
56     throws javax.xml.transform.TransformerException JavaDoc
57   {
58     XNodeSet xns = (XNodeSet)super.execute(xctxt, destructiveOK);
59     DTMManager dtmMgr = xctxt.getDTMManager();
60     int context = xctxt.getContextNode();
61     if(dtmMgr.getDTM(xns.getRoot()).getDocument() !=
62        dtmMgr.getDTM(context).getDocument())
63     {
64         Expression expr = (Expression)xns.getContainedIter();
65         xns = (XNodeSet)expr.asIterator(xctxt, context);
66     }
67     return xns;
68   }
69
70 }
71
72
Popular Tags