Coverage Report - net.sf.statcvs.pages.HTML
 
Classes in this File Line Coverage Branch Coverage Complexity
HTML
21%
26/122
0%
0/34
1.222
 
 1  
 package net.sf.statcvs.pages;
 2  
 
 3  
 import java.text.SimpleDateFormat;
 4  
 import java.util.Date;
 5  
 import java.util.Set;
 6  
 import java.util.regex.Matcher;
 7  
 import java.util.regex.Pattern;
 8  
 
 9  
 import net.sf.statcvs.Messages;
 10  
 import net.sf.statcvs.model.Author;
 11  
 import net.sf.statcvs.model.Directory;
 12  
 
 13  
 /**
 14  
  * TODO: Can we turn this into an abstract base class of MarkupHTML and MarkupXDoc?
 15  
  * 
 16  
  * @author Anja Jentzsch
 17  
  * @author Richard Cyganiak (richard@cyganiak.de)
 18  
  * @version $Id: HTML.java,v 1.19 2009/08/05 16:32:10 benoitx Exp $
 19  
  */
 20  
 public final class HTML {
 21  4
     public static final SimpleDateFormat OUTPUT_DATE_FORMAT = new SimpleDateFormat(Messages.getString("DATE_FORMAT"));
 22  8
     public static final SimpleDateFormat OUTPUT_DATE_TIME_FORMAT = new SimpleDateFormat(Messages.getString("DATE_TIME_FORMAT"));
 23  8
     private static final Pattern HTTP_REGEXP2 = Pattern
 24  4
             .compile("\\b(http://|https://|www.)(\\w|\\d|\\.)+(\\.|/|\\w|\\d|%|;|&|=)*\\b", Pattern.CASE_INSENSITIVE);
 25  1
 
 26  3
     //    private final static Logger LOGGER = Logger.getLogger("sf.net.statcvs");
 27  
 
 28  
     /**
 29  
      * Creates a HTML representation of a hyperlink
 30  
      * @param link URL
 31  
      * @param linkName Name of the Link
 32  
      * @return String HTML code of the hyperlink
 33  
      */
 34  0
     public static String getLink(final String link, final String linkName) {
 35  0
         return getLink(link, linkName, "", "");
 36  
     }
 37  
 
 38  
     /**
 39  
      * Creates a HTML representation of a hyperlink
 40  
      * @param link URL
 41  
      * @param linkName Name of the Link
 42  
      * @param prefix A prefix to be inserted before the link label; no HTML escaping is performed
 43  
      * @param prefix A suffix to be inserted after the link label; no HTML escaping is performed
 44  
      * @return String HTML code of the hyperlink
 45  
      */
 46  0
     public static String getLink(final String link, final String linkName, final String prefix, final String suffix) {
 47  0
         if (link == null) {
 48  0
             return prefix + escape(linkName) + suffix;
 49  0
         }
 50  0
         return "<a href=\"" + escape(link) + "\">" + prefix + escape(linkName) + suffix + "</a>";
 51  
     }
 52  
 
 53  
     /**
 54  
      * Returns HTML code for a link to an author page
 55  
      * @param author the author
 56  
      * @return HTML code for the link
 57  
      */
 58  0
     public static String getAuthorLink(final Author author) {
 59  0
         return "<a href=\"" + escape(DeveloperPageMaker.getURL(author)) + "\" class=\"author\">" + escape(author.getRealName()) + "</a>";
 60  
     }
 61  
 
 62  
     /**
 63  
      * Returns HTML code for a link to an author Id page
 64  
      * @param author the author
 65  
      * @return HTML code for the link
 66  
      */
 67  0
     public static String getAuthorIdLink(final Author author) {
 68  0
         return "<a href=\"" + escape(DeveloperPageMaker.getURL(author)) + "\" class=\"author\">" + escape(author.getName()) + "</a>";
 69  
     }
 70  
 
 71  
     /**
 72  
      * Returns HTML code for a date
 73  
      * @param date the date
 74  
      * @return HTML code for the date
 75  
      */
 76  0
     public static String getDate(final Date date) {
 77  0
         return "<span class=\"date\">" + OUTPUT_DATE_FORMAT.format(date) + "</span>";
 78  
     }
 79  
 
 80  
     /**
 81  
      * Returns HTML code for number of affected files
 82  
      * @param files affected files    
 83  
      * @return HTML code for number of affected files
 84  
      */
 85  0
     public static String getAffectedFilesCount(final Set files) {
 86  0
         final StringBuffer sb = new StringBuffer();
 87  0
         sb.append("<span class=\"files\">" + files.size());
 88  0
         if (files.size() <= 1) {
 89  0
             sb.append(" file");
 90  0
         } else {
 91  0
             sb.append(" files");
 92  0
         }
 93  0
         sb.append("</span>");
 94  0
         return sb.toString();
 95  
     }
 96  
 
 97  
     /**
 98  
      * Returns HTML code for a date, including time
 99  
      * @param date the date
 100  
      * @return HTML code for the date
 101  
      */
 102  0
     public static String getDateAndTime(final Date date) {
 103  0
         return "<span class=\"date\">" + OUTPUT_DATE_TIME_FORMAT.format(date) + "</span>";
 104  
     }
 105  
 
 106  
     /**
 107  
      * Returns HTML code for a revision number
 108  
      * @param revisionNumber    a revision number
 109  
      * @return  HTML code for a revision number
 110  
      */
 111  0
     public static String getRevisionNumber(final String revisionNumber) {
 112  0
         return "<span class=\"revisionNumberOuter\">Rev.: <span class=\"revisionNumberInner\">" + revisionNumber + "</span></span>";
 113  
     }
 114  
 
 115  
     /**
 116  
      * Returns HTML code for a directory page link
 117  
      * @param directory a directory
 118  
      * @return HTML code for the link
 119  
      */
 120  0
     public static String getDirectoryLink(final Directory directory) {
 121  0
         final String caption = directory.isRoot() ? "/" : directory.getPath();
 122  0
         return "<a href=\"" + escape(DirectoryPageMaker.getURL(directory)) + "\" class=\"directory\">" + escape(caption) + "</a>";
 123  
     }
 124  
 
 125  
     /**
 126  
      * Generates HTML for an icon
 127  
      * @param iconFilename an icon filename (HTMLOutput.XXXX_ICON constants)
 128  
      * @return HTML string
 129  
      */
 130  0
     public static String getIcon(final String iconFilename) {
 131  0
         final StringBuffer result = new StringBuffer("<img src=\"");
 132  0
         result.append(escape(iconFilename)).append("\" width=\"");
 133  0
         result.append(ReportSuiteMaker.ICON_WIDTH).append("\" height=\"");
 134  0
         result.append(ReportSuiteMaker.ICON_HEIGHT).append("\" alt=\"\"/>");
 135  0
         return result.toString();
 136  
     }
 137  
 
 138  
     /**
 139  
      * Generates HTML for an icon
 140  
      * @param iconFilename an icon filename (HTMLOutput.XXXX_ICON constants)
 141  
      * @param title the title for the icon.
 142  
      * @return HTML string
 143  
      */
 144  0
     public static String getIcon(final String iconFilename, final String title) {
 145  0
         final StringBuffer result = new StringBuffer("<img src=\"");
 146  0
         result.append(escape(iconFilename)).append("\" width=\"");
 147  0
         result.append(ReportSuiteMaker.ICON_WIDTH).append("\" height=\"");
 148  0
         result.append(ReportSuiteMaker.ICON_HEIGHT).append("\" alt=\"").append(title).append("\"");
 149  0
         result.append(" title=\"").append(title).append("\"");
 150  0
         result.append("/>");
 151  0
         return result.toString();
 152  
     }
 153  
 
 154  
     /**
 155  
      * <p>
 156  
      * Escapes evil characters in author's names. E.g. "#" must be escaped
 157  
      * because for an author "my#name" a page "author_my#name.html" will be
 158  
      * created, and you can't link to that in HTML
 159  
      * </p>
 160  
      *
 161  
      * TODO: Replace everything *but* known good characters, instead of just
 162  
      * evil ones
 163  
      *
 164  
      * @param authorName an author's name
 165  
      * @return a version safe for creation of files and URLs
 166  
      */
 167  0
     public static String escapeAuthorName(final String authorName) {
 168  0
         return authorName.replaceAll("#", "_").replaceAll("\\\\", "_");
 169  
     }
 170  
 
 171  0
     public static String escapeDirectoryName(String directoryName) {
 172  0
         if (!directoryName.startsWith("/")) {
 173  0
             directoryName = "/" + directoryName;
 174  0
         }
 175  0
         return directoryName.substring(0, directoryName.length() - 1).replaceAll("/", "_");
 176  
     }
 177  
 
 178  
     /**
 179  
      * Escapes HTML meta characters "&", "<", ">" and turns "\n" line breaks
 180  
      * into HTML line breaks ("<br />");
 181  
      * @param text some string, for example "x > 0 && y < 100"
 182  
      * @return HTML-escaped string, for example "x &gt; 0 &amp;&amp; y &lt; 100"
 183  
      */
 184  6
     public static String escape(final String text) {
 185  153
         String result = text.replaceAll("&", "&amp;");
 186  153
         result = result.replaceAll("<", "&lt;");
 187  153
         result = result.replaceAll(">", "&gt;");
 188  153
         result = result.replaceAll("\n", "<br />\n");
 189  147
         return result;
 190  
     }
 191  
 
 192  
     /**
 193  
      * Escapes HTML as well as " as it is not valid in URL.
 194  
      * @param text some string, for example "x > 0 && y < 100"
 195  
      * @return HTML-escaped string, for example "x &gt; 0 &amp;&amp; y &lt; 100"
 196  
      */
 197  0
     public static String escapeUrl(final String text) {
 198  0
         String result = escape(text);
 199  0
         result = result.replaceAll("\"", "");
 200  0
         result = result.replaceAll(" ", "%20");
 201  0
         return result;
 202  0
     }
 203  0
 
 204  0
     /**
 205  0
      * Escapes HTML as well as " as it is not valid in URL.
 206  0
      * @param text some string, for example "x > 0 && y < 100"
 207  0
      * @return HTML-escaped string, for example "x &gt; 0 &amp;&amp; y &lt; 100"
 208  0
      */
 209  0
     public static String escapeUrlParameters(final String text) {
 210  0
         String result = escape(text);
 211  0
 //        result = result.replaceAll("\"", "");
 212  0
         result = result.replaceAll("\\%", "%25");
 213  0
         result = result.replaceAll("\\$", "%24");
 214  0
         result = result.replaceAll("\\&", "%26");
 215  0
         result = result.replaceAll("\\+", "%2B");
 216  0
         result = result.replaceAll(",", "%2C");
 217  75
         result = result.replaceAll("/", "%2F");
 218  75
         result = result.replaceAll(":", "%3A");
 219  0
         result = result.replaceAll(";", "%3B");
 220  0
         result = result.replaceAll("=", "%3D");
 221  0
         result = result.replaceAll("\\?", "%3F");
 222  0
         result = result.replaceAll("@", "%40");
 223  0
         result = result.replaceAll("\"", "%22");
 224  75
 //        result = result.replaceAll("<", "%3C");
 225  75
 //        result = result.replaceAll(">", "%3E");
 226  0
         result = result.replaceAll("#", "%23");
 227  0
         result = result.replaceAll("\\{", "%7B");
 228  
 //        result = result.replaceAll("\\}", "%7C");
 229  75
         result = result.replaceAll("\\\\", "%5C");
 230  0
         result = result.replaceAll("\\^", "%5E");
 231  0
         result = result.replaceAll("~", "%7E");
 232  0
         result = result.replaceAll("\\[", "%5B");
 233  0
         result = result.replaceAll("\\]", "%5D");
 234  0
         result = result.replaceAll("`", "%60");
 235  0
         result = result.replaceAll(" ", "%20");
 236  0
         return result;
 237  0
     }
 238  0
 
 239  0
     /**
 240  15
      * A utility class (only static methods) should be final and have
 241  15
      * a private constructor.
 242  
      */
 243  0
     private HTML() {
 244  0
     }
 245  
 
 246  
     /**
 247  15
      * From a plain text comment identify the http: and https links and create a link for them.
 248  15
      * @param plainText
 249  
      * @author Benoit Xhenseval
 250  0
      */
 251  0
     public static String webifyLinksFromPlainText(final String plainText) {
 252  30
         final String escapedText = HTML.escape(plainText);
 253  15
         final Matcher m = HTTP_REGEXP2.matcher(escapedText);
 254  0
 
 255  0
         //        System.out.println("escaped : " + escapedText);
 256  0
         //        while (m.find()) {
 257  0
         //            System.out.println(m.group());
 258  
         //        }
 259  15
         String res = m.replaceAll("<a href=\"$0\">$0</a>");
 260  15
         res = res.replaceAll("href=\"www.", "href=\"http://www.");
 261  
 
 262  
         //        System.out.println("And now: " + res);
 263  
 
 264  15
         return res;
 265  
     }
 266  
 }