001: /*
002: *******************************************************************************
003: * Copyright (C) 2003-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.stringprep;
008:
009: import java.io.IOException;
010: import java.io.InputStream;
011: import java.io.UnsupportedEncodingException;
012: import java.util.MissingResourceException;
013:
014: import com.ibm.icu.impl.ICUData;
015: import com.ibm.icu.text.StringPrepParseException;
016: import com.ibm.icu.text.StringPrep;
017: import com.ibm.icu.text.UCharacterIterator;
018:
019: /**
020: * @author ram
021: *
022: * This is a dumb implementation of NFS4 profiles. It is a direct port of
023: * C code, does not use Object Oriented principles. Quick and Dirty implementation
024: * for testing.
025: */
026: public final class NFS4StringPrep {
027:
028: private StringPrep nfscss = null;
029: private StringPrep nfscsi = null;
030: private StringPrep nfscis = null;
031: private StringPrep nfsmxp = null;
032: private StringPrep nfsmxs = null;
033: //singleton instance
034: private static final NFS4StringPrep prep = new NFS4StringPrep();
035:
036: private NFS4StringPrep() {
037: ClassLoader loader = NFS4StringPrep.class.getClassLoader();
038: try {
039: InputStream nfscsiFile = loader
040: .getResourceAsStream("com/ibm/icu/dev/data/testdata/nfscsi.spp");
041: nfscsi = new StringPrep(nfscsiFile);
042: nfscsiFile.close();
043:
044: InputStream nfscssFile = loader
045: .getResourceAsStream("com/ibm/icu/dev/data/testdata/nfscss.spp");
046: nfscss = new StringPrep(nfscssFile);
047: nfscssFile.close();
048:
049: InputStream nfscisFile = loader
050: .getResourceAsStream("com/ibm/icu/dev/data/testdata/nfscis.spp");
051: nfscis = new StringPrep(nfscisFile);
052: nfscisFile.close();
053:
054: InputStream nfsmxpFile = loader
055: .getResourceAsStream("com/ibm/icu/dev/data/testdata/nfsmxp.spp");
056: nfsmxp = new StringPrep(nfsmxpFile);
057: nfsmxpFile.close();
058:
059: InputStream nfsmxsFile = loader
060: .getResourceAsStream("com/ibm/icu/dev/data/testdata/nfsmxs.spp");
061: nfsmxs = new StringPrep(nfsmxsFile);
062: nfsmxsFile.close();
063: } catch (IOException e) {
064: throw new MissingResourceException(e.toString(), "", "");
065: }
066: }
067:
068: private static byte[] prepare(byte[] src, StringPrep prep)
069: throws StringPrepParseException,
070: UnsupportedEncodingException {
071: String s = new String(src, "UTF-8");
072: UCharacterIterator iter = UCharacterIterator.getInstance(s);
073: StringBuffer out = prep.prepare(iter, StringPrep.DEFAULT);
074: return out.toString().getBytes("UTF-8");
075: }
076:
077: public static byte[] cs_prepare(byte[] src, boolean isCaseSensitive)
078: throws StringPrepParseException,
079: UnsupportedEncodingException {
080: if (isCaseSensitive == true) {
081: return prepare(src, prep.nfscss);
082: } else {
083: return prepare(src, prep.nfscsi);
084: }
085: }
086:
087: public static byte[] cis_prepare(byte[] src) throws IOException,
088: StringPrepParseException, UnsupportedEncodingException {
089: return prepare(src, prep.nfscis);
090: }
091:
092: /* sorted array for binary search*/
093: private static final String[] special_prefixes = { "ANONYMOUS",
094: "AUTHENTICATED", "BATCH", "DIALUP", "EVERYONE", "GROUP",
095: "INTERACTIVE", "NETWORK", "OWNER", };
096:
097: /* binary search the sorted array */
098: private static final int findStringIndex(String[] sortedArr,
099: String target) {
100:
101: int left, middle, right, rc;
102:
103: left = 0;
104: right = sortedArr.length - 1;
105:
106: while (left <= right) {
107: middle = (left + right) / 2;
108: rc = sortedArr[middle].compareTo(target);
109:
110: if (rc < 0) {
111: left = middle + 1;
112: } else if (rc > 0) {
113: right = middle - 1;
114: } else {
115: return middle;
116: }
117: }
118: return -1;
119: }
120:
121: private static final char AT_SIGN = '@';
122:
123: public static byte[] mixed_prepare(byte[] src) throws IOException,
124: StringPrepParseException, UnsupportedEncodingException {
125: String s = new String(src, "UTF-8");
126: int index = s.indexOf(AT_SIGN);
127: StringBuffer out = new StringBuffer();
128:
129: if (index > -1) {
130: /* special prefixes must not be followed by suffixes! */
131: String prefixString = s.substring(0, index);
132: int i = findStringIndex(special_prefixes, prefixString);
133: String suffixString = s.substring(index + 1, s.length());
134: if (i > -1 && !suffixString.equals("")) {
135: throw new StringPrepParseException(
136: "Suffix following a special index",
137: StringPrepParseException.INVALID_CHAR_FOUND);
138: }
139: UCharacterIterator prefix = UCharacterIterator
140: .getInstance(prefixString);
141: UCharacterIterator suffix = UCharacterIterator
142: .getInstance(suffixString);
143: out.append(prep.nfsmxp.prepare(prefix, StringPrep.DEFAULT));
144: out.append(AT_SIGN); // add the delimiter
145: out.append(prep.nfsmxs.prepare(suffix, StringPrep.DEFAULT));
146: } else {
147: UCharacterIterator iter = UCharacterIterator.getInstance(s);
148: out.append(prep.nfsmxp.prepare(iter, StringPrep.DEFAULT));
149:
150: }
151: return out.toString().getBytes("UTF-8");
152: }
153:
154: }
|