dk.netarkivet.common.webinterface
Class HTMLUtils

java.lang.Object
  extended by dk.netarkivet.common.webinterface.HTMLUtils

public class HTMLUtils
extends java.lang.Object

This is a utility class containing methods for use in the GUI for netarkivet.


Method Summary
static java.lang.String decode(java.lang.String s)
          Url decodes a string encoded in UTF-8.
static java.lang.String encode(java.lang.String s)
          Url encodes a string in UTF-8.
static java.lang.String encodeAndEscapeHTML(java.lang.String input)
          Encode a string for use in a URL, then escape characters that must be escaped in HTML.
static java.lang.String escapeHtmlValues(java.lang.String input)
          Escapes HTML special characters ", &, < and > (but not ').
static java.lang.String escapeJavascriptQuotes(java.lang.String input)
          Escapes a string for use in javascript.
static void forwardOnEmptyParameter(javax.servlet.jsp.PageContext context, java.lang.String... parameters)
          Checks that the given parameters exist and are not empty.
static void forwardOnIllegalParameter(javax.servlet.jsp.PageContext context, java.lang.String parameter, java.lang.String... legalValues)
          Checks that the given parameter exists and is one of a set of values.
static void forwardOnMissingParameter(javax.servlet.jsp.PageContext context, java.lang.String... parameters)
          Checks that the given parameters exist.
static void forwardWithErrorMessage(javax.servlet.jsp.PageContext context, I18n I18N, java.lang.String label, java.lang.Object... args)
          Forward to our standard error message page with an internationalized message.
static void forwardWithErrorMessage(javax.servlet.jsp.PageContext context, I18n i18n, java.lang.Throwable e, java.lang.String label, java.lang.Object... args)
          Forward to our standard error message page with an internationalized message, in case of exception.
static void forwardWithRawErrorMessage(javax.servlet.jsp.PageContext context, I18n i18n, java.lang.String label, java.lang.Object... args)
          Forward to our standard error message page with an internationalized message.
static void generateFooter(javax.servlet.jsp.JspWriter out)
          Writes out footer information to close the page.
static void generateHeader(javax.servlet.jsp.PageContext context)
          Prints the header information for the webpages in the GUI.
static void generateHeader(javax.servlet.jsp.PageContext context, long refreshInSeconds)
          Prints the header information for the webpages in the GUI.
static void generateHeader(javax.servlet.jsp.PageContext context, java.lang.String... jsToInclude)
          Prints the header information for the webpages in the GUI.
static void generateHeader(java.lang.String title, long refreshInSeconds, javax.servlet.jsp.PageContext context)
          Prints the header information for the webpages in the GUI.
static void generateHeader(java.lang.String title, javax.servlet.jsp.PageContext context, java.lang.String... jsToInclude)
          Prints the header information for the webpages in the GUI.
static java.lang.String getLocale(javax.servlet.http.HttpServletRequest request)
          Get a locale from cookie, if present.
static java.util.Locale getLocaleObject(javax.servlet.jsp.PageContext context)
          Get the locale according to header context information.
static java.lang.String getRowClass(int rowCount)
          Get the (CSS) class name for a row in a table.
static java.lang.String getTitle(java.lang.String url, java.util.Locale locale)
          Given a URL in the sitesection hierarchy, returns the corresponding page title.
static java.lang.String localiseLong(long i, java.util.Locale locale)
          Create a localized string representation of the given long.
static java.lang.String localiseLong(long i, javax.servlet.jsp.PageContext context)
          Create a localized string representation of the given long.
static java.lang.String makeDate(java.util.Date d)
          Deprecated. Please use from taglib instead.
static java.lang.String makeTableElement(java.lang.String s)
          Create a table element containing the given string, escaping HTML values in the process.
static java.lang.String makeTableHeader(java.lang.String contents)
          Create a table header element containing the given string, escaping HTML values in the process.
static java.lang.String makeTableRow(java.lang.String... contents)
          Create a table row.
static java.lang.String nullToHyphen(java.lang.Object o)
          Returns the toString() value of an object or a hyphen if the argument is null.
static int parseAndCheckInteger(javax.servlet.jsp.PageContext context, java.lang.String param, int minValue, int maxValue)
          Parses a integer request parameter and checks that it lies within a given interval.
static java.lang.Long parseLong(java.util.Locale loc, java.lang.String paramValue, java.lang.String parameterName, java.lang.Long defaultValue)
          Parse a given String for a long value.
static java.util.Date parseOptionalDate(javax.servlet.jsp.PageContext context, java.lang.String param, java.lang.String format, java.util.Date defaultValue)
          Parse an optionally present date-value from a request parameter.
static java.lang.Long parseOptionalLong(javax.servlet.jsp.PageContext context, java.lang.String param, java.lang.Long defaultValue)
          Parse an optionally present long-value from a request parameter.
static void setUTF8(javax.servlet.http.HttpServletRequest request)
          Sets the character encoding for reading parameters and content from a request in a JSP page.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

encode

public static java.lang.String encode(java.lang.String s)
Url encodes a string in UTF-8. This encodes _all_ non-letter non-number characters except '-', '_' and '.'. The characters '/' and ':' are encoded.

Parameters:
s - the string to encode
Returns:
the encoded string

decode

public static java.lang.String decode(java.lang.String s)
Url decodes a string encoded in UTF-8.

Parameters:
s - the string to decode
Returns:
the decoded string

generateHeader

public static void generateHeader(javax.servlet.jsp.PageContext context)
                           throws java.io.IOException
Prints the header information for the webpages in the GUI. This includes the navigation menu, and links for changing the language. The title of the page is generated internationalised from sitesections. If you want to specify it, use the overloaded method.

Parameters:
context - The context of the web page request.
Throws:
java.io.IOException - if an error occurs during writing of output.

generateHeader

public static void generateHeader(javax.servlet.jsp.PageContext context,
                                  java.lang.String... jsToInclude)
                           throws java.io.IOException
Prints the header information for the webpages in the GUI. This includes the navigation menu, and links for changing the language. The title of the page is generated internationalised from sitesections. If you want to specify it, use the overloaded method.

Parameters:
context - The context of the web page request.
Throws:
java.io.IOException - if an error occurs during writing of output.

generateHeader

public static void generateHeader(javax.servlet.jsp.PageContext context,
                                  long refreshInSeconds)
                           throws java.io.IOException
Prints the header information for the webpages in the GUI. This includes the navigation menu, and links for changing the language. The title of the page is generated internationalised from sitesections. If you want to specify it, use the overloaded method.

Parameters:
context - The context of the web page request.
refreshInSeconds - auto-refresh time in seconds
Throws:
java.io.IOException - if an error occurs during writing of output.

generateHeader

public static void generateHeader(java.lang.String title,
                                  javax.servlet.jsp.PageContext context,
                                  java.lang.String... jsToInclude)
                           throws java.io.IOException
Prints the header information for the webpages in the GUI. This includes the navigation menu, and links for changing the language.

Parameters:
title - An internationalised title of the page.
context - The context of the web page request.
jsToInclude - path(s) to external .js files to include in header.
Throws:
java.io.IOException - if an error occurs during writing to output.

generateHeader

public static void generateHeader(java.lang.String title,
                                  long refreshInSeconds,
                                  javax.servlet.jsp.PageContext context)
                           throws java.io.IOException
Prints the header information for the webpages in the GUI. This includes the navigation menu, and links for changing the language.

Parameters:
title - An internationalised title of the page.
context - The context of the web page request.
refreshInSeconds - auto-refresh time in seconds
Throws:
java.io.IOException - if an error occurs during writing to output.

getLocaleObject

public static java.util.Locale getLocaleObject(javax.servlet.jsp.PageContext context)
Get the locale according to header context information.

Parameters:
context - The context of the web page request.
Returns:
The locale given in the the page response.

generateFooter

public static void generateFooter(javax.servlet.jsp.JspWriter out)
                           throws java.io.IOException
Writes out footer information to close the page.

Parameters:
out - the writer to which the information is written
Throws:
java.io.IOException - if the output cannot be written

makeTableElement

public static java.lang.String makeTableElement(java.lang.String s)
Create a table element containing the given string, escaping HTML values in the process.

Parameters:
s - An unescaped string. Any HTML tags in this string will end up escaped away.
Returns:
The same string escaped and enclosed in td tags.

makeTableHeader

public static java.lang.String makeTableHeader(java.lang.String contents)
Create a table header element containing the given string, escaping HTML values in the process.

Parameters:
contents - An unescaped string. Any HTML tags in this string will end up escaped away.
Returns:
The same string escaped and enclosed in th tags.

makeTableRow

public static java.lang.String makeTableRow(java.lang.String... contents)
Create a table row. Note that in contrast to createTableElement and createTableHeader, the contents are not escaped. They are expected to contain table elements.

Parameters:
contents - The contents to put into the table row. The entries will be delimited by newline characters.
Returns:
The same string escaped and enclosed in td tags.

makeDate

public static java.lang.String makeDate(java.util.Date d)
Deprecated. Please use from taglib instead.

Get an HTML representation of the date given.

Parameters:
d - A date
Returns:
A representation of the date that can be directly inserted into an HTML document, or the empty string if d is null.

nullToHyphen

public static java.lang.String nullToHyphen(java.lang.Object o)
Returns the toString() value of an object or a hyphen if the argument is null.

Parameters:
o - the given object
Returns:
o.toString() or "-" if o is null

escapeHtmlValues

public static java.lang.String escapeHtmlValues(java.lang.String input)
Escapes HTML special characters ", &, < and > (but not ').

Parameters:
input - a string
Returns:
The string with values escaped. If input is null, the empty string is returned.

encodeAndEscapeHTML

public static java.lang.String encodeAndEscapeHTML(java.lang.String input)
Encode a string for use in a URL, then escape characters that must be escaped in HTML. This must be used whenever unknown strings are used in URLs that are placed in HTML.

Parameters:
input - A string
Returns:
The same string, encoded to be safely placed in a URL in HTML.

escapeJavascriptQuotes

public static java.lang.String escapeJavascriptQuotes(java.lang.String input)
Escapes a string for use in javascript. Replaces " with \" and ' with \', so e.g. escapeJavascriptQuotes("\"").equals("\\\"") Also, \ and any non-printable character is escaped for use in javascript

Parameters:
input - a string
Returns:
The string with values escaped. If input is null, the empty string is returned.

setUTF8

public static void setUTF8(javax.servlet.http.HttpServletRequest request)
Sets the character encoding for reading parameters and content from a request in a JSP page.

Parameters:
request - The servlet request object

getTitle

public static java.lang.String getTitle(java.lang.String url,
                                        java.util.Locale locale)
Given a URL in the sitesection hierarchy, returns the corresponding page title.

Parameters:
url - a given URL
locale - the current locale
Returns:
the corresponding page title, or string about "(no title)" if no title can be found
Throws:
ArgumentNotValid - if the given url or locale is null or url is empty.

getRowClass

public static java.lang.String getRowClass(int rowCount)
Get the (CSS) class name for a row in a table. The row count should start at 0.

Parameters:
rowCount - The number of the row
Returns:
A CSS class name that should be the class of the TR element.

getLocale

public static java.lang.String getLocale(javax.servlet.http.HttpServletRequest request)
Get a locale from cookie, if present. The default request locale otherwise.

Parameters:
request - The request to get the locale for.
Returns:
The cookie locale, if present. The default request locale otherwise.

forwardWithErrorMessage

public static void forwardWithErrorMessage(javax.servlet.jsp.PageContext context,
                                           I18n I18N,
                                           java.lang.String label,
                                           java.lang.Object... args)
Forward to our standard error message page with an internationalized message. Note that this doesn't throw ForwardedToErrorPage, it is the job of whoever calls this to do that if not within a JSP page (a JSP page can just return immediately). All text involved will be HTML-escaped.

Parameters:
context - The context that the error happened in (the JSP-defined pageContext, typically)
I18N - The i18n information
label - An i18n label for the error. This label should begin with "errormsg;".
args - Any extra args for i18n
Throws:
IOFailure - If the forward fails

forwardWithRawErrorMessage

public static void forwardWithRawErrorMessage(javax.servlet.jsp.PageContext context,
                                              I18n i18n,
                                              java.lang.String label,
                                              java.lang.Object... args)
Forward to our standard error message page with an internationalized message. Note that this doesn't throw ForwardedToErrorPage, it is the job of whoever calls this to do that if not within a JSP page (a JSP page can just return immediately). The text involved must be HTML-escaped before passing to this method.

Parameters:
context - The context that the error happened in (the JSP-defined pageContext, typically)
i18n - The i18n information
label - An i18n label for the error. This label should begin with "errormsg;".
args - Any extra args for i18n. These must be valid HTML.
Throws:
IOFailure - If the forward fails.

forwardWithErrorMessage

public static void forwardWithErrorMessage(javax.servlet.jsp.PageContext context,
                                           I18n i18n,
                                           java.lang.Throwable e,
                                           java.lang.String label,
                                           java.lang.Object... args)
Forward to our standard error message page with an internationalized message, in case of exception. Note that this doesn't throw ForwardedToErrorPage, it is the job of whoever calls this to do that if not within a JSP page (a JSP page can just return immediately). All text involved will be HTML-escaped.

Parameters:
context - The context that the error happened in (the JSP-defined pageContext, typically)
i18n - The i18n information
e - The exception that is being handled.
label - An i18n label for the error. This label should begin with "errormsg;".
args - Any extra args for i18n
Throws:
IOFailure - If the forward fails

forwardOnMissingParameter

public static void forwardOnMissingParameter(javax.servlet.jsp.PageContext context,
                                             java.lang.String... parameters)
                                      throws ForwardedToErrorPage
Checks that the given parameters exist. If any of them do not exist, forwards to the error page and throws ForwardedToErrorPage.

Parameters:
context - The context of the current JSP page
parameters - List of parameters that must exist
Throws:
IOFailure - If the forward fails
ForwardedToErrorPage - If a parameter is missing

forwardOnEmptyParameter

public static void forwardOnEmptyParameter(javax.servlet.jsp.PageContext context,
                                           java.lang.String... parameters)
Checks that the given parameters exist and are not empty. If any of them are missing or empty, forwards to the error page and throws ForwardedToErrorPage. A parameter with only whitespace is considered empty.

Parameters:
context - The context of the current JSP page
parameters - List of parameters that must exist and be non-empty
Throws:
IOFailure - If the forward fails
ForwardedToErrorPage - if a parameter was missing or empty

forwardOnIllegalParameter

public static void forwardOnIllegalParameter(javax.servlet.jsp.PageContext context,
                                             java.lang.String parameter,
                                             java.lang.String... legalValues)
                                      throws ForwardedToErrorPage
Checks that the given parameter exists and is one of a set of values. If is is missing or doesn't equal one of the given values, forwards to the error page and throws ForwardedToErrorPage.

Parameters:
context - The context of the current JSP page
parameter - parameter that must exist
legalValues - legal values for the parameter
Throws:
IOFailure - If the forward fails
ForwardedToErrorPage - if the parameter is none of the given values

parseAndCheckInteger

public static int parseAndCheckInteger(javax.servlet.jsp.PageContext context,
                                       java.lang.String param,
                                       int minValue,
                                       int maxValue)
                                throws ForwardedToErrorPage
Parses a integer request parameter and checks that it lies within a given interval. If it doesn't, forwards to an error page and throws ForwardedToErrorPage.

Parameters:
context - The context this call happens in
param - A parameter to parse.
minValue - The minimum allowed value
maxValue - The maximum allowed value
Returns:
The value x parsed from the string, if minValue <= x <= maxValue
Throws:
ForwardedToErrorPage - if the parameter doesn't exist, is not a parseable integer, or doesn't lie within the limits.

parseOptionalLong

public static java.lang.Long parseOptionalLong(javax.servlet.jsp.PageContext context,
                                               java.lang.String param,
                                               java.lang.Long defaultValue)
Parse an optionally present long-value from a request parameter.

Parameters:
context - The context of the web request.
param - The name of the parameter to parse.
defaultValue - A value to return if the parameter is not present (may be null).
Returns:
Parsed value or default value if the parameter is missing or empty. Null will only be returned if passed as the default value.
Throws:
ForwardedToErrorPage - if the parameter is present but not parseable as a long value.

parseOptionalDate

public static java.util.Date parseOptionalDate(javax.servlet.jsp.PageContext context,
                                               java.lang.String param,
                                               java.lang.String format,
                                               java.util.Date defaultValue)
Parse an optionally present date-value from a request parameter.

Parameters:
context - The context of the web request.
param - The name of the parameter to parse
format - The format of the date, in the format defined by SimpleDateFormat
defaultValue - A value to return if the parameter is not present (may be null)
Returns:
Parsed value or default value if the parameter is missing or empty. Null will only be returned if passed as the default value.
Throws:
ForwardedToErrorPage - if the parameter is present but not parseable as a date

localiseLong

public static java.lang.String localiseLong(long i,
                                            javax.servlet.jsp.PageContext context)
Create a localized string representation of the given long.

Parameters:
i - A long integer
context - The given JSP context
Returns:
a localized string representation of the given long TODO Should this method throw ArgumentNotValid if the context is null

localiseLong

public static java.lang.String localiseLong(long i,
                                            java.util.Locale locale)
Create a localized string representation of the given long.

Parameters:
i - A long integer
locale - The given locale.
Returns:
a localized string representation of the given long TODO Should this method throw ArgumentNotValid if the locale is null

parseLong

public static java.lang.Long parseLong(java.util.Locale loc,
                                       java.lang.String paramValue,
                                       java.lang.String parameterName,
                                       java.lang.Long defaultValue)
Parse a given String for a long value.

Parameters:
loc - The given Locale.
paramValue - The given parameter value
parameterName - The given parameter name (used for debugging)
defaultValue - The default value for the parameter, in case the string cannot be parsed
Returns:
the long value found in the paramValue