Class Util
public class Util extends Object
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Util.JavaeeNamespaceContext
-
Method Summary
Modifier and Type Method Description static String
addExactMappedMarker(String mapping)
static void
canSetAppArtifact(ApplicationAssociate applicationAssociate, String artifactName)
static void
checkIdUniqueness(FacesContext context, UIComponent component, Set<String> componentIds)
Utility method to validate ID uniqueness for the tree represented bycomponent
.static boolean
classHasAnnotations(Class<?> clazz)
static <T> T
coalesce(T... objects)
Returns the first non-null
object of the argument list, ornull
if there is no such element.static boolean
componentIsDisabled(UIComponent component)
static boolean
componentIsDisabledOrReadonly(UIComponent component)
static DocumentBuilderFactory
createDocumentBuilderFactory()
static SAXParserFactory
createSAXParserFactory()
static SchemaFactory
createSchemaFactory(String uri)
static TransformerFactory
createTransformerFactory()
static String
generateCreatedBy(FacesContext facesContext)
static jakarta.enterprise.inject.spi.BeanManager
getCdiBeanManager(FacesContext facesContext)
Get the CDI bean manager.static String
getClientWindowId(FacesContext context)
static String
getContentTypeFromResponse(Object response)
PRECONDITION: argumentresponse
is non-null and has a method calledgetContentType
that takes no arguments and returns a String, with no side-effects.static ClassLoader
getContextClassLoader2()
Identify and return the class loader that is associated with the calling web application.static Converter
getConverterForClass(Class converterClass, FacesContext context)
static Converter
getConverterForIdentifer(String converterId, FacesContext context)
static ClassLoader
getCurrentLoader(Object fallbackClass)
static String
getDOCTYPEFromFacesContextAttributes(FacesContext context)
static String
getFacesConfigXmlVersion(FacesContext facesContext)
Get the faces-config.xml version (if any).static String
getFacesMapping(FacesContext context)
Returns the URL pattern of theFacesServlet
that is executing the current request.static FeatureDescriptor
getFeatureDescriptor(String name, String displayName, String desc, boolean expert, boolean hidden, boolean preferred, Object type, Boolean designTime)
static String
getFirstWildCardMappingToFacesServlet(ExternalContext externalContext)
static long
getLastModified(URL url)
static Object
getListenerInstance(jakarta.el.ValueExpression type, jakarta.el.ValueExpression binding)
Factory method for creating the various Faces listener instances that may be referenced bytype
orbinding
.static Locale
getLocaleFromContextOrSystem(FacesContext context)
static Locale
getLocaleFromString(String localeStr)
static String
getNamingContainerPrefix(FacesContext context)
If view root is instance of naming container, return its container client id, suffixed with separator character.static String
getStackTraceString(Throwable e)
Leverage the Throwable.getStackTrace() method to produce a String version of the stack trace, with a "\n" before each line.static StateManager
getStateManager(FacesContext context)
static Class
getTypeFromString(String type)
static jakarta.el.ValueExpression
getValueExpressionNullSafe(UIComponent component, String name)
static ViewHandler
getViewHandler(FacesContext context)
static String
getViewStateId(FacesContext context)
static String
getWebXmlVersion(FacesContext facesContext)
Get the web.xml version (if any).static String
getXMLDECLFromFacesContextAttributes(FacesContext context)
static int
indexOfSet(String str, char[] set, int fromIndex)
static boolean
isAllEmpty(Object... values)
Returns true if all values are empty, false if at least one value is not empty.static boolean
isAllNull(Object... values)
static boolean
isAnyEmpty(Object... values)
Returnstrue
if at least one value is empty.static boolean
isAnyNull(Object... values)
static boolean
isCdiAvailable(FacesContext facesContext)
Is CDI available.static boolean
isCdiAvailable(jakarta.servlet.ServletContext servletContext)
Is CDI available (ServletContext variant)static boolean
isCdiOneOneOrLater(FacesContext facesContext)
Is CDI 1.1 or laterstatic boolean
isEmpty(Object value)
Returnstrue
if the given value is null or is empty.static boolean
isEmpty(Object[] array)
Returnstrue
if the given array is null or is empty.static boolean
isEmpty(String string)
Returns true if the given string is null or is empty.static boolean
isEmpty(Collection<?> collection)
Returnstrue
if the given collection is null or is empty.static boolean
isExactMapped(String mapping)
Returns true if the providedurl-mapping
is an exact mapping (starts withEXACT_MARKER
).static <T> boolean
isOneOf(T object, T... objects)
Returnstrue
if the given object equals one of the given objects.static boolean
isPortletRequest(FacesContext context)
Convenience method for determining if the request associated with the specifiedFacesContext
is a PortletRequest submitted by the JSR-301 bridge.static boolean
isPrefixMapped(String mapping)
Returns true if the providedurl-mapping
is a prefix path mapping (starts with/
).static boolean
isResourceExactMappedToFacesServlet(ExternalContext externalContext, String resource)
Checks if the FacesServlet is exact mapped to the given resource.static boolean
isSpecialAttributeName(String name)
static boolean
isUnitTestModeEnabled()
static boolean
isViewIdExactMappedToFacesServlet(String viewId)
Checks if the FacesServlet is exact mapped to the given resource.static boolean
isViewPopulated(FacesContext ctx, UIViewRoot viewToRender)
static Class
loadClass(String name, Object fallbackClass)
static Class<?>
loadClass2(String name, Object fallbackClass)
static <T> T
newInstance(Class<?> clazz)
static void
notNegative(String varname, long number)
static void
notNull(String varname, Object var)
static void
notNullAttribute(String attributeName, Object attribute)
static void
notNullNamedObject(Object object, String objectId, String logMsg)
static void
notNullViewId(String viewId)
static String
removeAllButLastSlashPathSegment(String input)
static String
removeAllButNextToLastSlashPathSegment(String input)
static String
removeExactMappedMarker(String mapping)
static String
removeLastPathSegment(String input)
static <T> List<T>
reverse(List<T> list)
static void
saveDOCTYPEToFacesContextAttributes(String DOCTYPE)
static void
saveXMLDECLToFacesContextAttributes(String XMLDECL)
static void
setUnitTestModeEnabled(boolean enabled)
static void
setViewPopulated(FacesContext ctx, UIViewRoot viewToRender)
Flag the specifiedUIViewRoot
as populated.static String[]
split(jakarta.servlet.ServletContext sc, String toSplit, String regex)
static String[]
split(Map<String,Object> appMap, String toSplit, String regex)
A slightly more efficient version ofString.split()
which caches thePattern
s in an LRUMap instead of creating a newPattern
on each invocation.static boolean
startsWithOneOf(String string, String... prefixes)
Returnstrue
if the given string starts with one of the given prefixes.
-
Method Details
-
isPortletRequest
Convenience method for determining if the request associated with the specified
FacesContext
is a PortletRequest submitted by the JSR-301 bridge.- Parameters:
context
- theFacesContext
associated with the request.
-
generateCreatedBy
-
getListenerInstance
public static Object getListenerInstance(jakarta.el.ValueExpression type, jakarta.el.ValueExpression binding)Factory method for creating the various Faces listener instances that may be referenced by
type
orbinding
.If
binding
is notnull
and the evaluation result is notnull
return that instance. Otherwise try to instantiate an instances based ontype
.- Parameters:
type
- theListener
typebinding
- aValueExpression
which resolves to aListener
instance- Returns:
- a
Listener
instance based off the providedtype
and
-
setUnitTestModeEnabled
public static void setUnitTestModeEnabled(boolean enabled) -
isUnitTestModeEnabled
public static boolean isUnitTestModeEnabled() -
createTransformerFactory
-
createSAXParserFactory
-
createDocumentBuilderFactory
-
createSchemaFactory
-
loadClass
- Throws:
ClassNotFoundException
-
loadClass2
-
newInstance
-
getCurrentLoader
-
getContextClassLoader2
Identify and return the class loader that is associated with the calling web application.
- Throws:
FacesException
- if the web application class loader cannot be identified
-
removeAllButLastSlashPathSegment
-
removeAllButNextToLastSlashPathSegment
-
removeLastPathSegment
-
notNegative
-
notNull
-
notNullViewId
-
notNullNamedObject
-
canSetAppArtifact
public static void canSetAppArtifact(ApplicationAssociate applicationAssociate, String artifactName) -
notNullAttribute
-
getValueExpressionNullSafe
public static jakarta.el.ValueExpression getValueExpressionNullSafe(UIComponent component, String name) -
isEmpty
Returns true if the given string is null or is empty.- Parameters:
string
- The string to be checked on emptiness.- Returns:
- True if the given string is null or is empty.
-
isEmpty
Returnstrue
if the given array is null or is empty.- Parameters:
array
- The array to be checked on emptiness.- Returns:
true
if the given array is null or is empty.
-
isEmpty
Returnstrue
if the given collection is null or is empty.- Parameters:
collection
- The collection to be checked on emptiness.- Returns:
true
if the given collection is null or is empty.
-
isEmpty
Returnstrue
if the given value is null or is empty. Types of String, Collection, Map, Optional and Array are recognized. If none is recognized, then examine the emptiness of the toString() representation instead.- Parameters:
value
- The value to be checked on emptiness.- Returns:
true
if the given value is null or is empty.
-
isAllEmpty
Returns true if all values are empty, false if at least one value is not empty.- Parameters:
values
- the values to be checked on emptiness- Returns:
- True if all values are empty, false otherwise
-
isAnyEmpty
Returnstrue
if at least one value is empty.- Parameters:
values
- the values to be checked on emptiness- Returns:
true
if any value is empty andfalse
if no values are empty
-
isAllNull
-
isAnyNull
-
isOneOf
Returnstrue
if the given object equals one of the given objects.- Type Parameters:
T
- The generic object type.- Parameters:
object
- The object to be checked if it equals one of the given objects.objects
- The argument list of objects to be tested for equality.- Returns:
true
if the given object equals one of the given objects.
-
coalesce
Returns the first non-null
object of the argument list, ornull
if there is no such element.- Type Parameters:
T
- The generic object type.- Parameters:
objects
- The argument list of objects to be tested for non-null
.- Returns:
- The first non-
null
object of the argument list, ornull
if there is no such element.
-
reverse
-
startsWithOneOf
Returnstrue
if the given string starts with one of the given prefixes.- Parameters:
string
- The object to be checked if it starts with one of the given prefixes.prefixes
- The argument list of prefixes to be checked- Returns:
true
if the given string starts with one of the given prefixes.
-
getLocaleFromContextOrSystem
- Parameters:
context
- theFacesContext
for the current request- Returns:
- the Locale from the UIViewRoot, the the value of Locale.getDefault()
-
getConverterForClass
-
getConverterForIdentifer
-
getStateManager
- Throws:
FacesException
-
getTypeFromString
- Throws:
ClassNotFoundException
-
getViewHandler
- Throws:
FacesException
-
componentIsDisabled
-
componentIsDisabledOrReadonly
-
getLocaleFromString
- Throws:
IllegalArgumentException
-
indexOfSet
- Parameters:
str
- local stringset
- the substringfromIndex
- starting index- Returns:
- starting at
fromIndex
, the index of the first occurrence of any substring fromset
intoSearch
, or -1 if no such match is found
-
getStackTraceString
Leverage the Throwable.getStackTrace() method to produce a String version of the stack trace, with a "\n" before each line.
- Parameters:
e
- the Throwable to obtain the stacktrace from- Returns:
- the String representation ofthe stack trace obtained by calling getStackTrace() on the passed in exception. If null is passed in, we return the empty String.
-
getContentTypeFromResponse
PRECONDITION: argument
response
is non-null and has a method calledgetContentType
that takes no arguments and returns a String, with no side-effects.This method allows us to get the contentType in both the servlet and portlet cases, without introducing a compile-time dependency on the portlet api.
- Parameters:
response
- the current response- Returns:
- the content type of the response
-
getFeatureDescriptor
-
split
A slightly more efficient version of
String.split()
which caches thePattern
s in an LRUMap instead of creating a newPattern
on each invocation.- Parameters:
appMap
- the Application MaptoSplit
- the string to splitregex
- the regex used for splitting- Returns:
- the result of
Pattern.spit(String, int)
-
split
-
getFacesMapping
Returns the URL pattern of the
If no mapping can be determined, it most likely means that this particular request wasn't dispatched through theFacesServlet
that is executing the current request. If there are multiple URL patterns, the value returned byHttpServletRequest.getServletPath()
andHttpServletRequest.getPathInfo()
is used to determine which mapping to return.FacesServlet
.NOTE: This method was supposed to be replaced with the "mapping API" from Servlet 4, but this has not been implemented in time for JSF 2.3 to depend on.
- Parameters:
context
- theFacesContext
of the current request- Returns:
- the URL pattern of the
FacesServlet
ornull
if no mapping can be determined - Throws:
NullPointerException
- ifcontext
is null
-
isViewIdExactMappedToFacesServlet
Checks if the FacesServlet is exact mapped to the given resource.Not to be confused with
isExactMapped(String)
, which checks if a string representing a mapping, not a resource, is an exact mapping.This should be replaced by the Servlet 4 mapping API when/if that becomes available and Faces/Mojarra can depend on it.
- Parameters:
viewId
- the view id to test- Returns:
- true if the FacesServlet is exact mapped to the given viewId, false otherwise
-
isResourceExactMappedToFacesServlet
public static boolean isResourceExactMappedToFacesServlet(ExternalContext externalContext, String resource)Checks if the FacesServlet is exact mapped to the given resource.Not to be confused with
isExactMapped(String)
, which checks if a string representing a mapping, not a resource, is an exact mapping.This should be replaced by the Servlet 4 mapping API when/if that becomes available and Faces/Mojarra can depend on it.
- Parameters:
externalContext
- the external context for this requestresource
- the resource to test- Returns:
- true if the FacesServlet is exact mapped to the given resource, false otherwise
-
getFirstWildCardMappingToFacesServlet
-
addExactMappedMarker
-
removeExactMappedMarker
-
isExactMapped
Returns true if the provided
url-mapping
is an exact mapping (starts withEXACT_MARKER
).- Parameters:
mapping
- aurl-pattern
- Returns:
- true if the mapping starts with
/
-
isPrefixMapped
Returns true if the provided
url-mapping
is a prefix path mapping (starts with/
).- Parameters:
mapping
- aurl-pattern
- Returns:
- true if the mapping starts with
/
-
isSpecialAttributeName
-
isViewPopulated
- Parameters:
ctx
- theFacesContext
for the current requestviewToRender
- theUIViewRoot
to check- Returns:
true
if theFacesContext
attributes map contains a reference to theUIViewRoot
's view ID
-
setViewPopulated
Flag the specified
UIViewRoot
as populated.- Parameters:
ctx
- theFacesContext
for the current requestviewToRender
- theUIViewRoot
to mark as populated
-
checkIdUniqueness
public static void checkIdUniqueness(FacesContext context, UIComponent component, Set<String> componentIds)Utility method to validate ID uniqueness for the tree represented bycomponent
. -
classHasAnnotations
-
getNamingContainerPrefix
If view root is instance of naming container, return its container client id, suffixed with separator character.- Parameters:
context
- Involved faces context.- Returns:
- The naming container prefix, or an empty string if the view root is not an instance of naming container.
-
getViewStateId
-
getClientWindowId
-
saveDOCTYPEToFacesContextAttributes
-
getDOCTYPEFromFacesContextAttributes
-
saveXMLDECLToFacesContextAttributes
-
getXMLDECLFromFacesContextAttributes
-
getLastModified
-
getFacesConfigXmlVersion
Get the faces-config.xml version (if any).- Parameters:
facesContext
- the Faces context.- Returns:
- the version found, or "" if none found.
-
getWebXmlVersion
Get the web.xml version (if any).- Parameters:
facesContext
- the Faces context.- Returns:
- the version found, or "" if none found.
-
getCdiBeanManager
public static jakarta.enterprise.inject.spi.BeanManager getCdiBeanManager(FacesContext facesContext)Get the CDI bean manager.- Parameters:
facesContext
- the Faces context to consult- Returns:
- the CDI bean manager.
-
isCdiAvailable
Is CDI available.- Parameters:
facesContext
- the Faces context to consult.- Returns:
- true if available, false otherwise.
-
isCdiAvailable
public static boolean isCdiAvailable(jakarta.servlet.ServletContext servletContext)Is CDI available (ServletContext variant)- Parameters:
servletContext
- the servlet context.- Returns:
- true if available, false otherwise.
-
isCdiOneOneOrLater
Is CDI 1.1 or later- Parameters:
facesContext
- the Faces context.- Returns:
- true if CDI 1.1 or later, false otherwise.
-