Class CmsJspElFunctions

java.lang.Object
org.opencms.jsp.util.CmsJspElFunctions

public final class CmsJspElFunctions extends Object
Provides utility methods to be used as functions from a JSP with the EL.

Since:
7.0.2
See Also:
  • Method Details

    • addToList

      public static void addToList(List<Object> list, Object value)
      Extends the given list by adding the provided object.

      Parameters:
      list - the list to extend
      value - the value to add to the list
    • convertCmsObject

      public static CmsObject convertCmsObject(Object input)
      Returns an OpenCms user context created from an Object.

      • If the input is already a CmsObject, it is casted and returned unchanged.
      • If the input is a ServletRequest, the OpenCms user context is read from the request context.
      • If the input is a PageContext, the OpenCms user context is read from the request of the page context.
      • Otherwise the input is converted to a String which should be a user name, and creation of a OpenCms user context with this name is attempted. Please note that this will only work if the user name is either the "Guest" user or the "Export" user.
      • If no valid OpenCms user context could be created with all of the above, then a new user context for the "Guest" user is created.
      Parameters:
      input - the input to create an OpenCms user context from
      Returns:
      an OpenCms user context created from an Object
    • convertDate

      public static Date convertDate(Object input)
      Returns a Date created from an Object.

      • The Object is first checked if it is a Date already, if so it is casted and returned unchanged.
      • If not, the input is checked if it is a Long, and if so the Date is created from the Long value.
      • If it's not a Date and not a Long, the Object is transformed to a String and then it's tried to parse a Long out of the String.
      • If this fails, it is tried to parse as a Date using the default date formatting.
      • If this also fails, a new Date is returned that has been initialized with 0.

      Parameters:
      input - the Object to create a Date from
      Returns:
      a Date created from the given Object
    • convertDouble

      public static Double convertDouble(Object input)
      Returns a Double created from an Object.

      • If the input already is a Number, this number is returned as Double.
      • If the input is of type A_CmsJspValueWrapper, the wrapper is converted to a Double using A_CmsJspValueWrapper.getToDouble().
      • If the input is a String, it is converted to a Double.
      • Otherwise null is returned.
      Parameters:
      input - the Object to create a Double from
      Returns:
      a Double created from the given Object
    • convertList

      public static List<Object> convertList(List<Object> input, String attributeName)
      Returns a list of attribute values specified by the attribute name of the items of the given list.

      Parameters:
      input - the list of objects to obtain the attribute values from
      attributeName - the name of the attribute to obtain
      Returns:
      a list of attributes specified by the attribute name of the items of the given list
    • convertLocale

      public static Locale convertLocale(Object input)
      Returns a Locale created from an Object.

      • The Object is first checked if it is a Locale already, if so it is casted and returned.
      • If not, the input is transformed to a String and then a Locale lookup with this String is done.
      • If the locale lookup fails, the OpenCms default locale is returned.
      Parameters:
      input - the Object to create a Locale from
      Returns:
      a Locale created from the given Object
    • convertRawResource

      public static CmsResource convertRawResource(CmsObject cms, Object input) throws CmsException
      Returns a resource created from an Object.

      • If the input is already a CmsResource, it is casted to the resource and returned unchanged.
      • If the input is a String, the given OpenCms context is used to read a resource with this name from the VFS.
      • If the input is a CmsUUID, the given OpenCms context is used to read a resource with this UUID from the VFS.
      • Otherwise the input is converted to a String, and then the given OpenCms context is used to read a resource with this name from the VFS.
      Parameters:
      cms - the current OpenCms user context
      input - the input to create a resource from
      Returns:
      a resource created from the given Object
      Throws:
      CmsException - in case of errors accessing the OpenCms VFS for reading the resource
    • convertRequest

      public static javax.servlet.ServletRequest convertRequest(Object input)
      Tries to convert the given input object into a request.

      This is only possible if the input object is already a request or if it is a page context.

      If everything else, this method returns null.

      Parameters:
      input - the input object to convert to a request
      Returns:
      a request object, or null
    • convertResource

      Returns a resource wrapper created from the input. The wrapped result of convertRawResource(CmsObject, Object) is returned.
      Parameters:
      cms - the current OpenCms user context
      input - the input to create a resource from
      Returns:
      a resource wrapper created from the given Object
      Throws:
      CmsException - in case of errors accessing the OpenCms VFS for reading the resource
    • convertResourceList

      Returns a list of resource wrappers created from the input list of resources.
      Parameters:
      cms - the current OpenCms user context
      list - the list to create the resource wrapper list from
      Returns:
      the list of wrapped resources.
    • convertUUID

      public static CmsUUID convertUUID(Object input)
      Returns a CmsUUID created from an Object.

      • The Object is first checked if it is a CmsUUID already, if so it is casted and returned.
      • If not, the input is transformed to a byte[] and a new CmsUUID is created with this byte[].
      • Otherwise the input is casted to a String and a new CmsUUID is created with this String.
      Parameters:
      input - the Object to create a CmsUUID from
      Returns:
      a CmsUUID created from the given Object
    • createList

      public static List<Object> createList()
      Returns a newly created, empty List object.

      There is no way to create an empty list using standard JSTL methods, hence this function.

      Returns:
      a newly created, empty List object
    • getCmsObject

      public static CmsObject getCmsObject(Object input)
      Returns the current OpenCms user context from the given page context.

      Parameters:
      input - the input to create a CmsObject from
      Returns:
      the current OpenCms user context from the given page context
    • getListSize

      public static Integer getListSize(Collection<Object> input)
      Returns the size of the given list.

      Parameters:
      input - the list of objects to obtain the size from
      Returns:
      the size of the given list
    • getModuleParam

      public static String getModuleParam(String name, String key)
      Returns a parameter value from the module parameters.

      Parameters:
      name - the name of the module
      key - the parameter to return the value for
      Returns:
      the parameter value from the module parameters, or null if the parameter is not set
    • getNavigationUri

      @Deprecated public static String getNavigationUri(Object input)
      Deprecated.
      On a JSP use ${cms.requestContext.uri} instead.
      Returns the current request URI.

      For OpenCms 10.5, this is the same as using ${cms.requestContext.uri} on a JSP.

      Parameters:
      input - the request convertible object to get the navigation URI from
      Returns:
      the current navigation URI
    • getRequestLink

      public static String getRequestLink(String url)
      Returns the link without parameters from a String that is formatted for a GET request.

      Parameters:
      url - the URL to remove the parameters from
      Returns:
      the link without parameters
    • getRequestParam

      public static String getRequestParam(String url, String paramName)
      Returns the value of a parameter from a String that is formatted for a GET request.

      Parameters:
      url - the URL to get the parameter value from
      paramName - the request parameter name
      Returns:
      the value of the parameter
    • getVfsAccessBean

      Returns a JSP / EL VFS access bean.

      Parameters:
      input - the Object to create a CmsObject from
      Returns:
      a JSP / EL VFS access bean
    • isSubSitemap

      public static boolean isSubSitemap(CmsResource resource)
      Returns whether the given resource is a sub sitemap folder.

      Parameters:
      resource - the resource to check
      Returns:
      true if the given resource is a sub sitemap folder
    • isWrapper

      public static boolean isWrapper(Object value)
      Returns whether the given value is an instance of A_CmsJspValueWrapper.

      Parameters:
      value - the value object to check
      Returns:
      true if the given value is an instance of A_CmsJspValueWrapper
    • jsonGetString

      public static String jsonGetString(Object maybeJsonString, Object key)
      Parses the JSON String and returns the requested value.

      Parameters:
      maybeJsonString - the JSON string
      key - the key
      Returns:
      the json value string
    • jsonToMap

      public static Map<String,String> jsonToMap(String jsonString)
      Converts a string (which is assumed to contain a JSON object whose values are strings only) to a map, for use in JSPs.

      If the input can't be interpreted as JSON, an empty map is returned.

      Parameters:
      jsonString - the JSON string
      Returns:
      the map with the keys/values from the JSON
    • lookup

      public static String lookup(String key, String map)
      Looks up the given key from the map that is passed as a String, and returns either the element found or the empty String.

      The map String must have the form "key1:value1|key2:value2" etc.

      Parameters:
      key - the key to look up
      map - the map represented as a String
      Returns:
      the element found in the map with the given key, or the empty String
    • lookup

      public static String lookup(String key, String map, String defaultValue)
      Looks up the given key from the map that is passed as a String, and returns either the element found or the default value.

      The map String must have the form "key1:value1|key2:value2" etc.

      Parameters:
      key - the key to look up
      map - the map represented as a String
      defaultValue - the default value
      Returns:
      the element found in the map with the given key, or the default value
    • mathCeil

      public static Long mathCeil(Object param)
      Calculates the next largest integer for the given number parameter.

      Note that the result is an Object of type Long, so in case the parameter can not be converted to a number, null is returned.

      Parameters:
      param - an Object that will be converted to a number
      Returns:
      the next largest integer for the given number parameter
    • mathFloor

      public static Long mathFloor(Object param)
      Calculates the next smallest integer for the given number parameter.

      Note that the result is an Object of type Long, so in case the parameter can not be converted to a number, null is returned.

      Parameters:
      param - an Object that will be converted to a number
      Returns:
      the next smallest integer for the given number parameter
    • mathRound

      public static Long mathRound(Object param)
      Calculates the next integer for the given number parameter by rounding.

      Note that the result is an Object of type Long, so in case the parameter can not be converted to a number, null is returned.

      Parameters:
      param - an Object that will be converted to a number
      Returns:
      the next integer for the given number parameter calculated by rounding
    • parseJson

      public static Object parseJson(String value)
      Parses JSON object.
      Parameters:
      value -
      Returns:
    • repairHtml

      public static String repairHtml(String input)
      Repairs the given HTML input by adding potentially missing closing tags.

      Parameters:
      input - the HTML input
      Returns:
      the repaired HTML or an empty string in case of errors
    • stripHtml

      public static String stripHtml(Object input)
      Strips all HTML markup from the given input.

      • In case the input is an instance of CmsJspContentAccessValueWrapper, an optimized method is used for the HTML stripping.
      • Otherwise the input is converted to a String and this String is stripped.
      Parameters:
      input - the input to Strip from HTML
      Returns:
      the given input with all HTML stripped.
    • toNumber

      public static Double toNumber(Object input, Object def)
      Converts the given Object to a (Double) number, falling back to a default if this is not possible.

      In case even the default can not be converted to a number, -1.0 is returned.

      Parameters:
      input - the Object to convert to a (Double) number
      def - the fall back default value in case the conversion is not possible
      Returns:
      the given Object converted to a (Double) number
    • trimToSize

      public static String trimToSize(String input, int length)
      Returns a substring of the source, which is at most length characters long.

      If a char is cut, " ..." is appended to the result.

      Parameters:
      input - the string to trim
      length - the maximum length of the string to be returned
      Returns:
      a substring of the source, which is at most length characters long
      See Also:
    • validateRegex

      public static boolean validateRegex(String value, String regex)
      Validates a value against a regular expression.

      Parameters:
      value - the value
      regex - the regex
      Returns:
      true if the value satisfies the validation