Home | History | Annotate | Download | only in stringprep
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 /*
      5  *******************************************************************************
      6  * Copyright (C) 2003-2010, International Business Machines Corporation and    *
      7  * others. All Rights Reserved.                                                *
      8  *******************************************************************************
      9 */
     10 package android.icu.dev.test.stringprep;
     11 
     12 import java.io.IOException;
     13 import java.io.InputStream;
     14 import java.io.UnsupportedEncodingException;
     15 import java.util.MissingResourceException;
     16 
     17 import android.icu.text.StringPrep;
     18 import android.icu.text.StringPrepParseException;
     19 import android.icu.text.UCharacterIterator;
     20 import android.icu.testsharding.MainTestShard;
     21 
     22 /**
     23  * @author ram
     24  *
     25  * This is a dumb implementation of NFS4 profiles. It is a direct port of
     26  * C code, does not use Object Oriented principles. Quick and Dirty implementation
     27  * for testing.
     28  */
     29 @MainTestShard
     30 public final class NFS4StringPrep {
     31 
     32     private StringPrep nfscss = null;
     33     private StringPrep nfscsi = null;
     34     private StringPrep nfscis = null;
     35     private StringPrep nfsmxp = null;
     36     private StringPrep nfsmxs = null;
     37     //singleton instance
     38     private static final NFS4StringPrep prep = new NFS4StringPrep();
     39 
     40 
     41     private  NFS4StringPrep (){
     42         ClassLoader loader = NFS4StringPrep.class.getClassLoader();
     43         try{
     44             InputStream  nfscsiFile = loader.getResourceAsStream("android/icu/dev/data/testdata/nfscsi.spp");
     45             nfscsi = new StringPrep(nfscsiFile);
     46             nfscsiFile.close();
     47 
     48             InputStream  nfscssFile = loader.getResourceAsStream("android/icu/dev/data/testdata/nfscss.spp");
     49             nfscss = new StringPrep(nfscssFile);
     50             nfscssFile.close();
     51 
     52             InputStream  nfscisFile = loader.getResourceAsStream("android/icu/dev/data/testdata/nfscis.spp");
     53             nfscis = new StringPrep(nfscisFile);
     54             nfscisFile.close();
     55 
     56             InputStream  nfsmxpFile = loader.getResourceAsStream("android/icu/dev/data/testdata/nfsmxp.spp");
     57             nfsmxp = new StringPrep(nfsmxpFile);
     58             nfsmxpFile.close();
     59 
     60             InputStream  nfsmxsFile = loader.getResourceAsStream("android/icu/dev/data/testdata/nfsmxs.spp");
     61             nfsmxs = new StringPrep(nfsmxsFile);
     62             nfsmxsFile.close();
     63         }catch(IOException e){
     64             throw new MissingResourceException(e.toString(),"","");
     65         }
     66     }
     67 
     68     private static byte[] prepare(byte[] src, StringPrep strprep)
     69                 throws StringPrepParseException, UnsupportedEncodingException{
     70         String s = new String(src, "UTF-8");
     71         UCharacterIterator iter =  UCharacterIterator.getInstance(s);
     72         StringBuffer out = strprep.prepare(iter,StringPrep.DEFAULT);
     73         return out.toString().getBytes("UTF-8");
     74     }
     75 
     76     public static byte[] cs_prepare(byte[] src, boolean isCaseSensitive)
     77                          throws StringPrepParseException, UnsupportedEncodingException{
     78         if(isCaseSensitive == true ){
     79             return prepare(src, prep.nfscss);
     80         }else{
     81             return prepare(src, prep.nfscsi);
     82         }
     83     }
     84 
     85     public static byte[] cis_prepare(byte[] src)
     86                          throws IOException, StringPrepParseException, UnsupportedEncodingException{
     87         return prepare(src, prep.nfscis);
     88     }
     89 
     90     /* sorted array for binary search*/
     91     private static final String[] special_prefixes={
     92         "ANONYMOUS",
     93         "AUTHENTICATED",
     94         "BATCH",
     95         "DIALUP",
     96         "EVERYONE",
     97         "GROUP",
     98         "INTERACTIVE",
     99         "NETWORK",
    100         "OWNER",
    101     };
    102 
    103 
    104     /* binary search the sorted array */
    105     private static final int findStringIndex(String[] sortedArr,String target){
    106 
    107         int left, middle, right,rc;
    108 
    109         left =0;
    110         right= sortedArr.length-1;
    111 
    112         while(left <= right){
    113             middle = (left+right)/2;
    114             rc= sortedArr[middle].compareTo(target);
    115 
    116             if(rc<0){
    117                 left = middle+1;
    118             }else if(rc >0){
    119                 right = middle -1;
    120             }else{
    121                 return middle;
    122             }
    123         }
    124         return -1;
    125     }
    126     private static final char AT_SIGN = '@';
    127 
    128     public static byte[] mixed_prepare(byte[] src)
    129                          throws IOException, StringPrepParseException, UnsupportedEncodingException{
    130         String s = new String(src, "UTF-8");
    131         int index = s.indexOf(AT_SIGN);
    132         StringBuffer out = new StringBuffer();
    133 
    134         if(index > -1){
    135             /* special prefixes must not be followed by suffixes! */
    136             String prefixString = s.substring(0,index);
    137             int i= findStringIndex(special_prefixes, prefixString);
    138             String suffixString = s.substring(index+1, s.length());
    139             if(i>-1 && !suffixString.equals("")){
    140                 throw new StringPrepParseException("Suffix following a special index", StringPrepParseException.INVALID_CHAR_FOUND);
    141             }
    142             UCharacterIterator prefix = UCharacterIterator.getInstance(prefixString);
    143             UCharacterIterator suffix = UCharacterIterator.getInstance(suffixString);
    144             out.append(prep.nfsmxp.prepare(prefix,StringPrep.DEFAULT));
    145             out.append(AT_SIGN); // add the delimiter
    146             out.append(prep.nfsmxs.prepare(suffix, StringPrep.DEFAULT));
    147         }else{
    148             UCharacterIterator iter = UCharacterIterator.getInstance(s);
    149             out.append(prep.nfsmxp.prepare(iter,StringPrep.DEFAULT));
    150 
    151         }
    152        return out.toString().getBytes("UTF-8");
    153     }
    154 
    155 }
    156