Class StringParser
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
StringParser
(String pattern) Constructs a new string parser from the pattern. -
Method Summary
Modifier and TypeMethodDescriptionBuilds a string from the parameter map if this parser is appropriate.static StringParser
static String
Escapes the special characters in the string so that they will have no special meaning in a regular expression.boolean
Populates the parameter map with values parsed from the string if this parser matches.void
setStringEncoder
(StringEncoder stringEncoder) Sets the string encoder to use for parsing or building a string.
-
Constructor Details
-
StringParser
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 ifURLStringEncoder
had been set as the string encoder.Do not include capturing subgroups in the pattern.
- Parameters:
pattern
- the pattern string
-
-
Method Details
-
create
-
escapeRegex
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
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
Populates the parameter map with values parsed from the string if this parser matches.- Parameters:
s
- the string to parseparameters
- the parameter map to populate if this parser matches the string- Returns:
true
if this parser matches;false
otherwise
-
setStringEncoder
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:
-