summaryrefslogtreecommitdiffstats
path: root/text/src
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2010-03-01 16:28:05 -0800
committerElliott Hughes <enh@google.com>2010-03-01 16:30:39 -0800
commita656b24ce0513b5f6115c40e6bb9181c5cea93d6 (patch)
treeb8a7f302a5d2fcbc633978eb360a2567c6b7d4b3 /text/src
parent63c166c5b299f008ee39378dc008de21af70d85a (diff)
downloadlibcore-a656b24ce0513b5f6115c40e6bb9181c5cea93d6.zip
libcore-a656b24ce0513b5f6115c40e6bb9181c5cea93d6.tar.gz
libcore-a656b24ce0513b5f6115c40e6bb9181c5cea93d6.tar.bz2
Implement (but @hide) java.text.Normalizer from Java 6.
Based on https://android-git.corp.google.com/g/42516. Includes the harmony tests from their Java 6 branch. Bug: 719001
Diffstat (limited to 'text/src')
-rw-r--r--text/src/main/java/java/text/Normalizer.java81
-rw-r--r--text/src/test/java/org/apache/harmony/text/tests/java/text/AllTests.java1
-rw-r--r--text/src/test/java/org/apache/harmony/text/tests/java/text/NormalizerTest.java183
3 files changed, 265 insertions, 0 deletions
diff --git a/text/src/main/java/java/text/Normalizer.java b/text/src/main/java/java/text/Normalizer.java
new file mode 100644
index 0000000..c395715
--- /dev/null
+++ b/text/src/main/java/java/text/Normalizer.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.text;
+
+import com.ibm.icu4jni.text.NativeNormalizer;
+
+/**
+ * Provides normalization functions according to
+ * <a href="http://www.unicode.org/unicode/reports/tr15/tr15-23.html">Unicode Standard Annex #15:
+ * Unicode Normalization Forms</a>. Normalization can decompose and compose
+ * characters for equivalency checking.
+ *
+ * @hide
+ * @since 1.6
+ */
+public final class Normalizer {
+ /**
+ * The normalization forms supported by the Normalizer. These are specified in
+ * <a href="http://www.unicode.org/unicode/reports/tr15/tr15-23.html">Unicode Standard
+ * Annex #15</a>.
+ */
+ public static enum Form {
+ /**
+ * Normalization Form D - Canonical Decomposition.
+ */
+ NFD,
+
+ /**
+ * Normalization Form C - Canonical Decomposition, followed by Canonical Composition.
+ */
+ NFC,
+
+ /**
+ * Normalization Form KD - Compatibility Decomposition.
+ */
+ NFKD,
+
+ /**
+ * Normalization Form KC - Compatibility Decomposition, followed by Canonical Composition.
+ */
+ NFKC;
+ }
+
+ /**
+ * Check whether the given character sequence <code>src</code> is normalized
+ * according to the normalization method <code>form</code>.
+ *
+ * @param src character sequence to check
+ * @param form normalization form to check against
+ * @return true if normalized according to <code>form</code>
+ */
+ public static boolean isNormalized(CharSequence src, Form form) {
+ return NativeNormalizer.isNormalized(src, form);
+ }
+
+ /**
+ * Normalize the character sequence <code>src</code> according to the
+ * normalization method <code>form</code>.
+ *
+ * @param src character sequence to read for normalization
+ * @param form normalization form
+ * @return string normalized according to <code>form</code>
+ */
+ public static String normalize(CharSequence src, Form form) {
+ return NativeNormalizer.normalize(src, form);
+ }
+}
diff --git a/text/src/test/java/org/apache/harmony/text/tests/java/text/AllTests.java b/text/src/test/java/org/apache/harmony/text/tests/java/text/AllTests.java
index c2cfbb2..c968b38 100644
--- a/text/src/test/java/org/apache/harmony/text/tests/java/text/AllTests.java
+++ b/text/src/test/java/org/apache/harmony/text/tests/java/text/AllTests.java
@@ -51,6 +51,7 @@ public class AllTests {
suite.addTestSuite(FormatTest.class);
suite.addTestSuite(MessageFormatFieldTest.class);
suite.addTestSuite(MessageFormatTest.class);
+ suite.addTestSuite(NormalizerTest.class);
suite.addTestSuite(NumberFormatFieldTest.class);
suite.addTestSuite(NumberFormatTest.class);
suite.addTestSuite(ParseExceptionTest.class);
diff --git a/text/src/test/java/org/apache/harmony/text/tests/java/text/NormalizerTest.java b/text/src/test/java/org/apache/harmony/text/tests/java/text/NormalizerTest.java
new file mode 100644
index 0000000..f877ae2
--- /dev/null
+++ b/text/src/test/java/org/apache/harmony/text/tests/java/text/NormalizerTest.java
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.text.tests.java.text;
+
+import java.text.Normalizer;
+import java.text.Normalizer.Form;
+
+import junit.framework.TestCase;
+
+public class NormalizerTest extends TestCase {
+ /**
+ * @tests java.text.Normalizer.Form#values()
+ */
+ public void test_form_values() throws Exception {
+ Form[] forms = Form.values();
+ assertEquals(4, forms.length);
+ assertEquals(Form.NFD, forms[0]);
+ assertEquals(Form.NFC, forms[1]);
+ assertEquals(Form.NFKD, forms[2]);
+ assertEquals(Form.NFKC, forms[3]);
+ }
+
+ /**
+ * @tests java.text.Normalizer.Form#valueOf(String)
+ */
+ public void test_form_valueOfLjava_lang_String() {
+ try {
+ Form.valueOf(null);
+ fail("Should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ assertEquals(Form.NFC, Form.valueOf("NFC"));
+ assertEquals(Form.NFD, Form.valueOf("NFD"));
+ assertEquals(Form.NFKC, Form.valueOf("NFKC"));
+ assertEquals(Form.NFKD, Form.valueOf("NFKD"));
+
+ try {
+ Form.valueOf("not exist");
+ fail("Should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ try {
+ Form.valueOf("nfc");
+ fail("Should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ try {
+ Form.valueOf("NFC ");
+ fail("Should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests java.text.Normalizer#isNormalized(CharSequence, Form)
+ */
+ public void test_isNormalized() throws Exception {
+ String src = "\u00c1";
+ assertTrue(Normalizer.isNormalized(src, Form.NFC));
+ assertFalse(Normalizer.isNormalized(src, Form.NFD));
+ assertTrue(Normalizer.isNormalized(src, Form.NFKC));
+ assertFalse(Normalizer.isNormalized(src, Form.NFKD));
+
+ src = "\u0041\u0301";
+ assertFalse(Normalizer.isNormalized(src, Form.NFC));
+ assertTrue(Normalizer.isNormalized(src, Form.NFD));
+ assertFalse(Normalizer.isNormalized(src, Form.NFKC));
+ assertTrue(Normalizer.isNormalized(src, Form.NFKD));
+
+ src = "\ufb03";
+ assertTrue(Normalizer.isNormalized(src, Form.NFC));
+ assertTrue(Normalizer.isNormalized(src, Form.NFD));
+ assertFalse(Normalizer.isNormalized(src, Form.NFKC));
+ assertFalse(Normalizer.isNormalized(src, Form.NFKD));
+
+ src = "\u0066\u0066\u0069";
+ assertTrue(Normalizer.isNormalized(src, Form.NFC));
+ assertTrue(Normalizer.isNormalized(src, Form.NFD));
+ assertTrue(Normalizer.isNormalized(src, Form.NFKC));
+ assertTrue(Normalizer.isNormalized(src, Form.NFKD));
+
+ src = "";
+ assertTrue(Normalizer.isNormalized(src, Form.NFC));
+ assertTrue(Normalizer.isNormalized(src, Form.NFD));
+ assertTrue(Normalizer.isNormalized(src, Form.NFKC));
+ assertTrue(Normalizer.isNormalized(src, Form.NFKD));
+ }
+
+ /**
+ * @tests java.text.Normalizer#isNormalized(CharSequence, Form)
+ */
+ public void test_isNormalized_exception() throws Exception {
+ try {
+ Normalizer.isNormalized(null, Form.NFC);
+ fail("Should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ try {
+ Normalizer.isNormalized("chars", null);
+ fail("Should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests java.text.Normalizer#normalize(CharSequence, Form)
+ */
+ public void test_normalize() throws Exception {
+ String src = "\u00c1";
+ assertEquals("\u00c1", Normalizer.normalize(src, Form.NFC));
+ assertEquals("\u0041\u0301", Normalizer.normalize(src, Form.NFD));
+ assertEquals("\u00c1", Normalizer.normalize(src, Form.NFKC));
+ assertEquals("\u0041\u0301", Normalizer.normalize(src, Form.NFKD));
+
+ src = "\u0041\u0301";
+ assertEquals("\u00c1", Normalizer.normalize(src, Form.NFC));
+ assertEquals("\u0041\u0301", Normalizer.normalize(src, Form.NFD));
+ assertEquals("\u00c1", Normalizer.normalize(src, Form.NFKC));
+ assertEquals("\u0041\u0301", Normalizer.normalize(src, Form.NFKD));
+
+ src = "\ufb03";
+ assertEquals("\ufb03", Normalizer.normalize(src, Form.NFC));
+ assertEquals("\ufb03", Normalizer.normalize(src, Form.NFD));
+ assertEquals("\u0066\u0066\u0069", Normalizer.normalize(src, Form.NFKC));
+ assertEquals("\u0066\u0066\u0069", Normalizer.normalize(src, Form.NFKD));
+
+ src = "\u0066\u0066\u0069";
+ assertEquals("\u0066\u0066\u0069", Normalizer.normalize(src, Form.NFC));
+ assertEquals("\u0066\u0066\u0069", Normalizer.normalize(src, Form.NFD));
+ assertEquals("\u0066\u0066\u0069", Normalizer.normalize(src, Form.NFKC));
+ assertEquals("\u0066\u0066\u0069", Normalizer.normalize(src, Form.NFKD));
+
+ src = "";
+ assertEquals("", Normalizer.normalize(src, Form.NFC));
+ assertEquals("", Normalizer.normalize(src, Form.NFD));
+ assertEquals("", Normalizer.normalize(src, Form.NFKC));
+ assertEquals("", Normalizer.normalize(src, Form.NFKD));
+ }
+
+ /**
+ * @tests java.text.Normalizer#normalize(CharSequence, Form)
+ */
+ public void test_normalize_exception() throws Exception {
+ try {
+ Normalizer.normalize(null, Form.NFC);
+ fail("Should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ try {
+ Normalizer.normalize("chars", null);
+ fail("Should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+ }
+}