|
Liferay 6.0.5 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.liferay.portal.kernel.util.StringParser
public class StringParser
Parses strings into parameter maps and vice versa.
Route
,
Pattern
Constructor Summary | |
---|---|
StringParser(String pattern)
Constructs a new string parser from the pattern. |
Method Summary | |
---|---|
String |
build(Map<String,String> parameters)
Builds a string from the parameter map if this parser is appropriate. |
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public 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 stringMethod Detail |
---|
public 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 from
null
if this parser is not
appropriatepublic 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 escape
public boolean parse(String s, Map<String,String> parameters)
s
- the string to parseparameters
- the parameter map to populate if this parser matches
the string
true
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
|
Liferay 6.0.5 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |