Coverage Report - net.sf.statcvs.pages.xml.XML
 
Classes in this File Line Coverage Branch Coverage Complexity
XML
0%
0/25
0%
0/6
1.25
 
 1  
 package net.sf.statcvs.pages.xml;
 2  
 
 3  
 import java.text.SimpleDateFormat;
 4  
 import java.util.Date;
 5  
 
 6  
 import net.sf.statcvs.Messages;
 7  
 import net.sf.statcvs.model.Author;
 8  
 import net.sf.statcvs.model.Directory;
 9  
 import net.sf.statcvs.pages.DirectoryPageMaker;
 10  
 import net.sf.statcvs.pages.ReportSuiteMaker;
 11  
 
 12  
 /**
 13  
  * TODO: Can we turn this into an abstract base class of MarkupHTML and MarkupXDoc?
 14  
  *
 15  
  * @author Anja Jentzsch
 16  
  * @author Richard Cyganiak (richard@cyganiak.de)
 17  
  * @version $Id: XML.java,v 1.5 2009/03/13 23:04:28 benoitx Exp $
 18  
  */
 19  
 public final class XML {
 20  0
     private static final SimpleDateFormat OUTPUT_DATE_FORMAT = new SimpleDateFormat(Messages.getString("DATE_FORMAT"));
 21  0
     private static final SimpleDateFormat OUTPUT_DATE_TIME_FORMAT = new SimpleDateFormat(Messages.getString("DATE_TIME_FORMAT"));
 22  
 
 23  
     /**
 24  
      * Creates a HTML representation of a hyperlink
 25  
      * @param link URL
 26  
      * @param linkName Name of the Link
 27  
      * @return String HTML code of the hyperlink
 28  
      */
 29  
     public static String getLink(final String link, final String linkName) {
 30  0
         return getLink(link, linkName, "", "");
 31  
     }
 32  
 
 33  
     /**
 34  
      * Creates a HTML representation of a hyperlink
 35  
      * @param link URL
 36  
      * @param linkName Name of the Link
 37  
      * @param prefix A prefix to be inserted before the link label; no HTML escaping is performed
 38  
      * @param prefix A suffix to be inserted after the link label; no HTML escaping is performed
 39  
      * @return String HTML code of the hyperlink
 40  
      */
 41  
     public static String getLink(final String link, final String linkName, final String prefix, final String suffix) {
 42  0
         if (link == null) {
 43  0
             return prefix + escape(linkName) + suffix;
 44  
         }
 45  0
         return "<a href=\"" + escape(link) + "\">" + prefix + escape(linkName) + suffix + "</a>";
 46  
     }
 47  
 
 48  
     /**
 49  
      * Returns HTML code for a link to an author page
 50  
      * @param author the author
 51  
      * @return HTML code for the link
 52  
      */
 53  
     public static String getAuthorLink(final Author author) {
 54  0
         return escape(author.getName());
 55  
     }
 56  
 
 57  
     /**
 58  
      * Returns HTML code for a link to an author Id page
 59  
      * @param author the author
 60  
      * @return HTML code for the link
 61  
      */
 62  
     public static String getAuthorIdLink(final Author author) {
 63  0
         return escape(author.getName());
 64  
     }
 65  
 
 66  
     /**
 67  
      * Returns HTML code for a date
 68  
      * @param date the date
 69  
      * @return HTML code for the date
 70  
      */
 71  
     public static String getDate(final Date date) {
 72  0
         return OUTPUT_DATE_FORMAT.format(date);
 73  
     }
 74  
 
 75  
     /**
 76  
      * Returns HTML code for a date, including time
 77  
      * @param date the date
 78  
      * @return HTML code for the date
 79  
      */
 80  
     public static String getDateAndTime(final Date date) {
 81  0
         return OUTPUT_DATE_TIME_FORMAT.format(date);
 82  
     }
 83  
 
 84  
     /**
 85  
      * Returns HTML code for a directory page link
 86  
      * @param directory a directory
 87  
      * @return HTML code for the link
 88  
      */
 89  
     public static String getDirectoryLink(final Directory directory) {
 90  0
         final String caption = directory.isRoot() ? "/" : directory.getPath();
 91  0
         return escape(DirectoryPageMaker.getURL(directory)) + escape(caption);
 92  
     }
 93  
 
 94  
     /**
 95  
      * Generates HTML for an icon
 96  
      * @param iconFilename an icon filename (HTMLOutput.XXXX_ICON constants)
 97  
      * @return HTML string
 98  
      */
 99  
     public static String getIcon(final String iconFilename) {
 100  0
         final StringBuffer result = new StringBuffer("<img src=\"");
 101  0
         result.append(escape(iconFilename)).append("\" width=\"");
 102  0
         result.append(ReportSuiteMaker.ICON_WIDTH).append("\" height=\"");
 103  0
         result.append(ReportSuiteMaker.ICON_HEIGHT).append("\" alt=\"\"/>");
 104  0
         return result.toString();
 105  
     }
 106  
 
 107  
     /**
 108  
      * <p>
 109  
      * Escapes evil characters in author's names. E.g. "#" must be escaped
 110  
      * because for an author "my#name" a page "author_my#name.html" will be
 111  
      * created, and you can't link to that in HTML
 112  
      * </p>
 113  
      *
 114  
      * TODO: Replace everything *but* known good characters, instead of just
 115  
      * evil ones
 116  
      *
 117  
      * @param authorName an author's name
 118  
      * @return a version safe for creation of files and URLs
 119  
      */
 120  
     public static String escapeAuthorName(final String authorName) {
 121  0
         return authorName.replaceAll("#", "_").replaceAll("\\\\", "_");
 122  
     }
 123  
 
 124  
     public static String escapeDirectoryName(String directoryName) {
 125  0
         if (!directoryName.startsWith("/")) {
 126  0
             directoryName = "/" + directoryName;
 127  
         }
 128  0
         return directoryName.substring(0, directoryName.length() - 1).replaceAll("/", "_");
 129  
     }
 130  
 
 131  
     /**
 132  
      * Escapes HTML meta characters "&", "<", ">" and turns "\n" line breaks
 133  
      * into HTML line breaks ("<br />");
 134  
      * @param text some string, for example "x > 0 && y < 100"
 135  
      * @return HTML-escaped string, for example "x &gt; 0 &amp;&amp; y &lt; 100"
 136  
      */
 137  
     public static String escape(final String text) {
 138  0
         final String result = text; /*.replaceAll("&", "&amp;");
 139  
                                             result = result.replaceAll("<", "&lt;");
 140  
                                             result = result.replaceAll(">", "&gt;");
 141  
                                             result = result.replaceAll("\n", "<br />\n");*/
 142  0
         return result;
 143  
     }
 144  
 
 145  
     /**
 146  
      * A utility class (only static methods) should be final and have
 147  
      * a private constructor.
 148  
      */
 149  0
     private XML() {
 150  0
     }
 151  
 }