Coverage Report - net.sf.statcvs.pages.Page
 
Classes in this File Line Coverage Branch Coverage Complexity
Page
0%
0/199
0%
0/92
2
 
 1  
 package net.sf.statcvs.pages;
 2  
 
 3  
 import java.io.FileWriter;
 4  
 import java.io.IOException;
 5  
 import java.text.DecimalFormat;
 6  
 import java.text.NumberFormat;
 7  
 import java.util.ArrayList;
 8  
 import java.util.Collections;
 9  
 import java.util.Date;
 10  
 import java.util.Iterator;
 11  
 import java.util.List;
 12  
 import java.util.logging.Logger;
 13  
 
 14  
 import net.sf.statcvs.Messages;
 15  
 import net.sf.statcvs.charts.ChartImage;
 16  
 import net.sf.statcvs.model.Directory;
 17  
 import net.sf.statcvs.output.ReportConfig;
 18  
 import net.sf.statcvs.renderer.TableRenderer;
 19  
 import net.sf.statcvs.reports.TableReport;
 20  
 
 21  
 public class Page implements NavigationNode {
 22  0
     private final static NumberFormat[] DOUBLE_FORMATS = { new DecimalFormat("0"), new DecimalFormat("0.0"), new DecimalFormat("0.00"),
 23  
             new DecimalFormat("0.000"), new DecimalFormat("0.0000") };
 24  0
     private final static Logger logger = Logger.getLogger("sf.net.statcvs");
 25  
 
 26  
     private final ReportConfig config;
 27  
     private final String fileName;
 28  
     private final String shortTitle;
 29  
     private final String fullTitle;
 30  
     private final MarkupSyntax outputFormat;
 31  0
     private StringBuffer contents = new StringBuffer();
 32  0
     private NavigationNode parent = null;
 33  0
     private String siblingsTitle = null;
 34  0
     private List siblings = Collections.EMPTY_LIST;
 35  0
     private final List children = new ArrayList(0);
 36  0
     private final List attributeKeys = new ArrayList(0);
 37  0
     private final List attributeValues = new ArrayList(0);
 38  0
     private boolean showLinkToPreviousSibling = false;
 39  0
     private boolean inSection = false;
 40  0
     private boolean written = false;
 41  
 
 42  
     /**
 43  
      * Creates a new page.
 44  
      * @param config The configuration to use
 45  
      * @param fileName File name for the page, <em>without</em> file extension
 46  
      * @param shortTitle A short navigation title
 47  
      * @param fullTitle A full headline title
 48  
      */
 49  0
     public Page(final ReportConfig config, final String fileName, final String shortTitle, final String fullTitle) {
 50  0
         this.config = config;
 51  0
         this.fileName = fileName;
 52  0
         this.shortTitle = shortTitle;
 53  0
         this.fullTitle = fullTitle;
 54  0
         this.outputFormat = config.getMarkup();
 55  0
     }
 56  
 
 57  
     /* (non-Javadoc)
 58  
      * @see net.sf.statcvs.pages.NavigationNode#setParent(net.sf.statcvs.pages.NavigationNode)
 59  
      */
 60  
     public void setParent(final NavigationNode parent) {
 61  0
         this.parent = parent;
 62  0
     }
 63  
 
 64  
     /**
 65  
      * Sets a list of {@link Page}s that are siblings of this page.
 66  
      * The generated page will contain a navigation list that links
 67  
      * to all siblings. The sibling list may contain the page
 68  
      * itself.
 69  
      * @param siblingsTitle Title for navigation list, e.g. "Monthly Reports"
 70  
      * @param sibling A list of {@link Page}s
 71  
      */
 72  
     public void setSiblings(final String siblingsTitle, final List siblingPages) {
 73  0
         this.siblingsTitle = siblingsTitle;
 74  0
         this.siblings = siblingPages;
 75  0
     }
 76  
 
 77  
     public void addChild(final NavigationNode child) {
 78  0
         this.children.add(child);
 79  0
         child.setParent(this);
 80  0
     }
 81  
 
 82  
     /* (non-Javadoc)
 83  
      * @see net.sf.statcvs.pages.NavigationNode#getURL()
 84  
      */
 85  
     public String getURL() {
 86  0
         return this.fileName + ".html";
 87  
     }
 88  
 
 89  
     /* (non-Javadoc)
 90  
      * @see net.sf.statcvs.pages.NavigationNode#getShortTitle()
 91  
      */
 92  
     public String getShortTitle() {
 93  0
         return this.shortTitle;
 94  
     }
 95  
 
 96  
     /* (non-Javadoc)
 97  
      * @see net.sf.statcvs.pages.NavigationNode#getFullTitle()
 98  
      */
 99  
     public String getFullTitle() {
 100  0
         return this.fullTitle;
 101  
     }
 102  
 
 103  
     public void setShowLinkToPreviousSibling(final boolean showLink) {
 104  0
         this.showLinkToPreviousSibling = showLink;
 105  0
     }
 106  
 
 107  
     public void addAttribute(final String key, final int value) {
 108  0
         addAttribute(key, Integer.toString(value));
 109  0
     }
 110  
 
 111  
     public void addAttribute(final String key, final int value, final String unit) {
 112  0
         addAttribute(key, Integer.toString(value) + " " + unit);
 113  0
     }
 114  
 
 115  
     public void addAttribute(final String key, final Date value) {
 116  0
         addRawAttribute(key, HTML.getDateAndTime(value));
 117  0
     }
 118  
 
 119  
     public void addAttribute(final String key, final String value) {
 120  0
         addRawAttribute(key, HTML.escape(value));
 121  0
     }
 122  
 
 123  
     public void addAttribute(final String key, final double value, final int decimalPlaces) {
 124  0
         addAttribute(key, DOUBLE_FORMATS[decimalPlaces].format(value));
 125  0
     }
 126  
 
 127  
     public void addAttribute(final String key, final double value, final int decimalPlaces, final String unit) {
 128  0
         addAttribute(key, DOUBLE_FORMATS[decimalPlaces].format(value) + " " + unit);
 129  0
     }
 130  
 
 131  
     public void addRawAttribute(final String key, final String rawValue) {
 132  0
         this.attributeKeys.add(key);
 133  0
         this.attributeValues.add(rawValue);
 134  0
     }
 135  
 
 136  
     public void addRawContent(final String s) {
 137  0
         this.contents.append(s);
 138  0
     }
 139  
 
 140  
     public void addSection(final String title) {
 141  0
         if (this.inSection) {
 142  0
             this.contents.append(this.outputFormat.endSection2());
 143  
         }
 144  0
         this.contents.append(this.outputFormat.startSection2(title));
 145  0
         this.inSection = true;
 146  0
     }
 147  
 
 148  
     public void addLink(final String url, final String text) {
 149  0
         this.addRawContent("<p>" + HTML.getLink(url, text) + "</p>\n");
 150  0
     }
 151  
 
 152  
     public void add(final ChartImage chart) {
 153  0
         if (chart == null) {
 154  0
             return;
 155  
         }
 156  0
         addRawContent("<p class=\"chart\"><img src=\"" + HTML.escape(chart.getURL()) + "\" alt=\"" + HTML.escape(chart.getFullTitle()) + "\" width=\""
 157  
                 + chart.getWidth() + "\" height=\"" + chart.getHeight() + "\" /></p>");
 158  0
         chart.write();
 159  0
     }
 160  
 
 161  
     public void add(final ChartImage chart, final String linkURL) {
 162  0
         if (chart == null) {
 163  0
             return;
 164  
         }
 165  0
         addRawContent("<p class=\"chart\"><a href=\"" + HTML.escape(linkURL) + "\"><img src=\"" + HTML.escape(chart.getURL()) + "\" alt=\""
 166  
                 + HTML.escape(chart.getFullTitle()) + "\" width=\"" + chart.getWidth() + "\" height=\"" + chart.getHeight() + "\" /></a></p>");
 167  0
         chart.write();
 168  0
     }
 169  
 
 170  
     public void add(final TableReport table) {
 171  0
         table.calculate();
 172  0
         addRawContent(new TableRenderer(table.getTable(), this.outputFormat).getRenderedTable());
 173  0
     }
 174  
 
 175  
     public void add(final Directory directory, final boolean withRootLinks) {
 176  0
         addRawContent(new DirectoryTreeFormatter(directory, withRootLinks).getFormatted());
 177  0
     }
 178  
 
 179  
     public void add(final PageGroup pages) {
 180  0
         addRawContent(pages.asLinkList());
 181  0
         addChild(pages);
 182  0
     }
 183  
 
 184  
     /* (non-Javadoc)
 185  
      * @see net.sf.statcvs.pages.NavigationNode#write()
 186  
      */
 187  
     public void write() {
 188  0
         if (this.written) {
 189  0
             return;
 190  
         }
 191  0
         if (this.inSection) {
 192  0
             this.contents.append(this.outputFormat.endSection2());
 193  
         }
 194  0
         final Iterator it = this.children.iterator();
 195  0
         while (it.hasNext()) {
 196  0
             final NavigationNode page = (NavigationNode) it.next();
 197  0
             page.setParent(this);
 198  0
             page.write();
 199  0
         }
 200  0
         final String fileWithExtension = this.fileName + "." + this.config.getMarkup().getExtension();
 201  0
         logger.info("writing page '" + this.fullTitle + "' to " + fileWithExtension);
 202  0
         FileWriter w = null;
 203  0
         try {
 204  0
             w = new FileWriter(this.config.getRootDirectory() + fileWithExtension);
 205  0
             w.write(this.outputFormat.getHeader(this.fullTitle, this.config.getCssHandler().getLink(), config.getCharSet()));
 206  0
             w.write(this.outputFormat.startSection1(this.fullTitle));
 207  0
             w.write(getLinkToParent());
 208  0
             w.write(getNavigationLinks());
 209  0
             w.write(getAttributes());
 210  0
             w.write(this.contents.toString());
 211  0
             w.write(getLinkToPreviousSibling());
 212  0
             w.write(this.outputFormat.endSection1());
 213  0
             w.write(getGeneratedByBlock());
 214  0
             w.write(this.outputFormat.getEndOfPage());
 215  0
         } catch (final IOException ex) {
 216  0
             logger.warning(ex.getMessage());
 217  0
         } finally {
 218  0
             if (w != null) {
 219  0
                 try {
 220  0
                     w.close();
 221  0
                 } catch (final IOException e) {
 222  0
                     logger.warning(e.getMessage());
 223  0
                 }
 224  0
             }
 225  0
         }
 226  0
         this.written = true;
 227  
 
 228  0
         // Free memory? Not sure if this has any effect ...
 229  0
         this.contents = null;
 230  0
     }
 231  
 
 232  0
     public String asParentLink() {
 233  0
         String result = "&#171; " + HTML.getLink(getURL(), getShortTitle());
 234  0
         if (this.parent != null) {
 235  0
             result = this.parent.asParentLink() + " " + result;
 236  0
         }
 237  0
         return result;
 238  
     }
 239  0
 
 240  0
     private String getLinkToParent() {
 241  0
         if (this.parent == null) {
 242  0
             return "";
 243  0
         }
 244  0
         return "<div id=\"parentlink\">" + this.parent.asParentLink() + "</div>\n";
 245  0
     }
 246  0
 
 247  0
     private String getNavigationLinks() {
 248  0
         if (this.siblingsTitle == null || this.siblings.isEmpty()) {
 249  0
             return "";
 250  0
         }
 251  0
         final StringBuffer s = new StringBuffer();
 252  0
         s.append(this.outputFormat.startSection2(this.siblingsTitle, "nav"));
 253  0
         s.append("<ul>\n");
 254  0
         final Iterator it = this.siblings.iterator();
 255  0
         while (it.hasNext()) {
 256  0
             final NavigationNode sibling = (NavigationNode) it.next();
 257  0
             s.append("    <li>");
 258  0
             if (sibling == this) {
 259  0
                 s.append("<span class=\"here\">" + HTML.escape(sibling.getShortTitle()) + "</span>");
 260  0
             } else {
 261  0
                 s.append(HTML.getLink(sibling.getURL(), sibling.getShortTitle()));
 262  0
             }
 263  0
             s.append("</li>\n");
 264  0
         }
 265  0
         s.append("</ul>\n");
 266  0
         s.append(this.outputFormat.endSection2());
 267  0
         return s.toString();
 268  0
     }
 269  0
 
 270  0
     private String getAttributes() {
 271  0
         if (this.attributeKeys.isEmpty()) {
 272  0
             return "";
 273  0
         }
 274  0
         final StringBuffer s = new StringBuffer();
 275  0
         s.append("<dl class=\"attributes\">\n");
 276  0
         for (int i = 0; i < this.attributeKeys.size(); i++) {
 277  0
             final String key = (String) this.attributeKeys.get(i);
 278  0
             final String value = (String) this.attributeValues.get(i);
 279  0
             s.append("    <dt>" + HTML.escape(key) + ":</dt>\n");
 280  0
             s.append("    <dd>" + value + "</dd>\n");
 281  0
         }
 282  0
         s.append("</dl>\n");
 283  0
         return s.toString();
 284  0
     }
 285  
 
 286  0
     private String getGeneratedByBlock() {
 287  0
         final StringBuffer s = new StringBuffer();
 288  0
         s.append("<div id=\"generatedby\">");
 289  0
         s.append(Messages.getString("PAGE_GENERATED_BY"));
 290  0
         s.append(" ");
 291  0
         s.append(HTML.getLink(Messages.getString("PROJECT_URL"), Messages.getString("PROJECT_SHORTNAME")) + " " + Messages.getString("PROJECT_VERSION"));
 292  0
         s.append("</div>\n");
 293  0
         return s.toString();
 294  
     }
 295  0
 
 296  0
     private String getLinkToPreviousSibling() {
 297  0
         if (!this.showLinkToPreviousSibling) {
 298  0
             return "";
 299  0
         }
 300  0
         final NavigationNode sibling = findPreviousSibling();
 301  0
         if (sibling == null) {
 302  0
             return "";
 303  0
         }
 304  0
         return "<p class=\"previous\">" + HTML.getLink(sibling.getURL(), sibling.getShortTitle()) + " &#187; </p>\n";
 305  0
     }
 306  0
 
 307  0
     private NavigationNode findPreviousSibling() {
 308  0
         final Iterator it = this.siblings.iterator();
 309  0
         while (it.hasNext()) {
 310  0
             final NavigationNode sibling = (NavigationNode) it.next();
 311  0
             if (sibling != this) {
 312  0
                 continue;
 313  0
             }
 314  0
             if (!it.hasNext()) {
 315  0
                 return null;
 316  0
             }
 317  0
             return (NavigationNode) it.next();
 318  0
         }
 319  0
         return null;
 320  
     }
 321  
 }