1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 
27 /*
28  *******************************************************************************
29  * (C) Copyright IBM Corp. 1996-2005 - All Rights Reserved                     *
30  *                                                                             *
31  * The original version of this source code and documentation is copyrighted   *
32  * and owned by IBM, These materials are provided under terms of a License     *
33  * Agreement between IBM and Sun. This technology is protected by multiple     *
34  * US and International patents. This notice and attribution to IBM may not    *
35  * to removed.                                                                 *
36  *******************************************************************************
37  */
38 
39 package java.text;
40 
41 /**
42  * This class provides the method <code>normalize</code> which transforms Unicode
43  * text into an equivalent composed or decomposed form, allowing for easier
44  * sorting and searching of text.
45  * The <code>normalize</code> method supports the standard normalization forms
46  * described in
47  * <a href="http://www.unicode.org/unicode/reports/tr15/tr15-23.html">
48  * Unicode Standard Annex #15 &mdash; Unicode Normalization Forms</a>.
49  * <p>
50  * Characters with accents or other adornments can be encoded in
51  * several different ways in Unicode.  For example, take the character A-acute.
52  * In Unicode, this can be encoded as a single character (the "composed" form):
53  *
54  * <pre>
55  *      U+00C1    LATIN CAPITAL LETTER A WITH ACUTE</pre>
56  *
57  * or as two separate characters (the "decomposed" form):
58  *
59  * <pre>
60  *      U+0041    LATIN CAPITAL LETTER A
61  *      U+0301    COMBINING ACUTE ACCENT</pre>
62  *
63  * To a user of your program, however, both of these sequences should be
64  * treated as the same "user-level" character "A with acute accent".  When you
65  * are searching or comparing text, you must ensure that these two sequences are
66  * treated as equivalent.  In addition, you must handle characters with more than
67  * one accent. Sometimes the order of a character's combining accents is
68  * significant, while in other cases accent sequences in different orders are
69  * really equivalent.
70  * <p>
71  * Similarly, the string "ffi" can be encoded as three separate letters:
72  *
73  * <pre>
74  *      U+0066    LATIN SMALL LETTER F
75  *      U+0066    LATIN SMALL LETTER F
76  *      U+0069    LATIN SMALL LETTER I</pre>
77  *
78  * or as the single character
79  *
80  * <pre>
81  *      U+FB03    LATIN SMALL LIGATURE FFI</pre>
82  *
83  * The ffi ligature is not a distinct semantic character, and strictly speaking
84  * it shouldn't be in Unicode at all, but it was included for compatibility
85  * with existing character sets that already provided it.  The Unicode standard
86  * identifies such characters by giving them "compatibility" decompositions
87  * into the corresponding semantic characters.  When sorting and searching, you
88  * will often want to use these mappings.
89  * <p>
90  * The <code>normalize</code> method helps solve these problems by transforming
91  * text into the canonical composed and decomposed forms as shown in the first
92  * example above. In addition, you can have it perform compatibility
93  * decompositions so that you can treat compatibility characters the same as
94  * their equivalents.
95  * Finally, the <code>normalize</code> method rearranges accents into the
96  * proper canonical order, so that you do not have to worry about accent
97  * rearrangement on your own.
98  * <p>
99  * The W3C generally recommends to exchange texts in NFC.
100  * Note also that most legacy character encodings use only precomposed forms and
101  * often do not encode any combining marks by themselves. For conversion to such
102  * character encodings the Unicode text needs to be normalized to NFC.
103  * For more usage examples, see the Unicode Standard Annex.
104  *
105  * @since 1.6
106  */
107 public final class Normalizer {
108 
Normalizer()109    private Normalizer() {};
110 
111     /**
112      * This enum provides constants of the four Unicode normalization forms
113      * that are described in
114      * <a href="http://www.unicode.org/unicode/reports/tr15/tr15-23.html">
115      * Unicode Standard Annex #15 &mdash; Unicode Normalization Forms</a>
116      * and two methods to access them.
117      *
118      * @since 1.6
119      */
120     // BEGIN Android-changed: remove static modifier and add mapping to equivalent ICU values.
121     public enum Form {
122 
123         /**
124          * Canonical decomposition.
125          */
126         NFD(android.icu.text.Normalizer.NFD),
127 
128         /**
129          * Canonical decomposition, followed by canonical composition.
130          */
131         NFC(android.icu.text.Normalizer.NFC),
132 
133         /**
134          * Compatibility decomposition.
135          */
136         NFKD(android.icu.text.Normalizer.NFKD),
137 
138         /**
139          * Compatibility decomposition, followed by canonical composition.
140          */
141         NFKC(android.icu.text.Normalizer.NFKC);
142 
143         private final android.icu.text.Normalizer.Mode icuMode;
144 
Form(android.icu.text.Normalizer.Mode icuMode)145         Form(android.icu.text.Normalizer.Mode icuMode) {
146             this.icuMode = icuMode;
147         }
148     }
149     // END Android-changed: remove static modifier and add mapping to equivalent ICU values.
150 
151     /**
152      * Normalize a sequence of char values.
153      * The sequence will be normalized according to the specified normalization
154      * from.
155      * @param src        The sequence of char values to normalize.
156      * @param form       The normalization form; one of
157      *                   {@link java.text.Normalizer.Form#NFC},
158      *                   {@link java.text.Normalizer.Form#NFD},
159      *                   {@link java.text.Normalizer.Form#NFKC},
160      *                   {@link java.text.Normalizer.Form#NFKD}
161      * @return The normalized String
162      * @throws NullPointerException If <code>src</code> or <code>form</code>
163      * is null.
164      */
normalize(CharSequence src, Form form)165     public static String normalize(CharSequence src, Form form) {
166         // Android-changed: Switched to ICU.
167         return android.icu.text.Normalizer.normalize(src.toString(), form.icuMode);
168     }
169 
170     /**
171      * Determines if the given sequence of char values is normalized.
172      * @param src        The sequence of char values to be checked.
173      * @param form       The normalization form; one of
174      *                   {@link java.text.Normalizer.Form#NFC},
175      *                   {@link java.text.Normalizer.Form#NFD},
176      *                   {@link java.text.Normalizer.Form#NFKC},
177      *                   {@link java.text.Normalizer.Form#NFKD}
178      * @return true if the sequence of char values is normalized;
179      * false otherwise.
180      * @throws NullPointerException If <code>src</code> or <code>form</code>
181      * is null.
182      */
isNormalized(CharSequence src, Form form)183     public static boolean isNormalized(CharSequence src, Form form) {
184         // Android-changed: Switched to ICU.
185         return android.icu.text.Normalizer.isNormalized(src.toString(), form.icuMode, 0);
186     }
187 }
188