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: VarNameCollector.java 468643 2006-10-28 06:56:03Z minchau $ 020 */ 021 package org.apache.xalan.templates; 022 023 import java.util.Vector; 024 025 import org.apache.xml.utils.QName; 026 import org.apache.xpath.ExpressionOwner; 027 import org.apache.xpath.XPathVisitor; 028 import org.apache.xpath.operations.Variable; 029 030 /** 031 * This class visits variable refs in an XPath and collects their QNames. 032 */ 033 public class VarNameCollector extends XPathVisitor 034 { 035 Vector m_refs = new Vector(); 036 037 /** 038 * Reset the list for a fresh visitation and collection. 039 */ 040 public void reset() 041 { 042 m_refs.removeAllElements(); //.clear(); 043 } 044 045 /** 046 * Get the number of variable references that were collected. 047 * @return the size of the list. 048 */ 049 public int getVarCount() 050 { 051 return m_refs.size(); 052 } 053 054 /** 055 * Tell if the given qualified name occurs in 056 * the list of qualified names collected. 057 * 058 * @param refName Must be a valid qualified name. 059 * @return true if the list contains the qualified name. 060 */ 061 boolean doesOccur(QName refName) 062 { 063 return m_refs.contains(refName); 064 } 065 066 /** 067 * Visit a variable reference. 068 * @param owner The owner of the expression, to which the expression can 069 * be reset if rewriting takes place. 070 * @param var The variable reference object. 071 * @return true if the sub expressions should be traversed. 072 */ 073 public boolean visitVariableRef(ExpressionOwner owner, Variable var) 074 { 075 m_refs.addElement(var.getQName()); 076 return true; 077 } 078 079 } 080