Home | History | Annotate | Download | only in text
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 /*
      5  *******************************************************************************
      6  * Copyright (C) 2004-2016, International Business Machines Corporation and    *
      7  * others. All Rights Reserved.                                                *
      8  * Copyright (C) 2009 , Yahoo! Inc.                                            *
      9  *******************************************************************************
     10  */
     11 package android.icu.text;
     12 
     13 import java.io.IOException;
     14 import java.io.ObjectInputStream;
     15 import java.text.FieldPosition;
     16 import java.text.Format;
     17 import java.text.ParsePosition;
     18 
     19 import android.icu.impl.PatternProps;
     20 
     21 /**
     22  * <p><code>SelectFormat</code> supports the creation of  internationalized
     23  * messages by selecting phrases based on keywords. The pattern  specifies
     24  * how to map keywords to phrases and provides a default phrase. The
     25  * object provided to the format method is a string that's matched
     26  * against the keywords. If there is a match, the corresponding phrase
     27  * is selected; otherwise, the default phrase is used.
     28  *
     29  * <h3>Using <code>SelectFormat</code> for Gender Agreement</h3>
     30  *
     31  * <p>Note: Typically, select formatting is done via <code>MessageFormat</code>
     32  * with a <code>select</code> argument type,
     33  * rather than using a stand-alone <code>SelectFormat</code>.
     34  *
     35  * <p>The main use case for the select format is gender based  inflection.
     36  * When names or nouns are inserted into sentences, their gender can  affect pronouns,
     37  * verb forms, articles, and adjectives. Special care needs to be
     38  * taken for the case where the gender cannot be determined.
     39  * The impact varies between languages:
     40  *
     41  * <ul>
     42  * <li>English has three genders, and unknown gender is handled as a  special
     43  * case. Names use the gender of the named person (if known), nouns  referring
     44  * to people use natural gender, and inanimate objects are usually  neutral.
     45  * The gender only affects pronouns: "he", "she", "it", "they".
     46  *
     47  * <li>German differs from English in that the gender of nouns is  rather
     48  * arbitrary, even for nouns referring to people ("M&#xE4;dchen", girl, is  neutral).
     49  * The gender affects pronouns ("er", "sie", "es"), articles ("der",  "die",
     50  * "das"), and adjective forms ("guter Mann", "gute Frau", "gutes  M&#xE4;dchen").
     51  *
     52  * <li>French has only two genders; as in German the gender of nouns
     53  * is rather arbitrary - for sun and moon, the genders
     54  * are the opposite of those in German. The gender affects
     55  * pronouns ("il", "elle"), articles ("le", "la"),
     56  * adjective forms ("bon", "bonne"), and sometimes
     57  * verb forms ("all&#xE9;", "all&#xE9;e").
     58  *
     59  * <li>Polish distinguishes five genders (or noun classes),
     60  * human masculine, animate non-human masculine, inanimate masculine,
     61  * feminine, and neuter.
     62  * </ul>
     63  *
     64  * <p>Some other languages have noun classes that are not related to  gender,
     65  * but similar in grammatical use.
     66  * Some African languages have around 20 noun classes.
     67  *
     68  * <p><b>Note:</b>For the gender of a <i>person</i> in a given sentence,
     69  * we usually need to distinguish only between female, male and other/unknown.
     70  *
     71  * <p>To enable localizers to create sentence patterns that take their
     72  * language's gender dependencies into consideration, software has to  provide
     73  * information about the gender associated with a noun or name to
     74  * <code>MessageFormat</code>.
     75  * Two main cases can be distinguished:
     76  *
     77  * <ul>
     78  * <li>For people, natural gender information should be maintained  for each person.
     79  * Keywords like "male", "female", "mixed" (for groups of people)
     80  * and "unknown" could be used.
     81  *
     82  * <li>For nouns, grammatical gender information should be maintained  for
     83  * each noun and per language, e.g., in resource bundles.
     84  * The keywords "masculine", "feminine", and "neuter" are commonly  used,
     85  * but some languages may require other keywords.
     86  * </ul>
     87  *
     88  * <p>The resulting keyword is provided to <code>MessageFormat</code>  as a
     89  * parameter separate from the name or noun it's associated with. For  example,
     90  * to generate a message such as "Jean went to Paris", three separate  arguments
     91  * would be provided: The name of the person as argument 0, the  gender of
     92  * the person as argument 1, and the name of the city as argument 2.
     93  * The sentence pattern for English, where the gender of the person has
     94  * no impact on this simple sentence, would not refer to argument 1  at all:
     95  *
     96  * <pre>{0} went to {2}.</pre>
     97  *
     98  * <p><b>Note:</b> The entire sentence should be included (and partially repeated)
     99  * inside each phrase. Otherwise translators would have to be trained on how to
    100  * move bits of the sentence in and out of the select argument of a message.
    101  * (The examples below do not follow this recommendation!)
    102  *
    103  * <p>The sentence pattern for French, where the gender of the person affects
    104  * the form of the participle, uses a select format based on argument 1:
    105  *
    106  * <pre>{0} est {1, select, female {all&#xE9;e} other {all&#xE9;}} &#xE0; {2}.</pre>
    107  *
    108  * <p>Patterns can be nested, so that it's possible to handle  interactions of
    109  * number and gender where necessary. For example, if the above  sentence should
    110  * allow for the names of several people to be inserted, the  following sentence
    111  * pattern can be used (with argument 0 the list of people's names,
    112  * argument 1 the number of people, argument 2 their combined gender, and
    113  * argument 3 the city name):
    114  *
    115  * <pre>{0} {1, plural,
    116  * one {est {2, select, female {all&#xE9;e} other  {all&#xE9;}}}
    117  * other {sont {2, select, female {all&#xE9;es} other {all&#xE9;s}}}
    118  * }&#xE0; {3}.</pre>
    119  *
    120  * <h4>Patterns and Their Interpretation</h4>
    121  *
    122  * <p>The <code>SelectFormat</code> pattern string defines the phrase  output
    123  * for each user-defined keyword.
    124  * The pattern is a sequence of (keyword, message) pairs.
    125  * A keyword is a "pattern identifier": [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+
    126  *
    127  * <p>Each message is a MessageFormat pattern string enclosed in {curly braces}.
    128  *
    129  * <p>You always have to define a phrase for the default keyword
    130  * <code>other</code>; this phrase is returned when the keyword
    131  * provided to
    132  * the <code>format</code> method matches no other keyword.
    133  * If a pattern does not provide a phrase for <code>other</code>, the  method
    134  * it's provided to returns the error  <code>U_DEFAULT_KEYWORD_MISSING</code>.
    135  * <br>
    136  * Pattern_White_Space between keywords and messages is ignored.
    137  * Pattern_White_Space within a message is preserved and output.
    138  *
    139  * <pre>Example:
    140  * MessageFormat msgFmt = new MessageFormat("{0} est " +
    141  *     "{1, select, female {all&#xE9;e} other {all&#xE9;}} &#xE0; Paris.",
    142  *     new ULocale("fr"));
    143  * Object args[] = {"Kirti","female"};
    144  * System.out.println(msgFmt.format(args));
    145  * </pre>
    146  * <p>
    147  * Produces the output:<br>
    148  * <code>Kirti est all&#xE9;e &#xE0; Paris.</code>
    149  */
    150 
    151 public class SelectFormat extends Format{
    152     // Generated by serialver from JDK 1.5
    153     private static final long serialVersionUID = 2993154333257524984L;
    154 
    155     /*
    156      * The applied pattern string.
    157      */
    158     private String pattern = null;
    159 
    160     /**
    161      * The MessagePattern which contains the parsed structure of the pattern string.
    162      */
    163     transient private MessagePattern msgPattern;
    164 
    165     /**
    166      * Creates a new <code>SelectFormat</code> for a given pattern string.
    167      * @param  pattern the pattern for this <code>SelectFormat</code>.
    168      */
    169     public SelectFormat(String pattern) {
    170         applyPattern(pattern);
    171     }
    172 
    173     /*
    174      * Resets the <code>SelectFormat</code> object.
    175      */
    176     private void reset() {
    177         pattern = null;
    178         if(msgPattern != null) {
    179             msgPattern.clear();
    180         }
    181     }
    182 
    183     /**
    184      * Sets the pattern used by this select format.
    185      * Patterns and their interpretation are specified in the class description.
    186      *
    187      * @param pattern the pattern for this select format.
    188      * @throws IllegalArgumentException when the pattern is not a valid select format pattern.
    189      */
    190     public void applyPattern(String pattern) {
    191         this.pattern = pattern;
    192         if (msgPattern == null) {
    193             msgPattern = new MessagePattern();
    194         }
    195         try {
    196             msgPattern.parseSelectStyle(pattern);
    197         } catch(RuntimeException e) {
    198             reset();
    199             throw e;
    200         }
    201     }
    202 
    203     /**
    204      * Returns the pattern for this <code>SelectFormat</code>
    205      *
    206      * @return the pattern string
    207      */
    208     public String toPattern() {
    209         return pattern;
    210     }
    211 
    212     /**
    213      * Finds the SelectFormat sub-message for the given keyword, or the "other" sub-message.
    214      * @param pattern A MessagePattern.
    215      * @param partIndex the index of the first SelectFormat argument style part.
    216      * @param keyword a keyword to be matched to one of the SelectFormat argument's keywords.
    217      * @return the sub-message start part index.
    218      */
    219     /*package*/ static int findSubMessage(MessagePattern pattern, int partIndex, String keyword) {
    220         int count=pattern.countParts();
    221         int msgStart=0;
    222         // Iterate over (ARG_SELECTOR, message) pairs until ARG_LIMIT or end of select-only pattern.
    223         do {
    224             MessagePattern.Part part=pattern.getPart(partIndex++);
    225             MessagePattern.Part.Type type=part.getType();
    226             if(type==MessagePattern.Part.Type.ARG_LIMIT) {
    227                 break;
    228             }
    229             assert type==MessagePattern.Part.Type.ARG_SELECTOR;
    230             // part is an ARG_SELECTOR followed by a message
    231             if(pattern.partSubstringMatches(part, keyword)) {
    232                 // keyword matches
    233                 return partIndex;
    234             } else if(msgStart==0 && pattern.partSubstringMatches(part, "other")) {
    235                 msgStart=partIndex;
    236             }
    237             partIndex=pattern.getLimitPartIndex(partIndex);
    238         } while(++partIndex<count);
    239         return msgStart;
    240     }
    241 
    242     /**
    243      * Selects the phrase for the given keyword.
    244      *
    245      * @param keyword a phrase selection keyword.
    246      * @return the string containing the formatted select message.
    247      * @throws IllegalArgumentException when the given keyword is not a "pattern identifier"
    248      */
    249     public final String format(String keyword) {
    250         //Check for the validity of the keyword
    251         if (!PatternProps.isIdentifier(keyword)) {
    252             throw new IllegalArgumentException("Invalid formatting argument.");
    253         }
    254         // If no pattern was applied, throw an exception
    255         if (msgPattern == null || msgPattern.countParts() == 0) {
    256             throw new IllegalStateException("Invalid format error.");
    257         }
    258 
    259         // Get the appropriate sub-message.
    260         int msgStart = findSubMessage(msgPattern, 0, keyword);
    261         if (!msgPattern.jdkAposMode()) {
    262             int msgLimit = msgPattern.getLimitPartIndex(msgStart);
    263             return msgPattern.getPatternString().substring(msgPattern.getPart(msgStart).getLimit(),
    264                                                            msgPattern.getPatternIndex(msgLimit));
    265         }
    266         // JDK compatibility mode: Remove SKIP_SYNTAX.
    267         StringBuilder result = null;
    268         int prevIndex = msgPattern.getPart(msgStart).getLimit();
    269         for (int i = msgStart;;) {
    270             MessagePattern.Part part = msgPattern.getPart(++i);
    271             MessagePattern.Part.Type type = part.getType();
    272             int index = part.getIndex();
    273             if (type == MessagePattern.Part.Type.MSG_LIMIT) {
    274                 if (result == null) {
    275                     return pattern.substring(prevIndex, index);
    276                 } else {
    277                     return result.append(pattern, prevIndex, index).toString();
    278                 }
    279             } else if (type == MessagePattern.Part.Type.SKIP_SYNTAX) {
    280                 if (result == null) {
    281                     result = new StringBuilder();
    282                 }
    283                 result.append(pattern, prevIndex, index);
    284                 prevIndex = part.getLimit();
    285             } else if (type == MessagePattern.Part.Type.ARG_START) {
    286                 if (result == null) {
    287                     result = new StringBuilder();
    288                 }
    289                 result.append(pattern, prevIndex, index);
    290                 prevIndex = index;
    291                 i = msgPattern.getLimitPartIndex(i);
    292                 index = msgPattern.getPart(i).getLimit();
    293                 MessagePattern.appendReducedApostrophes(pattern, prevIndex, index, result);
    294                 prevIndex = index;
    295             }
    296         }
    297     }
    298 
    299     /**
    300      * Selects the phrase for the given keyword.
    301      * and appends the formatted message to the given <code>StringBuffer</code>.
    302      * @param keyword a phrase selection keyword.
    303      * @param toAppendTo the selected phrase will be appended to this
    304      *        <code>StringBuffer</code>.
    305      * @param pos will be ignored by this method.
    306      * @throws IllegalArgumentException when the given keyword is not a String
    307      *         or not a "pattern identifier"
    308      * @return the string buffer passed in as toAppendTo, with formatted text
    309      *         appended.
    310      */
    311     public StringBuffer format(Object keyword, StringBuffer toAppendTo,
    312             FieldPosition pos) {
    313         if (keyword instanceof String) {
    314             toAppendTo.append(format( (String)keyword));
    315         }else{
    316             throw new IllegalArgumentException("'" + keyword + "' is not a String");
    317         }
    318         return toAppendTo;
    319     }
    320 
    321     /**
    322      * This method is not supported by <code>SelectFormat</code>.
    323      * @param source the string to be parsed.
    324      * @param pos defines the position where parsing is to begin,
    325      * and upon return, the position where parsing left off.  If the position
    326      * has not changed upon return, then parsing failed.
    327      * @return nothing because this method is not supported.
    328      * @throws UnsupportedOperationException thrown always.
    329      */
    330     public Object parseObject(String source, ParsePosition pos) {
    331         throw new UnsupportedOperationException();
    332     }
    333 
    334     /**
    335      * {@inheritDoc}
    336      */
    337     @Override
    338     public boolean equals(Object obj) {
    339         if(this == obj) {
    340             return true;
    341         }
    342         if(obj == null || getClass() != obj.getClass()) {
    343             return false;
    344         }
    345         SelectFormat sf = (SelectFormat) obj;
    346         return msgPattern == null ? sf.msgPattern == null : msgPattern.equals(sf.msgPattern);
    347     }
    348 
    349     /**
    350      * {@inheritDoc}
    351      */
    352     @Override
    353     public int hashCode() {
    354         if (pattern != null) {
    355             return pattern.hashCode();
    356         }
    357         return 0;
    358     }
    359 
    360     /**
    361      * {@inheritDoc}
    362      */
    363     @Override
    364     public String toString() {
    365         return "pattern='" + pattern + "'";
    366     }
    367 
    368     private void readObject(ObjectInputStream in)
    369         throws IOException, ClassNotFoundException {
    370         in.defaultReadObject();
    371         if (pattern != null) {
    372             applyPattern(pattern);
    373         }
    374     }
    375 }
    376