1 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 packageName) { 48 return createPackage(packageName, false); 49 } 50 51 public PackageNode createPackage(String 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 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 className) { 82 return createClass(className, false); 83 } 84 85 public ClassNode createClass(String 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 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 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 featureName) { 124 return createFeature(featureName, false); 125 } 126 127 public FeatureNode createFeature(String 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 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 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 |