001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.HasNodeVisitor
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: /**
031: * Find out if we have a particular node anywhere in the
032: * tree. Stop traversal as soon as we find one.
033: * <p>
034: * Can find any type of node -- the class or class name
035: * of the target node is passed in as a constructor
036: * parameter.
037: *
038: * @author jamie
039: */
040: public class HasNodeVisitor implements Visitor {
041: private boolean hasNode;
042: private Class nodeClass;
043: private Class skipOverClass;
044:
045: /**
046: * Construct a visitor
047: *
048: * @param nodeClass the class of the node that
049: * we are looking for.
050: */
051: public HasNodeVisitor(Class nodeClass) {
052: this .nodeClass = nodeClass;
053: }
054:
055: /**
056: * Construct a visitor
057: *
058: * @param nodeClass the class of the node that
059: * we are looking for.
060: * @param skipOverClass do not go below this
061: * node when searching for nodeClass.
062: */
063: public HasNodeVisitor(Class nodeClass, Class skipOverClass) {
064: this .nodeClass = nodeClass;
065: this .skipOverClass = skipOverClass;
066: }
067:
068: ////////////////////////////////////////////////
069: //
070: // VISITOR INTERFACE
071: //
072: ////////////////////////////////////////////////
073:
074: /**
075: * If we have found the target node, we are done.
076: *
077: * @param node the node to process
078: *
079: * @return me
080: */
081: public Visitable visit(Visitable node) {
082: if (nodeClass.isInstance(node)) {
083: hasNode = true;
084: }
085: return node;
086: }
087:
088: /**
089: * Stop traversal if we found the target node
090: *
091: * @return true/false
092: */
093: public boolean stopTraversal() {
094: return hasNode;
095: }
096:
097: /**
098: * Don't visit childen under the skipOverClass
099: * node, if it isn't null.
100: *
101: * @return true/false
102: */
103: public boolean skipChildren(Visitable node) {
104: return (skipOverClass == null) ? false : skipOverClass
105: .isInstance(node);
106: }
107:
108: ////////////////////////////////////////////////
109: //
110: // CLASS INTERFACE
111: //
112: ////////////////////////////////////////////////
113: /**
114: * Indicate whether we found the node in
115: * question
116: *
117: * @return true/false
118: */
119: public boolean hasNode() {
120: return hasNode;
121: }
122:
123: /**
124: * Reset the status so it can be run again.
125: *
126: */
127: public void reset() {
128: hasNode = false;
129: }
130: }
|