KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > util > BaseTreeVisitor


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

19
20 package org.apache.cayenne.util;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.cayenne.project.ProjectPath;
26
27 /**
28  * A common superclass for specialized tree visitors. Can also be used as a noop
29  * pass-through visitor for nodes that need no processing by themselves.
30  *
31  * @author Andrus Adamchik
32  */

33 // TODO, andrus, 4/24/2006 - move to Cayenne core in 2.0
34
public class BaseTreeVisitor implements HierarchicalTreeVisitor {
35
36     protected Map JavaDoc childVisitors;
37     protected boolean terminatingOnNoChildVisitor;
38
39     public BaseTreeVisitor() {
40         this.terminatingOnNoChildVisitor = true;
41     }
42
43     public boolean isTerminatingOnNoChildVisitor() {
44         return terminatingOnNoChildVisitor;
45     }
46
47     public void setTerminatingOnNoChildVisitor(boolean terminatingOnNoChildVisitor) {
48         this.terminatingOnNoChildVisitor = terminatingOnNoChildVisitor;
49     }
50
51     public HierarchicalTreeVisitor childVisitor(ProjectPath path, Class JavaDoc childType) {
52         if (childVisitors == null) {
53             return terminatingOnNoChildVisitor ? null : this;
54         }
55
56         HierarchicalTreeVisitor childVisitor = (HierarchicalTreeVisitor) childVisitors
57                 .get(childType.getName());
58         return childVisitor != null ? childVisitor : terminatingOnNoChildVisitor
59                 ? null
60                 : this;
61     }
62
63     public void onFinishNode(ProjectPath path) {
64     }
65
66     public boolean onStartNode(ProjectPath path) {
67         return true;
68     }
69
70     public void addChildVisitor(Class JavaDoc childClass, HierarchicalTreeVisitor visitor) {
71         if (childVisitors == null) {
72             childVisitors = new HashMap JavaDoc();
73         }
74
75         childVisitors.put(childClass.getName(), visitor);
76     }
77 }
78
Popular Tags