Class MessagePattern
- java.lang.Object
-
- com.vmware.vipclient.i18n.l2.text.MessagePattern
-
public class MessagePattern extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMessagePattern.ApostropheModestatic classMessagePattern.ArgTypeArgument type constants.static classMessagePattern.Part
-
Field Summary
Fields Modifier and Type Field Description static intARG_NAME_NOT_NUMBERReturn value fromvalidateArgumentName(String)for when the string is a valid "pattern identifier" but not a number.static intARG_NAME_NOT_VALIDReturn value fromvalidateArgumentName(String)for when the string is invalid.static doubleNO_NUMERIC_VALUESpecial value that is returned by getNumericValue(Part) when no numeric value is defined for a part.
-
Constructor Summary
Constructors Constructor Description MessagePattern()MessagePattern(MessagePattern.ApostropheMode mode)Constructs an empty MessagePattern.MessagePattern(java.lang.String pattern)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringautoQuoteApostropheDeep()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.voidclear()Clears this MessagePattern.voidclearPatternAndSetApostropheMode(MessagePattern.ApostropheMode mode)Clears this MessagePattern and sets the ApostropheMode.intcountParts()Returns the number of "parts" created by parsing the pattern string.intfindEndQuoteIndex(int index)MessagePattern.ApostropheModegetApostropheMode()intgetLimitPartIndex(int start)Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start.doublegetNumericValue(MessagePattern.Part part)Returns the numeric value associated with an ARG_INT or ARG_DOUBLE.MessagePattern.PartgetPart(int i)Gets the i-th pattern "part".MessagePattern.Part.TypegetPartType(int i)Returns the Part.Type of the i-th pattern "part".intgetPatternIndex(int partIndex)Returns the pattern index of the specified pattern "part".java.lang.StringgetPatternString()doublegetPluralOffset(int pluralStart)Returns the "offset:" value of a PluralFormat argument, or 0 if none is specified.java.lang.StringgetSubstring(MessagePattern.Part part)Returns the substring of the pattern string indicated by the Part.booleanhasNamedArguments()Does the parsed pattern have named arguments like {first_name}?booleanhasNumberedArguments()Does the parsed pattern have numbered arguments like {2}?booleanisFrozen()Determines whether this object is frozen (immutable) or not.static booleanisNumeric(java.lang.String str)MessagePatternparse(java.lang.String pattern)intparseArgNameOrNumber(int nameIndex, int index)intparseArgStyle(MessagePattern.ArgType argType, int nameIndex, int index, int nestingLevel)java.util.Map<java.lang.String,java.lang.Object>parseArgType(int argStart, int nameIndex, int index)intparseExplicitValuePluralSelector(MessagePattern.ArgType argType, int start, int selectorIndex, int index)intparsePluralOffset(boolean isEmpty, int start, int index)java.util.Map<java.lang.String,java.lang.Object>parsePluralOffsetOrNormalSelector(MessagePattern.ArgType argType, int start, int selectorIndex, int index, boolean isEmpty)MessagePatternparsePluralStyle(java.lang.String pattern)Parses a PluralFormat pattern string.booleanpartSubstringMatches(MessagePattern.Part part, java.lang.String s)Compares the part's substring with the input string s.java.lang.StringtoString()static intvalidateArgumentName(java.lang.String name)Validates and parses an argument name or argument number string.
-
-
-
Field Detail
-
ARG_NAME_NOT_NUMBER
public static final int ARG_NAME_NOT_NUMBER
Return value fromvalidateArgumentName(String)for when the string is a valid "pattern identifier" but not a number.- See Also:
- Constant Field Values
-
ARG_NAME_NOT_VALID
public static final int ARG_NAME_NOT_VALID
Return value fromvalidateArgumentName(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.- See Also:
- Constant Field Values
-
NO_NUMERIC_VALUE
public static final double NO_NUMERIC_VALUE
Special value that is returned by getNumericValue(Part) when no numeric value is defined for a part.
-
-
Constructor Detail
-
MessagePattern
public MessagePattern()
-
MessagePattern
public MessagePattern(java.lang.String pattern)
-
MessagePattern
public MessagePattern(MessagePattern.ApostropheMode mode)
Constructs an empty MessagePattern.- Parameters:
mode- Explicit ApostropheMode.
-
-
Method Detail
-
parse
public MessagePattern parse(java.lang.String pattern)
-
parsePluralStyle
public MessagePattern parsePluralStyle(java.lang.String pattern)
Parses a PluralFormat pattern string.- Parameters:
pattern- a PluralFormat pattern string- Returns:
- this
- Throws:
java.lang.IllegalArgumentException- for syntax errors in the pattern stringjava.lang.IndexOutOfBoundsException- if certain limits are exceeded (e.g., argument number too high, argument name too long, etc.)java.lang.NumberFormatException- if a number could not be parsed
-
findEndQuoteIndex
public int findEndQuoteIndex(int index)
-
parseArgNameOrNumber
public int parseArgNameOrNumber(int nameIndex, int index)
-
parseArgType
public java.util.Map<java.lang.String,java.lang.Object> parseArgType(int argStart, int nameIndex, int index)
-
parseArgStyle
public int parseArgStyle(MessagePattern.ArgType argType, int nameIndex, int index, int nestingLevel)
-
parseExplicitValuePluralSelector
public int parseExplicitValuePluralSelector(MessagePattern.ArgType argType, int start, int selectorIndex, int index)
-
parsePluralOffsetOrNormalSelector
public java.util.Map<java.lang.String,java.lang.Object> parsePluralOffsetOrNormalSelector(MessagePattern.ArgType argType, int start, int selectorIndex, int index, boolean isEmpty)
-
parsePluralOffset
public int parsePluralOffset(boolean isEmpty, int start, int index)
-
isNumeric
public static boolean isNumeric(java.lang.String str)
-
clear
public void clear()
Clears this MessagePattern. countParts() will return 0.
-
clearPatternAndSetApostropheMode
public void clearPatternAndSetApostropheMode(MessagePattern.ApostropheMode mode)
Clears this MessagePattern and sets the ApostropheMode. countParts() will return 0.- Parameters:
mode- The new ApostropheMode.
-
getApostropheMode
public MessagePattern.ApostropheMode getApostropheMode()
- Returns:
- this instance's ApostropheMode.
-
getPatternString
public java.lang.String getPatternString()
- Returns:
- the parsed pattern string (null if none was parsed).
-
hasNamedArguments
public boolean hasNamedArguments()
Does the parsed pattern have named arguments like {first_name}?- Returns:
- true if the parsed pattern has at least one named argument.
-
hasNumberedArguments
public boolean hasNumberedArguments()
Does the parsed pattern have numbered arguments like {2}?- Returns:
- true if the parsed pattern has at least one numbered argument.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
validateArgumentName
public static int validateArgumentName(java.lang.String name)
Validates and parses an argument name or argument number string. An argument name must be a "pattern identifier", that is, it must contain no Unicode Pattern_Syntax or Pattern_White_Space characters. If it only contains ASCII digits, then it must be a small integer with no leading zero.- Parameters:
name- Input string.- 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.
-
autoQuoteApostropheDeep
public java.lang.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.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}}."
- Returns:
- the deep-auto-quoted version of the parsed pattern string.
- See Also:
MessageFormat#autoQuoteApostrophe(String)
-
countParts
public int countParts()
Returns the number of "parts" created by parsing the pattern string. Returns 0 if no pattern has been parsed or clear() was called.- Returns:
- the number of pattern parts.
-
getPart
public MessagePattern.Part getPart(int i)
Gets the i-th pattern "part".- Parameters:
i- The index of the Part data. (0..countParts()-1)- Returns:
- the i-th pattern "part".
- Throws:
java.lang.IndexOutOfBoundsException- if i is outside the (0..countParts()-1) range
-
getPartType
public MessagePattern.Part.Type getPartType(int i)
Returns the Part.Type of the i-th pattern "part". Convenience method for getPart(i).getType().- Parameters:
i- The index of the Part data. (0..countParts()-1)- Returns:
- The Part.Type of the i-th Part.
- Throws:
java.lang.IndexOutOfBoundsException- if i is outside the (0..countParts()-1) range
-
getPatternIndex
public int getPatternIndex(int partIndex)
Returns the pattern index of the specified pattern "part". Convenience method for getPart(partIndex).getIndex().- Parameters:
partIndex- The index of the Part data. (0..countParts()-1)- Returns:
- The pattern index of this Part.
- Throws:
java.lang.IndexOutOfBoundsException- if partIndex is outside the (0..countParts()-1) range
-
getSubstring
public java.lang.String getSubstring(MessagePattern.Part part)
Returns the substring of the pattern string indicated by the Part. Convenience method for getPatternString().substring(part.getIndex(), part.getLimit()).- Parameters:
part- a part of this MessagePattern.- Returns:
- the substring associated with part.
-
partSubstringMatches
public boolean partSubstringMatches(MessagePattern.Part part, java.lang.String s)
Compares the part's substring with the input string s.- Parameters:
part- a part of this MessagePattern.s- a string.- Returns:
- true if getSubstring(part).equals(s).
-
getNumericValue
public double getNumericValue(MessagePattern.Part part)
Returns the numeric value associated with an ARG_INT or ARG_DOUBLE.- Parameters:
part- a part of this MessagePattern.- Returns:
- the part's numeric value, or NO_NUMERIC_VALUE if this is not a numeric part.
-
getPluralOffset
public double getPluralOffset(int pluralStart)
Returns the "offset:" value of a PluralFormat argument, or 0 if none is specified.- Parameters:
pluralStart- the index of the first PluralFormat argument style part. (0..countParts()-1)- Returns:
- the "offset:" value.
- Throws:
java.lang.IndexOutOfBoundsException- if pluralStart is outside the (0..countParts()-1) range
-
getLimitPartIndex
public int getLimitPartIndex(int start)
Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start.- Parameters:
start- The index of some Part data (0..countParts()-1); this Part should be of Type ARG_START or MSG_START.- 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.
- Throws:
java.lang.IndexOutOfBoundsException- if start is outside the (0..countParts()-1) range
-
isFrozen
public boolean isFrozen()
Determines whether this object is frozen (immutable) or not.- Returns:
- true if this object is frozen.
-
-