Top Description Inners Fields Constructors Methods
com.ibm.icu.text

public final Class MessagePattern

extends Object
implements Cloneable, Freezable<MessagePattern>
Class Inheritance
All Implemented Interfaces
com.ibm.icu.util.Freezable, java.lang.Cloneable
Imports
java.util.ArrayList, .Locale, com.ibm.icu.impl.ICUConfig, .PatternProps, com.ibm.icu.util.Freezable, .ICUCloneNotSupportedException

Parses and represents ICU MessageFormat patterns. Also handles patterns for ChoiceFormat, PluralFormat and SelectFormat. Used in the implementations of those classes as well as in tools for message validation, translation and format conversion.

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)+

This class is not intended for public subclassing.

Author
Markus Scherer
ICU Status
Stable since ICU 4.8.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
MessagePattern.ApostropheMode

Mode for when an apostrophe starts quoted literal text for MessageFormat output.

public static enum
MessagePattern.ArgType

Argument type constants.

public static class
MessagePattern.Part

A message pattern "part", representing a pattern parsing event.

Field Summary

Modifier and TypeField and Description
private MessagePattern.ApostropheMode
public static final int
ARG_NAME_NOT_NUMBER

Return value from validateArgumentName(String) for when the string is a valid "pattern identifier" but not a number.

public static final int
ARG_NAME_NOT_VALID

Return value from validateArgumentName(String) for when the string is invalid.

private static final MessagePattern.ArgType[]
private static final MessagePattern.ApostropheMode
private volatile boolean
private boolean
private boolean
private static final int
private String
private boolean
public static final double
NO_NUMERIC_VALUE

Special value that is returned by getNumericValue(Part) when no numeric value is defined for a part.

private ArrayList<Double>
private ArrayList<MessagePattern.Part>

Constructor Summary

AccessConstructor and Description
public
MessagePattern()

Constructs an empty MessagePattern with default ApostropheMode.

public
MessagePattern(MessagePattern.ApostropheMode
Explicit ApostropheMode.
mode
)

Constructs an empty MessagePattern.

public
MessagePattern(String
a MessageFormat pattern string
pattern
)

Constructs a MessagePattern with default ApostropheMode and parses the MessageFormat pattern string.

Method Summary

Modifier and TypeMethod and Description
private void
addArgDoublePart(double numericValue, int start, int length)

private void
addLimitPart(int start, MessagePattern.Part.Type type, int index, int length, int value)

private void
addPart(MessagePattern.Part.Type type, int index, int length, int value)

pack-priv static void
appendReducedApostrophes(String s, int start, int limit, StringBuilder sb)

Appends the s[start, limit[ substring to sb, but with only half of the apostrophes according to JDK pattern behavior.

public String

Returns:

the deep-auto-quoted version of the parsed pattern 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.

public void
clear()

Clears this MessagePattern.

public void
clearPatternAndSetApostropheMode(MessagePattern.ApostropheMode
The new ApostropheMode.
mode
)

Clears this MessagePattern and sets the ApostropheMode.

public Object

Returns:

a copy of this object (or itself if frozen).
clone
()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.
public MessagePattern

Returns:

a copy of this object.
cloneAsThawed
()

Implements com.ibm.icu.util.Freezable.cloneAsThawed.

Creates and returns an unfrozen copy of this object.
public int

Returns:

the number of pattern parts.
countParts
()

Returns the number of "parts" created by parsing the pattern string.

public boolean

Returns:

true if this object is equivalent to the other one.
equals
(Object
another object to compare with.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public MessagePattern

Returns:

this
freeze
()

Implements com.ibm.icu.util.Freezable.freeze.

Freezes this object, making it immutable and thread-safe.
public MessagePattern.ApostropheMode

Returns:

this instance's ApostropheMode.
getApostropheMode
()

public int

Returns:

The first i>start where getPart(i).getType()==ARG|MSG_LIMIT at the same nesting level, or start itself if getPartType(msgStart)!=ARG|MSG_START.
getLimitPartIndex
(int
The index of some Part data (0..countParts()-1); this Part should be of Type ARG_START or MSG_START.
start
)

Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start.

public double

Returns:

the part's numeric value, or NO_NUMERIC_VALUE if this is not a numeric part.
getNumericValue
(MessagePattern.Part
a part of this MessagePattern.
part
)

Returns the numeric value associated with an ARG_INT or ARG_DOUBLE.

public MessagePattern.Part

Returns:

the i-th pattern "part".
getPart
(int
The index of the Part data. (0..countParts()-1)
i
)

Gets the i-th pattern "part".

public MessagePattern.Part.Type

Returns:

The Part.Type of the i-th Part.
getPartType
(int
The index of the Part data. (0..countParts()-1)
i
)

Returns the Part.Type of the i-th pattern "part".

public int

Returns:

The pattern index of this Part.
getPatternIndex
(int
The index of the Part data. (0..countParts()-1)
partIndex
)

Returns the pattern index of the specified pattern "part".

public String

Returns:

the parsed pattern string (null if none was parsed).
getPatternString
()

public double

Returns:

the "offset:" value.
getPluralOffset
(int
the index of the first PluralFormat argument style part. (0..countParts()-1)
pluralStart
)

Returns the "offset:" value of a PluralFormat argument, or 0 if none is specified.

public String

Returns:

the substring associated with part.
getSubstring
(MessagePattern.Part
a part of this MessagePattern.
part
)

Returns the substring of the pattern string indicated by the Part.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public boolean

Returns:

true if the parsed pattern has at least one named argument.
hasNamedArguments
()

Does the parsed pattern have named arguments like {first_name}?

public boolean

Returns:

true if the parsed pattern has at least one numbered argument.
hasNumberedArguments
()

Does the parsed pattern have numbered arguments like {2}?

private boolean

Returns:

true if we are inside a MessageFormat (sub-)pattern, as opposed to inside a top-level choice/plural/select pattern.
inMessageFormatPattern
(int nestingLevel)

private boolean

Returns:

true if we are in a MessageFormat sub-pattern of a top-level ChoiceFormat pattern.
inTopLevelChoiceMessage
(int nestingLevel, MessagePattern.ArgType parentType)

private static boolean
private boolean
isChoice(int index)

public boolean

Returns:

true if this object is frozen.
isFrozen
()

Implements com.ibm.icu.util.Freezable.isFrozen.

Determines whether this object is frozen (immutable) or not.
private boolean
isOrdinal(int index)

private boolean
isPlural(int index)

private boolean
isSelect(int index)

pack-priv boolean

Returns:

true if getApostropheMode() == ApostropheMode.DOUBLE_REQUIRED
jdkAposMode
()

public MessagePattern

Returns:

this
parse
(String
a MessageFormat pattern string
pattern
)

Parses a MessageFormat pattern string.

private int
parseArg(int index, int argStartLength, int nestingLevel)

private static int

Returns:

>=0 if the name is a valid number, ARG_NAME_NOT_NUMBER (-1) if it is a "pattern identifier" but not all ASCII digits, ARG_NAME_NOT_VALID (-2) if it is neither.
parseArgNumber
(CharSequence s, int start, int limit)

Validates and parses an argument name or argument number string.

private int
parseArgNumber(int start, int limit)

public MessagePattern

Returns:

this
parseChoiceStyle
(String
a ChoiceFormat pattern string
pattern
)

Parses a ChoiceFormat pattern string.

private int
parseChoiceStyle(int index, int nestingLevel)

private void
parseDouble(int
start index into the message string
start
,
int
limit index into the message string, must be startlimit, boolean
true if U+221E is allowed (for ChoiceFormat)
allowInfinity
)

Parses a number from the specified message substring.

private int
parseMessage(int index, int msgStartLength, int nestingLevel, MessagePattern.ArgType parentType)

private int
parsePluralOrSelectStyle(MessagePattern.ArgType argType, int index, int nestingLevel)

public MessagePattern

Returns:

this
parsePluralStyle
(String
a PluralFormat pattern string
pattern
)

Parses a PluralFormat pattern string.

public MessagePattern

Returns:

this
parseSelectStyle
(String
a SelectFormat pattern string
pattern
)

Parses a SelectFormat pattern string.

private int
parseSimpleStyle(int index)

public boolean

Returns:

true if getSubstring(part).equals(s).
partSubstringMatches
(MessagePattern.Part
a part of this MessagePattern.
part
,
String
a string.
s
)

Compares the part's substring with the input string s.

private void
private static String

Returns:

s.substring(start) or a prefix of that
prefix
(String s, int
start index in s
start
)

Returns a prefix of s.substring(start).

private static String
private String
prefix(int start)

private String
private void
preParse(String pattern)

private int
skipDouble(int index)

Skips a sequence of characters that could occur in a double value.

private int
skipIdentifier(int index)

private int
skipWhiteSpace(int index)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public static int

Returns:

>=0 if the name is a valid number, ARG_NAME_NOT_NUMBER (-1) if it is a "pattern identifier" but not all ASCII digits, ARG_NAME_NOT_VALID (-2) if it is neither.
validateArgumentName
(String
Input string.
name
)

Validates and parses an argument name or argument number string.

Inherited from java.lang.Object:
finalizegetClassnotifynotifyAllwaitwaitwait