Class FunctionLibrary


  • public class FunctionLibrary
    extends Object
    Implements the wmfn taglibrary functions. The functions may be used in the expressionlanguage used in jsp pages.
    Usage: ${wmfn:function(arg0, ...)}

    Example of usage:
     < script type="text/javascript" >
     alert('page title is ${wmfn:escapeToJavascript(pageVersion.title)}');
     < /script >
     
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static String addLineFeed()
      Get a line feed (usually line feeds in JSP pages are ignored).
      static String constructCDATA​(String value)
      Constructs an unparsed character data section for the given value.
      static boolean contains​(Object instance, Object[] array)
      Return true if the array contains the instance.
      static String convertToJavaScript​(String value)
      Convert html code to javascript document.write statements.
      static String convertToNmtoken​(String source)
      Converts a given source String to a valid nmtoken.
      static String escapeToHTML​(String value)
      Umlifies the string value (except elements and elements> for use in HTML
      static String escapeToHTMLAttribute​(String value)
      Umlifies the string value (except elements> for use in a HTML attribute
      static String escapeToJavaScript​(String value)
      Replace \\ by \\\\ and ' by \\' in a string so that it can be used in a javascript.
      static String escapeToJSON​(String value)
      Escape to json.
      This will escape / relieve the given value of any illegal characters necessary for the successful rendering of the content in JSON format.
      The following are escaped.
      " double quote replaced with \" \r carriage return replaced with \\r \n line feed replaced with \\n \b backspace replaced with \\b \t horizontal tab replaced with \\t \\ backslash replaced with \\\\ / switch replaced with \\/
      static String escapeToText​(String value)
      Surround the text part by a <pre> tag so that we know while processing that this part should be taken literally and so that line feeds should be preserved.
      static String filterHTML​(String html, String whitelistName, String[] tags)
      Deprecated.
      Use one of these methods instead: filterHTMLBasic, filterHTMLRelaxed, filterHTMLTags, filterHTMLTagsAndAttributes
      static String filterHTMLBasic​(String html)
      Filters the HTML using a basic filtering configuration.
      static String filterHTMLRelaxed​(String html)
      Filters the HTML using a relaxed filtering configuration.
      static String filterHTMLTags​(String html, String tags)
      Filters the HTML leaving only the comma separated list of passed tags.
      static String filterHTMLTagsAndAttributes​(String html, String tags, String attributes)
      Filters the HTML leaving only the comma separated list of passed tags and attributes.
      static String formatExternalUrl​(String value)
      Returns the value prepended with "http://", unless it starts with "mailto:", "javascript:", "/", "#" or if it matches against the "[A-Za-z]*://.*" pattern using a regular expression.
      static String formSignatureParameterName​(javax.servlet.ServletContext context)
      Get the name of the secure form request parameter If secure forms are turned on, then this function will return the name of the form parameter which should be used for sending the form signature.
      static int indexOf​(Object instance, Object[] array)
      Returns the index of the object in the array or -1 if the array doesn't contain the object.
      static boolean instanceOf​(Object instance, String className)
      More flexible instanceOf implementation than te built-in Java instanceof operator.
      static Map<Object,​Object> putAll​(Map<Object,​Object> initialMap, Map<Object,​Object> additionalMap)
      Copies all of the mappings from the additional map to the initial map.
      static Map<String,​Object> putAllIgnoreCase​(Map<String,​Object> initialMap, Map<String,​Object> additionalMap)
      Copies all of the mappings from the additional map to the initial map.
      static String replaceNewLines​(String value)
      Replace new lines by <br />.
      static String signForm​(javax.servlet.ServletContext context, javax.servlet.http.HttpServletRequest request, String html)
      Adds a secure form signature to a fragment of HTML.
      static String signFormParameters​(javax.servlet.ServletContext context, javax.servlet.http.HttpServletRequest request, String[] names)
      Computes a signature for a set of names for the use of secureforms in SiteWorks.
      static String signFragment​(javax.servlet.ServletContext context, javax.servlet.http.HttpServletRequest request, String source)
      Generates a secure forms signature for the given HTML fragment.
      static SortedMap[] sortMaps​(SortedMap[] rows, String key, boolean ascending)
      Sorts an array of SortedMaps by the elements at a specified key.
      static Object[][] sortRows​(Object[][] rows, int column, boolean ascending)
      Sorts a two dimensional array (an array of arrays) by the objects in a column.
      static Object[] subArray​(Object[] rows, int from, int to)
      Returns a sub array of the original array.
      static String urlEncode​(String s)
      Url encode an utf-8 string.
    • Field Detail

      • LITERAL_START_TAG

        public static final String LITERAL_START_TAG
        Define literal start and tag identifiers for text mode. Text between these tags are not normalized.
        See Also:
        Constant Field Values
    • Method Detail

      • subArray

        public static Object[] subArray​(Object[] rows,
                                        int from,
                                        int to)
        Returns a sub array of the original array.
        Parameters:
        rows - The original array.
        from - The index of the first element in the resulting array.
        to - The index of the last element in the resulting array.
        Returns:
        The subarray containing the elements from the original array from index from to index to.
      • sortRows

        public static Object[][] sortRows​(Object[][] rows,
                                          int column,
                                          boolean ascending)
        Sorts a two dimensional array (an array of arrays) by the objects in a column.
        Parameters:
        rows - The array that should be sorted.
        column - The column index for comparing rows.
        ascending - Indicates whether the sort order is ascending or descending.
        Returns:
        The sorted array.
      • urlEncode

        public static String urlEncode​(String s)
        Url encode an utf-8 string.
        Parameters:
        s - the original string.
        Returns:
        the urlencoded string.
      • sortMaps

        public static SortedMap[] sortMaps​(SortedMap[] rows,
                                           String key,
                                           boolean ascending)
        Sorts an array of SortedMaps by the elements at a specified key.
        Parameters:
        rows - The array that should be sorted.
        key - The key for the elements that should be used for comparing rows.
        ascending - Indicates whether the sort order is ascending or descending.
        Returns:
        The array containing the elements of the original array in which the elements have been sorted by the values for the specified key.
      • contains

        public static boolean contains​(Object instance,
                                       Object[] array)
        Return true if the array contains the instance.
        Parameters:
        instance - an object
        array - an array of objects
        Returns:
        true if the instance is in the array.
      • indexOf

        public static int indexOf​(Object instance,
                                  Object[] array)
        Returns the index of the object in the array or -1 if the array doesn't contain the object.
        Parameters:
        instance - an object
        array - an array of objects
        Returns:
        the index of the object in the array or -1 if the array doesn't contain the object.
      • instanceOf

        public static boolean instanceOf​(Object instance,
                                         String className)
        More flexible instanceOf implementation than te built-in Java instanceof operator.
        Parameters:
        instance -
        className -
        Returns:
        true if instance is of type className,
      • escapeToJSON

        public static String escapeToJSON​(String value)
        Escape to json.
        This will escape / relieve the given value of any illegal characters necessary for the successful rendering of the content in JSON format.
        The following are escaped.
        1. " double quote replaced with \"
        2. \r carriage return replaced with \\r
        3. \n line feed replaced with \\n
        4. \b backspace replaced with \\b
        5. \t horizontal tab replaced with \\t
        6. \\ backslash replaced with \\\\
        7. / switch replaced with \\/
        Parameters:
        value - the value to escape
        Returns:
        the string
        See Also:
        StringLibrary.escapeToJson(String)
      • escapeToJavaScript

        public static String escapeToJavaScript​(String value)
        Replace \\ by \\\\ and ' by \\' in a string so that it can be used in a javascript.
        Parameters:
        value -
        Returns:
        escaped text.
        See Also:
        StringLibrary.escapeToJavaScript(String)
      • replaceNewLines

        public static String replaceNewLines​(String value)
        Replace new lines by <br />.
        Parameters:
        value -
        Returns:
        value with all newlines replaced by br tags
      • escapeToText

        public static String escapeToText​(String value)
        Surround the text part by a <pre> tag so that we know while processing that this part should be taken literally and so that line feeds should be preserved.
        Parameters:
        value - The string to be escaped
        Returns:
        Escaped text string
      • addLineFeed

        public static String addLineFeed()
        Get a line feed (usually line feeds in JSP pages are ignored).
        Returns:
        <pre>\r\n</pre>
      • convertToJavaScript

        public static String convertToJavaScript​(String value)
        Convert html code to javascript document.write statements.
        Parameters:
        value - Usually some HTML fragment that needs to be written in the document
        Returns:
        javascript string that will write the provided string
      • convertToNmtoken

        public static final String convertToNmtoken​(String source)
        Converts a given source String to a valid nmtoken. Every invalid character is replaced by an underscore (_).
        Parameters:
        source - the source String
        Returns:
        a valid nmtoken
      • formatExternalUrl

        public static String formatExternalUrl​(String value)
        Returns the value prepended with "http://", unless it starts with "mailto:", "javascript:", "/", "#" or if it matches against the "[A-Za-z]*://.*" pattern using a regular expression.
        Parameters:
        value - The external url which may or may not start with a protocol.
        Returns:
        The value prepended with "http://", unless it starts with "mailto:", "javascript:", "/", "#" or if it matches against the "[A-Za-z]*://.*" pattern using a regular expression.
      • signFragment

        public static String signFragment​(javax.servlet.ServletContext context,
                                          javax.servlet.http.HttpServletRequest request,
                                          String source)
        Generates a secure forms signature for the given HTML fragment.
        Parameters:
        context - Current ServletContext object.
        request - Current HttpServletRequest object.
        source - HTML fragment containing HTML INPUT tags.
        Returns:
        the HTML with the signature hidden field attached
      • signFormParameters

        public static String signFormParameters​(javax.servlet.ServletContext context,
                                                javax.servlet.http.HttpServletRequest request,
                                                String[] names)
        Computes a signature for a set of names for the use of secureforms in SiteWorks. If secureforms is switched on, then the form will be signed automatically by siteworks. If the form is written by for example JavaScript then this function can be used for signing the form.
        Parameters:
        context - Current ServletContext object.
        request - Current HttpServletRequest object.
        names - The names of the inputs
        Returns:
        The signature for the input names, or "" if an exception occurred.
      • formSignatureParameterName

        public static String formSignatureParameterName​(javax.servlet.ServletContext context)
        Get the name of the secure form request parameter If secure forms are turned on, then this function will return the name of the form parameter which should be used for sending the form signature. This is normally not needed most of the time. The main use case is when performing Ajax style requests to WM.
        Parameters:
        context - Current ServletContext object.
        Returns:
        parameter name
      • signForm

        public static String signForm​(javax.servlet.ServletContext context,
                                      javax.servlet.http.HttpServletRequest request,
                                      String html)
                               throws SignException
        Adds a secure form signature to a fragment of HTML.
        Parameters:
        context - The current ServletContext object. From JSP this will typicall be "pageContext.servletContext".
        request - The current HttpServletRequest object .From JSP this will typically be "pageContext.request".
        html - The fragment of HTML to sign. This code should contain
        Returns:
        The HTML fragment with the signature INPUT tag added.
        Throws:
        SignException
      • putAll

        public static Map<Object,​Object> putAll​(Map<Object,​Object> initialMap,
                                                      Map<Object,​Object> additionalMap)
        Copies all of the mappings from the additional map to the initial map. If a key exists in both maps the value of that key in the initial map is overwritten.
        Parameters:
        initialMap - Initial map.
        additionalMap - Mappings to be stored in the initial map.
        Returns:
        Reference to the initial map.
      • putAllIgnoreCase

        public static Map<String,​Object> putAllIgnoreCase​(Map<String,​Object> initialMap,
                                                                Map<String,​Object> additionalMap)
        Copies all of the mappings from the additional map to the initial map. The key of both maps is a string. If a key exists in both maps the value of that key in the initial map is overwritten. A key exists in both maps if keyOne.equalsIgnoreCase(keyTwo) is true.
        Parameters:
        initialMap - Initial map.
        additionalMap - Mappings to be stored in the initial map.
        Returns:
        Reference to the initial map.
      • constructCDATA

        public static String constructCDATA​(String value)
        Constructs an unparsed character data section for the given value. Primarily used for character data used in XML documents that might contain illegal characters in context of XML parsers, that cannot be escaped another way.

        A typical case are tags in character data that represent 'personalization' tags which should trigger XSL transformations on the frontend, while JSPs are rendered on the backend. If XML characters like "<" or "&" would be entity escaped (eg. by fn:escapeXML()), than these personalization tags becomes corrupted, and no transformations will take place.

        Using this function the string will be surrounded by the tags ''. If the value contains the closing tag ']]>' this tag will be split, so that in between the CDATA section is closed and opened again.

        Parameters:
        value - Input string.
        Returns:
        Output string that will not be parsed by XML parsers.
      • escapeToHTML

        public static String escapeToHTML​(String value)
        Umlifies the string value (except elements and elements> for use in HTML
        Parameters:
        value - The string value
        Returns:
        the umlified string value
      • escapeToHTMLAttribute

        public static String escapeToHTMLAttribute​(String value)
        Umlifies the string value (except elements> for use in a HTML attribute
        Parameters:
        value - The string value
        Returns:
        the umlified string value
      • filterHTMLBasic

        public static String filterHTMLBasic​(String html)
        Filters the HTML using a basic filtering configuration. This method relies on the JSoup clean method.
        Parameters:
        html - The HTML to be filtered
        Returns:
        The filtered HTML
        See Also:
        org.jsoup.Jsoup, org.jsoup.safety.Whitelist
      • filterHTMLRelaxed

        public static String filterHTMLRelaxed​(String html)
        Filters the HTML using a relaxed filtering configuration. This method relies on the JSoup clean method.
        Parameters:
        html - The HTML to be filtered
        Returns:
        The filtered HTML
        See Also:
        org.jsoup.Jsoup, org.jsoup.safety.Whitelist
      • filterHTMLTags

        public static String filterHTMLTags​(String html,
                                            String tags)
        Filters the HTML leaving only the comma separated list of passed tags. This method relies on the JSoup clean method.
        Parameters:
        html - The HTML to be filtered
        tags - The list of accepted tags, passed as comma separated string
        Returns:
        The filtered HTML
        See Also:
        org.jsoup.Jsoup, org.jsoup.safety.Whitelist
      • filterHTMLTagsAndAttributes

        public static String filterHTMLTagsAndAttributes​(String html,
                                                         String tags,
                                                         String attributes)
        Filters the HTML leaving only the comma separated list of passed tags and attributes. Attributes in the list are accepted on any tag allowed. This method relies on the JSoup clean method.
        Parameters:
        html - The HTML to be filtered
        tags - The list of accepted tags, passed as comma separated string
        attributes - The list of accepted attributes, passed as comma separated string.
        Returns:
        The filtered HTML
        See Also:
        org.jsoup.Jsoup, org.jsoup.safety.Whitelist
      • filterHTML

        public static String filterHTML​(String html,
                                        String whitelistName,
                                        String[] tags)
        Deprecated.
        Use one of these methods instead: filterHTMLBasic, filterHTMLRelaxed, filterHTMLTags, filterHTMLTagsAndAttributes
        Filters the HTML by a named whitelist or tag list. Filters can be provided by a name or list of tags. The tag supports the following named filters:
        • basic: allows a range of text nodes: a, b, blockquote, br, cite, code, dd, dl, dt, em, i, li, ol, p, pre, q, small, span, strike, strong, sub, sup, u, ul, l, and appropriate attributes.

        • relaxed: allows a full range of text and structural body HTML: a, b, blockquote, br, caption, cite, code, col, colgroup, dd, div, dl, dt, em, h1, h2, h3, h4, h5, h6, i, img, li, ol, p, pre, q, small, span, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, u, ul, l
        This method relies on the JSoup clean method.
        Parameters:
        html - The HTML to filter
        whitelistName - The name of the filter to apply
        tags - The list of tags to apply when the whitelistName is not provided
        Returns:
        The filtered HTML
        See Also:
        org.jsoup.Jsoup, org.jsoup.safety.Whitelist