KickJava   Java API By Example, From Geeks To Geeks.

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


1 package csdl.jblanket.app.tree;
2
3 import csdl.jblanket.methodset.MethodInfo;
4 import csdl.jblanket.methodset.MethodSet;
5
6 import java.io.IOException JavaDoc;
7 import java.io.OutputStream JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Date JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.StringTokenizer JavaDoc;
12
13 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
14 import javax.swing.tree.DefaultTreeModel JavaDoc;
15 import javax.swing.tree.TreePath JavaDoc;
16
17 /**
18  * Provides a writer for the application. All methods that are individually excluded
19  * by the user are stored in an XML file similar to the other JBlanket files, like
20  * totalMethods.xml.
21  *
22  * @author Joy M. Agustin
23  * @version $Id: TreeWriter.java,v 1.1 2004/11/07 00:32:40 timshadel Exp $
24  */

25 public class TreeWriter {
26   
27   /** Tree to check for individually excluded methods */
28   private DefaultTreeModel JavaDoc tree;
29   /** MethodSet containing all individually excluded methods found in tree */
30   private MethodSet methodSet;
31   
32   /**
33    * Constructs a new TreeWriter object.
34    *
35    * @param tree the tree containing the individually excluded methods.
36    */

37   public TreeWriter(DefaultTreeModel JavaDoc tree) {
38     
39     this.tree = tree;
40     this.methodSet = new MethodSet();
41   }
42   
43   /**
44    * Stores the individually excluded methods to <code>fostream</code>.
45    *
46    * @param ostream the OutputStream to store the tree data to.
47    * @throws IOException if unable to store the tree data to <code>fostream</code>.
48    */

49   public void store(OutputStream JavaDoc ostream) throws IOException JavaDoc {
50
51     fillMethodSet((DefaultMutableTreeNode JavaDoc) tree.getRoot());
52     this.methodSet.store(ostream, "", new Date JavaDoc());
53   }
54   
55   /**
56    * Fills a MethodSet with the excluded methods found in this <code>tree</code>. This method
57    * is recursive.
58    *
59    * @param node the current node to process.
60    */

61   private void fillMethodSet(DefaultMutableTreeNode JavaDoc node) {
62     
63     if (node.isLeaf()) {
64       
65       MethodNode methodNode = (MethodNode) node.getUserObject();
66       if (methodNode.isIndividualExclude()) {
67         
68         // create the fully qualified class name from the node's path in the tree
69
TreePath JavaDoc path = new TreePath JavaDoc(tree.getPathToRoot(node));
70         
71         StringBuffer JavaDoc classBuffer = new StringBuffer JavaDoc();;
72         for (int i = 1; i < path.getPathCount() - 1; i++) {
73           Node nextNode =
74               (Node) ((DefaultMutableTreeNode JavaDoc) path.getPathComponent(i)).getUserObject();
75           classBuffer.append(nextNode.toString());
76           
77           // don't want to add '.<method name>' to the class name
78
if (i < path.getPathCount() - 2) {
79             classBuffer.append(".");
80           }
81         }
82         
83         // get the method name
84
int paramIndex = methodNode.toString().indexOf("(");
85         String JavaDoc methodName = methodNode.toString().substring(0, paramIndex);
86         // get the list of parameters
87
String JavaDoc params = methodNode.toString().substring(paramIndex + 1,
88                                                         methodNode.toString().length() - 1);
89         List JavaDoc paramList = new ArrayList JavaDoc();
90         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(params, ",");
91         while (tokens.hasMoreTokens()) {
92           paramList.add(tokens.nextToken().trim());
93         }
94         // add the method
95
this.methodSet.add(new MethodInfo(classBuffer.toString(), methodName, paramList));
96       }
97     }
98     else {
99       for (int childIndex = 0; childIndex < tree.getChildCount(node); childIndex++) {
100         fillMethodSet((DefaultMutableTreeNode JavaDoc) tree.getChild(node, childIndex));
101       }
102     }
103   }
104 }
Popular Tags