Class FormUtil


  • public final class FormUtil
    extends java.lang.Object
    A collection of (static) utilities used by the WikiForms code. FormUtil is mainly concerned with mapping HTTP parameters to WikiPlugin parameters.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.List<?> getNumberedValues​(java.util.Map<?,​?> params, java.lang.String keyPrefix)
      Looks up all keys starting with a given prefix and returns the values in an ArrayList.
      static java.util.List<?> getValues​(java.util.Map<?,​?> params, java.lang.String key)
      Looks for a named value in the Map.
      static java.util.Map<java.lang.String,​java.lang.String> requestToMap​(javax.servlet.http.HttpServletRequest req, java.lang.String filterPrefix)
      Converts the parameter contents of an HTTP request into a map, modifying the keys to preserve multiple values per key.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getValues

        public static java.util.List<?> getValues​(java.util.Map<?,​?> params,
                                                  java.lang.String key)

        Looks for a named value in the Map. Returns either the value named by key, or values named by key.0, key.1, ... if the direct value is not found. The values are packed in an ArrayList.

        This is a utility method, mainly used when we don't know whether there was just one value, or several, in a mapping list (e.g. an HttpRequest / FORM checkbox).

        Parameters:
        params - the Map container form parameters
        key - the key to look up
        Returns:
        the List of keys
      • getNumberedValues

        public static java.util.List<?> getNumberedValues​(java.util.Map<?,​?> params,
                                                          java.lang.String keyPrefix)
        Looks up all keys starting with a given prefix and returns the values in an ArrayList. The keys must be Strings.

        For example, calling this method for a Map containing key-value pairs foo.1 = a, foo.2 = b, and foo.3 = c returns an ArrayList containing [a, b, c].

        Handles both 0- and 1-indexed names. Parsing stops at the first gap in the numeric postfix.

        Parameters:
        params - a Map of string-object pairs, presumably containing key.1, key.2,...
        keyPrefix - a String prefix; values will be looked up by adding ".0", ".1", and so on, until the first gap.
        Returns:
        ArrayList, containing the values corresponding to the keyPrefix, in order.
      • requestToMap

        public static java.util.Map<java.lang.String,​java.lang.String> requestToMap​(javax.servlet.http.HttpServletRequest req,
                                                                                          java.lang.String filterPrefix)

        Converts the parameter contents of an HTTP request into a map, modifying the keys to preserve multiple values per key. This is done by adding an ordered suffix to the key:

        foo=bar,baz,xyzzy

        becomes

        foo.0=bar foo.1=baz foo.2=xyzzy

        If filterPrefix is specified, only keys starting with the prefix are included in the result map. If the prefix is null, all keys are checked.

        FIX: this is not necessarily encoding-safe: see WikiContext.getHttpParameter().

        Parameters:
        req - the HTTP request
        filterPrefix - the prefix
        Returns:
        the Map containing parsed key/value pairs