1 package csdl.jblanket.app.tree; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 import java.util.StringTokenizer ; 9 10 import javax.swing.tree.DefaultMutableTreeNode ; 11 import javax.swing.tree.DefaultTreeModel ; 12 import javax.swing.tree.TreePath ; 13 14 import org.xml.sax.Attributes ; 15 import org.xml.sax.ContentHandler ; 16 import org.xml.sax.Locator ; 17 import org.xml.sax.SAXException ; 18 19 28 public class NodeContentHandler implements ContentHandler { 29 30 31 private Locator locator; 32 33 private Map namespaceMappings; 34 35 36 private DefaultTreeModel tree; 37 38 private List pathList; 39 40 private String methodCategory; 41 42 43 private String methodName; 44 45 private List parameters; 46 47 53 public NodeContentHandler(DefaultTreeModel tree, String methodCategory) { 54 super(); 55 56 this.namespaceMappings = new HashMap (); 57 this.tree = tree; 58 59 this.pathList = new ArrayList (); 60 this.methodCategory = methodCategory; 61 62 this.parameters = new ArrayList (); 63 } 64 65 73 public void characters(char[] chars, int start, int length) throws SAXException { 74 } 75 76 81 public void endDocument() throws SAXException { 82 } 83 84 93 public void endElement(String namespaceURI, String localName, String qName) 94 throws SAXException { 95 if (localName.equals("Method")) { 97 98 MethodNode methodNode = new MethodNode(this.methodName, this.parameters); 99 DefaultMutableTreeNode method = new DefaultMutableTreeNode (methodNode); 100 this.parameters = new ArrayList (); 102 103 TreePath path = new TreePath (this.pathList.toArray(new Object [] { })); 105 DefaultMutableTreeNode parent = (DefaultMutableTreeNode ) path.getLastPathComponent(); 106 DefaultMutableTreeNode sibling = null; 107 108 if (this.tree.getChildCount(parent) == 0) { 109 110 this.tree.insertNodeInto(method, parent, parent.getChildCount()); 111 sibling = method; 112 } 113 else { 114 115 int methodIndex = 0; 116 for ( ; methodIndex < this.tree.getChildCount(parent); methodIndex++) { 117 sibling = (DefaultMutableTreeNode ) this.tree.getChild(parent, methodIndex); 118 if (((MethodNode) sibling.getUserObject()).toString().compareTo( 120 ((MethodNode) method.getUserObject()).toString()) == 0) { 121 122 break; 123 } 124 } 125 if (!((MethodNode) sibling.getUserObject()).toString().equals( 126 ((MethodNode) method.getUserObject()).toString())) { 127 128 this.tree.insertNodeInto(method, parent, methodIndex); 129 sibling = method; 130 } 131 } 132 133 if ("total.testedFile".equals(this.methodCategory)) { 135 ((MethodNode) sibling.getUserObject()).setTested(); 136 } 137 else if ("total.untestedFile".equals(this.methodCategory)) { 138 ((MethodNode) sibling.getUserObject()).setUntested(); 139 } 140 else if ("oneLineFile".equals(this.methodCategory)) { 141 ((MethodNode) sibling.getUserObject()).setOneLineMethod(); 142 } 143 else if ("constructorFile".equals(this.methodCategory)) { 144 ((MethodNode) sibling.getUserObject()).setConstructor(); 145 } 146 else if ("excludedIndividualFile".equals(this.methodCategory)) { 147 ((MethodNode) sibling.getUserObject()).setIndividualExclude(); 148 } 149 } 150 } 151 152 158 public void endPrefixMapping(String prefix) throws SAXException { 159 160 for (Iterator i = this.namespaceMappings.keySet().iterator(); i.hasNext(); ) { 161 String uri = (String ) i.next(); 162 String thisPrefix = (String ) this.namespaceMappings.get(uri); 163 164 if (prefix.equals(thisPrefix)) { 165 166 this.namespaceMappings.remove(uri); 167 break; 168 } 169 } 170 } 171 172 181 public void ignorableWhitespace(char[] chars, int start, int length) throws SAXException { 182 } 183 184 191 public void processingInstruction(String target, String data) throws SAXException { 192 } 193 194 199 public void setDocumentLocator(Locator locator) { 200 this.locator = locator; 201 } 202 203 209 public void skippedEntity(String name) throws SAXException { 210 } 211 212 217 public void startDocument() throws SAXException { 218 } 219 220 230 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 231 throws SAXException { 232 233 if (localName.equals("Method")) { 234 235 this.methodName = atts.getValue("method"); 236 processClassAttribute(atts.getValue("class")); 237 } 238 else if (localName.equals("Parameter")) { 239 240 this.parameters.add(atts.getValue("type").trim()); 241 } 242 } 243 244 251 public void startPrefixMapping(String prefix, String uri) throws SAXException { 252 this.namespaceMappings.put(uri, prefix); 253 } 254 255 260 private void processClassAttribute(String className) { 261 this.pathList.add((DefaultMutableTreeNode ) this.tree.getRoot()); 263 264 DefaultMutableTreeNode child; 265 DefaultMutableTreeNode parent = (DefaultMutableTreeNode ) this.tree.getRoot(); 266 DefaultMutableTreeNode sibling = null; 267 268 StringTokenizer tokens = new StringTokenizer (className, "."); 270 while (tokens.hasMoreTokens()) { 271 272 String token = tokens.nextToken(); 273 child = new DefaultMutableTreeNode (new Node(token)); 274 275 if (this.tree.getChildCount(parent) == 0) { 276 277 this.tree.insertNodeInto(child, parent, parent.getChildCount()); 278 sibling = child; 279 } 280 else { 281 282 int childIndex = 0; 283 for ( ; childIndex < this.tree.getChildCount(parent); childIndex++) { 284 sibling = (DefaultMutableTreeNode ) this.tree.getChild(parent, childIndex); 285 if (((Node) sibling.getUserObject()).toString().compareTo( 287 ((Node) child.getUserObject()).toString()) == 0) { 288 289 break; 290 } 291 } 292 if (!((Node) sibling.getUserObject()).toString().equals( 293 ((Node) child.getUserObject()).toString())) { 294 295 this.tree.insertNodeInto(child, parent, childIndex); 296 sibling = child; 297 } 298 } 299 300 this.pathList.add(sibling); 301 parent = sibling; 302 } 303 } 304 } | Popular Tags |