Class WebUtilities
- java.lang.Object
-
- com.github.bordertech.wcomponents.WebUtilities
-
public final class WebUtilities extends Object
WComponent and HTML related utility methods.- Since:
- 1.0.0
- Author:
- James Gifford
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
WebUtilities.NumericEntityIgnorer
Implementation of the CodePointTranslator to throw away the matching characters.
-
Field Summary
Fields Modifier and Type Field Description static String
AMP_ESCAPE
The HTML escape sequence for an ampersand (&).static String
CLOSE_BRACKET_DOUBLE_ESCAPE
The HTML escape sequence for a close bracket with ampersand double escaped (}).static String
CLOSE_BRACKET_ESCAPE
The HTML escape sequence for a close bracket (}).static String
CONTENT_TYPE_CSS
CSS Content Type.static String
CONTENT_TYPE_HTML
HTML Content Type.static String
CONTENT_TYPE_JS
Javascript Content Type.static String
CONTENT_TYPE_JSON
JSON Content Type.static String
CONTENT_TYPE_XML
XML Content Type.static String
GT_ESCAPE
The HTML escape sequence for greater than (>).static String
LT_ESCAPE
The HTML escape sequence for less than (<).static String
OPEN_BRACKET_DOUBLE_ESCAPE
The HTML escape sequence for an open bracket with ampersand double escaped ({).static String
OPEN_BRACKET_ESCAPE
The HTML escape sequence for an open bracket ({).static String
QUOT_ESCAPE
The HTML escape sequence for a double quote (").
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static void
appendGetParam(String key, String value, StringBuffer vars, boolean existingVars)
Appends a key/value pair to a query string.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.static String
decode(String encoded)
This method is required on occasion because WebSphere Portal by default escapes "<" and ">" characters for security reasons.static String
decodeBrackets(String input)
Decode open or closed brackets in the input String.static String
doubleDecodeBrackets(String input)
Decode double encoded open or closed brackets in the input String.static String
doubleEncodeBrackets(String input)
Double encode open or closed brackets in the input String.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.static String
encodeBrackets(String input)
Encode open or closed brackets in the input String.static String
encodeForContentDispositionHeader(String fileName)
Encodes the given fileName for output in the HTTP Content-Disposition header.static String
encodeUrl(String urlStr)
Encode URL for XML.static String
escapeForUrl(String input)
Escapes the given string to make it presentable in a URL.static UIContext
findClosestContext(String id)
Finds the closest context for the given component id.static String
generateRandom()
Generates a random String.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.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.static ComponentWithContext
getComponentById(String id)
Finds a component by its id.static ComponentWithContext
getComponentById(String id, boolean visibleOnly)
Finds a component by its id.static String
getContentType(String fileName)
Attempts to guess the content-type for the given file name.static UIContext
getContextForComponent(WComponent component)
Returns the context for this component.static NamingContextable
getParentNamingContext(WComponent component)
Get this component's parent naming context.static String
getPath(String url, Map<String,String> parameters)
Adds GET parameters to a path.static String
getPath(String url, Map<String,String> parameters, boolean javascript)
Adds GET parameters to a path.static String
getPathToRoot(WComponent component)
Retrieves a "path" of component classes from the given component to the root node.static UIContext
getPrimaryContext(UIContext uic, WComponent component)
Deprecated.Badly named Method.static String
getProjectVersion()
static WComponent
getTop(WComponent comp)
Retrieves the top-level WComponent in the tree.static boolean
isActiveNamingContext(WComponent component)
Determine if this component is an active naming context.static boolean
isAncestor(WComponent component1, WComponent component2)
Indicates whether a component is an ancestor of another.static boolean
isDescendant(WComponent component1, WComponent component2)
Indicates whether a component is a descendant of another.static String
percentEncodeUrl(String urlStr)
Percent encode a URL to include in HTML.static String
render(Request request, WComponent component)
Renders the given WComponent to a String outside of the context of a Servlet.static String
render(WComponent component)
Renders the given WComponent to a String outside of the context of a Servlet.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.static String
renderWithTransformToHTML(WComponent component)
Renders and transforms the given WComponent to a HTML String outside of the context of a Servlet.static void
updateBeanValue(WComponent component)
Updates the bean value with the current value of the component and all its bean-bound children.static void
updateBeanValue(WComponent component, boolean visibleOnly)
Updates the bean value with the current value of the component and all its bean-bound children.
-
-
-
Field Detail
-
CONTENT_TYPE_HTML
public static final String CONTENT_TYPE_HTML
HTML Content Type.- See Also:
- Constant Field Values
-
CONTENT_TYPE_JS
public static final String CONTENT_TYPE_JS
Javascript Content Type.- See Also:
- Constant Field Values
-
CONTENT_TYPE_CSS
public static final String CONTENT_TYPE_CSS
CSS Content Type.- See Also:
- Constant Field Values
-
CONTENT_TYPE_XML
public static final String CONTENT_TYPE_XML
XML Content Type.- See Also:
- Constant Field Values
-
CONTENT_TYPE_JSON
public static final String CONTENT_TYPE_JSON
JSON Content Type.- See Also:
- Constant Field Values
-
QUOT_ESCAPE
public static final String QUOT_ESCAPE
The HTML escape sequence for a double quote (").- See Also:
- Constant Field Values
-
AMP_ESCAPE
public static final String AMP_ESCAPE
The HTML escape sequence for an ampersand (&).- See Also:
- Constant Field Values
-
OPEN_BRACKET_ESCAPE
public static final String OPEN_BRACKET_ESCAPE
The HTML escape sequence for an open bracket ({).- See Also:
- Constant Field Values
-
CLOSE_BRACKET_ESCAPE
public static final String CLOSE_BRACKET_ESCAPE
The HTML escape sequence for a close bracket (}).- See Also:
- Constant Field Values
-
OPEN_BRACKET_DOUBLE_ESCAPE
public static final String OPEN_BRACKET_DOUBLE_ESCAPE
The HTML escape sequence for an open bracket with ampersand double escaped ({).- See Also:
- Constant Field Values
-
CLOSE_BRACKET_DOUBLE_ESCAPE
public static final String CLOSE_BRACKET_DOUBLE_ESCAPE
The HTML escape sequence for a close bracket with ampersand double escaped (}).- See Also:
- Constant Field Values
-
LT_ESCAPE
public static final String LT_ESCAPE
The HTML escape sequence for less than (<).- See Also:
- Constant Field Values
-
GT_ESCAPE
public static final String GT_ESCAPE
The HTML escape sequence for greater than (>).- See Also:
- Constant Field Values
-
-
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 forcomp
- 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 forcomp
- 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&dog > 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&dog > 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 pathparameters
- 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 pathparameters
- 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 appendvalue
- the value to appendvars
- the existing query stringexistingVars
- 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 appendvalue
- the value to appendvars
- the existing query stringexistingVars
- 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 ofcomponent2
, 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 ofcomponent2
, false otherwise.
-
getPrimaryContext
@Deprecated public static UIContext getPrimaryContext(UIContext uic, WComponent component)
Deprecated.Badly named Method. UsegetContextForComponent(com.github.bordertech.wcomponents.WComponent)
insteadReturns the context for the given component.- Parameters:
uic
- the current user's UIContext.component
- the component to retrieve its context for.- Returns:
- the context for the given component.
-
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 tocomponent
- the root WComponent to renderincludePageShell
- 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
-
-