286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: VariableSafeAbsRef.java,v 1.2.4.1 2005/09/14 21:31:45 jeffsuttor Exp $
286N/A */
286N/Apackage com.sun.org.apache.xpath.internal.operations;
286N/A
286N/Aimport com.sun.org.apache.xml.internal.dtm.DTMManager;
286N/Aimport com.sun.org.apache.xpath.internal.Expression;
286N/Aimport com.sun.org.apache.xpath.internal.XPathContext;
286N/Aimport com.sun.org.apache.xpath.internal.objects.XNodeSet;
286N/Aimport com.sun.org.apache.xpath.internal.objects.XObject;
286N/A
286N/A
286N/A/**
286N/A * This is a "smart" variable reference that is used in situations where
286N/A * an absolute path is optimized into a variable reference, but may
286N/A * be used in some situations where the document context may have changed.
286N/A * For instance, in select="document(doc/@href)//name[//salary > 7250]", the
286N/A * root in the predicate will be different for each node in the set. While
286N/A * this is easy to detect statically in this case, in other cases static
286N/A * detection would be very hard or impossible. So, this class does a dynamic check
286N/A * to make sure the document context of the referenced variable is the same as
286N/A * the current document context, and, if it is not, execute the referenced variable's
286N/A * expression with the current context instead.
286N/A */
286N/Apublic class VariableSafeAbsRef extends Variable
286N/A{
286N/A static final long serialVersionUID = -9174661990819967452L;
286N/A
286N/A /**
286N/A * Dereference the variable, and return the reference value. Note that lazy
286N/A * evaluation will occur. If a variable within scope is not found, a warning
286N/A * will be sent to the error listener, and an empty nodeset will be returned.
286N/A *
286N/A *
286N/A * @param xctxt The runtime execution context.
286N/A *
286N/A * @return The evaluated variable, or an empty nodeset if not found.
286N/A *
286N/A * @throws javax.xml.transform.TransformerException
286N/A */
286N/A public XObject execute(XPathContext xctxt, boolean destructiveOK)
286N/A throws javax.xml.transform.TransformerException
286N/A {
286N/A XNodeSet xns = (XNodeSet)super.execute(xctxt, destructiveOK);
286N/A DTMManager dtmMgr = xctxt.getDTMManager();
286N/A int context = xctxt.getContextNode();
286N/A if(dtmMgr.getDTM(xns.getRoot()).getDocument() !=
286N/A dtmMgr.getDTM(context).getDocument())
286N/A {
286N/A Expression expr = (Expression)xns.getContainedIter();
286N/A xns = (XNodeSet)expr.asIterator(xctxt, context);
286N/A }
286N/A return xns;
286N/A }
286N/A
286N/A}