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