Class WebUtilities


  • public final class WebUtilities
    extends Object
    WComponent and HTML related utility methods.
    Since:
    1.0.0
    Author:
    James Gifford
    • Method Detail

      • getProjectVersion

        public static String getProjectVersion()
        Returns:
        the project version of WComponents.
      • getPathToRoot

        public static String getPathToRoot​(WComponent component)
        Retrieves a "path" of component classes from the given component to the root node. The path is formatted with one component on each line, with the first line being the root node.
        Parameters:
        component - the component to retrieve the path for
        Returns:
        a "path" of class names from the component to the root.
      • getAncestorOfClass

        public static <T> T getAncestorOfClass​(Class<T> clazz,
                                               WComponent comp)
        Attempts to find a component which is an ancestor of the given component, and that is assignable to the given class.
        Type Parameters:
        T - the ancestor class
        Parameters:
        clazz - the class to look for
        comp - the component to start at.
        Returns:
        the matching ancestor, if found, otherwise null.
      • getClosestOfClass

        public static <T> T getClosestOfClass​(Class<T> clazz,
                                              WComponent comp)
        Attempts to find the nearest component (may be the component itself) that is assignable to the given class.
        Type Parameters:
        T - the class to find
        Parameters:
        clazz - the class to look for
        comp - the component to start at.
        Returns:
        the component or matching ancestor, if found, otherwise null.
      • getTop

        public static WComponent getTop​(WComponent comp)
        Retrieves the top-level WComponent in the tree.
        Parameters:
        comp - the component branch to start from.
        Returns:
        the top-level WComponent in the tree.
      • encodeUrl

        public static String encodeUrl​(String urlStr)
        Encode URL for XML.
        Parameters:
        urlStr - the URL to escape
        Returns:
        the URL percent encoded
      • percentEncodeUrl

        public static String percentEncodeUrl​(String urlStr)
        Percent encode a URL to include in HTML.
        Parameters:
        urlStr - the URL to escape
        Returns:
        the URL percent encoded
      • escapeForUrl

        public static String escapeForUrl​(String input)
        Escapes the given string to make it presentable in a URL. This follows RFC 3986, with some extensions for UTF-8.
        Parameters:
        input - the String to escape.
        Returns:
        an escaped copy of the string.
      • encode

        public static String encode​(String input)
        Encode all the special characters found in the given string to their escape sequences according to the XML specification, and returns the resultant string. Eg. "cat&dog > ant" becomes "cat&amp;dog &gt; ant".
        Parameters:
        input - the String to encode
        Returns:
        an encoded copy of the input String.
      • encodeForContentDispositionHeader

        public static String encodeForContentDispositionHeader​(String fileName)

        Encodes the given fileName for output in the HTTP Content-Disposition header.

        This is a complete mess. There are multiple standards (rfc 2183, rfc 2231, rfc 5987) and some browsers don't even adhere to those properly. We take a cautious approach and just replace all extended characters with underscores.

        Parameters:
        fileName - the file name to encode.
        Returns:
        the encoded file name.
      • decode

        public static String decode​(String encoded)
        This method is required on occasion because WebSphere Portal by default escapes "<" and ">" characters for security reasons. Decode any escape sequences to their original character, and return the resultant string. Eg. "cat&amp;dog &gt; ant" becomes "cat&dog > ant"
        Parameters:
        encoded - the String to decode
        Returns:
        a decoded copy of the input String.
      • encodeBrackets

        public static String encodeBrackets​(String input)
        Encode open or closed brackets in the input String.
        Parameters:
        input - the String to encode open or closed brackets
        Returns:
        the String with encoded open or closed brackets
      • decodeBrackets

        public static String decodeBrackets​(String input)
        Decode open or closed brackets in the input String.
        Parameters:
        input - the String to decode open or closed brackets
        Returns:
        the String with decode open or closed brackets
      • doubleEncodeBrackets

        public static String doubleEncodeBrackets​(String input)
        Double encode open or closed brackets in the input String.
        Parameters:
        input - the String to double encode open or closed brackets
        Returns:
        the String with double encoded open or closed brackets
      • doubleDecodeBrackets

        public static String doubleDecodeBrackets​(String input)
        Decode double encoded open or closed brackets in the input String.
        Parameters:
        input - the String to decode double encoded open or closed brackets
        Returns:
        the String with decoded double encoded open or closed brackets
      • getPath

        public static String getPath​(String url,
                                     Map<String,​String> parameters)
        Adds GET parameters to a path.
        Parameters:
        url - the existing url path
        parameters - are put into the URL as get parameters.
        Returns:
        the complete url eg http://localhost/app?step=1
      • getPath

        public static String getPath​(String url,
                                     Map<String,​String> parameters,
                                     boolean javascript)
        Adds GET parameters to a path.
        Parameters:
        url - the existing url path
        parameters - are put into the URL as get parameters.
        javascript - true if the URL is intended for use within a javascript function
        Returns:
        the complete url eg http://localhost/app?step=1
      • appendGetParamForJavascript

        public static void appendGetParamForJavascript​(String key,
                                                       String value,
                                                       StringBuffer vars,
                                                       boolean existingVars)
        This is a slightly different version of appendGetParam that doesn't encode the ampersand seperator. It is intended to be used in urls that are generated for javascript functions.
        Parameters:
        key - the key to append
        value - the value to append
        vars - the existing query string
        existingVars - true if there are already existing query string key/value pairs
      • appendGetParam

        public static void appendGetParam​(String key,
                                          String value,
                                          StringBuffer vars,
                                          boolean existingVars)

        Appends a key/value pair to a query string.

        A '?' or '&' token will first be appended to the end of the vars StringBuffer, according to the presence of other vars. We quote the '&' using XML character entity, because otherwise the resulting URL will be invalid XML parsed character data and so we can't generate XHTML.

        Parameters:
        key - the key to append
        value - the value to append
        vars - the existing query string
        existingVars - true if there are already existing query string key/value pairs
      • generateRandom

        public static String generateRandom()
        Generates a random String. Can be useful for creating unique URLs by adding the String as a query parameter to the URL.
        Returns:
        a random string
      • isAncestor

        public static boolean isAncestor​(WComponent component1,
                                         WComponent component2)
        Indicates whether a component is an ancestor of another.
        Parameters:
        component1 - a possible ancestor.
        component2 - the component to check.
        Returns:
        true if component1 is an ancestor of component2, false otherwise.
      • isDescendant

        public static boolean isDescendant​(WComponent component1,
                                           WComponent component2)
        Indicates whether a component is a descendant of another.
        Parameters:
        component1 - a possible descendent.
        component2 - the component to check.
        Returns:
        true if component1 is a descendant of component2, false otherwise.
      • getContextForComponent

        public static UIContext getContextForComponent​(WComponent component)
        Returns the context for this component. The component may not be in the current context.
        Parameters:
        component - the component to find the context it belongs to
        Returns:
        the component's context
      • getComponentById

        public static ComponentWithContext getComponentById​(String id)
        Finds a component by its id.

        Searches visible and not visible components.

        Parameters:
        id - the id of the component to search for.
        Returns:
        the component and context for the given id, or null if not found.
      • getComponentById

        public static ComponentWithContext getComponentById​(String id,
                                                            boolean visibleOnly)
        Finds a component by its id.
        Parameters:
        id - the id of the component to search for.
        visibleOnly - true if process visible only
        Returns:
        the component and context for the given id, or null if not found.
      • findClosestContext

        public static UIContext findClosestContext​(String id)
        Finds the closest context for the given component id. This handles the case where the component no longer exists due to having been removed from the UI, or having a SubUIContext removed.
        Parameters:
        id - the id of the component to search for.
        Returns:
        the component and context for the given id, or null if not found.
      • updateBeanValue

        public static void updateBeanValue​(WComponent component,
                                           boolean visibleOnly)
        Updates the bean value with the current value of the component and all its bean-bound children.
        Parameters:
        component - the component whose contents need to be copied to the bean.
        visibleOnly - - whether to include visible components only.
      • updateBeanValue

        public static void updateBeanValue​(WComponent component)
        Updates the bean value with the current value of the component and all its bean-bound children. By default this method will only process visible components.
        Parameters:
        component - the component whose contents need to be copied to the bean.
      • render

        public static String render​(WComponent component)
        Renders the given WComponent to a String outside of the context of a Servlet. This is good for getting hold of the XML for debugging, unit testing etc. Also it is good for using the WComponent framework as a more generic templating framework.
        Parameters:
        component - the root WComponent to render.
        Returns:
        the rendered output as a String.
      • render

        public static String render​(Request request,
                                    WComponent component)
        Renders the given WComponent to a String outside of the context of a Servlet. This is good for getting hold of the XML for debugging, unit testing etc. Also it is good for using the WComponent framework as a more generic templating framework.
        Parameters:
        request - the request being responded to.
        component - the root WComponent to render.
        Returns:
        the rendered output as a String.
      • renderWithTransformToHTML

        public static String renderWithTransformToHTML​(WComponent component)
        Renders and transforms the given WComponent to a HTML String outside of the context of a Servlet.
        Parameters:
        component - the root WComponent to render
        Returns:
        the rendered output as a String
      • renderWithTransformToHTML

        public static String renderWithTransformToHTML​(Request request,
                                                       WComponent component,
                                                       boolean includePageShell)
        Renders and transforms the given WComponent to a HTML String outside of the context of a Servlet.
        Parameters:
        request - the request being responded to
        component - the root WComponent to render
        includePageShell - true if include page shell
        Returns:
        the rendered output as a String.
      • getContentType

        public static String getContentType​(String fileName)
        Attempts to guess the content-type for the given file name.
        Parameters:
        fileName - the file name to return the content-type for.
        Returns:
        the content-type for the given fileName, or a generic type if unknown.
      • isActiveNamingContext

        public static boolean isActiveNamingContext​(WComponent component)
        Determine if this component is an active naming context.

        Can only be considered active if an id name has been set and flagged as a naming context.

        Parameters:
        component - the component to test for naming context
        Returns:
        true if component is an active naming context
      • getParentNamingContext

        public static NamingContextable getParentNamingContext​(WComponent component)
        Get this component's parent naming context.
        Parameters:
        component - the component to process
        Returns:
        true the parent naming context or null