From 26009b31ae9019e67c7e61ff4fafd3bb9e29a3cc Mon Sep 17 00:00:00 2001 From: Yoshito Umaoka Date: Mon, 2 Feb 2009 17:03:23 +0000 Subject: [PATCH] ICU-6238 Support standard stringprep profiles in ICU4J. Merging changes from the work branch to the trunk. X-SVN-Rev: 25358 --- icu4j/src/com/ibm/icu/dev/data/testdata.jar | 4 +- .../ibm/icu/dev/test/stringprep/TestIDNA.java | 12 +- .../icu/dev/test/util/DebugUtilitiesData.java | 2 +- icu4j/src/com/ibm/icu/impl/data/icudata.jar | 4 +- icu4j/src/com/ibm/icu/text/IDNA.java | 21 +- icu4j/src/com/ibm/icu/text/StringPrep.java | 221 +++++++++++++++++- 6 files changed, 227 insertions(+), 37 deletions(-) diff --git a/icu4j/src/com/ibm/icu/dev/data/testdata.jar b/icu4j/src/com/ibm/icu/dev/data/testdata.jar index 81b9923744b..b0054f8891a 100755 --- a/icu4j/src/com/ibm/icu/dev/data/testdata.jar +++ b/icu4j/src/com/ibm/icu/dev/data/testdata.jar @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b74afbda7f9c8f15b595f8c58a5f35dabfe22057d0ba10daf8a62657834f66a9 -size 767202 +oid sha256:56a5b21ddeedead9564ef4adcc69d949fd798c859a7c8f4fa4874d276b9e9148 +size 767198 diff --git a/icu4j/src/com/ibm/icu/dev/test/stringprep/TestIDNA.java b/icu4j/src/com/ibm/icu/dev/test/stringprep/TestIDNA.java index 0a0a89074b0..a81063126a3 100644 --- a/icu4j/src/com/ibm/icu/dev/test/stringprep/TestIDNA.java +++ b/icu4j/src/com/ibm/icu/dev/test/stringprep/TestIDNA.java @@ -1,23 +1,20 @@ /* ******************************************************************************* - * Copyright (C) 2003-2008, International Business Machines Corporation and * + * Copyright (C) 2003-2009, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ package com.ibm.icu.dev.test.stringprep; -import java.io.InputStream; import java.util.Random; import com.ibm.icu.dev.test.TestFmwk; +import com.ibm.icu.impl.Utility; import com.ibm.icu.text.IDNA; -import com.ibm.icu.text.StringPrepParseException; import com.ibm.icu.text.StringPrep; +import com.ibm.icu.text.StringPrepParseException; import com.ibm.icu.text.UCharacterIterator; import com.ibm.icu.text.UTF16; -import com.ibm.icu.impl.ICUData; -import com.ibm.icu.impl.ICUResourceBundle; -import com.ibm.icu.impl.Utility; /** * @author ram @@ -289,8 +286,7 @@ public class TestIDNA extends TestFmwk { } } public void TestNamePrepConformance() throws Exception{ - InputStream stream = ICUData.getRequiredStream(ICUResourceBundle.ICU_BUNDLE+"/uidna.spp"); - StringPrep namePrep = new StringPrep(stream); + StringPrep namePrep = StringPrep.getInstance(StringPrep.RFC3491_NAMEPREP); for(int i=0; i MAX_PROFILE) { + throw new IllegalArgumentException("Bad profile type"); + } + + StringPrep instance = null; + + // A StringPrep instance is immutable. We use a single instance + // per type and store it in the internal cache. + synchronized (CACHE) { + WeakReference ref = CACHE[profile]; + if (ref != null) { + instance = (StringPrep)ref.get(); + } + + if (instance == null) { + InputStream stream = ICUData.getRequiredStream(ICUResourceBundle.ICU_BUNDLE + "/" + + PROFILE_NAMES[profile] + ".spp"); + if (stream != null) { + try { + try { + instance = new StringPrep(stream); + } finally { + stream.close(); + } + } catch (IOException e) { + throw new RuntimeException(e.toString()); + } + } + if (instance != null) { + CACHE[profile] = new WeakReference(instance); + } + } + } + return instance; + } + private static final class Values{ boolean isIndex; int value; @@ -357,7 +538,7 @@ public final class StringPrep { */ /** * Prepare the input buffer for use in applications with the given profile. This operation maps, normalizes(NFKC), - * checks for prohited and BiDi characters in the order defined by RFC 3454 + * checks for prohibited and BiDi characters in the order defined by RFC 3454 * depending on the options specified in the profile. * * @param src A UCharacterIterator object containing the source string @@ -369,7 +550,7 @@ public final class StringPrep { * as normal Unicode code points. * * @return StringBuffer A StringBuffer containing the output - * @throws ParseException + * @throws StringPrepParseException * @stable ICU 2.8 */ public StringBuffer prepare(UCharacterIterator src, int options) @@ -438,4 +619,28 @@ public final class StringPrep { return normOut; } + + /** + * Prepare the input String for use in applications with the given profile. This operation maps, normalizes(NFKC), + * checks for prohibited and BiDi characters in the order defined by RFC 3454 + * depending on the options specified in the profile. + * + * @param src A string + * @param options A bit set of options: + * + * - StringPrep.NONE Prohibit processing of unassigned code points in the input + * + * - StringPrep.ALLOW_UNASSIGNED Treat the unassigned code points are in the input + * as normal Unicode code points. + * + * @return String A String containing the output + * @throws StringPrepParseException + * @draft ICU 4.2 + * @provisional This API might change or be removed in a future release. + */ + public String prepare(String src, int options) + throws StringPrepParseException{ + StringBuffer result = prepare(UCharacterIterator.getInstance(src), options); + return result.toString(); + } }