1/* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5/* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22package com.sun.org.apache.xpath.internal.operations; 23 24import com.sun.org.apache.xml.internal.dtm.DTMManager; 25import com.sun.org.apache.xpath.internal.Expression; 26import com.sun.org.apache.xpath.internal.XPathContext; 27import com.sun.org.apache.xpath.internal.objects.XNodeSet; 28import com.sun.org.apache.xpath.internal.objects.XObject; 29 30 31/** 32 * This is a "smart" variable reference that is used in situations where 33 * an absolute path is optimized into a variable reference, but may 34 * be used in some situations where the document context may have changed. 35 * For instance, in select="document(doc/@href)//name[//salary > 7250]", the 36 * root in the predicate will be different for each node in the set. While 37 * this is easy to detect statically in this case, in other cases static 38 * detection would be very hard or impossible. So, this class does a dynamic check 39 * to make sure the document context of the referenced variable is the same as 40 * the current document context, and, if it is not, execute the referenced variable's 41 * expression with the current context instead. 42 */ 43public class VariableSafeAbsRef extends Variable 44{ 45 static final long serialVersionUID = -9174661990819967452L; 46 47 /** 48 * Dereference the variable, and return the reference value. Note that lazy 49 * evaluation will occur. If a variable within scope is not found, a warning 50 * will be sent to the error listener, and an empty nodeset will be returned. 51 * 52 * 53 * @param xctxt The runtime execution context. 54 * 55 * @return The evaluated variable, or an empty nodeset if not found. 56 * 57 * @throws javax.xml.transform.TransformerException 58 */ 59 public XObject execute(XPathContext xctxt, boolean destructiveOK) 60 throws javax.xml.transform.TransformerException 61 { 62 XNodeSet xns = (XNodeSet)super.execute(xctxt, destructiveOK); 63 DTMManager dtmMgr = xctxt.getDTMManager(); 64 int context = xctxt.getContextNode(); 65 if(dtmMgr.getDTM(xns.getRoot()).getDocument() != 66 dtmMgr.getDTM(context).getDocument()) 67 { 68 Expression expr = (Expression)xns.getContainedIter(); 69 xns = (XNodeSet)expr.asIterator(xctxt, context); 70 } 71 return xns; 72 } 73 74} 75