public final class MessagePattern extends Object implements Cloneable, Freezable<MessagePattern>
The parser handles all syntax relevant for identifying message arguments. This includes "complex" arguments whose style strings contain nested MessageFormat pattern substrings. For "simple" arguments (with no nested MessageFormat pattern substrings), the argument style is not parsed any further.
The parser handles named and numbered message arguments and allows both in one message.
Once a pattern has been parsed successfully, iterate through the parsed data with countParts(), getPart() and related methods.
The data logically represents a parse tree, but is stored and accessed
as a list of "parts" for fast and simple parsing and to minimize object allocations.
Arguments and nested messages are best handled via recursion.
For every _START "part", getLimitPartIndex(int)
efficiently returns
the index of the corresponding _LIMIT "part".
List of "parts":
message = MSG_START (SKIP_SYNTAX | INSERT_CHAR | REPLACE_NUMBER | argument)* MSG_LIMIT argument = noneArg | simpleArg | complexArg complexArg = choiceArg | pluralArg | selectArg noneArg = ARG_START.NONE (ARG_NAME | ARG_NUMBER) ARG_LIMIT.NONE simpleArg = ARG_START.SIMPLE (ARG_NAME | ARG_NUMBER) ARG_TYPE [ARG_STYLE] ARG_LIMIT.SIMPLE choiceArg = ARG_START.CHOICE (ARG_NAME | ARG_NUMBER) choiceStyle ARG_LIMIT.CHOICE pluralArg = ARG_START.PLURAL (ARG_NAME | ARG_NUMBER) pluralStyle ARG_LIMIT.PLURAL selectArg = ARG_START.SELECT (ARG_NAME | ARG_NUMBER) selectStyle ARG_LIMIT.SELECT choiceStyle = ((ARG_INT | ARG_DOUBLE) ARG_SELECTOR message)+ pluralStyle = [ARG_INT | ARG_DOUBLE] (ARG_SELECTOR [ARG_INT | ARG_DOUBLE] message)+ selectStyle = (ARG_SELECTOR message)+
ARG_START.CHOICE
stands for an ARG_START Part with ArgType CHOICE.
This class is not intended for public subclassing.
Modifier and Type | Class and Description |
---|---|
static class |
MessagePattern.ApostropheMode
Mode for when an apostrophe starts quoted literal text for MessageFormat output.
|
static class |
MessagePattern.ArgType
Argument type constants.
|
static class |
MessagePattern.Part
A message pattern "part", representing a pattern parsing event.
|
Modifier and Type | Field and Description |
---|---|
static int |
ARG_NAME_NOT_NUMBER
Return value from
validateArgumentName(String) for when
the string is a valid "pattern identifier" but not a number. |
static int |
ARG_NAME_NOT_VALID
Return value from
validateArgumentName(String) for when
the string is invalid. |
static double |
NO_NUMERIC_VALUE
Special value that is returned by getNumericValue(Part) when no
numeric value is defined for a part.
|
Constructor and Description |
---|
MessagePattern()
Constructs an empty MessagePattern with default ApostropheMode.
|
MessagePattern(MessagePattern.ApostropheMode mode)
Constructs an empty MessagePattern.
|
MessagePattern(String pattern)
Constructs a MessagePattern with default ApostropheMode and
parses the MessageFormat pattern string.
|
Modifier and Type | Method and Description |
---|---|
String |
autoQuoteApostropheDeep()
Returns a version of the parsed pattern string where each ASCII apostrophe
is doubled (escaped) if it is not already, and if it is not interpreted as quoting syntax.
|
void |
clear()
Clears this MessagePattern.
|
void |
clearPatternAndSetApostropheMode(MessagePattern.ApostropheMode mode)
Clears this MessagePattern and sets the ApostropheMode.
|
Object |
clone()
Creates and returns a copy of this object.
|
MessagePattern |
cloneAsThawed()
Creates and returns an unfrozen copy of this object.
|
int |
countParts()
Returns the number of "parts" created by parsing the pattern string.
|
boolean |
equals(Object other) |
MessagePattern |
freeze()
Freezes this object, making it immutable and thread-safe.
|
MessagePattern.ApostropheMode |
getApostropheMode() |
int |
getLimitPartIndex(int start)
Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start.
|
double |
getNumericValue(MessagePattern.Part part)
Returns the numeric value associated with an ARG_INT or ARG_DOUBLE.
|
MessagePattern.Part |
getPart(int i)
Gets the i-th pattern "part".
|
MessagePattern.Part.Type |
getPartType(int i)
Returns the Part.Type of the i-th pattern "part".
|
int |
getPatternIndex(int partIndex)
Returns the pattern index of the specified pattern "part".
|
String |
getPatternString() |
double |
getPluralOffset(int pluralStart)
Returns the "offset:" value of a PluralFormat argument, or 0 if none is specified.
|
String |
getSubstring(MessagePattern.Part part)
Returns the substring of the pattern string indicated by the Part.
|
int |
hashCode() |
boolean |
hasNamedArguments()
Does the parsed pattern have named arguments like {first_name}?
|
boolean |
hasNumberedArguments()
Does the parsed pattern have numbered arguments like {2}?
|
boolean |
isFrozen()
Determines whether this object is frozen (immutable) or not.
|
MessagePattern |
parse(String pattern)
Parses a MessageFormat pattern string.
|
MessagePattern |
parseChoiceStyle(String pattern)
Parses a ChoiceFormat pattern string.
|
MessagePattern |
parsePluralStyle(String pattern)
Parses a PluralFormat pattern string.
|
MessagePattern |
parseSelectStyle(String pattern)
Parses a SelectFormat pattern string.
|
boolean |
partSubstringMatches(MessagePattern.Part part,
String s)
Compares the part's substring with the input string s.
|
String |
toString() |
static int |
validateArgumentName(String name)
Validates and parses an argument name or argument number string.
|
public static final int ARG_NAME_NOT_NUMBER
validateArgumentName(String)
for when
the string is a valid "pattern identifier" but not a number.public static final int ARG_NAME_NOT_VALID
validateArgumentName(String)
for when
the string is invalid.
It might not be a valid "pattern identifier",
or it have only ASCII digits but there is a leading zero or the number is too large.public static final double NO_NUMERIC_VALUE
getNumericValue(com.ibm.icu.text.MessagePattern.Part)
,
Constant Field Valuespublic MessagePattern()
public MessagePattern(MessagePattern.ApostropheMode mode)
mode
- Explicit ApostropheMode.public MessagePattern(String pattern)
pattern
- a MessageFormat pattern stringIllegalArgumentException
- for syntax errors in the pattern stringIndexOutOfBoundsException
- if certain limits are exceeded
(e.g., argument number too high, argument name too long, etc.)NumberFormatException
- if a number could not be parsedpublic MessagePattern parse(String pattern)
pattern
- a MessageFormat pattern stringIllegalArgumentException
- for syntax errors in the pattern stringIndexOutOfBoundsException
- if certain limits are exceeded
(e.g., argument number too high, argument name too long, etc.)NumberFormatException
- if a number could not be parsedpublic MessagePattern parseChoiceStyle(String pattern)
pattern
- a ChoiceFormat pattern stringIllegalArgumentException
- for syntax errors in the pattern stringIndexOutOfBoundsException
- if certain limits are exceeded
(e.g., argument number too high, argument name too long, etc.)NumberFormatException
- if a number could not be parsedpublic MessagePattern parsePluralStyle(String pattern)
pattern
- a PluralFormat pattern stringIllegalArgumentException
- for syntax errors in the pattern stringIndexOutOfBoundsException
- if certain limits are exceeded
(e.g., argument number too high, argument name too long, etc.)NumberFormatException
- if a number could not be parsedpublic MessagePattern parseSelectStyle(String pattern)
pattern
- a SelectFormat pattern stringIllegalArgumentException
- for syntax errors in the pattern stringIndexOutOfBoundsException
- if certain limits are exceeded
(e.g., argument number too high, argument name too long, etc.)NumberFormatException
- if a number could not be parsedpublic void clear()
public void clearPatternAndSetApostropheMode(MessagePattern.ApostropheMode mode)
mode
- The new ApostropheMode.public boolean equals(Object other)
public MessagePattern.ApostropheMode getApostropheMode()
public String getPatternString()
public boolean hasNamedArguments()
public boolean hasNumberedArguments()
public static int validateArgumentName(String name)
name
- Input string.public String autoQuoteApostropheDeep()
For example, this turns "I don't '{know}' {gender,select,female{h''er}other{h'im}}." into "I don''t '{know}' {gender,select,female{h''er}other{h''im}}."
MessageFormat.autoQuoteApostrophe(String)
public int countParts()
public MessagePattern.Part getPart(int i)
i
- The index of the Part data. (0..countParts()-1)IndexOutOfBoundsException
- if i is outside the (0..countParts()-1) rangepublic MessagePattern.Part.Type getPartType(int i)
i
- The index of the Part data. (0..countParts()-1)IndexOutOfBoundsException
- if i is outside the (0..countParts()-1) rangepublic int getPatternIndex(int partIndex)
partIndex
- The index of the Part data. (0..countParts()-1)IndexOutOfBoundsException
- if partIndex is outside the (0..countParts()-1) rangepublic String getSubstring(MessagePattern.Part part)
part
- a part of this MessagePattern.public boolean partSubstringMatches(MessagePattern.Part part, String s)
part
- a part of this MessagePattern.s
- a string.public double getNumericValue(MessagePattern.Part part)
part
- a part of this MessagePattern.public double getPluralOffset(int pluralStart)
pluralStart
- the index of the first PluralFormat argument style part. (0..countParts()-1)IndexOutOfBoundsException
- if pluralStart is outside the (0..countParts()-1) rangepublic int getLimitPartIndex(int start)
start
- The index of some Part data (0..countParts()-1);
this Part should be of Type ARG_START or MSG_START.IndexOutOfBoundsException
- if start is outside the (0..countParts()-1) rangepublic Object clone()
public MessagePattern cloneAsThawed()
cloneAsThawed
in interface Freezable<MessagePattern>
public MessagePattern freeze()
freeze
in interface Freezable<MessagePattern>
public boolean isFrozen()
isFrozen
in interface Freezable<MessagePattern>
Copyright © 2016 Unicode, Inc. and others.