Class Util

java.lang.Object
com.sun.faces.util.Util

public class Util extends Object
Util is a class ... Lifetime And Scope
  • Method Details

    • getFacesServletRegistration

      public static Optional<jakarta.servlet.ServletRegistration> getFacesServletRegistration(FacesContext context)
    • isPortletRequest

      public static boolean isPortletRequest(FacesContext context)

      Convenience method for determining if the request associated with the specified FacesContext is a PortletRequest submitted by the JSR-301 bridge.

      Parameters:
      context - the FacesContext associated with the request.
    • generateCreatedBy

      public static String generateCreatedBy(FacesContext facesContext)
    • 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 or binding.

      If binding is not null and the evaluation result is not null return that instance. Otherwise try to instantiate an instances based on type.

      Parameters:
      type - the Listener type
      binding - a ValueExpression which resolves to a Listener instance
      Returns:
      a Listener instance based off the provided type and binding
    • setUnitTestModeEnabled

      public static void setUnitTestModeEnabled(boolean enabled)
    • isUnitTestModeEnabled

      public static boolean isUnitTestModeEnabled()
    • createTransformerFactory

      public static TransformerFactory createTransformerFactory()
    • createSAXParserFactory

      public static SAXParserFactory createSAXParserFactory()
    • createDocumentBuilderFactory

      public static DocumentBuilderFactory createDocumentBuilderFactory()
    • createSchemaFactory

      public static SchemaFactory createSchemaFactory(String uri)
    • loadClass

      public static Class loadClass(String name, Object fallbackClass) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • loadClass2

      public static Class<?> loadClass2(String name, Object fallbackClass)
    • newInstance

      public static <T> T newInstance(Class<?> clazz)
    • getCurrentLoader

      public static ClassLoader getCurrentLoader(Object fallbackClass)
    • getContextClassLoader2

      public static ClassLoader getContextClassLoader2() throws FacesException

      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

      public static String removeAllButLastSlashPathSegment(String input)
    • removeAllButNextToLastSlashPathSegment

      public static String removeAllButNextToLastSlashPathSegment(String input)
    • removeLastPathSegment

      public static String removeLastPathSegment(String input)
    • notNegative

      public static void notNegative(String varname, long number)
    • notNull

      public static void notNull(String varname, Object var)
    • notNullViewId

      public static void notNullViewId(String viewId)
    • notNullNamedObject

      public static void notNullNamedObject(Object object, String objectId, String logMsg)
    • canSetAppArtifact

      public static void canSetAppArtifact(ApplicationAssociate applicationAssociate, String artifactName)
    • notNullAttribute

      public static void notNullAttribute(String attributeName, Object attribute)
    • getValueExpressionNullSafe

      public static jakarta.el.ValueExpression getValueExpressionNullSafe(UIComponent component, String name)
    • isEmpty

      public static boolean isEmpty(String string)
      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

      public static boolean isEmpty(Object[] array)
      Returns true 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

      public static boolean isEmpty(Collection<?> collection)
      Returns true 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

      public static boolean isEmpty(Object value)
      Returns true 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

      public static boolean isAllEmpty(Object... values)
      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

      public static boolean isAnyEmpty(Object... values)
      Returns true if at least one value is empty.
      Parameters:
      values - the values to be checked on emptiness
      Returns:
      true if any value is empty and false if no values are empty
    • isAllNull

      public static boolean isAllNull(Object... values)
    • isAnyNull

      public static boolean isAnyNull(Object... values)
    • isOneOf

      @SafeVarargs public static <T> boolean isOneOf(T object, T... objects)
      Returns true 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

      @SafeVarargs public static <T> T coalesce(T... objects)
      Returns the first non-null object of the argument list, or null 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, or null if there is no such element.
    • reverse

      public static <T> List<T> reverse(List<T> list)
    • startsWithOneOf

      public static boolean startsWithOneOf(String string, String... prefixes)
      Returns true 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

      public static Locale getLocaleFromContextOrSystem(FacesContext context)
      Parameters:
      context - the FacesContext for the current request
      Returns:
      the Locale from the UIViewRoot, the the value of Locale.getDefault()
    • getConverterForClass

      public static Converter getConverterForClass(Class converterClass, FacesContext context)
    • getConverterForIdentifer

      public static Converter getConverterForIdentifer(String converterId, FacesContext context)
    • getStateManager

      public static StateManager getStateManager(FacesContext context) throws FacesException
      Throws:
      FacesException
    • getTypeFromString

      public static Class getTypeFromString(String type) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • getViewHandler

      public static ViewHandler getViewHandler(FacesContext context) throws FacesException
      Throws:
      FacesException
    • componentIsDisabled

      public static boolean componentIsDisabled(UIComponent component)
    • componentIsDisabledOrReadonly

      public static boolean componentIsDisabledOrReadonly(UIComponent component)
    • getLocaleFromString

      public static Locale getLocaleFromString(String localeStr) throws IllegalArgumentException
      Throws:
      IllegalArgumentException
    • indexOfSet

      public static int indexOfSet(String str, char[] set, int fromIndex)
      Parameters:
      str - local string
      set - the substring
      fromIndex - starting index
      Returns:
      starting at fromIndex, the index of the first occurrence of any substring from set in toSearch, or -1 if no such match is found
    • getStackTraceString

      public 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.

      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

      public static String getContentTypeFromResponse(Object response)

      PRECONDITION: argument response is non-null and has a method called getContentType 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

      public static FeatureDescriptor getFeatureDescriptor(String name, String displayName, String desc, boolean expert, boolean hidden, boolean preferred, Object type, Boolean designTime)
    • split

      public static String[] split(Map<String,Object> appMap, String toSplit, String regex)

      A slightly more efficient version of String.split() which caches the Patterns in an LRUMap instead of creating a new Pattern on each invocation.

      Parameters:
      appMap - the Application Map
      toSplit - the string to split
      regex - the regex used for splitting
      Returns:
      the result of Pattern.spit(String, int)
    • split

      public static String[] split(Map<String,Object> appMap, String toSplit, String regex, int splitLimit)

      A slightly more efficient version of String.split() which caches the Patterns in an LRUMap instead of creating a new Pattern on each invocation. Limited by splitLimit.

      Parameters:
      appMap - the Application Map
      toSplit - the string to split
      regex - the regex used for splitting
      splitLimit - split result threshold
      Returns:
      the result of Pattern.spit(String, int)
    • split

      public static String[] split(jakarta.servlet.ServletContext sc, String toSplit, String regex)
    • getFacesMapping

      public static jakarta.servlet.http.HttpServletMapping getFacesMapping(FacesContext context)

      Returns the URL pattern of the FacesServlet that is executing the current request. If there are multiple URL patterns, the value returned by HttpServletRequest.getServletPath() and HttpServletRequest.getPathInfo() is used to determine which mapping to return.

      If no mapping can be determined, it most likely means that this particular request wasn't dispatched through the FacesServlet.

      Parameters:
      context - the FacesContext of the current request
      Returns:
      the URL pattern of the FacesServlet or null if no mapping can be determined
      Throws:
      NullPointerException - if context is null
    • isViewIdExactMappedToFacesServlet

      public static boolean isViewIdExactMappedToFacesServlet(String viewId)
      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.

      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.

      Parameters:
      externalContext - the external context for this request
      resource - the resource to test
      Returns:
      true if the FacesServlet is exact mapped to the given resource, false otherwise
    • getFirstWildCardMappingToFacesServlet

      public static jakarta.servlet.http.HttpServletMapping getFirstWildCardMappingToFacesServlet(ExternalContext externalContext)
    • isPrefixMapped

      public static boolean isPrefixMapped(String mapping)

      Returns true if the provided url-mapping is a prefix path mapping (starts with /).

      Parameters:
      mapping - a url-pattern
      Returns:
      true if the mapping starts with /
    • isSpecialAttributeName

      public static boolean isSpecialAttributeName(String name)
    • isViewPopulated

      public static boolean isViewPopulated(FacesContext ctx, UIViewRoot viewToRender)
      Parameters:
      ctx - the FacesContext for the current request
      viewToRender - the UIViewRoot to check
      Returns:
      true if the FacesContext attributes map contains a reference to the UIViewRoot's view ID
    • setViewPopulated

      public static void setViewPopulated(FacesContext ctx, UIViewRoot viewToRender)

      Flag the specified UIViewRoot as populated.

      Parameters:
      ctx - the FacesContext for the current request
      viewToRender - the UIViewRoot 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 by component.
    • classHasAnnotations

      public static boolean classHasAnnotations(Class<?> clazz)
    • getNamingContainerPrefix

      public static String getNamingContainerPrefix(FacesContext context)
      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

      public static String getViewStateId(FacesContext context)
    • getClientWindowId

      public static String getClientWindowId(FacesContext context)
    • saveDOCTYPEToFacesContextAttributes

      public static void saveDOCTYPEToFacesContextAttributes(Doctype doctype)
    • getDOCTYPEFromFacesContextAttributes

      public static Doctype getDOCTYPEFromFacesContextAttributes(FacesContext context)
    • saveXMLDECLToFacesContextAttributes

      public static void saveXMLDECLToFacesContextAttributes(String XMLDECL)
    • getXMLDECLFromFacesContextAttributes

      public static String getXMLDECLFromFacesContextAttributes(FacesContext context)
    • getLastModified

      public static long getLastModified(URL url)
    • getFacesConfigXmlVersion

      public static String getFacesConfigXmlVersion(FacesContext facesContext)
      Get the faces-config.xml version (if any).
      Parameters:
      facesContext - the Faces context.
      Returns:
      the version found, or "" if none found.
    • getWebXmlVersion

      public static String getWebXmlVersion(FacesContext facesContext)
      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.
    • stream

      public static <T> Stream<T> stream(Object object)
    • unmodifiableSet

      @SafeVarargs public static <E> Set<E> unmodifiableSet(E... elements)
    • isNestedInIterator

      public static boolean isNestedInIterator(FacesContext context, UIComponent component)