KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependency > NodeFactory


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.dependency;
34
35 import java.util.*;
36
37 import org.apache.log4j.*;
38 import org.apache.oro.text.perl.*;
39
40 public class NodeFactory {
41     private static final Perl5Util perl = new Perl5Util();
42
43     private Map packages = new HashMap();
44     private Map classes = new HashMap();
45     private Map features = new HashMap();
46
47     public PackageNode createPackage(String JavaDoc packageName) {
48         return createPackage(packageName, false);
49     }
50     
51     public PackageNode createPackage(String JavaDoc packageName, boolean confirmed) {
52         Logger.getLogger(getClass()).debug("Create package \"" + packageName + "\"");
53
54         PackageNode result = (PackageNode) packages.get(packageName);
55
56         if (result == null) {
57             result = new PackageNode(packageName, confirmed);
58             packages.put(packageName, result);
59             Logger.getLogger(getClass()).debug("Added package \"" + packageName + "\"");
60         }
61
62         if (confirmed && !result.isConfirmed()) {
63             result.setConfirmed(confirmed);
64             Logger.getLogger(getClass()).debug("Package \"" + packageName + "\" is confirmed");
65         }
66
67         return result;
68     }
69     
70     // Only to be used by DeletingVisitor
71
void deletePackage(PackageNode node) {
72         Logger.getLogger(getClass()).debug("Delete package \"" + node + "\"");
73
74         packages.remove(node.getName());
75     }
76
77     public Map getPackages() {
78         return Collections.unmodifiableMap(packages);
79     }
80
81     public ClassNode createClass(String JavaDoc className) {
82         return createClass(className, false);
83     }
84     
85     public ClassNode createClass(String JavaDoc className, boolean confirmed) {
86         Logger.getLogger(getClass()).debug("Create class \"" + className + "\"");
87
88         ClassNode result = (ClassNode) classes.get(className);
89
90         if (result == null) {
91             String JavaDoc packageName = "";
92             int pos = className.lastIndexOf('.');
93             if (pos != -1) {
94                 packageName = className.substring(0, pos);
95             }
96             PackageNode parent = createPackage(packageName, confirmed);
97             result = new ClassNode(parent, className, confirmed);
98             parent.addClass(result);
99             classes.put(className, result);
100             Logger.getLogger(getClass()).debug("Added class \"" + className + "\"");
101         }
102
103         if (confirmed && !result.isConfirmed()) {
104             result.setConfirmed(confirmed);
105             Logger.getLogger(getClass()).debug("Class \"" + className + "\" is confirmed");
106         }
107
108         return result;
109     }
110
111     // Only to be used by DeletingVisitor
112
void deleteClass(ClassNode node) {
113         Logger.getLogger(getClass()).debug("Delete class \"" + node + "\"");
114
115         node.getPackageNode().removeClass(node);
116         classes.remove(node.getName());
117     }
118
119     public Map getClasses() {
120         return Collections.unmodifiableMap(classes);
121     }
122
123     public FeatureNode createFeature(String JavaDoc featureName) {
124         return createFeature(featureName, false);
125     }
126     
127     public FeatureNode createFeature(String JavaDoc featureName, boolean confirmed) {
128         Logger.getLogger(getClass()).debug("Create feature \"" + featureName + "\"");
129
130         FeatureNode result = (FeatureNode) features.get(featureName);
131
132         if (result == null) {
133             String JavaDoc parentName = null;
134
135             if (perl.match("/^(.*)\\.[^\\.]*\\(.*\\)$/", featureName)) {
136                 parentName = perl.group(1);
137             } else if (perl.match("/^(.*)\\.[^\\.]*$/", featureName)) {
138                 parentName = perl.group(1);
139             } else {
140                 parentName = "";
141             }
142
143             ClassNode parent = createClass(parentName, confirmed);
144             result = new FeatureNode(parent, featureName, confirmed);
145             parent.addFeature(result);
146             features.put(featureName, result);
147             Logger.getLogger(getClass()).debug("Added feature \"" + featureName + "\"");
148         }
149
150         if (confirmed && !result.isConfirmed()) {
151             result.setConfirmed(confirmed);
152             Logger.getLogger(getClass()).debug("Feature \"" + featureName + "\" is confirmed");
153         }
154
155         return result;
156     }
157     
158     // Only to be used by DeletingVisitor
159
void deleteFeature(FeatureNode node) {
160         Logger.getLogger(getClass()).debug("Delete feature \"" + node + "\"");
161
162         node.getClassNode().removeFeature(node);
163         features.remove(node.getName());
164     }
165     
166     public Map getFeatures() {
167         return Collections.unmodifiableMap(features);
168     }
169 }
170
Popular Tags