001: package csdl.jblanket.app.tree;
002:
003: import csdl.jblanket.methodset.MethodInfo;
004: import csdl.jblanket.methodset.MethodSet;
005:
006: import java.io.IOException;
007: import java.io.OutputStream;
008: import java.util.ArrayList;
009: import java.util.Date;
010: import java.util.List;
011: import java.util.StringTokenizer;
012:
013: import javax.swing.tree.DefaultMutableTreeNode;
014: import javax.swing.tree.DefaultTreeModel;
015: import javax.swing.tree.TreePath;
016:
017: /**
018: * Provides a writer for the application. All methods that are individually excluded
019: * by the user are stored in an XML file similar to the other JBlanket files, like
020: * totalMethods.xml.
021: *
022: * @author Joy M. Agustin
023: * @version $Id: TreeWriter.java,v 1.1 2004/11/07 00:32:40 timshadel Exp $
024: */
025: public class TreeWriter {
026:
027: /** Tree to check for individually excluded methods */
028: private DefaultTreeModel tree;
029: /** MethodSet containing all individually excluded methods found in tree */
030: private MethodSet methodSet;
031:
032: /**
033: * Constructs a new TreeWriter object.
034: *
035: * @param tree the tree containing the individually excluded methods.
036: */
037: public TreeWriter(DefaultTreeModel tree) {
038:
039: this .tree = tree;
040: this .methodSet = new MethodSet();
041: }
042:
043: /**
044: * Stores the individually excluded methods to <code>fostream</code>.
045: *
046: * @param ostream the OutputStream to store the tree data to.
047: * @throws IOException if unable to store the tree data to <code>fostream</code>.
048: */
049: public void store(OutputStream ostream) throws IOException {
050:
051: fillMethodSet((DefaultMutableTreeNode) tree.getRoot());
052: this .methodSet.store(ostream, "", new Date());
053: }
054:
055: /**
056: * Fills a MethodSet with the excluded methods found in this <code>tree</code>. This method
057: * is recursive.
058: *
059: * @param node the current node to process.
060: */
061: private void fillMethodSet(DefaultMutableTreeNode node) {
062:
063: if (node.isLeaf()) {
064:
065: MethodNode methodNode = (MethodNode) node.getUserObject();
066: if (methodNode.isIndividualExclude()) {
067:
068: // create the fully qualified class name from the node's path in the tree
069: TreePath path = new TreePath(tree.getPathToRoot(node));
070:
071: StringBuffer classBuffer = new StringBuffer();
072: ;
073: for (int i = 1; i < path.getPathCount() - 1; i++) {
074: Node nextNode = (Node) ((DefaultMutableTreeNode) path
075: .getPathComponent(i)).getUserObject();
076: classBuffer.append(nextNode.toString());
077:
078: // don't want to add '.<method name>' to the class name
079: if (i < path.getPathCount() - 2) {
080: classBuffer.append(".");
081: }
082: }
083:
084: // get the method name
085: int paramIndex = methodNode.toString().indexOf("(");
086: String methodName = methodNode.toString().substring(0,
087: paramIndex);
088: // get the list of parameters
089: String params = methodNode.toString().substring(
090: paramIndex + 1,
091: methodNode.toString().length() - 1);
092: List paramList = new ArrayList();
093: StringTokenizer tokens = new StringTokenizer(params,
094: ",");
095: while (tokens.hasMoreTokens()) {
096: paramList.add(tokens.nextToken().trim());
097: }
098: // add the method
099: this .methodSet.add(new MethodInfo(classBuffer
100: .toString(), methodName, paramList));
101: }
102: } else {
103: for (int childIndex = 0; childIndex < tree
104: .getChildCount(node); childIndex++) {
105: fillMethodSet((DefaultMutableTreeNode) tree.getChild(
106: node, childIndex));
107: }
108: }
109: }
110: }
|