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