| 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 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 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 | 0 | public DirectoryTreeFormatter(final Directory directory, final boolean withRootLinks) { |
| 21 | 0 | this.directory = directory; |
| 22 | 0 | this.withRootLinks = withRootLinks; |
| 23 | 0 | } |
| 24 | |
|
| 25 | |
private String getRootLinks(Directory dir) { |
| 26 | 0 | String result = getFormattedName(dir, false, true); |
| 27 | 0 | while (!dir.isRoot()) { |
| 28 | 0 | final Directory parent = dir.getParent(); |
| 29 | 0 | result = getFormattedName(parent, true, false) + "/" + result; |
| 30 | 0 | dir = parent; |
| 31 | 0 | } |
| 32 | 0 | return result; |
| 33 | |
} |
| 34 | |
|
| 35 | |
public String getFormatted() { |
| 36 | 0 | final StringBuffer result = new StringBuffer("<p class=\"dirtree\">\n"); |
| 37 | 0 | final Iterator it = this.directory.getSubdirectoriesRecursive().iterator(); |
| 38 | 0 | if (this.withRootLinks) { |
| 39 | 0 | final Directory current = (Directory) it.next(); |
| 40 | 0 | result.append(getRootLinks(current)).append("<br/>\n"); |
| 41 | |
} |
| 42 | 0 | while (it.hasNext()) { |
| 43 | 0 | final Directory subdirectory = (Directory) it.next(); |
| 44 | 0 | format(subdirectory, 0, result); |
| 45 | 0 | } |
| 46 | 0 | result.append("</p>\n"); |
| 47 | 0 | return result.toString(); |
| 48 | |
} |
| 49 | |
|
| 50 | |
private void format(final Directory dir, final int currentDepth, final StringBuffer s) { |
| 51 | 0 | s.append(getSpaces(dir.getDepth() - currentDepth)); |
| 52 | 0 | if (dir.isEmpty()) { |
| 53 | 0 | s.append(HTML.getIcon(ReportSuiteMaker.DELETED_DIRECTORY_ICON, Messages.getString("DELETED_DIRECTORY_ICON"))); |
| 54 | |
} else { |
| 55 | 0 | s.append(HTML.getIcon(ReportSuiteMaker.DIRECTORY_ICON, Messages.getString("DIRECTORY_ICON"))); |
| 56 | |
} |
| 57 | 0 | s.append(" \n").append(getFormattedName(dir, true, false)); |
| 58 | 0 | s.append(" \n(").append(dir.getCurrentFileCount()).append(" "); |
| 59 | 0 | s.append(Messages.getString("DIRECTORY_TREE_FILES")).append(", "); |
| 60 | 0 | s.append(dir.getCurrentLOC()).append(" "); |
| 61 | 0 | s.append(Messages.getString("DIRECTORY_TREE_LINES")).append(")<br />\n"); |
| 62 | 0 | } |
| 63 | |
|
| 64 | |
private String getFormattedName(final Directory directory, final boolean link, final boolean bold) { |
| 65 | 0 | String name = directory.isRoot() ? Messages.getString("NAVIGATION_ROOT") : directory.getName(); |
| 66 | 0 | if (link) { |
| 67 | 0 | final String url = DirectoryPageMaker.getURL(directory); |
| 68 | 0 | name = HTML.getLink(url, name); |
| 69 | 0 | } else { |
| 70 | 0 | name = HTML.escape(name); |
| 71 | |
} |
| 72 | 0 | if (bold) { |
| 73 | 0 | return "<strong>" + name + "</strong>"; |
| 74 | |
} |
| 75 | 0 | return name; |
| 76 | |
} |
| 77 | |
|
| 78 | |
private String getSpaces(final int count) { |
| 79 | 0 | final StringBuffer result = new StringBuffer(); |
| 80 | 0 | for (int i = 0; i < count * SPACE_COUNT; i++) { |
| 81 | 0 | result.append(" "); |
| 82 | |
} |
| 83 | 0 | return result.toString(); |
| 84 | |
} |
| 85 | |
} |