001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: VariableSafeAbsRef.java 468655 2006-10-28 07:12:06Z minchau $
020     */
021    package org.apache.xpath.operations;
022    
023    import org.apache.xml.dtm.DTMManager;
024    import org.apache.xpath.Expression;
025    import org.apache.xpath.XPathContext;
026    import org.apache.xpath.objects.XNodeSet;
027    import org.apache.xpath.objects.XObject;
028    
029    
030    /**
031     * This is a "smart" variable reference that is used in situations where 
032     * an absolute path is optimized into a variable reference, but may 
033     * be used in some situations where the document context may have changed.
034     * For instance, in select="document(doc/@href)//name[//salary > 7250]", the 
035     * root in the predicate will be different for each node in the set.  While 
036     * this is easy to detect statically in this case, in other cases static 
037     * detection would be very hard or impossible.  So, this class does a dynamic check 
038     * to make sure the document context of the referenced variable is the same as 
039     * the current document context, and, if it is not, execute the referenced variable's 
040     * expression with the current context instead.
041     */
042    public class VariableSafeAbsRef extends Variable
043    {
044        static final long serialVersionUID = -9174661990819967452L;
045            
046      /**
047       * Dereference the variable, and return the reference value.  Note that lazy 
048       * evaluation will occur.  If a variable within scope is not found, a warning 
049       * will be sent to the error listener, and an empty nodeset will be returned.
050       *
051       *
052       * @param xctxt The runtime execution context.
053       *
054       * @return The evaluated variable, or an empty nodeset if not found.
055       *
056       * @throws javax.xml.transform.TransformerException
057       */
058      public XObject execute(XPathContext xctxt, boolean destructiveOK) 
059            throws javax.xml.transform.TransformerException
060      {
061            XNodeSet xns = (XNodeSet)super.execute(xctxt, destructiveOK);
062            DTMManager dtmMgr = xctxt.getDTMManager();
063            int context = xctxt.getContextNode();
064            if(dtmMgr.getDTM(xns.getRoot()).getDocument() != 
065               dtmMgr.getDTM(context).getDocument())
066            {
067                    Expression expr = (Expression)xns.getContainedIter();
068                    xns = (XNodeSet)expr.asIterator(xctxt, context);
069            }
070            return xns;
071      }
072    
073    }
074