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: AbsPathChecker.java 468643 2006-10-28 06:56:03Z minchau $
020     */
021    package org.apache.xalan.templates;
022    
023    import org.apache.xpath.ExpressionOwner;
024    import org.apache.xpath.XPathVisitor;
025    import org.apache.xpath.axes.LocPathIterator;
026    import org.apache.xpath.functions.FuncCurrent;
027    import org.apache.xpath.functions.FuncExtFunction;
028    import org.apache.xpath.functions.Function;
029    import org.apache.xpath.operations.Variable;
030    
031    /**
032     * This class runs over a path expression that is assumed to be absolute, and 
033     * checks for variables and the like that may make it context dependent.
034     */
035    public class AbsPathChecker extends XPathVisitor
036    {
037            private boolean m_isAbs = true;
038            
039            /**
040             * Process the LocPathIterator to see if it contains variables 
041             * or functions that may make it context dependent.
042             * @param path LocPathIterator that is assumed to be absolute, but needs checking.
043             * @return true if the path is confirmed to be absolute, false if it 
044             * may contain context dependencies.
045             */
046            public boolean checkAbsolute(LocPathIterator path)
047            {
048                    m_isAbs = true;
049                    path.callVisitors(null, this);
050                    return m_isAbs;
051            }
052            
053            /**
054             * Visit a function.
055             * @param owner The owner of the expression, to which the expression can 
056             *              be reset if rewriting takes place.
057             * @param func The function reference object.
058             * @return true if the sub expressions should be traversed.
059             */
060            public boolean visitFunction(ExpressionOwner owner, Function func)
061            {
062                    if((func instanceof FuncCurrent) ||
063                       (func instanceof FuncExtFunction))
064                            m_isAbs = false;
065                    return true;
066            }
067            
068            /**
069             * Visit a variable reference.
070             * @param owner The owner of the expression, to which the expression can 
071             *              be reset if rewriting takes place.
072             * @param var The variable reference object.
073             * @return true if the sub expressions should be traversed.
074             */
075            public boolean visitVariableRef(ExpressionOwner owner, Variable var)
076            {
077                    m_isAbs = false;
078                    return true;
079            }
080    }
081