/* GENERATED SOURCE. DO NOT MODIFY. */ // © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /* ******************************************************************************* * Copyright (C) 2010-2016, International Business Machines * Corporation and others. All Rights Reserved. ******************************************************************************* * created on: 2010aug21 * created by: Markus W. Scherer */ package android.icu.text; import java.util.ArrayList; import java.util.Locale; import android.icu.impl.ICUConfig; import android.icu.impl.PatternProps; import android.icu.util.Freezable; import android.icu.util.ICUCloneNotSupportedException; //Note: Minimize ICU dependencies, only use a very small part of the ICU core. //In particular, do not depend on *Format classes. /** * 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", {@link #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.
*
* @author Markus Scherer
*/
public final class MessagePattern implements Cloneable, Freezable
* A pair of adjacent apostrophes always results in a single apostrophe in the output,
* even when the pair is between two single, text-quoting apostrophes.
*
* The following table shows examples of desired MessageFormat.format() output
* with the pattern strings that yield that output.
*
*
* This is the default behavior starting with ICU 4.8.
*/
DOUBLE_OPTIONAL,
/**
* A literal apostrophe must be represented by
* a double apostrophe pattern character.
* A single apostrophe always starts quoted literal text.
*
* This is the behavior of ICU 4.6 and earlier, and of {@link java.text.MessageFormat}.
*/
DOUBLE_REQUIRED
}
/**
* Constructs an empty MessagePattern with default ApostropheMode.
*/
public MessagePattern() {
aposMode=defaultAposMode;
}
/**
* Constructs an empty MessagePattern.
* @param mode Explicit ApostropheMode.
*/
public MessagePattern(ApostropheMode mode) {
aposMode=mode;
}
/**
* Constructs a MessagePattern with default ApostropheMode and
* parses the MessageFormat pattern string.
* @param pattern a MessageFormat pattern string
* @throws IllegalArgumentException for syntax errors in the pattern string
* @throws IndexOutOfBoundsException if certain limits are exceeded
* (e.g., argument number too high, argument name too long, etc.)
* @throws NumberFormatException if a number could not be parsed
*/
public MessagePattern(String pattern) {
aposMode=defaultAposMode;
parse(pattern);
}
/**
* Parses a MessageFormat pattern string.
* @param pattern a MessageFormat pattern string
* @return this
* @throws IllegalArgumentException for syntax errors in the pattern string
* @throws IndexOutOfBoundsException if certain limits are exceeded
* (e.g., argument number too high, argument name too long, etc.)
* @throws NumberFormatException if a number could not be parsed
*/
public MessagePattern parse(String pattern) {
preParse(pattern);
parseMessage(0, 0, 0, ArgType.NONE);
postParse();
return this;
}
/**
* Parses a ChoiceFormat pattern string.
* @param pattern a ChoiceFormat pattern string
* @return this
* @throws IllegalArgumentException for syntax errors in the pattern string
* @throws IndexOutOfBoundsException if certain limits are exceeded
* (e.g., argument number too high, argument name too long, etc.)
* @throws NumberFormatException if a number could not be parsed
*/
public MessagePattern parseChoiceStyle(String pattern) {
preParse(pattern);
parseChoiceStyle(0, 0);
postParse();
return this;
}
/**
* Parses a PluralFormat pattern string.
* @param pattern a PluralFormat pattern string
* @return this
* @throws IllegalArgumentException for syntax errors in the pattern string
* @throws IndexOutOfBoundsException if certain limits are exceeded
* (e.g., argument number too high, argument name too long, etc.)
* @throws NumberFormatException if a number could not be parsed
*/
public MessagePattern parsePluralStyle(String pattern) {
preParse(pattern);
parsePluralOrSelectStyle(ArgType.PLURAL, 0, 0);
postParse();
return this;
}
/**
* Parses a SelectFormat pattern string.
* @param pattern a SelectFormat pattern string
* @return this
* @throws IllegalArgumentException for syntax errors in the pattern string
* @throws IndexOutOfBoundsException if certain limits are exceeded
* (e.g., argument number too high, argument name too long, etc.)
* @throws NumberFormatException if a number could not be parsed
*/
public MessagePattern parseSelectStyle(String pattern) {
preParse(pattern);
parsePluralOrSelectStyle(ArgType.SELECT, 0, 0);
postParse();
return this;
}
/**
* Clears this MessagePattern.
* countParts() will return 0.
*/
public void clear() {
// Mostly the same as preParse().
if(isFrozen()) {
throw new UnsupportedOperationException(
"Attempt to clear() a frozen MessagePattern instance.");
}
msg=null;
hasArgNames=hasArgNumbers=false;
needsAutoQuoting=false;
parts.clear();
if(numericValues!=null) {
numericValues.clear();
}
}
/**
* Clears this MessagePattern and sets the ApostropheMode.
* countParts() will return 0.
* @param mode The new ApostropheMode.
*/
public void clearPatternAndSetApostropheMode(ApostropheMode mode) {
clear();
aposMode=mode;
}
/**
* @param other another object to compare with.
* @return true if this object is equivalent to the other one.
*/
@Override
public boolean equals(Object other) {
if(this==other) {
return true;
}
if(other==null || getClass()!=other.getClass()) {
return false;
}
MessagePattern o=(MessagePattern)other;
return
aposMode.equals(o.aposMode) &&
(msg==null ? o.msg==null : msg.equals(o.msg)) &&
parts.equals(o.parts);
// No need to compare numericValues if msg and parts are the same.
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return (aposMode.hashCode()*37+(msg!=null ? msg.hashCode() : 0))*37+parts.hashCode();
}
/**
* @return this instance's ApostropheMode.
*/
public ApostropheMode getApostropheMode() {
return aposMode;
}
/**
* @return true if getApostropheMode() == ApostropheMode.DOUBLE_REQUIRED
* @hide draft / provisional / internal are hidden on Android
*/
/* package */ boolean jdkAposMode() {
return aposMode == ApostropheMode.DOUBLE_REQUIRED;
}
/**
* @return the parsed pattern string (null if none was parsed).
*/
public String getPatternString() {
return msg;
}
/**
* Does the parsed pattern have named arguments like {first_name}?
* @return true if the parsed pattern has at least one named argument.
*/
public boolean hasNamedArguments() {
return hasArgNames;
}
/**
* Does the parsed pattern have numbered arguments like {2}?
* @return true if the parsed pattern has at least one numbered argument.
*/
public boolean hasNumberedArguments() {
return hasArgNumbers;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return msg;
}
/**
* 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.
* @param name Input string.
* @return >=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.
*/
public static int validateArgumentName(String name) {
if(!PatternProps.isIdentifier(name)) {
return ARG_NAME_NOT_VALID;
}
return parseArgNumber(name, 0, name.length());
}
/**
* Return value from {@link #validateArgumentName(String)} for when
* the string is a valid "pattern identifier" but not a number.
*/
public static final int ARG_NAME_NOT_NUMBER=-1;
/**
* Return value from {@link #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 int ARG_NAME_NOT_VALID=-2;
/**
* 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}}."
* @return the deep-auto-quoted version of the parsed pattern string.
* @see MessageFormat#autoQuoteApostrophe(String)
*/
public String autoQuoteApostropheDeep() {
if(!needsAutoQuoting) {
return msg;
}
StringBuilder modified=null;
// Iterate backward so that the insertion indexes do not change.
int count=countParts();
for(int i=count; i>0;) {
Part part;
if((part=getPart(--i)).getType()==Part.Type.INSERT_CHAR) {
if(modified==null) {
modified=new StringBuilder(msg.length()+10).append(msg);
}
modified.insert(part.index, (char)part.value);
}
}
if(modified==null) {
return msg;
} else {
return modified.toString();
}
}
/**
* Returns the number of "parts" created by parsing the pattern string.
* Returns 0 if no pattern has been parsed or clear() was called.
* @return the number of pattern parts.
*/
public int countParts() {
return parts.size();
}
/**
* Gets the i-th pattern "part".
* @param i The index of the Part data. (0..countParts()-1)
* @return the i-th pattern "part".
* @throws IndexOutOfBoundsException if i is outside the (0..countParts()-1) range
*/
public Part getPart(int i) {
return parts.get(i);
}
/**
* Returns the Part.Type of the i-th pattern "part".
* Convenience method for getPart(i).getType().
* @param i The index of the Part data. (0..countParts()-1)
* @return The Part.Type of the i-th Part.
* @throws IndexOutOfBoundsException if i is outside the (0..countParts()-1) range
*/
public Part.Type getPartType(int i) {
return parts.get(i).type;
}
/**
* Returns the pattern index of the specified pattern "part".
* Convenience method for getPart(partIndex).getIndex().
* @param partIndex The index of the Part data. (0..countParts()-1)
* @return The pattern index of this Part.
* @throws IndexOutOfBoundsException if partIndex is outside the (0..countParts()-1) range
*/
public int getPatternIndex(int partIndex) {
return parts.get(partIndex).index;
}
/**
* Returns the substring of the pattern string indicated by the Part.
* Convenience method for getPatternString().substring(part.getIndex(), part.getLimit()).
* @param part a part of this MessagePattern.
* @return the substring associated with part.
*/
public String getSubstring(Part part) {
int index=part.index;
return msg.substring(index, index+part.length);
}
/**
* Compares the part's substring with the input string s.
* @param part a part of this MessagePattern.
* @param s a string.
* @return true if getSubstring(part).equals(s).
*/
public boolean partSubstringMatches(Part part, String s) {
return part.length == s.length() && msg.regionMatches(part.index, s, 0, part.length);
}
/**
* Returns the numeric value associated with an ARG_INT or ARG_DOUBLE.
* @param part a part of this MessagePattern.
* @return the part's numeric value, or NO_NUMERIC_VALUE if this is not a numeric part.
*/
public double getNumericValue(Part part) {
Part.Type type=part.type;
if(type==Part.Type.ARG_INT) {
return part.value;
} else if(type==Part.Type.ARG_DOUBLE) {
return numericValues.get(part.value);
} else {
return NO_NUMERIC_VALUE;
}
}
/**
* Special value that is returned by getNumericValue(Part) when no
* numeric value is defined for a part.
* @see #getNumericValue
*/
public static final double NO_NUMERIC_VALUE=-123456789;
/**
* Returns the "offset:" value of a PluralFormat argument, or 0 if none is specified.
* @param pluralStart the index of the first PluralFormat argument style part. (0..countParts()-1)
* @return the "offset:" value.
* @throws IndexOutOfBoundsException if pluralStart is outside the (0..countParts()-1) range
*/
public double getPluralOffset(int pluralStart) {
Part part=parts.get(pluralStart);
if(part.type.hasNumericValue()) {
return getNumericValue(part);
} else {
return 0;
}
}
/**
* Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start.
* @param start The index of some Part data (0..countParts()-1);
* this Part should be of Type ARG_START or MSG_START.
* @return 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 IndexOutOfBoundsException if start is outside the (0..countParts()-1) range
*/
public int getLimitPartIndex(int start) {
int limit=parts.get(start).limitPartIndex;
if(limit
* This part is followed by either an ARG_NUMBER or ARG_NAME,
* followed by optional argument sub-parts (see ArgType constants)
* and finally an ARG_LIMIT part.
*/
ARG_START,
/**
* End of an argument.
* The length is 1 for the '}'.
* The value is the ordinal value of the ArgType. Use getArgType().
*/
ARG_LIMIT,
/**
* The argument number, provided by the value.
*/
ARG_NUMBER,
/**
* The argument name.
* The value is undefined and currently always 0.
*/
ARG_NAME,
/**
* The argument type.
* The value is undefined and currently always 0.
*/
ARG_TYPE,
/**
* The argument style text.
* The value is undefined and currently always 0.
*/
ARG_STYLE,
/**
* A selector substring in a "complex" argument style.
* The value is undefined and currently always 0.
*/
ARG_SELECTOR,
/**
* An integer value, for example the offset or an explicit selector value
* in a PluralFormat style.
* The part value is the integer value.
*/
ARG_INT,
/**
* A numeric value, for example the offset or an explicit selector value
* in a PluralFormat style.
* The part value is an index into an internal array of numeric values;
* use getNumericValue().
*/
ARG_DOUBLE;
/**
* Indicates whether this part has a numeric value.
* If so, then that numeric value can be retrieved via {@link MessagePattern#getNumericValue(Part)}.
* @return true if this part has a numeric value.
*/
public boolean hasNumericValue() {
return this==ARG_INT || this==ARG_DOUBLE;
}
}
/**
* @return a string representation of this part.
*/
@Override
public String toString() {
String valueString=(type==Type.ARG_START || type==Type.ARG_LIMIT) ?
getArgType().name() : Integer.toString(value);
return type.name()+"("+valueString+")@"+index;
}
/**
* @param other another object to compare with.
* @return true if this object is equivalent to the other one.
*/
@Override
public boolean equals(Object other) {
if(this==other) {
return true;
}
if(other==null || getClass()!=other.getClass()) {
return false;
}
Part o=(Part)other;
return
type.equals(o.type) &&
index==o.index &&
length==o.length &&
value==o.value &&
limitPartIndex==o.limitPartIndex;
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return ((type.hashCode()*37+index)*37+length)*37+value;
}
private static final int MAX_LENGTH=0xffff;
private static final int MAX_VALUE=Short.MAX_VALUE;
// Some fields are not final because they are modified during pattern parsing.
// After pattern parsing, the parts are effectively immutable.
private final Type type;
private final int index;
private final char length;
private short value;
private int limitPartIndex;
}
/**
* Argument type constants.
* Returned by Part.getArgType() for ARG_START and ARG_LIMIT parts.
*
* Messages nested inside an argument are each delimited by MSG_START and MSG_LIMIT,
* with a nesting level one greater than the surrounding message.
*/
public enum ArgType {
/**
* The argument has no specified type.
*/
NONE,
/**
* The argument has a "simple" type which is provided by the ARG_TYPE part.
* An ARG_STYLE part might follow that.
*/
SIMPLE,
/**
* The argument is a ChoiceFormat with one or more
* ((ARG_INT | ARG_DOUBLE), ARG_SELECTOR, message) tuples.
*/
CHOICE,
/**
* The argument is a cardinal-number PluralFormat with an optional ARG_INT or ARG_DOUBLE offset
* (e.g., offset:1)
* and one or more (ARG_SELECTOR [explicit-value] message) tuples.
* If the selector has an explicit value (e.g., =2), then
* that value is provided by the ARG_INT or ARG_DOUBLE part preceding the message.
* Otherwise the message immediately follows the ARG_SELECTOR.
*/
PLURAL,
/**
* The argument is a SelectFormat with one or more (ARG_SELECTOR, message) pairs.
*/
SELECT,
/**
* The argument is an ordinal-number PluralFormat
* with the same style parts sequence and semantics as {@link ArgType#PLURAL}.
*/
SELECTORDINAL;
/**
* @return true if the argument type has a plural style part sequence and semantics,
* for example {@link ArgType#PLURAL} and {@link ArgType#SELECTORDINAL}.
*/
public boolean hasPluralStyle() {
return this == PLURAL || this == SELECTORDINAL;
}
}
/**
* Creates and returns a copy of this object.
* @return a copy of this object (or itself if frozen).
*/
@Override
public Object clone() {
if(isFrozen()) {
return this;
} else {
return cloneAsThawed();
}
}
/**
* Creates and returns an unfrozen copy of this object.
* @return a copy of this object.
*/
@Override
@SuppressWarnings("unchecked")
public MessagePattern cloneAsThawed() {
MessagePattern newMsg;
try {
newMsg=(MessagePattern)super.clone();
} catch (CloneNotSupportedException e) {
throw new ICUCloneNotSupportedException(e);
}
newMsg.parts=(ArrayList
*
*/
public enum ApostropheMode {
/**
* A literal apostrophe is represented by
* either a single or a double apostrophe pattern character.
* Within a MessageFormat pattern, a single apostrophe only starts quoted literal text
* if it immediately precedes a curly brace {},
* or a pipe symbol | if inside a choice format,
* or a pound symbol # if inside a plural format.
*
*
* Desired output
* DOUBLE_OPTIONAL
* DOUBLE_REQUIRED
*
*
* I see {many}
* I see '{many}'
* (same)
*
*
* I said {'Wow!'}
* I said '{''Wow!''}'
* (same)
*
*
* I don't know
* I don't know OR
*
I don''t knowI don''t know
*