KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > stringprep > Stringprep


1 /**
2  * Copyright (C) 2004 Free Software Foundation, Inc.
3  *
4   * Author: Oliver Hitz
5  *
6  * This file is part of GNU Libidn.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  * USA
22  */

23
24 package org.jivesoftware.stringprep;
25
26 public class Stringprep {
27
28     /**
29      * Additional characters prohibited by nodeprep that aren't defined as part of the
30      * RFC3454 tables.
31      */

32     private static final char [] NODEPREP_PROHIBIT = new char [] { '\u0022', '\u0026', '\'',
33             '\u002F', '\u003A', '\u003C', '\u003E', '\u003E', '\u0040'};
34
35     public static String JavaDoc nameprep(String JavaDoc input, boolean allowUnassigned)
36             throws StringprepException
37     {
38         if (input == null) {
39             return null;
40         }
41         StringBuilder JavaDoc s = new StringBuilder JavaDoc(input);
42
43         if (allowUnassigned) {
44             if (contains(s, RFC3454.A1)) {
45                 throw new StringprepException(StringprepException.CONTAINS_UNASSIGNED);
46             }
47         }
48
49         filter(s, RFC3454.B1);
50         map(s, RFC3454.B2search, RFC3454.B2replace);
51
52         s = new StringBuilder JavaDoc(NFKC.normalizeNFKC(s.toString()));
53         // B.3 is only needed if NFKC is not used, right?
54
// map(s, RFC3454.B3search, RFC3454.B3replace);
55

56         if (contains(s, RFC3454.C1_2) ||
57                 contains(s, RFC3454.C2_2) ||
58                 contains(s, RFC3454.C3) ||
59                 contains(s, RFC3454.C4) ||
60                 contains(s, RFC3454.C5) ||
61                 contains(s, RFC3454.C6) ||
62                 contains(s, RFC3454.C7) ||
63                 contains(s, RFC3454.C8)) {
64             // Table C.9 only contains code points > 0xFFFF which Java
65
// doesn't handle
66
throw new StringprepException(StringprepException.CONTAINS_PROHIBITED);
67         }
68
69         // Bidi handling
70
boolean r = contains(s, RFC3454.D1);
71         boolean l = contains(s, RFC3454.D2);
72
73         // RFC 3454, section 6, requirement 1: already handled above (table C.8)
74

75         // RFC 3454, section 6, requirement 2
76
if (r && l) {
77             throw new StringprepException(StringprepException.BIDI_BOTHRAL);
78         }
79
80         // RFC 3454, section 6, requirement 3
81
if (r) {
82             if (!contains(s.charAt(0), RFC3454.D1) ||
83                     !contains(s.charAt(s.length() - 1), RFC3454.D1)) {
84                 throw new StringprepException(StringprepException.BIDI_LTRAL);
85             }
86         }
87
88         return s.toString();
89     }
90
91     public static String JavaDoc nodeprep(String JavaDoc input) throws StringprepException {
92         if (input == null) {
93             return null;
94         }
95         StringBuilder JavaDoc s = new StringBuilder JavaDoc(input);
96
97         if (contains(s, RFC3454.A1)) {
98             throw new StringprepException(StringprepException.CONTAINS_UNASSIGNED);
99         }
100
101         filter(s, RFC3454.B1);
102         map(s, RFC3454.B2search, RFC3454.B2replace);
103
104         s = new StringBuilder JavaDoc(NFKC.normalizeNFKC(s.toString()));
105
106         if (contains(s, RFC3454.C1_1) ||
107             contains(s, RFC3454.C1_2) ||
108             contains(s, RFC3454.C2_1) ||
109             contains(s, RFC3454.C2_2) ||
110             contains(s, RFC3454.C3) ||
111             contains(s, RFC3454.C4) ||
112             contains(s, RFC3454.C5) ||
113             contains(s, RFC3454.C6) ||
114             contains(s, RFC3454.C7) ||
115             contains(s, RFC3454.C8) ||
116             contains(s, NODEPREP_PROHIBIT))
117         {
118             // Table C.9 only contains code points > 0xFFFF which Java
119
// doesn't handle
120
throw new StringprepException(StringprepException.CONTAINS_PROHIBITED);
121         }
122
123         // Bidi handling
124
boolean r = contains(s, RFC3454.D1);
125         boolean l = contains(s, RFC3454.D2);
126
127         // RFC 3454, section 6, requirement 1: already handled above (table C.8)
128

129         // RFC 3454, section 6, requirement 2
130
if (r && l) {
131             throw new StringprepException(StringprepException.BIDI_BOTHRAL);
132         }
133
134         // RFC 3454, section 6, requirement 3
135
if (r) {
136             if (!contains(s.charAt(0), RFC3454.D1) ||
137                     !contains(s.charAt(s.length() - 1), RFC3454.D1)) {
138                 throw new StringprepException(StringprepException.BIDI_LTRAL);
139             }
140         }
141
142         return s.toString();
143     }
144
145     public static String JavaDoc resourceprep(String JavaDoc input) throws StringprepException {
146         if (input == null) {
147             return null;
148         }
149         StringBuilder JavaDoc s = new StringBuilder JavaDoc(input);
150
151         if (contains(s, RFC3454.A1)) {
152             throw new StringprepException(StringprepException.CONTAINS_UNASSIGNED);
153         }
154
155         filter(s, RFC3454.B1);
156
157         s = new StringBuilder JavaDoc(NFKC.normalizeNFKC(s.toString()));
158
159         if (contains(s, RFC3454.C1_2) &&
160             contains(s, RFC3454.C2_1) &&
161             contains(s, RFC3454.C2_2) &&
162             contains(s, RFC3454.C3) &&
163             contains(s, RFC3454.C4) &&
164             contains(s, RFC3454.C5) &&
165             contains(s, RFC3454.C6) &&
166             contains(s, RFC3454.C7) &&
167             contains(s, RFC3454.C8))
168         {
169             // Table C.9 only contains code points > 0xFFFF which Java
170
// doesn't handle
171
throw new StringprepException(StringprepException.CONTAINS_PROHIBITED);
172         }
173
174         // Bidi handling
175
boolean r = contains(s, RFC3454.D1);
176         boolean l = contains(s, RFC3454.D2);
177
178         // RFC 3454, section 6, requirement 1: already handled above (table C.8)
179

180         // RFC 3454, section 6, requirement 2
181
if (r && l) {
182             throw new StringprepException(StringprepException.BIDI_BOTHRAL);
183         }
184
185         // RFC 3454, section 6, requirement 3
186
if (r) {
187             if (!contains(s.charAt(0), RFC3454.D1) ||
188                     !contains(s.charAt(s.length() - 1), RFC3454.D1)) {
189                 throw new StringprepException(StringprepException.BIDI_LTRAL);
190             }
191         }
192
193         return s.toString();
194     }
195
196     private static boolean contains(StringBuilder JavaDoc s, char[] p) {
197         for (int i = 0; i < p.length; i++) {
198             char c = p[i];
199             for (int j = 0; j < s.length(); j++) {
200                 if (c == s.charAt(j)) {
201                     return true;
202                 }
203             }
204         }
205         return false;
206     }
207
208     private static boolean contains(StringBuilder JavaDoc s, char[][] p) {
209         for (int i = 0; i < p.length; i++) {
210             char[] r = p[i];
211             if (1 == r.length) {
212                 char c = r[0];
213                 for (int j = 0; j < s.length(); j++) {
214                     if (c == s.charAt(j)) {
215                         return true;
216                     }
217                 }
218             } else if (2 == r.length) {
219                 char f = r[0];
220                 char t = r[1];
221                 for (int j = 0; j < s.length(); j++) {
222                     if (f <= s.charAt(j) && t >= s.charAt(j)) {
223                         return true;
224                     }
225                 }
226             }
227         }
228         return false;
229     }
230
231     private static boolean contains(char c, char[][] p) {
232         for (int i = 0; i < p.length; i++) {
233             char[] r = p[i];
234             if (1 == r.length) {
235                 if (c == r[0]) {
236                     return true;
237                 }
238             } else if (2 == r.length) {
239                 char f = r[0];
240                 char t = r[1];
241                 if (f <= c && t >= c) {
242                     return true;
243                 }
244             }
245         }
246         return false;
247     }
248
249     private static void filter(StringBuilder JavaDoc s, char[] f) {
250         for (int i = 0; i < f.length; i++) {
251             char c = f[i];
252
253             int j = 0;
254             while (j < s.length()) {
255                 if (c == s.charAt(j)) {
256                     s.deleteCharAt(j);
257                 } else {
258                     j++;
259                 }
260             }
261         }
262     }
263
264     private static void filter(StringBuilder JavaDoc s, char[][] f) {
265         for (int i = 0; i < f.length; i++) {
266             char[] r = f[i];
267
268             if (1 == r.length) {
269                 char c = r[0];
270
271                 int j = 0;
272                 while (j < s.length()) {
273                     if (c == s.charAt(j)) {
274                         s.deleteCharAt(j);
275                     } else {
276                         j++;
277                     }
278                 }
279             } else if (2 == r.length) {
280                 char from = r[0];
281                 char to = r[1];
282
283                 int j = 0;
284                 while (j < s.length()) {
285                     if (from <= s.charAt(j) && to >= s.charAt(j)) {
286                         s.deleteCharAt(j);
287                     } else {
288                         j++;
289                     }
290                 }
291             }
292         }
293     }
294
295     private static void map(StringBuilder JavaDoc s, char[] search, String JavaDoc[] replace) {
296         for (int i = 0; i < search.length; i++) {
297             char c = search[i];
298
299             int j = 0;
300             while (j < s.length()) {
301                 if (c == s.charAt(j)) {
302                     s.deleteCharAt(j);
303                     if (null != replace[i]) {
304                         s.insert(j, replace[i]);
305                         j += replace[i].length() - 1;
306                     }
307                 } else {
308                     j++;
309                 }
310             }
311         }
312     }
313 }
Popular Tags