KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > jblanket > app > tree > NodeContentHandler


1 package csdl.jblanket.app.tree;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9
10 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
11 import javax.swing.tree.DefaultTreeModel JavaDoc;
12 import javax.swing.tree.TreePath JavaDoc;
13
14 import org.xml.sax.Attributes JavaDoc;
15 import org.xml.sax.ContentHandler JavaDoc;
16 import org.xml.sax.Locator JavaDoc;
17 import org.xml.sax.SAXException JavaDoc;
18
19 /**
20  * Handles the content of an XML file as it is encountered by the SAX parser for the exclude
21  * individual methods application.
22  * <p>
23  * NOTE: Method descriptions taken from org.xml.sax.ContentHandler.
24  *
25  * @author Joy M. Agustin
26  * @version $Id: NodeContentHandler.java,v 1.1 2004/11/07 00:32:40 timshadel Exp $
27  */

28 public class NodeContentHandler implements ContentHandler JavaDoc {
29   
30   /** Locates any content handler event in the XML source document */
31   private Locator JavaDoc locator;
32   /** Container for names spaces */
33   private Map JavaDoc namespaceMappings;
34
35   /** Reference to the tree that will be displayed */
36   private DefaultTreeModel JavaDoc tree;
37   /** List of the DefaultMutableTreeNodes making up the current path */
38   private List JavaDoc pathList;
39   /** Category of methods being processed */
40   private String JavaDoc methodCategory;
41
42   /** Name of the current method element being processed */
43   private String JavaDoc methodName;
44   /** List of parameters of the current method element being processed */
45   private List JavaDoc parameters;
46   
47   /**
48    * Constructs a new NodeContentHandler object.
49    *
50    * @param tree reference to the tree that will be built from the contents of an XML file.
51    * @param methodCategory the category of methods being processed.
52    */

53   public NodeContentHandler(DefaultTreeModel JavaDoc tree, String JavaDoc methodCategory) {
54     super();
55     
56     this.namespaceMappings = new HashMap JavaDoc();
57     this.tree = tree;
58     
59     this.pathList = new ArrayList JavaDoc();
60     this.methodCategory = methodCategory;
61     
62     this.parameters = new ArrayList JavaDoc();
63   }
64
65   /**
66    * Processes notification of character data -- not implemented.
67    *
68    * @param chars the characters making up the data within the element.
69    * @param start the starting index of characters.
70    * @param length the number of characters in <code>chars</code>.
71    * @throws SAXException if any SAX error occurs.
72    */

73   public void characters(char[] chars, int start, int length) throws SAXException JavaDoc {
74   }
75
76   /**
77    * Processes notification of the end of a document -- not implemented.
78    *
79    * @throws SAXException if any SAX error occurs.
80    */

81   public void endDocument() throws SAXException JavaDoc {
82   }
83
84   /**
85    * Processes notification of the end of an element.
86    *
87    * @param namespaceURI namespace URI of current element in context wrt document's complete set of
88    * namespaces.
89    * @param localName local, unprefixed name of current element.
90    * @param qName unmodified, unchanged name of current element.
91    * @throws SAXException if any SAX error occurs.
92    */

93   public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
94     throws SAXException JavaDoc {
95     // when leaving the 'Method' element, add method to tree if doesn't already exist
96
if (localName.equals("Method")) {
97
98       MethodNode methodNode = new MethodNode(this.methodName, this.parameters);
99       DefaultMutableTreeNode JavaDoc method = new DefaultMutableTreeNode JavaDoc(methodNode);
100       // reset parameter list for next 'Method' element
101
this.parameters = new ArrayList JavaDoc();
102
103       // search if current method already exists in the tree
104
TreePath JavaDoc path = new TreePath JavaDoc(this.pathList.toArray(new Object JavaDoc[] { }));
105       DefaultMutableTreeNode JavaDoc parent = (DefaultMutableTreeNode JavaDoc) path.getLastPathComponent();
106       DefaultMutableTreeNode JavaDoc 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 ( /* empty */ ; methodIndex < this.tree.getChildCount(parent); methodIndex++) {
117           sibling = (DefaultMutableTreeNode JavaDoc) this.tree.getChild(parent, methodIndex);
118           // TODO entries into the tree are not sorted.
119
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       // modify the font characteristics as needed
134
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   /**
153    * Processes end of the scope of a prefix-URI mapping.
154    *
155    * @param prefix the prefix that was being mapped.
156    * @throws SAXException if any SAX error occurs.
157    */

158   public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
159     
160     for (Iterator JavaDoc i = this.namespaceMappings.keySet().iterator(); i.hasNext(); ) {
161       String JavaDoc uri = (String JavaDoc) i.next();
162       String JavaDoc thisPrefix = (String JavaDoc) this.namespaceMappings.get(uri);
163
164       if (prefix.equals(thisPrefix)) {
165
166         this.namespaceMappings.remove(uri);
167         break;
168       }
169     }
170   }
171
172   /**
173    * Processes notification of ignorable whitespace in element content -- not implemented.
174    *
175    * @param chars the characters making up the data within the element.
176    * @param start the starting index of characters.
177    * @param length the number of characters in <code>chars</code>.
178
179    * @throws SAXException if any SAX error occurs.
180    */

181   public void ignorableWhitespace(char[] chars, int start, int length) throws SAXException JavaDoc {
182   }
183
184   /**
185    * Processes notification of a processing instruction -- not implemented.
186    *
187    * @param target the processing instruction target.
188    * @param data the processing instruction data, or null if none was supplied.
189    * @throws SAXException if any SAX error occurs.
190    */

191   public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
192   }
193
194   /**
195    * Processes an object for locating the origin of SAX document events.
196    *
197    * @param locator an object that can return the location of any SAX document event.
198    */

199   public void setDocumentLocator(Locator JavaDoc locator) {
200     this.locator = locator;
201   }
202
203   /**
204    * Processes notification of a skipped entity -- not implemented.
205    *
206    * @param name the name of the skipped entry.
207    * @throws SAXException if any SAX error occurs.
208    */

209   public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
210   }
211
212   /**
213    * Processes notification of the beginning of a document - not implememented.
214    *
215    * @throws SAXException if any SAX error occurs.
216    */

217   public void startDocument() throws SAXException JavaDoc {
218   }
219
220   /**
221    * Processes notification of the beginning of an element.
222    *
223    * @param namespaceURI namespace URI of current element in context wrt document's complete set of
224    * namespaces.
225    * @param localName local, unprefixed name of current element.
226    * @param qName unmodified, unchanged name of current element.
227    * @param atts reference to all of the attributes within current element.
228    * @throws SAXException if any SAX error occurs.
229    */

230   public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
231     throws SAXException JavaDoc {
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   /**
245    * Processes begin of the scope of a prefix-URI Namespace mapping.
246    *
247    * @param prefix the namespace prefix being declared.
248    * @param uri the namespace URI the prefix is mapped to.
249    * @throws SAXException if any SAX error occurs.
250    */

251   public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
252     this.namespaceMappings.put(uri, prefix);
253   }
254   
255   /**
256    * Processes the fully qualified name of a class into a tree.
257    *
258    * @param className the fully qualified class name.
259    */

260   private void processClassAttribute(String JavaDoc className) {
261     // begin building list of nodes in current path
262
this.pathList.add((DefaultMutableTreeNode JavaDoc) this.tree.getRoot());
263
264     DefaultMutableTreeNode JavaDoc child;
265     DefaultMutableTreeNode JavaDoc parent = (DefaultMutableTreeNode JavaDoc) this.tree.getRoot();
266     DefaultMutableTreeNode JavaDoc sibling = null;
267
268     // find equivalent node, or create node as the last child
269
StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(className, ".");
270     while (tokens.hasMoreTokens()) {
271
272       String JavaDoc token = tokens.nextToken();
273       child = new DefaultMutableTreeNode JavaDoc(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 ( /* empty */ ; childIndex < this.tree.getChildCount(parent); childIndex++) {
284           sibling = (DefaultMutableTreeNode JavaDoc) this.tree.getChild(parent, childIndex);
285           // TODO creation does not sort
286
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