1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 2005, 2006, 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 — 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 * <p><pre> 55 * U+00C1 LATIN CAPITAL LETTER A WITH ACUTE</pre> 56 * </p> 57 * 58 * or as two separate characters (the "decomposed" form): 59 * 60 * <p><pre> 61 * U+0041 LATIN CAPITAL LETTER A 62 * U+0301 COMBINING ACUTE ACCENT</pre> 63 * </p> 64 * 65 * To a user of your program, however, both of these sequences should be 66 * treated as the same "user-level" character "A with acute accent". When you 67 * are searching or comparing text, you must ensure that these two sequences are 68 * treated as equivalent. In addition, you must handle characters with more than 69 * one accent. Sometimes the order of a character's combining accents is 70 * significant, while in other cases accent sequences in different orders are 71 * really equivalent. 72 * <p> 73 * Similarly, the string "ffi" can be encoded as three separate letters: 74 * 75 * <p><pre> 76 * U+0066 LATIN SMALL LETTER F 77 * U+0066 LATIN SMALL LETTER F 78 * U+0069 LATIN SMALL LETTER I</pre> 79 * </p> 80 * 81 * or as the single character 82 * 83 * <p><pre> 84 * U+FB03 LATIN SMALL LIGATURE FFI</pre> 85 * </p> 86 * 87 * The ffi ligature is not a distinct semantic character, and strictly speaking 88 * it shouldn't be in Unicode at all, but it was included for compatibility 89 * with existing character sets that already provided it. The Unicode standard 90 * identifies such characters by giving them "compatibility" decompositions 91 * into the corresponding semantic characters. When sorting and searching, you 92 * will often want to use these mappings. 93 * <p> 94 * The <code>normalize</code> method helps solve these problems by transforming 95 * text into the canonical composed and decomposed forms as shown in the first 96 * example above. In addition, you can have it perform compatibility 97 * decompositions so that you can treat compatibility characters the same as 98 * their equivalents. 99 * Finally, the <code>normalize</code> method rearranges accents into the 100 * proper canonical order, so that you do not have to worry about accent 101 * rearrangement on your own. 102 * <p> 103 * The W3C generally recommends to exchange texts in NFC. 104 * Note also that most legacy character encodings use only precomposed forms and 105 * often do not encode any combining marks by themselves. For conversion to such 106 * character encodings the Unicode text needs to be normalized to NFC. 107 * For more usage examples, see the Unicode Standard Annex. 108 * 109 * @since 1.6 110 */ 111 public final class Normalizer { 112 Normalizer()113 private Normalizer() { 114 } 115 116 /** 117 * This enum provides constants of the four Unicode normalization forms 118 * that are described in 119 * <a href="http://www.unicode.org/unicode/reports/tr15/tr15-23.html"> 120 * Unicode Standard Annex #15 — Unicode Normalization Forms</a> 121 * and two methods to access them. 122 * 123 * @since 1.6 124 */ 125 public enum Form { 126 127 /** 128 * Canonical decomposition. 129 */ 130 NFD(android.icu.text.Normalizer.NFD), 131 132 /** 133 * Canonical decomposition, followed by canonical composition. 134 */ 135 NFC(android.icu.text.Normalizer.NFC), 136 137 /** 138 * Compatibility decomposition. 139 */ 140 NFKD(android.icu.text.Normalizer.NFKD), 141 142 /** 143 * Compatibility decomposition, followed by canonical composition. 144 */ 145 NFKC(android.icu.text.Normalizer.NFKC); 146 147 private final android.icu.text.Normalizer.Mode icuMode; 148 Form(android.icu.text.Normalizer.Mode icuMode)149 Form(android.icu.text.Normalizer.Mode icuMode) { 150 this.icuMode = icuMode; 151 } 152 } 153 154 /** 155 * Normalize a sequence of char values. 156 * The sequence will be normalized according to the specified normalization 157 * from. 158 * 159 * @param src The sequence of char values to normalize. 160 * @param form The normalization form; one of 161 * {@link java.text.Normalizer.Form#NFC}, 162 * {@link java.text.Normalizer.Form#NFD}, 163 * {@link java.text.Normalizer.Form#NFKC}, 164 * {@link java.text.Normalizer.Form#NFKD} 165 * @return The normalized String 166 * @throws NullPointerException If <code>src</code> or <code>form</code> 167 * is null. 168 */ normalize(CharSequence src, Form form)169 public static String normalize(CharSequence src, Form form) { 170 return android.icu.text.Normalizer.normalize(src.toString(), form.icuMode); 171 } 172 173 /** 174 * Determines if the given sequence of char values is normalized. 175 * 176 * @param src The sequence of char values to be checked. 177 * @param form The normalization form; one of 178 * {@link java.text.Normalizer.Form#NFC}, 179 * {@link java.text.Normalizer.Form#NFD}, 180 * {@link java.text.Normalizer.Form#NFKC}, 181 * {@link java.text.Normalizer.Form#NFKD} 182 * @return true if the sequence of char values is normalized; 183 * false otherwise. 184 * @throws NullPointerException If <code>src</code> or <code>form</code> 185 * is null. 186 */ isNormalized(CharSequence src, Form form)187 public static boolean isNormalized(CharSequence src, Form form) { 188 return android.icu.text.Normalizer.isNormalized(src.toString(), form.icuMode, 0); 189 } 190 } 191