Class StringParser

Object
com.liferay.portal.kernel.util.StringParser

public class StringParser extends Object
Parses strings into parameter maps and vice versa.
Author:
Connor McKay, Brian Wing Shun Chan
See Also:
  • Constructor Details

    • StringParser

      protected StringParser(String pattern)
      Constructs a new string parser from the pattern.

      The pattern can be any string containing named fragments in brackets. The following is a valid pattern for greeting:

       
       Hi {name}! How are you?
       
       

      This pattern would match the string "Hi Tom! How are you?". The format of a fragment may optionally be specified by inserting a colon followed by a regular expression after the fragment name. For instance, name could be set to match only lower case letters with the following:

       
       Hi {name:[a-z]+}! How are you?
       
       

      By default, a fragment will match anything except a forward slash or a period.

      If a string parser is set to encode fragments using a StringEncoder, an individual fragment can be specified as raw by prefixing its name with a percent sign, as shown below:

       
       /view_page/{%path:.*}
       
       

      The format of the path fragment has also been specified to match anything using the pattern ".*". This pattern could be used to parse the string:

       
       /view_page/root/home/mysite/pages/index.htm
       
       

      path would be set to "root/home/mysite/pages/index.htm", even if URLStringEncoder had been set as the string encoder.

      Do not include capturing subgroups in the pattern.

      Parameters:
      pattern - the pattern string
  • Method Details

    • create

      public static StringParser create(String chunk)
    • escapeRegex

      public static String escapeRegex(String s)
      Escapes the special characters in the string so that they will have no special meaning in a regular expression.

      This method differs from Pattern.quote(String) by escaping each special character with a backslash, rather than enclosing the entire string in special quote tags. This allows the escaped string to be manipulated or have sections replaced with non-literal sequences.

      Parameters:
      s - the string to escape
      Returns:
      the escaped string
    • build

      public String build(Map<String,String> parameters)
      Builds a string from the parameter map if this parser is appropriate.

      A parser is appropriate if each parameter matches the format of its accompanying fragment.

      If this parser is appropriate, all the parameters used in the pattern will be removed from the parameter map. If this parser is not appropriate, the parameter map will not be modified.

      Parameters:
      parameters - the parameter map to build the string from
      Returns:
      the string, or null if this parser is not appropriate
    • parse

      public boolean parse(String s, Map<String,String> parameters)
      Populates the parameter map with values parsed from the string if this parser matches.
      Parameters:
      s - the string to parse
      parameters - the parameter map to populate if this parser matches the string
      Returns:
      true if this parser matches; false otherwise
    • setStringEncoder

      public void setStringEncoder(StringEncoder stringEncoder)
      Sets the string encoder to use for parsing or building a string.

      The string encoder will not be used for fragments marked as raw. A fragment can be marked as raw by prefixing its name with a percent sign.

      Parameters:
      stringEncoder - the string encoder to use for parsing or building a string
      See Also: