jdk/test/sun/net/idn/NFS4StringPrep.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 31680 88c53c2293b4
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *******************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * Copyright (C) 2003-2004, International Business Machines Corporation and    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * others. All Rights Reserved.                                                *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *******************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.text.ParseException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.text.normalizer.ICUData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.net.idn.StringPrep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.text.normalizer.UCharacterIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author ram
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This is a dumb implementation of NFS4 profiles. It is a direct port of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * C code, does not use Object Oriented principles. Quick and Dirty implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * for testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public final class NFS4StringPrep {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private StringPrep nfscss = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private StringPrep nfscsi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private StringPrep nfscis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private StringPrep nfsmxp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private StringPrep nfsmxs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    //singleton instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final NFS4StringPrep prep = new NFS4StringPrep();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private  NFS4StringPrep (){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        ClassLoader loader = NFS4StringPrep.class.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
      try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
          InputStream  nfscsiFile = loader.getResourceAsStream("nfscsi.spp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
          nfscsi = new StringPrep(nfscsiFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
          nfscsiFile.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
          InputStream  nfscssFile = loader.getResourceAsStream("nfscss.spp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
          nfscss = new StringPrep(nfscssFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
          nfscssFile.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
          InputStream  nfscisFile = loader.getResourceAsStream("nfscis.spp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
          nfscis = new StringPrep(nfscisFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
          nfscisFile.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
          InputStream  nfsmxpFile = loader.getResourceAsStream("nfsmxp.spp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
          nfsmxp = new StringPrep(nfsmxpFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
          nfsmxpFile.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
          InputStream  nfsmxsFile = loader.getResourceAsStream("nfsmxs.spp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
          nfsmxs = new StringPrep(nfsmxsFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
          nfsmxsFile.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
      }catch(IOException e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
          throw new RuntimeException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static byte[] prepare(byte[] src, StringPrep prep)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                throws ParseException, UnsupportedEncodingException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        String s = new String(src, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        UCharacterIterator iter =  UCharacterIterator.getInstance(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        StringBuffer out = prep.prepare(iter,StringPrep.DEFAULT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return out.toString().getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static byte[] cs_prepare(byte[] src, boolean isCaseSensitive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                         throws ParseException, UnsupportedEncodingException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if(isCaseSensitive == true ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return prepare(src, prep.nfscss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return prepare(src, prep.nfscsi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public static byte[] cis_prepare(byte[] src)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                         throws IOException, ParseException, UnsupportedEncodingException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return prepare(src, prep.nfscis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /* sorted array for binary search*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static final String[] special_prefixes={
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        "ANONYMOUS",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        "AUTHENTICATED",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        "BATCH",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        "DIALUP",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        "EVERYONE",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        "GROUP",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        "INTERACTIVE",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        "NETWORK",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        "OWNER",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /* binary search the sorted array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static final int findStringIndex(String[] sortedArr,String target){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        int left, middle, right,rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        left =0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        right= sortedArr.length-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        while(left <= right){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            middle = (left+right)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            rc= sortedArr[middle].compareTo(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if(rc<0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                left = middle+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }else if(rc >0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                right = middle -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                return middle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static final char AT_SIGN = '@';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public static byte[] mixed_prepare(byte[] src)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                         throws IOException, ParseException, UnsupportedEncodingException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        String s = new String(src, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        int index = s.indexOf(AT_SIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        StringBuffer out = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if(index > -1){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            /* special prefixes must not be followed by suffixes! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            String prefixString = s.substring(0,index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            int i= findStringIndex(special_prefixes, prefixString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            String suffixString = s.substring(index+1, s.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if(i>-1 && !suffixString.equals("")){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                throw new ParseException("Suffix following a special index", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            UCharacterIterator prefix = UCharacterIterator.getInstance(prefixString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            UCharacterIterator suffix = UCharacterIterator.getInstance(suffixString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            out.append(prep.nfsmxp.prepare(prefix,StringPrep.DEFAULT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            out.append(AT_SIGN); // add the delimiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            out.append(prep.nfsmxs.prepare(suffix, StringPrep.DEFAULT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            UCharacterIterator iter = UCharacterIterator.getInstance(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            out.append(prep.nfsmxp.prepare(iter,StringPrep.DEFAULT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
       return out.toString().getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}