001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependency;
034:
035: import java.util.*;
036:
037: import org.apache.log4j.*;
038: import org.apache.oro.text.perl.*;
039:
040: public class NodeFactory {
041: private static final Perl5Util perl = new Perl5Util();
042:
043: private Map<String, PackageNode> packages = new HashMap<String, PackageNode>();
044: private Map<String, ClassNode> classes = new HashMap<String, ClassNode>();
045: private Map<String, FeatureNode> features = new HashMap<String, FeatureNode>();
046:
047: public PackageNode createPackage(String packageName) {
048: return createPackage(packageName, false);
049: }
050:
051: public PackageNode createPackage(String packageName,
052: boolean confirmed) {
053: Logger.getLogger(getClass()).debug(
054: "Create package \"" + packageName + "\"");
055:
056: PackageNode result = packages.get(packageName);
057:
058: if (result == null) {
059: result = new PackageNode(packageName, confirmed);
060: packages.put(packageName, result);
061: Logger.getLogger(getClass()).debug(
062: "Added package \"" + packageName + "\"");
063: }
064:
065: if (confirmed && !result.isConfirmed()) {
066: result.setConfirmed(confirmed);
067: Logger.getLogger(getClass()).debug(
068: "Package \"" + packageName + "\" is confirmed");
069: }
070:
071: return result;
072: }
073:
074: // Only to be used by DeletingVisitor
075: void deletePackage(PackageNode node) {
076: Logger.getLogger(getClass()).debug(
077: "Delete package \"" + node + "\"");
078:
079: packages.remove(node.getName());
080: }
081:
082: public Map<String, PackageNode> getPackages() {
083: return Collections.unmodifiableMap(packages);
084: }
085:
086: public ClassNode createClass(String className) {
087: return createClass(className, false);
088: }
089:
090: public ClassNode createClass(String className, boolean confirmed) {
091: Logger.getLogger(getClass()).debug(
092: "Create class \"" + className + "\"");
093:
094: ClassNode result = classes.get(className);
095:
096: if (result == null) {
097: String packageName = "";
098: int pos = className.lastIndexOf('.');
099: if (pos != -1) {
100: packageName = className.substring(0, pos);
101: }
102: PackageNode parent = createPackage(packageName, confirmed);
103: result = new ClassNode(parent, className, confirmed);
104: parent.addClass(result);
105: classes.put(className, result);
106: Logger.getLogger(getClass()).debug(
107: "Added class \"" + className + "\"");
108: }
109:
110: if (confirmed && !result.isConfirmed()) {
111: result.setConfirmed(confirmed);
112: Logger.getLogger(getClass()).debug(
113: "Class \"" + className + "\" is confirmed");
114: }
115:
116: return result;
117: }
118:
119: // Only to be used by DeletingVisitor
120: void deleteClass(ClassNode node) {
121: Logger.getLogger(getClass()).debug(
122: "Delete class \"" + node + "\"");
123:
124: node.getPackageNode().removeClass(node);
125: classes.remove(node.getName());
126: }
127:
128: public Map<String, ClassNode> getClasses() {
129: return Collections.unmodifiableMap(classes);
130: }
131:
132: public FeatureNode createFeature(String featureName) {
133: return createFeature(featureName, false);
134: }
135:
136: public FeatureNode createFeature(String featureName,
137: boolean confirmed) {
138: Logger.getLogger(getClass()).debug(
139: "Create feature \"" + featureName + "\"");
140:
141: FeatureNode result = features.get(featureName);
142:
143: if (result == null) {
144: String parentName;
145:
146: if (perl.match("/^(.*)\\.[^\\.]*\\(.*\\)$/", featureName)) {
147: parentName = perl.group(1);
148: } else if (perl.match("/^(.*)\\.[^\\.]*$/", featureName)) {
149: parentName = perl.group(1);
150: } else {
151: parentName = "";
152: }
153:
154: ClassNode parent = createClass(parentName, confirmed);
155: result = new FeatureNode(parent, featureName, confirmed);
156: parent.addFeature(result);
157: features.put(featureName, result);
158: Logger.getLogger(getClass()).debug(
159: "Added feature \"" + featureName + "\"");
160: }
161:
162: if (confirmed && !result.isConfirmed()) {
163: result.setConfirmed(confirmed);
164: Logger.getLogger(getClass()).debug(
165: "Feature \"" + featureName + "\" is confirmed");
166: }
167:
168: return result;
169: }
170:
171: // Only to be used by DeletingVisitor
172: void deleteFeature(FeatureNode node) {
173: Logger.getLogger(getClass()).debug(
174: "Delete feature \"" + node + "\"");
175:
176: node.getClassNode().removeFeature(node);
177: features.remove(node.getName());
178: }
179:
180: public Map<String, FeatureNode> getFeatures() {
181: return Collections.unmodifiableMap(features);
182: }
183: }
|