public class StringParser
extends Object
Route
,
Pattern
Modifier | Constructor and Description |
---|---|
protected |
StringParser(String pattern)
Constructs a new string parser from the pattern.
|
Modifier and Type | Method and Description |
---|---|
String |
build(Map<String,String> parameters)
Builds a string from the parameter map if this parser is appropriate.
|
static StringParser |
create(String chunk) |
static String |
escapeRegex(String s)
Escapes the special characters in the string so that they will have no
special meaning in a regular expression.
|
boolean |
parse(String s,
Map<String,String> parameters)
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.
|
protected StringParser(String 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.
pattern
- the pattern stringpublic static StringParser create(String chunk)
public static String escapeRegex(String s)
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.
s
- the string to escapepublic String build(Map<String,String> parameters)
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
- the parameter map to build the string fromnull
if this parser is not
appropriatepublic boolean parse(String s, Map<String,String> parameters)
s
- the string to parseparameters
- the parameter map to populate if this parser matches
the stringtrue
if this parser matches; false
otherwisepublic void setStringEncoder(StringEncoder stringEncoder)
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.
stringEncoder
- the string encoder to use for parsing or building a
stringStringEncoder