View Javadoc

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.3 2008/04/02 11:52:02 benoitx Exp $
18   */
19  public final class XML {
20      private static final SimpleDateFormat OUTPUT_DATE_FORMAT = new SimpleDateFormat(Messages.getString("DATE_FORMAT"));
21      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          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          if (link == null) {
43              return prefix + escape(linkName) + suffix;
44          }
45          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          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          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          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          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          final String caption = directory.isRoot() ? "/" : directory.getPath();
91          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         final StringBuffer result = new StringBuffer("<img src=\"");
101         result.append(escape(iconFilename)).append("\" width=\"");
102         result.append(ReportSuiteMaker.ICON_WIDTH).append("\" height=\"");
103         result.append(ReportSuiteMaker.ICON_HEIGHT).append("\" alt=\"\"/>");
104         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         return authorName.replaceAll("#", "_").replaceAll("\\\\", "_");
122     }
123 
124     public static String escapeDirectoryName(String directoryName) {
125         if (!directoryName.startsWith("/")) {
126             directoryName = "/" + directoryName;
127         }
128         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         final String result = text; /*.replaceAll("&", "&amp;");
139                       	result = result.replaceAll("<", "&lt;");
140                       	result = result.replaceAll(">", "&gt;");
141                       	result = result.replaceAll("\n", "<br />\n");*/
142         return result;
143     }
144 
145     /**
146      * A utility class (only static methods) should be final and have
147      * a private constructor.
148      */
149     private XML() {
150     }
151 }