Class HTMLUtils


  • public class HTMLUtils
    extends Object
    This is a utility class containing methods for use in the GUI for netarkivet.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static String decode​(String s)
      Url decodes a string encoded in UTF-8.
      static String encode​(String s)
      Url encodes a string in UTF-8.
      static String encodeAndEscapeHTML​(String input)
      Encode a string for use in a URL, then escape characters that must be escaped in HTML.
      static String escapeHtmlValues​(String input)
      Escapes HTML special characters ", &, < and > (but not ').
      static String escapeJavascriptQuotes​(String input)
      Escapes a string for use in javascript.
      static void forwardOnEmptyParameter​(javax.servlet.jsp.PageContext context, String... parameters)
      Checks that the given parameters exist and are not empty.
      static void forwardOnIllegalParameter​(javax.servlet.jsp.PageContext context, String parameter, String... legalValues)
      Checks that the given parameter exists and is one of a set of values.
      static void forwardOnMissingParameter​(javax.servlet.jsp.PageContext context, String... parameters)
      Checks that the given parameters exist.
      static void forwardWithErrorMessage​(javax.servlet.jsp.PageContext context, I18n I18N, String label, Object... args)
      Forward to our standard error message page with an internationalized message.
      static void forwardWithErrorMessage​(javax.servlet.jsp.PageContext context, I18n i18n, Throwable e, String label, 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, String label, 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​(String title, long refreshInSeconds, javax.servlet.jsp.PageContext context)
      Prints the header information for the webpages in the GUI.
      static void generateHeader​(String title, javax.servlet.jsp.PageContext context, String... jsToInclude)
      Prints the header information for the webpages in the GUI.
      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, String... jsToInclude)
      Prints the header information for the webpages in the GUI.
      static void generateNavigationTree​(StringBuilder sb, javax.servlet.http.HttpServletRequest req, String url, String subMenu, Locale locale)
      Prints out the navigation tree appearing as a in the left column of the "main_table" table.
      static String getLocale​(javax.servlet.http.HttpServletRequest request)
      Get a locale from cookie, if present.
      static Locale getLocaleObject​(javax.servlet.jsp.PageContext context)
      Get the locale according to header context information.
      static String getRowClass​(int rowCount)
      Get the (CSS) class name for a row in a table.
      static String getTitle​(javax.servlet.http.HttpServletRequest req, String url, Locale locale)
      Given a URL in the sitesection hierarchy, returns the corresponding page title.
      static String localiseLong​(long i, Locale locale)
      Create a localized string representation of the given long.
      static String localiseLong​(long i, javax.servlet.jsp.PageContext context)
      Create a localized string representation of the given long.
      static void log​(String classname, String msg)  
      static String makeDate​(Date d)
      Deprecated.
      Please use from taglib instead.
      static String makeTableElement​(String s)
      Create a table element containing the given string, escaping HTML values in the process.
      static String makeTableHeader​(String contents)
      Create a table header element containing the given string, escaping HTML values in the process.
      static String makeTableRow​(String... contents)
      Create a table row.
      static String nullToHyphen​(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, String param, int minValue, int maxValue)
      Parses a integer request parameter and checks that it lies within a given interval.
      static String parseDate​(Date timestamp)
      Parse Date to be displayed in 24 Hour format.
      static Long parseLong​(Locale loc, String paramValue, String parameterName, Long defaultValue)
      Parse a given String for a long value.
      static boolean parseOptionalBoolean​(javax.servlet.jsp.PageContext context, String param, boolean defaultValue)
      Parse an optionally present boolean from a request parameter.
      static Date parseOptionalDate​(javax.servlet.jsp.PageContext context, String param, String format, Date defaultValue)
      Parse an optionally present date-value from a request parameter.
      static Long parseOptionalLong​(javax.servlet.jsp.PageContext context, String param, 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.
    • Method Detail

      • encode

        public static String encode​(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 String decode​(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 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:
        IOException - if an error occurs during writing of output.
      • generateHeader

        public static void generateHeader​(javax.servlet.jsp.PageContext context,
                                          String... jsToInclude)
                                   throws 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:
        IOException - if an error occurs during writing of output.
      • generateHeader

        public static void generateHeader​(javax.servlet.jsp.PageContext context,
                                          long refreshInSeconds)
                                   throws 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:
        IOException - if an error occurs during writing of output.
      • generateHeader

        public static void generateHeader​(String title,
                                          javax.servlet.jsp.PageContext context,
                                          String... jsToInclude)
                                   throws 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:
        IOException - if an error occurs during writing to output.
      • generateHeader

        public static void generateHeader​(String title,
                                          long refreshInSeconds,
                                          javax.servlet.jsp.PageContext context)
                                   throws 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:
        IOException - if an error occurs during writing to output.
      • getLocaleObject

        public static 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.
      • generateNavigationTree

        public static void generateNavigationTree​(StringBuilder sb,
                                                  javax.servlet.http.HttpServletRequest req,
                                                  String url,
                                                  String subMenu,
                                                  Locale locale)
                                           throws IOException
        Prints out the navigation tree appearing as a in the left column of the "main_table" table. Subpages are shown only for the currently-active main-heading of the sections defined in settings.
        Parameters:
        sb - the StringBuilder to which the output must be written.
        req - the HTTP request object to respond to
        url - the url of the page.
        subMenu - submenu HTML to insert when required by non JSP pages
        locale - The locale selecting the language.
        Throws:
        IOException - if the output cannot be written.
      • generateFooter

        public static void generateFooter​(javax.servlet.jsp.JspWriter out)
                                   throws IOException
        Writes out footer information to close the page.
        Parameters:
        out - the writer to which the information is written
        Throws:
        IOException - if the output cannot be written
      • makeTableElement

        public static String makeTableElement​(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 String makeTableHeader​(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 String makeTableRow​(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 String makeDate​(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 String nullToHyphen​(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 String escapeHtmlValues​(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 String encodeAndEscapeHTML​(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 String escapeJavascriptQuotes​(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 String getTitle​(javax.servlet.http.HttpServletRequest req,
                                      String url,
                                      Locale locale)
        Given a URL in the sitesection hierarchy, returns the corresponding page title.
        Parameters:
        req - the HTTP request object to respond to
        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 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 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,
                                                   String label,
                                                   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,
                                                      String label,
                                                      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,
                                                   Throwable e,
                                                   String label,
                                                   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,
                                                     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,
                                                   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,
                                                     String parameter,
                                                     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,
                                               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 Long parseOptionalLong​(javax.servlet.jsp.PageContext context,
                                             String param,
                                             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 Date parseOptionalDate​(javax.servlet.jsp.PageContext context,
                                             String param,
                                             String format,
                                             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
      • parseOptionalBoolean

        public static boolean parseOptionalBoolean​(javax.servlet.jsp.PageContext context,
                                                   String param,
                                                   boolean defaultValue)
        Parse an optionally present boolean 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.
      • localiseLong

        public static 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 String localiseLong​(long i,
                                          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 Long parseLong​(Locale loc,
                                     String paramValue,
                                     String parameterName,
                                     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
      • parseDate

        public static String parseDate​(Date timestamp)
        Parse Date to be displayed in 24 Hour format.
        Parameters:
        timestamp - The given date to parse.
        Returns:
        the String value found in the timestamp
      • log

        public static void log​(String classname,
                               String msg)