From db3dc35976d0f89bbe0330d4d2505f72ffd33c84 Mon Sep 17 00:00:00 2001 From: Ram Viswanadha Date: Thu, 28 Aug 2003 23:03:47 +0000 Subject: [PATCH] ICU-3064 code review changes X-SVN-Rev: 12977 --- .../dev/test/stringprep/NFS4StringPrep.java | 49 ++++------ .../ibm/icu/dev/test/stringprep/TestAll.java | 17 ++-- .../ibm/icu/dev/test/stringprep/TestIDNA.java | 6 +- .../dev/test/stringprep/TestStringPrep.java | 18 +++- icu4j/src/com/ibm/icu/text/IDNA.java | 17 ++-- icu4j/src/com/ibm/icu/text/StringPrep.java | 96 ++++++++++--------- 6 files changed, 106 insertions(+), 97 deletions(-) diff --git a/icu4j/src/com/ibm/icu/dev/test/stringprep/NFS4StringPrep.java b/icu4j/src/com/ibm/icu/dev/test/stringprep/NFS4StringPrep.java index 7c2e39b9a9..89ee6f88f0 100644 --- a/icu4j/src/com/ibm/icu/dev/test/stringprep/NFS4StringPrep.java +++ b/icu4j/src/com/ibm/icu/dev/test/stringprep/NFS4StringPrep.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/stringprep/NFS4StringPrep.java,v $ - * $Date: 2003/08/27 21:19:01 $ - * $Revision: 1.5 $ + * $Date: 2003/08/28 23:03:06 $ + * $Revision: 1.6 $ * ******************************************************************************* */ @@ -29,13 +29,7 @@ import com.ibm.icu.text.UCharacterIterator; * for testing. */ public final class NFS4StringPrep { - private static final String[] NFS4DataFileNames ={ - "nfscss.spp", - "nfscsi.spp", - "nfscis.spp", - "nfsmxp.spp", - "nfsmxs.spp" - }; + private StringPrep nfscss = null; private StringPrep nfscsi = null; private StringPrep nfscis = null; @@ -47,25 +41,24 @@ public final class NFS4StringPrep { private NFS4StringPrep (){ try{ - - InputStream nfscssFile = TestUtil.getDataStream(NFS4DataFileNames[0]); - nfscss = StringPrep.getInstance(nfscssFile); + InputStream nfscsiFile = TestUtil.getDataStream("nfscsi.spp"); + nfscsi = new StringPrep(nfscsiFile); + nfscsiFile.close(); + + InputStream nfscssFile = TestUtil.getDataStream("nfscss.spp"); + nfscss = new StringPrep(nfscssFile); nfscssFile.close(); - InputStream nfscsiFile = TestUtil.getDataStream(NFS4DataFileNames[1]); - nfscsi = StringPrep.getInstance(nfscsiFile); - nfscsiFile.close(); + InputStream nfscisFile = TestUtil.getDataStream("nfscis.spp"); + nfscis = new StringPrep(nfscisFile); + nfscisFile.close(); - InputStream nfscisFile = TestUtil.getDataStream(NFS4DataFileNames[2]); - nfscis = StringPrep.getInstance(nfscisFile); - nfscsiFile.close(); + InputStream nfsmxpFile = TestUtil.getDataStream("nfsmxp.spp"); + nfsmxp = new StringPrep(nfsmxpFile); + nfsmxpFile.close(); - InputStream nfsmxpFile = TestUtil.getDataStream(NFS4DataFileNames[3]); - nfsmxp = StringPrep.getInstance(nfsmxpFile); - nfscsiFile.close(); - - InputStream nfsmxsFile = TestUtil.getDataStream(NFS4DataFileNames[4]); - nfsmxs = StringPrep.getInstance(nfsmxsFile); + InputStream nfsmxsFile = TestUtil.getDataStream("nfsmxs.spp"); + nfsmxs = new StringPrep(nfsmxsFile); nfsmxsFile.close(); }catch(IOException e){ throw new RuntimeException(e.toString()); @@ -83,12 +76,12 @@ public final class NFS4StringPrep { return out.toString().getBytes("UTF-8"); } - public static byte[] cs_prepare(byte[] src, boolean caseInsensitive) + public static byte[] cs_prepare(byte[] src, boolean isCaseSensitive) throws StringPrepParseException, UnsupportedEncodingException{ - if(caseInsensitive){ - return prepare(src, prep.nfscsi); - }else{ + if(isCaseSensitive == true ){ return prepare(src, prep.nfscss); + }else{ + return prepare(src, prep.nfscsi); } } diff --git a/icu4j/src/com/ibm/icu/dev/test/stringprep/TestAll.java b/icu4j/src/com/ibm/icu/dev/test/stringprep/TestAll.java index b40d346933..39962d04db 100644 --- a/icu4j/src/com/ibm/icu/dev/test/stringprep/TestAll.java +++ b/icu4j/src/com/ibm/icu/dev/test/stringprep/TestAll.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/stringprep/TestAll.java,v $ - * $Date: 2003/08/21 23:42:25 $ - * $Revision: 1.1 $ + * $Date: 2003/08/28 23:03:06 $ + * $Revision: 1.2 $ * ******************************************************************************* */ @@ -28,12 +28,13 @@ public class TestAll extends TestGroup { public TestAll() { super( - new String[] { - "TestIDNA", - "TestStringPrep", - "TestIDNARef" - }, - "StringPrep and IDNA test"); + new String[] { + "TestIDNA", + "TestStringPrep", + "TestIDNARef" + }, + "StringPrep and IDNA test" + ); } public static final String CLASS_TARGET_NAME = "StringPrep"; 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 302c50dc3f..706a756f26 100644 --- a/icu4j/src/com/ibm/icu/dev/test/stringprep/TestIDNA.java +++ b/icu4j/src/com/ibm/icu/dev/test/stringprep/TestIDNA.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/stringprep/TestIDNA.java,v $ - * $Date: 2003/08/27 21:19:00 $ - * $Revision: 1.3 $ + * $Date: 2003/08/28 23:03:06 $ + * $Revision: 1.4 $ * ******************************************************************************* */ @@ -295,7 +295,7 @@ public class TestIDNA extends TestFmwk { } public void TestNamePrepConformance() throws Exception{ InputStream stream = LocaleUtility.getImplDataResourceAsStream("uidna.spp"); - StringPrep namePrep = StringPrep.getInstance(stream); + StringPrep namePrep = new StringPrep(stream); for(int i=0; iIDNA RFC. * The draft defines 2 operations: ToASCII and ToUnicode. Domain labels * containing non-ASCII code points are required to be processed by * ToASCII operation before passing it to resolver libraries. Domain names * that are obtained from resolver libraries are required to be processed by * ToUnicode operation before displaying the domain name to the user. - * IDNA requires that implementations process input strings with Nameprep - * (http://www.ietf.org/rfc/rfc3491.txt), - * which is a profile of Stringprep (http://www.ietf.org/rfc/rfc3454.txt), - * and then with Punycode (http://www.ietf.org/rfc/rfc3492.txt). + * IDNA requires that implementations process input strings with + * Nameprep, + * which is a profile of Stringprep , + * and then with Punycode. * Implementations of IDNA MUST fully implement Nameprep and Punycode; * neither Nameprep nor Punycode are optional. * The input and output of ToASCII and ToUnicode operations are Unicode @@ -89,7 +88,7 @@ public final class IDNA { private IDNA(){ try{ InputStream stream = LocaleUtility.getImplDataResourceAsStream("uidna.spp"); - namePrep = StringPrep.getInstance(stream); + namePrep = new StringPrep(stream); stream.close(); }catch (IOException e){ throw new RuntimeException(e.toString()); diff --git a/icu4j/src/com/ibm/icu/text/StringPrep.java b/icu4j/src/com/ibm/icu/text/StringPrep.java index ff3966a6cd..4d66bb3f7e 100644 --- a/icu4j/src/com/ibm/icu/text/StringPrep.java +++ b/icu4j/src/com/ibm/icu/text/StringPrep.java @@ -4,8 +4,8 @@ * others. All Rights Reserved. * ******************************************************************************* * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/StringPrep.java,v $ - * $Date: 2003/08/27 21:12:04 $ - * $Revision: 1.1 $ + * $Date: 2003/08/28 23:03:47 $ + * $Revision: 1.2 $ * ***************************************************************************************** */ @@ -24,7 +24,8 @@ import com.ibm.icu.lang.UCharacter; import com.ibm.icu.lang.UCharacterDirection; /** - * StringPrep API implements the StingPrep framework as described by RFC 3454. + * StringPrep API implements the StingPrep framework as described by + * RFC 3454. * StringPrep prepares Unicode strings for use in network protocols. * Profiles of StingPrep are set of rules and data according to which the * Unicode Strings are prepared. Each profiles contains tables which describe @@ -104,7 +105,7 @@ public final class StringPrep { /* Wrappers for Trie implementations */ private static final class StringPrepTrieImpl implements Trie.DataManipulate{ - static CharTrie sprepTrie = null; + private CharTrie sprepTrie = null; /** * Called by com.ibm.icu.util.Trie to extract from a lead surrogate's * data the index array offset of the indexes for that lead surrogate. @@ -117,23 +118,30 @@ public final class StringPrep { } } - private static StringPrepTrieImpl sprepTrieImpl; - private static int[] indexes; - private static char[] mappingData; - private static byte[] formatVersion; + // CharTrie implmentation for reading the trie data + private StringPrepTrieImpl sprepTrieImpl; + // Indexes read from the data file + private int[] indexes; + // mapping data read from the data file + private char[] mappingData; + // format version of the data file + private byte[] formatVersion; + // the version of Unicode supported by the data file + private VersionInfo unicodeVersion; + // the Unicode version of last entry in the + // NormalizationCorrections.txt file if normalization + // is turned on + private VersionInfo normVersion; + // Option to turn on Normalization + private boolean doNFKC; + // Option to turn on checking for BiDi rules + private boolean checkBiDi; + private char getCodePointValue(int ch){ - return StringPrepTrieImpl.sprepTrie.getCodePointValue(ch); + return sprepTrieImpl.sprepTrie.getCodePointValue(ch); } - - //protected - private boolean doNFKC = false; - private boolean checkBiDi = false; - - private VersionInfo unicodeVersion; - private VersionInfo normVersion; - - + private static VersionInfo getVersionInfo(int comp){ int micro = comp & 0xFF; int milli =(comp >> 8) & 0xFF; @@ -147,8 +155,17 @@ public final class StringPrep { } return VersionInfo.getInstance((int)version[0],(int) version[1],(int) version[2],(int) version[3]); } - - private StringPrep(InputStream inputStream) throws IOException{ + /** + * Creates an StringPrep object after reading the input stream. + * The object does not hold a reference to the input steam, so the stream can be + * closed after the method returns. + * + * @param inputStream The stream for reading the StringPrep profile binary + * @return StringPrep object created from the input stream + * @throws IOException + * @draft ICU 2.8 + */ + public StringPrep(InputStream inputStream) throws IOException{ BufferedInputStream b = new BufferedInputStream(inputStream,DATA_BUFFER_SIZE); @@ -159,14 +176,15 @@ public final class StringPrep { byte[] sprepBytes = new byte[indexes[INDEX_TRIE_SIZE]]; - sprepTrieImpl = new StringPrepTrieImpl(); + //indexes[INDEX_MAPPING_DATA_SIZE] store the size of mappingData in bytes mappingData = new char[indexes[INDEX_MAPPING_DATA_SIZE]/2]; // load the rest of the data data and initialize the data members reader.read(sprepBytes,mappingData); - StringPrepTrieImpl.sprepTrie = new CharTrie( new ByteArrayInputStream(sprepBytes),sprepTrieImpl ); - + sprepTrieImpl = new StringPrepTrieImpl(); + sprepTrieImpl.sprepTrie = new CharTrie( new ByteArrayInputStream(sprepBytes),sprepTrieImpl ); + // get the data format version formatVersion = reader.getDataFormatVersion(); @@ -180,38 +198,24 @@ public final class StringPrep { } b.close(); } - /** - * Returns the StringPrep instance created after reading the input stream. - * The object does not hold a reference to the input steam, so the stream can be - * closed after the method returns. - * - * @param inputStream The stream for reading the StringPrep profile binary - * @return StringPrep object created from the input stream - * @throws IOException - * @draft ICU 2.8 - */ - public static final StringPrep getInstance(InputStream inputStream) - throws IOException{ - - StringPrep prep = null; - // load the file and create the object - prep = new StringPrep(inputStream); - - return prep; - } - - private class Values{ + + private static final class Values{ boolean isIndex; int value; int type; + public void reset(){ + isIndex = false; + value = 0; + type = -1; + } } private static final void getValues(char trieWord,Values values){ - + values.reset(); if(trieWord == 0){ /* * Initial value stored in the mapping table - * just return USPREP_TYPE_LIMIT .. so that + * just return TYPE_LIMIT .. so that * the source codepoint is copied to the destination */ values.type = TYPE_LIMIT;