View Javadoc

1   package net.sf.statcvs.pages;
2   
3   import java.util.Iterator;
4   
5   import net.sf.statcvs.Messages;
6   import net.sf.statcvs.model.Directory;
7   
8   /**
9    * Formats a directory and its subdirectories into a tree.
10   *
11   * @author Richard Cyganiak (richard@cyganiak.de)
12   * @version $Id: DirectoryTreeFormatter.java,v 1.6 2008/04/02 11:22:14 benoitx Exp $
13   */
14  public class DirectoryTreeFormatter {
15      private static final int SPACE_COUNT = 4;
16  
17      private final Directory directory;
18      private final boolean withRootLinks;
19  
20      public DirectoryTreeFormatter(final Directory directory, final boolean withRootLinks) {
21          this.directory = directory;
22          this.withRootLinks = withRootLinks;
23      }
24  
25      private String getRootLinks(Directory dir) {
26          String result = getFormattedName(dir, false, true);
27          while (!dir.isRoot()) {
28              final Directory parent = dir.getParent();
29              result = getFormattedName(parent, true, false) + "/" + result;
30              dir = parent;
31          }
32          return result;
33      }
34  
35      public String getFormatted() {
36          final StringBuffer result = new StringBuffer("<p class=\"dirtree\">\n");
37          final Iterator it = this.directory.getSubdirectoriesRecursive().iterator();
38          if (this.withRootLinks) {
39              final Directory current = (Directory) it.next();
40              result.append(getRootLinks(current)).append("<br/>\n");
41          }
42          while (it.hasNext()) {
43              final Directory subdirectory = (Directory) it.next();
44              format(subdirectory, 0, result);
45          }
46          result.append("</p>\n");
47          return result.toString();
48      }
49  
50      private void format(final Directory dir, final int currentDepth, final StringBuffer s) {
51          s.append(getSpaces(dir.getDepth() - currentDepth));
52          if (dir.isEmpty()) {
53              s.append(HTML.getIcon(ReportSuiteMaker.DELETED_DIRECTORY_ICON, Messages.getString("DELETED_DIRECTORY_ICON")));
54          } else {
55              s.append(HTML.getIcon(ReportSuiteMaker.DIRECTORY_ICON, Messages.getString("DIRECTORY_ICON")));
56          }
57          s.append(" \n").append(getFormattedName(dir, true, false));
58          s.append(" \n(").append(dir.getCurrentFileCount()).append(" ");
59          s.append(Messages.getString("DIRECTORY_TREE_FILES")).append(", ");
60          s.append(dir.getCurrentLOC()).append(" ");
61          s.append(Messages.getString("DIRECTORY_TREE_LINES")).append(")<br />\n");
62      }
63  
64      private String getFormattedName(final Directory directory, final boolean link, final boolean bold) {
65          String name = directory.isRoot() ? Messages.getString("NAVIGATION_ROOT") : directory.getName();
66          if (link) {
67              final String url = DirectoryPageMaker.getURL(directory);
68              name = HTML.getLink(url, name);
69          } else {
70              name = HTML.escape(name);
71          }
72          if (bold) {
73              return "<strong>" + name + "</strong>";
74          }
75          return name;
76      }
77  
78      private String getSpaces(final int count) {
79          final StringBuffer result = new StringBuffer();
80          for (int i = 0; i < count * SPACE_COUNT; i++) {
81              result.append("&#160;");
82          }
83          return result.toString();
84      }
85  }