1 32 33 package com.jeantessier.dependency; 34 35 import java.io.*; 36 import java.net.*; 37 import java.util.*; 38 39 import org.apache.log4j.*; 40 41 import org.xml.sax.*; 42 import org.xml.sax.helpers.*; 43 44 public class NodeHandler extends DefaultHandler { 45 private static final int PACKAGE = 1; 46 private static final int CLASS = 2; 47 private static final int FEATURE = 3; 48 49 private NodeFactory factory; 50 51 private int currentNodeType; 52 private int currentDependencyType; 53 private Attributes currentDependencyAttributes; 54 private Node currentNode; 55 private PackageNode currentPackage; 56 private Attributes currentPackageAttributes; 57 private ClassNode currentClass; 58 private Attributes currentClassAttributes; 59 private FeatureNode currentFeature; 60 private Attributes currentFeatureAttributes; 61 private StringBuffer currentName = new StringBuffer (); 62 63 private HashSet dependencyListeners = new HashSet(); 64 65 public NodeHandler() { 66 this(new NodeFactory()); 67 } 68 69 public NodeHandler(NodeFactory factory) { 70 this.factory = factory; 71 } 72 73 public NodeFactory getFactory() { 74 return factory; 75 } 76 77 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 78 Logger.getLogger(getClass()).debug("qName = " + qName); 79 80 for (int i=0; i<atts.getLength(); i++) { 81 Logger.getLogger(getClass()).debug(" " + atts.getQName(i) + ": " + atts.getValue(i)); 82 } 83 84 currentName.delete(0, currentName.length()); 85 86 if ("dependencies".equals(qName)) { 87 fireBeginSession(); 88 } else if ("package".equals(qName)) { 89 currentNodeType = PACKAGE; 90 currentPackageAttributes = new AttributesImpl(atts); 91 } else if ("class".equals(qName)) { 92 currentNodeType = CLASS; 93 currentClassAttributes = new AttributesImpl(atts); 94 } else if ("feature".equals(qName)) { 95 currentNodeType = FEATURE; 96 currentFeatureAttributes = new AttributesImpl(atts); 97 } else if ("inbound".equals(qName) || "outbound".equals(qName)) { 98 if ("package".equals(atts.getValue("type"))) { 99 currentDependencyType = PACKAGE; 100 } else if ("class".equals(atts.getValue("type"))) { 101 currentDependencyType = CLASS; 102 } else if ("feature".equals(atts.getValue("type"))) { 103 currentDependencyType = FEATURE; 104 } 105 currentDependencyAttributes = new AttributesImpl(atts); 106 } 107 108 Logger.getLogger(getClass()).debug(" current node type: " + currentNodeType); 109 Logger.getLogger(getClass()).debug(" current dependency type: " + currentDependencyType); 110 } 111 112 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 113 Logger.getLogger(getClass()).debug("qName = " + qName); 114 115 if ("dependencies".equals(qName)) { 116 fireEndSession(); 117 } else if ("name".equals(qName)) { 118 Logger.getLogger(getClass()).debug(" Processing <name> tag:"); 119 Logger.getLogger(getClass()).debug(" current name: " + currentName); 120 Logger.getLogger(getClass()).debug(" current node type: " + currentNodeType); 121 122 switch (currentNodeType) { 123 case PACKAGE: 124 currentPackage = getFactory().createPackage(currentName.toString(), isConfirmed(currentPackageAttributes)); 125 currentNode = currentPackage; 126 break; 127 case CLASS: 128 currentClass = getFactory().createClass(currentName.toString(), isConfirmed(currentClassAttributes)); 129 currentNode = currentClass; 130 fireBeginClass(currentClass.toString()); 131 break; 132 case FEATURE: 133 currentFeature = getFactory().createFeature(currentName.toString(), isConfirmed(currentFeatureAttributes)); 134 currentNode = currentFeature; 135 break; 136 } 137 } else if ("outbound".equals(qName)) { 138 Logger.getLogger(getClass()).debug(" Processing <outbound> tag:"); 139 Logger.getLogger(getClass()).debug(" current_name: " + currentName); 140 Logger.getLogger(getClass()).debug(" current_dependency_type: " + currentDependencyType); 141 142 Node other = null; 143 switch (currentDependencyType) { 144 case PACKAGE: 145 other = getFactory().createPackage(currentName.toString(), isConfirmed(currentDependencyAttributes)); 146 break; 147 case CLASS: 148 other = getFactory().createClass(currentName.toString(), isConfirmed(currentDependencyAttributes)); 149 break; 150 case FEATURE: 151 other = getFactory().createFeature(currentName.toString(), isConfirmed(currentDependencyAttributes)); 152 break; 153 } 154 currentNode.addDependency(other); 155 fireDependency(currentNode, other); 156 } else if ("inbound".equals(qName)) { 157 Logger.getLogger(getClass()).debug(" Processing <inbound> tag:"); 158 Logger.getLogger(getClass()).debug(" current_name: " + currentName); 159 Logger.getLogger(getClass()).debug(" current_dependency_type: " + currentDependencyType); 160 161 Node other = null; 162 switch (currentDependencyType) { 163 case PACKAGE: 164 other = getFactory().createPackage(currentName.toString(), isConfirmed(currentDependencyAttributes)); 165 break; 166 case CLASS: 167 other = getFactory().createClass(currentName.toString(), isConfirmed(currentDependencyAttributes)); 168 break; 169 case FEATURE: 170 other = getFactory().createFeature(currentName.toString(), isConfirmed(currentDependencyAttributes)); 171 break; 172 } 173 other.addDependency(currentNode); 174 fireDependency(other, currentNode); 175 } 176 } 177 178 public void characters(char[] ch, int start, int length) throws SAXException { 179 currentName.append(ch, start, length); 180 Logger.getLogger(getClass()).debug("characters: \"" + new String (ch, start, length) + "\""); 181 } 182 183 public void addDependencyListener(DependencyListener listener) { 184 synchronized(dependencyListeners) { 185 dependencyListeners.add(listener); 186 } 187 } 188 189 public void removeDependencyListener(DependencyListener listener) { 190 synchronized(dependencyListeners) { 191 dependencyListeners.remove(listener); 192 } 193 } 194 195 protected void fireBeginSession() { 196 DependencyEvent event = new DependencyEvent(this); 197 198 HashSet listeners; 199 synchronized(dependencyListeners) { 200 listeners = (HashSet) dependencyListeners.clone(); 201 } 202 203 Iterator i = listeners.iterator(); 204 while(i.hasNext()) { 205 ((DependencyListener) i.next()).beginSession(event); 206 } 207 } 208 209 protected void fireBeginClass(String classname) { 210 DependencyEvent event = new DependencyEvent(this, classname); 211 212 HashSet listeners; 213 synchronized(dependencyListeners) { 214 listeners = (HashSet) dependencyListeners.clone(); 215 } 216 217 Iterator i = listeners.iterator(); 218 while(i.hasNext()) { 219 ((DependencyListener) i.next()).beginClass(event); 220 } 221 } 222 223 protected void fireDependency(Node dependent, Node dependable) { 224 DependencyEvent event = new DependencyEvent(this, dependent, dependable); 225 226 HashSet listeners; 227 synchronized(dependencyListeners) { 228 listeners = (HashSet) dependencyListeners.clone(); 229 } 230 231 Iterator i = listeners.iterator(); 232 while(i.hasNext()) { 233 ((DependencyListener) i.next()).dependency(event); 234 } 235 } 236 237 protected void fireEndClass(String classname) { 238 DependencyEvent event = new DependencyEvent(this, classname); 239 240 HashSet listeners; 241 synchronized(dependencyListeners) { 242 listeners = (HashSet) dependencyListeners.clone(); 243 } 244 245 Iterator i = listeners.iterator(); 246 while(i.hasNext()) { 247 ((DependencyListener) i.next()).endClass(event); 248 } 249 } 250 251 protected void fireEndSession() { 252 DependencyEvent event = new DependencyEvent(this); 253 254 HashSet listeners; 255 synchronized(dependencyListeners) { 256 listeners = (HashSet) dependencyListeners.clone(); 257 } 258 259 Iterator i = listeners.iterator(); 260 while(i.hasNext()) { 261 ((DependencyListener) i.next()).endSession(event); 262 } 263 } 264 265 private boolean isConfirmed(Attributes atts) { 266 return atts.getValue("confirmed") == null || "yes".equalsIgnoreCase(atts.getValue("confirmed")); 267 } 268 } 269 | Popular Tags |