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