View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.client.utils;
28  
29  import java.util.StringTokenizer;
30  
31  import org.apache.http.annotation.Immutable;
32  
33  /**
34   * Implementation from pseudo code in RFC 3492.
35   *
36   * @deprecated (4.4) use standard {@link java.net.IDN}.
37   *
38   * @since 4.0
39   */
40  @Immutable
41  @Deprecated
42  public class Rfc3492Idn implements Idn {
43      private static final int base = 36;
44      private static final int tmin = 1;
45      private static final int tmax = 26;
46      private static final int skew = 38;
47      private static final int damp = 700;
48      private static final int initial_bias = 72;
49      private static final int initial_n = 128;
50      private static final char delimiter = '-';
51      private static final String ACE_PREFIX = "xn--";
52  
53      private int adapt(final int delta, final int numpoints, final boolean firsttime) {
54          int d = delta;
55          if (firsttime) {
56              d = d / damp;
57          } else {
58              d = d / 2;
59          }
60          d = d + (d / numpoints);
61          int k = 0;
62          while (d > ((base - tmin) * tmax) / 2) {
63            d = d / (base - tmin);
64            k = k + base;
65          }
66          return k + (((base - tmin + 1) * d) / (d + skew));
67      }
68  
69      private int digit(final char c) {
70          if ((c >= 'A') && (c <= 'Z')) {
71              return (c - 'A');
72          }
73          if ((c >= 'a') && (c <= 'z')) {
74              return (c - 'a');
75          }
76          if ((c >= '0') && (c <= '9')) {
77              return (c - '0') + 26;
78          }
79          throw new IllegalArgumentException("illegal digit: "+ c);
80      }
81  
82      @Override
83      public String toUnicode(final String punycode) {
84          final StringBuilder unicode = new StringBuilder(punycode.length());
85          final StringTokenizer tok = new StringTokenizer(punycode, ".");
86          while (tok.hasMoreTokens()) {
87              String t = tok.nextToken();
88              if (unicode.length() > 0) {
89                  unicode.append('.');
90              }
91              if (t.startsWith(ACE_PREFIX)) {
92                  t = decode(t.substring(4));
93              }
94              unicode.append(t);
95          }
96          return unicode.toString();
97      }
98  
99      protected String decode(final String s) {
100         String input = s;
101         int n = initial_n;
102         int i = 0;
103         int bias = initial_bias;
104         final StringBuilder output = new StringBuilder(input.length());
105         final int lastdelim = input.lastIndexOf(delimiter);
106         if (lastdelim != -1) {
107             output.append(input.subSequence(0, lastdelim));
108             input = input.substring(lastdelim + 1);
109         }
110 
111         while (!input.isEmpty()) {
112             final int oldi = i;
113             int w = 1;
114             for (int k = base;; k += base) {
115                 if (input.isEmpty()) {
116                     break;
117                 }
118                 final char c = input.charAt(0);
119                 input = input.substring(1);
120                 final int digit = digit(c);
121                 i = i + digit * w; // FIXME fail on overflow
122                 final int t;
123                 if (k <= bias + tmin) {
124                     t = tmin;
125                 } else if (k >= bias + tmax) {
126                     t = tmax;
127                 } else {
128                     t = k - bias;
129                 }
130                 if (digit < t) {
131                     break;
132                 }
133                 w = w * (base - t); // FIXME fail on overflow
134             }
135             bias = adapt(i - oldi, output.length() + 1, (oldi == 0));
136             n = n + i / (output.length() + 1); // FIXME fail on overflow
137             i = i % (output.length() + 1);
138             // {if n is a basic code point then fail}
139             output.insert(i, (char) n);
140             i++;
141         }
142         return output.toString();
143     }
144 
145 }