View Javadoc

1   package net.sf.statcvs.pages.xml;
2   
3   /**
4    * @author Nilendra Weerasinghe (nilendraw@gmail.com)
5    * @version $Id: DirectoriesXml.java,v 1.2 2008/04/02 11:22:16 benoitx Exp $
6    *
7    * This is the class which generates the per directory information of the xml report
8    */
9   
10  import java.util.Iterator;
11  import java.util.SortedSet;
12  import java.util.Stack;
13  
14  import net.sf.statcvs.model.Directory;
15  import net.sf.statcvs.model.Repository;
16  import net.sf.statcvs.output.ReportConfig;
17  
18  import org.jdom.Element;
19  
20  public class DirectoriesXml {
21      private final SortedSet directory;
22      private final Repository repository;
23  
24      /**
25       * @param config Configuration and data for the report suite
26       */
27      public DirectoriesXml(final ReportConfig config) {
28          this.repository = config.getRepository();
29          directory = this.repository.getDirectories();
30      }
31  
32      /**
33       * returns jdom element which contains data extracted from the SortedSet of directories
34       *
35       * @returns Element
36       */
37      public Element toFile() {
38          final Element dir = new Element(XmlTags.TAG_DIRECTORIES);
39          final Iterator it = this.directory.iterator();
40  
41          //        int depth = -1;
42          final Element parent = dir;
43          final Stack stack = new Stack();
44          stack.add(dir);
45          while (it.hasNext()) {
46              final Directory direc = (Directory) it.next();
47              final Element item = format(direc, 0);
48  
49              //            if (direc.getDepth() < depth) {
50              //                int toReach = depth - direc.getDepth();
51              //                while (toReach-- > 0) {
52              //                    parent = (Element) stack.pop();
53              //                }
54              //            } else if (direc.getDepth() == depth) {
55              //                parent = (Element) stack.pop();
56              //            }
57              //            System.err.println("Directory depth " + direc.getDepth() + " " + depth + " " + getFormattedName(direc) + " " + parent.getName());
58  
59              parent.addContent(item);
60              //            if (direc.getDepth() >= depth) {
61              //                stack.push(parent);
62              //                parent = item;
63              //            }
64              //            depth = direc.getDepth();
65          }
66  
67          return dir;
68      }
69  
70      /**
71       * returns jdom element properly formatted with the attributes and child elements
72       *
73       * @param Directory the current directory
74       * @param int the depth of the directory
75       */
76      private Element format(final Directory dir, final int currentDepth) {
77          final Element element = new Element(XmlTags.TAG_DIRECTORY);
78          final Element path = new Element(XmlTags.TAG_PATH);
79          //        path.setText(getFormattedName(dir));
80          path.setText(dir.getPath());
81          if (dir.isEmpty()) {
82              element.setAttribute("status", "deleted");
83          }
84          final Element child1 = new Element(XmlTags.TAG_FILES);
85          final String str = Integer.toString(dir.getCurrentFileCount());
86          child1.setText(str);
87          final Element child2 = new Element(XmlTags.TAG_LINES_CHANGED);
88          final String str2 = Integer.toString(dir.getCurrentLOC());
89          child2.setText(str2);
90          element.addContent(path);
91          element.addContent(child1);
92          element.addContent(child2);
93          return element;
94      }
95  }