1 /*
2 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 /*
27 *******************************************************************************
28 * Copyright (C) 2009-2014, International Business Machines
29 * Corporation and others. All Rights Reserved.
30 *******************************************************************************
31 */
32
33 package sun.text.normalizer;
34
35 import java.io.IOException;
36
37 final class Norm2AllModes {
38 // Public API dispatch via Normalizer2 subclasses -------------------------- ***
39
40 // Normalizer2 implementation for the old UNORM_NONE.
41 public static final class NoopNormalizer2 extends Normalizer2 {
42 @Override
43 public StringBuilder normalize(CharSequence src, StringBuilder dest) {
44 if(dest!=src) {
45 dest.setLength(0);
46 return dest.append(src);
47 } else {
48 throw new IllegalArgumentException();
49 }
50 }
51
52 @Override
53 public Appendable normalize(CharSequence src, Appendable dest) {
54 if(dest!=src) {
55 try {
56 return dest.append(src);
57 } catch(IOException e) {
248 private static Norm2AllModes getInstanceFromSingleton(Norm2AllModesSingleton singleton) {
249 if(singleton.exception!=null) {
250 throw singleton.exception;
251 }
252 return singleton.allModes;
253 }
254
255 public static Norm2AllModes getNFCInstance() {
256 return getInstanceFromSingleton(NFCSingleton.INSTANCE);
257 }
258
259 public static Norm2AllModes getNFKCInstance() {
260 return getInstanceFromSingleton(NFKCSingleton.INSTANCE);
261 }
262
263 public static final NoopNormalizer2 NOOP_NORMALIZER2=new NoopNormalizer2();
264
265 private static final class Norm2AllModesSingleton {
266 private Norm2AllModesSingleton(String name) {
267 try {
268 String DATA_FILE_NAME = "/sun/text/resources/" + name + ".nrm";
269 NormalizerImpl impl=new NormalizerImpl().load(DATA_FILE_NAME);
270 allModes=new Norm2AllModes(impl);
271 } catch (RuntimeException e) {
272 exception=e;
273 }
274 }
275
276 private Norm2AllModes allModes;
277 private RuntimeException exception;
278 }
279
280 private static final class NFCSingleton {
281 private static final Norm2AllModesSingleton INSTANCE=new Norm2AllModesSingleton("nfc");
282 }
283
284 private static final class NFKCSingleton {
285 private static final Norm2AllModesSingleton INSTANCE=new Norm2AllModesSingleton("nfkc");
286 }
287 }
|
1 /*
2 * Copyright (c) 2015, 2020, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 /*
27 *******************************************************************************
28 * Copyright (C) 2009-2014, International Business Machines
29 * Corporation and others. All Rights Reserved.
30 *******************************************************************************
31 */
32
33 package jdk.internal.icu.impl;
34
35 import java.io.IOException;
36
37 import jdk.internal.icu.text.Normalizer2;
38 import jdk.internal.icu.util.VersionInfo;
39
40 public final class Norm2AllModes {
41 // Public API dispatch via Normalizer2 subclasses -------------------------- ***
42
43 // Normalizer2 implementation for the old UNORM_NONE.
44 public static final class NoopNormalizer2 extends Normalizer2 {
45 @Override
46 public StringBuilder normalize(CharSequence src, StringBuilder dest) {
47 if(dest!=src) {
48 dest.setLength(0);
49 return dest.append(src);
50 } else {
51 throw new IllegalArgumentException();
52 }
53 }
54
55 @Override
56 public Appendable normalize(CharSequence src, Appendable dest) {
57 if(dest!=src) {
58 try {
59 return dest.append(src);
60 } catch(IOException e) {
251 private static Norm2AllModes getInstanceFromSingleton(Norm2AllModesSingleton singleton) {
252 if(singleton.exception!=null) {
253 throw singleton.exception;
254 }
255 return singleton.allModes;
256 }
257
258 public static Norm2AllModes getNFCInstance() {
259 return getInstanceFromSingleton(NFCSingleton.INSTANCE);
260 }
261
262 public static Norm2AllModes getNFKCInstance() {
263 return getInstanceFromSingleton(NFKCSingleton.INSTANCE);
264 }
265
266 public static final NoopNormalizer2 NOOP_NORMALIZER2=new NoopNormalizer2();
267
268 private static final class Norm2AllModesSingleton {
269 private Norm2AllModesSingleton(String name) {
270 try {
271 @SuppressWarnings("deprecation")
272 String DATA_FILE_NAME = "/jdk/internal/icu/impl/data/icudt" +
273 VersionInfo.ICU_DATA_VERSION_PATH + "/" + name + ".nrm";
274 NormalizerImpl impl=new NormalizerImpl().load(DATA_FILE_NAME);
275 allModes=new Norm2AllModes(impl);
276 } catch (RuntimeException e) {
277 exception=e;
278 }
279 }
280
281 private Norm2AllModes allModes;
282 private RuntimeException exception;
283 }
284
285 private static final class NFCSingleton {
286 private static final Norm2AllModesSingleton INSTANCE=new Norm2AllModesSingleton("nfc");
287 }
288
289 private static final class NFKCSingleton {
290 private static final Norm2AllModesSingleton INSTANCE=new Norm2AllModesSingleton("nfkc");
291 }
292 }
|