KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > HasNodeVisitor


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.HasNodeVisitor
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25 import org.apache.derby.iapi.sql.compile.Visitable;
26 import org.apache.derby.iapi.sql.compile.Visitor;
27
28 import org.apache.derby.iapi.error.StandardException;
29
30 /**
31  * Find out if we have a particular node anywhere in the
32  * tree. Stop traversal as soon as we find one.
33  * <p>
34  * Can find any type of node -- the class or class name
35  * of the target node is passed in as a constructor
36  * parameter.
37  *
38  * @author jamie
39  */

40 public class HasNodeVisitor implements Visitor
41 {
42     private boolean hasNode;
43     private Class JavaDoc nodeClass;
44     private Class JavaDoc skipOverClass;
45     /**
46      * Construct a visitor
47      *
48      * @param nodeClass the class of the node that
49      * we are looking for.
50      */

51     public HasNodeVisitor(Class JavaDoc nodeClass)
52     {
53         this.nodeClass = nodeClass;
54     }
55
56     /**
57      * Construct a visitor
58      *
59      * @param nodeClass the class of the node that
60      * we are looking for.
61      * @param skipOverClass do not go below this
62      * node when searching for nodeClass.
63      */

64     public HasNodeVisitor(Class JavaDoc nodeClass, Class JavaDoc skipOverClass)
65     {
66         this.nodeClass = nodeClass;
67         this.skipOverClass = skipOverClass;
68     }
69
70     ////////////////////////////////////////////////
71
//
72
// VISITOR INTERFACE
73
//
74
////////////////////////////////////////////////
75

76     /**
77      * If we have found the target node, we are done.
78      *
79      * @param node the node to process
80      *
81      * @return me
82      */

83     public Visitable visit(Visitable node)
84     {
85         if (nodeClass.isInstance(node))
86         {
87             hasNode = true;
88         }
89         return node;
90     }
91
92     /**
93      * Stop traversal if we found the target node
94      *
95      * @return true/false
96      */

97     public boolean stopTraversal()
98     {
99         return hasNode;
100     }
101
102     /**
103      * Don't visit childen under the skipOverClass
104      * node, if it isn't null.
105      *
106      * @return true/false
107      */

108     public boolean skipChildren(Visitable node)
109     {
110         return (skipOverClass == null) ?
111                 false:
112                 skipOverClass.isInstance(node);
113     }
114
115     ////////////////////////////////////////////////
116
//
117
// CLASS INTERFACE
118
//
119
////////////////////////////////////////////////
120
/**
121      * Indicate whether we found the node in
122      * question
123      *
124      * @return true/false
125      */

126     public boolean hasNode()
127     {
128         return hasNode;
129     }
130
131     /**
132      * Reset the status so it can be run again.
133      *
134      */

135     public void reset()
136     {
137         hasNode = false;
138     }
139 }
140
Popular Tags