001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.CollectNodesVisitor
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.compile;
023:
024: import org.apache.derby.iapi.services.sanity.SanityManager;
025: import org.apache.derby.iapi.sql.compile.Visitable;
026: import org.apache.derby.iapi.sql.compile.Visitor;
027:
028: import org.apache.derby.iapi.error.StandardException;
029:
030: import java.util.Vector;
031:
032: /**
033: * Collect all nodes of the designated type to be returned
034: * in a vector.
035: * <p>
036: * Can find any type of node -- the class or class name
037: * of the target node is passed in as a constructor
038: * parameter.
039: *
040: * @author jamie
041: */
042: public class CollectNodesVisitor implements Visitor {
043: private Vector nodeList;
044: private Class nodeClass;
045: private Class skipOverClass;
046:
047: /**
048: * Construct a visitor
049: *
050: * @param nodeClass the class of the node that
051: * we are looking for.
052: */
053: public CollectNodesVisitor(Class nodeClass) {
054: this .nodeClass = nodeClass;
055: nodeList = new Vector();
056: }
057:
058: /**
059: * Construct a visitor
060: *
061: * @param nodeClass the class of the node that
062: * we are looking for.
063: * @param skipOverClass do not go below this
064: * node when searching for nodeClass.
065: */
066: public CollectNodesVisitor(Class nodeClass, Class skipOverClass) {
067: this (nodeClass);
068: this .skipOverClass = skipOverClass;
069: }
070:
071: public boolean stopTraversal() {
072: return false;
073: }
074:
075: ////////////////////////////////////////////////
076: //
077: // VISITOR INTERFACE
078: //
079: ////////////////////////////////////////////////
080:
081: /**
082: * If we have found the target node, we are done.
083: *
084: * @param node the node to process
085: *
086: * @return me
087: */
088: public Visitable visit(Visitable node) {
089: if (nodeClass.isInstance(node)) {
090: nodeList.addElement(node);
091: }
092: return node;
093: }
094:
095: /**
096: * Don't visit childen under the skipOverClass
097: * node, if it isn't null.
098: *
099: * @return true/false
100: */
101: public boolean skipChildren(Visitable node) {
102: return (skipOverClass == null) ? false : skipOverClass
103: .isInstance(node);
104: }
105:
106: ////////////////////////////////////////////////
107: //
108: // CLASS INTERFACE
109: //
110: ////////////////////////////////////////////////
111: /**
112: * Reset the status so it can be run again.
113: *
114: */
115: public Vector getList() {
116: return nodeList;
117: }
118: }
|