KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > utils > RFC2253Parser


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security.utils;
18
19
20
21 import java.io.IOException JavaDoc;
22 import java.io.StringReader JavaDoc;
23
24
25 /**
26  *
27  * @author $Author: raul $
28  */

29 public class RFC2253Parser {
30
31     
32    /** {@link java.util.logging} logging facility */
33    /* static java.util.logging.Logger log =
34         java.util.logging.Logger.getLogger(RFC2253Parser.class.getName());
35    */

36     
37    static boolean _TOXML = true;
38
39    /**
40     * Method rfc2253toXMLdsig
41     *
42     * @param dn
43     * @return normalized string
44     *
45     */

46    public static String JavaDoc rfc2253toXMLdsig(String JavaDoc dn) {
47
48       _TOXML = true;
49
50       // Transform from RFC1779 to RFC2253
51
String JavaDoc normalized = normalize(dn);
52
53       return rfctoXML(normalized);
54    }
55
56    /**
57     * Method xmldsigtoRFC2253
58     *
59     * @param dn
60     * @return normalized string
61     */

62    public static String JavaDoc xmldsigtoRFC2253(String JavaDoc dn) {
63
64       _TOXML = false;
65
66       // Transform from RFC1779 to RFC2253
67
String JavaDoc normalized = normalize(dn);
68
69       return xmltoRFC(normalized);
70    }
71
72    /**
73     * Method normalize
74     *
75     * @param dn
76     * @return normalized string
77     */

78    public static String JavaDoc normalize(String JavaDoc dn) {
79
80       //if empty string
81
if ((dn == null) || dn.equals("")) {
82          return "";
83       }
84
85       try {
86          String JavaDoc _DN = semicolonToComma(dn);
87          StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
88          int i = 0;
89          int l = 0;
90          int k;
91
92          //for name component
93
for (int j = 0; (k = _DN.indexOf(",", j)) >= 0; j = k + 1) {
94             l += countQuotes(_DN, j, k);
95
96             if ((k > 0) && (_DN.charAt(k - 1) != '\\') && (l % 2) != 1) {
97                sb.append(parseRDN(_DN.substring(i, k).trim()) + ",");
98
99                i = k + 1;
100                l = 0;
101             }
102          }
103
104          sb.append(parseRDN(trim(_DN.substring(i))));
105
106          return sb.toString();
107       } catch (IOException JavaDoc ex) {
108          return dn;
109       }
110    }
111
112    /**
113     * Method parseRDN
114     *
115     * @param str
116     * @return normalized string
117     * @throws IOException
118     */

119    static String JavaDoc parseRDN(String JavaDoc str) throws IOException JavaDoc {
120
121       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
122       int i = 0;
123       int l = 0;
124       int k;
125
126       for (int j = 0; (k = str.indexOf("+", j)) >= 0; j = k + 1) {
127          l += countQuotes(str, j, k);
128
129          if ((k > 0) && (str.charAt(k - 1) != '\\') && (l % 2) != 1) {
130             sb.append(parseATAV(trim(str.substring(i, k))) + "+");
131
132             i = k + 1;
133             l = 0;
134          }
135       }
136
137       sb.append(parseATAV(trim(str.substring(i))));
138
139       return sb.toString();
140    }
141
142    /**
143     * Method parseATAV
144     *
145     * @param str
146     * @return normalized string
147     * @throws IOException
148     */

149    static String JavaDoc parseATAV(String JavaDoc str) throws IOException JavaDoc {
150
151       int i = str.indexOf("=");
152
153       if ((i == -1) || ((i > 0) && (str.charAt(i - 1) == '\\'))) {
154          return str;
155       }
156       String JavaDoc attrType = normalizeAT(str.substring(0, i));
157       String JavaDoc attrValue = normalizeV(str.substring(i + 1));
158
159       return attrType + "=" + attrValue;
160       
161    }
162
163    /**
164     * Method normalizeAT
165     *
166     * @param str
167     * @return normalized string
168     */

169    static String JavaDoc normalizeAT(String JavaDoc str) {
170
171       String JavaDoc at = str.toUpperCase().trim();
172
173       if (at.startsWith("OID")) {
174          at = at.substring(3);
175       }
176
177       return at;
178    }
179
180    /**
181     * Method normalizeV
182     *
183     * @param str
184     * @return normalized string
185     * @throws IOException
186     */

187    static String JavaDoc normalizeV(String JavaDoc str) throws IOException JavaDoc {
188
189       String JavaDoc value = trim(str);
190
191       if (value.startsWith("\"")) {
192          StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
193          StringReader JavaDoc sr = new StringReader JavaDoc(value.substring(1,
194                               value.length() - 1));
195          int i = 0;
196          char c;
197
198          for (; (i = sr.read()) > -1; ) {
199             c = (char) i;
200
201             //the following char is defined at 4.Relationship with RFC1779 and LDAPv2 inrfc2253
202
if ((c == ',') || (c == '=') || (c == '+') || (c == '<')
203                     || (c == '>') || (c == '#') || (c == ';')) {
204                sb.append('\\');
205             }
206
207             sb.append(c);
208          }
209
210          value = trim(sb.toString());
211       }
212
213       if (_TOXML == true) {
214          if (value.startsWith("#")) {
215             value = '\\' + value;
216          }
217       } else {
218          if (value.startsWith("\\#")) {
219             value = value.substring(1);
220          }
221       }
222
223       return value;
224    }
225
226    /**
227     * Method rfctoXML
228     *
229     * @param string
230     * @return normalized string
231     */

232    static String JavaDoc rfctoXML(String JavaDoc string) {
233
234       try {
235          String JavaDoc s = changeLess32toXML(string);
236
237          return changeWStoXML(s);
238       } catch (Exception JavaDoc e) {
239          return string;
240       }
241    }
242
243    /**
244     * Method xmltoRFC
245     *
246     * @param string
247     * @return normalized string
248     */

249    static String JavaDoc xmltoRFC(String JavaDoc string) {
250
251       try {
252          String JavaDoc s = changeLess32toRFC(string);
253
254          return changeWStoRFC(s);
255       } catch (Exception JavaDoc e) {
256          return string;
257       }
258    }
259
260    /**
261     * Method changeLess32toRFC
262     *
263     * @param string
264     * @return normalized string
265     * @throws IOException
266     */

267    static String JavaDoc changeLess32toRFC(String JavaDoc string) throws IOException JavaDoc {
268
269       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
270       StringReader JavaDoc sr = new StringReader JavaDoc(string);
271       int i = 0;
272       char c;
273
274       for (; (i = sr.read()) > -1; ) {
275          c = (char) i;
276
277          if (c == '\\') {
278             sb.append(c);
279
280             char c1 = (char) sr.read();
281             char c2 = (char) sr.read();
282
283             //65 (A) 97 (a)
284
if ((((c1 >= 48) && (c1 <= 57)) || ((c1 >= 65) && (c1 <= 70)) || ((c1 >= 97) && (c1 <= 102)))
285                     && (((c2 >= 48) && (c2 <= 57))
286                         || ((c2 >= 65) && (c2 <= 70))
287                         || ((c2 >= 97) && (c2 <= 102)))) {
288                char ch = (char) Byte.parseByte("" + c1 + c2, 16);
289
290                sb.append(ch);
291             } else {
292                sb.append(c1);
293                sb.append(c2);
294             }
295          } else {
296             sb.append(c);
297          }
298       }
299
300       return sb.toString();
301    }
302
303    /**
304     * Method changeLess32toXML
305     *
306     * @param string
307     * @return normalized string
308     * @throws IOException
309     */

310    static String JavaDoc changeLess32toXML(String JavaDoc string) throws IOException JavaDoc {
311
312       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
313       StringReader JavaDoc sr = new StringReader JavaDoc(string);
314       int i = 0;
315
316       for (; (i = sr.read()) > -1; ) {
317          if (i < 32) {
318             sb.append('\\');
319             sb.append(Integer.toHexString(i));
320          } else {
321             sb.append((char) i);
322          }
323       }
324
325       return sb.toString();
326    }
327
328    /**
329     * Method changeWStoXML
330     *
331     * @param string
332     * @return normalized string
333     * @throws IOException
334     */

335    static String JavaDoc changeWStoXML(String JavaDoc string) throws IOException JavaDoc {
336
337       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
338       StringReader JavaDoc sr = new StringReader JavaDoc(string);
339       int i = 0;
340       char c;
341
342       for (; (i = sr.read()) > -1; ) {
343          c = (char) i;
344
345          if (c == '\\') {
346             char c1 = (char) sr.read();
347
348             if (c1 == ' ') {
349                sb.append('\\');
350
351                String JavaDoc s = "20";
352
353                sb.append(s);
354             } else {
355                sb.append('\\');
356                sb.append(c1);
357             }
358          } else {
359             sb.append(c);
360          }
361       }
362
363       return sb.toString();
364    }
365
366    /**
367     * Method changeWStoRFC
368     *
369     * @param string
370     * @return normalized string
371     */

372    static String JavaDoc changeWStoRFC(String JavaDoc string) {
373
374       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
375       int i = 0;
376       int k;
377
378       for (int j = 0; (k = string.indexOf("\\20", j)) >= 0; j = k + 3) {
379          sb.append(trim(string.substring(i, k)) + "\\ ");
380
381          i = k + 3;
382       }
383
384       sb.append(string.substring(i));
385
386       return sb.toString();
387    }
388
389    /**
390     * Method semicolonToComma
391     *
392     * @param str
393     * @return normalized string
394     */

395    static String JavaDoc semicolonToComma(String JavaDoc str) {
396       return removeWSandReplace(str, ";", ",");
397    }
398
399    /**
400     * Method removeWhiteSpace
401     *
402     * @param str
403     * @param symbol
404     * @return normalized string
405     */

406    static String JavaDoc removeWhiteSpace(String JavaDoc str, String JavaDoc symbol) {
407       return removeWSandReplace(str, symbol, symbol);
408    }
409
410    /**
411     * Method removeWSandReplace
412     *
413     * @param str
414     * @param symbol
415     * @param replace
416     * @return normalized string
417     */

418    static String JavaDoc removeWSandReplace(String JavaDoc str, String JavaDoc symbol, String JavaDoc replace) {
419
420       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
421       int i = 0;
422       int l = 0;
423       int k;
424
425       for (int j = 0; (k = str.indexOf(symbol, j)) >= 0; j = k + 1) {
426          l += countQuotes(str, j, k);
427
428          if ((k > 0) && (str.charAt(k - 1) != '\\') && (l % 2) != 1) {
429             sb.append(trim(str.substring(i, k)) + replace);
430
431             i = k + 1;
432             l = 0;
433          }
434       }
435
436       sb.append(trim(str.substring(i)));
437
438       return sb.toString();
439    }
440
441    /**
442     * Returns the number of Quotation from i to j
443     *
444     * @param s
445     * @param i
446     * @param j
447     * @return number of quotes
448     */

449    private static int countQuotes(String JavaDoc s, int i, int j) {
450
451       int k = 0;
452
453       for (int l = i; l < j; l++) {
454          if (s.charAt(l) == '"') {
455             k++;
456          }
457       }
458
459       return k;
460    }
461
462    //only for the end of a space character occurring at the end of the string from rfc2253
463

464    /**
465     * Method trim
466     *
467     * @param str
468     * @return the string
469     */

470    static String JavaDoc trim(String JavaDoc str) {
471
472       String JavaDoc trimed = str.trim();
473       int i = str.indexOf(trimed.substring(0)) + trimed.length();
474
475       if ((str.length() > i) && trimed.endsWith("\\")
476               &&!trimed.endsWith("\\\\")) {
477          if (str.charAt(i) == ' ') {
478             trimed = trimed + " ";
479          }
480       }
481
482       return trimed;
483    }
484
485    /**
486     * Method main
487     *
488     * @param args
489     * @throws Exception
490     */

491    public static void main(String JavaDoc[] args) throws Exception JavaDoc {
492
493       testToXML("CN=\"Steve, Kille\", O=Isode Limited, C=GB");
494       testToXML("CN=Steve Kille , O=Isode Limited,C=GB");
495       testToXML("\\ OU=Sales+CN=J. Smith,O=Widget Inc.,C=US\\ \\ ");
496       testToXML("CN=L. Eagle,O=Sue\\, Grabbit and Runn,C=GB");
497       testToXML("CN=Before\\0DAfter,O=Test,C=GB");
498       testToXML("CN=\"L. Eagle,O=Sue, = + < > # ;Grabbit and Runn\",C=GB");
499       testToXML("1.3.6.1.4.1.1466.0=#04024869,O=Test,C=GB");
500
501       {
502          StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
503
504          sb.append('L');
505          sb.append('u');
506          sb.append('\uc48d');
507          sb.append('i');
508          sb.append('\uc487');
509
510          String JavaDoc test7 = "SN=" + sb.toString();
511
512          testToXML(test7);
513       }
514
515       testToRFC("CN=\"Steve, Kille\", O=Isode Limited, C=GB");
516       testToRFC("CN=Steve Kille , O=Isode Limited,C=GB");
517       testToRFC("\\20OU=Sales+CN=J. Smith,O=Widget Inc.,C=US\\20\\20 ");
518       testToRFC("CN=L. Eagle,O=Sue\\, Grabbit and Runn,C=GB");
519       testToRFC("CN=Before\\12After,O=Test,C=GB");
520       testToRFC("CN=\"L. Eagle,O=Sue, = + < > # ;Grabbit and Runn\",C=GB");
521       testToRFC("1.3.6.1.4.1.1466.0=\\#04024869,O=Test,C=GB");
522
523       {
524          StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
525
526          sb.append('L');
527          sb.append('u');
528          sb.append('\uc48d');
529          sb.append('i');
530          sb.append('\uc487');
531
532          String JavaDoc test7 = "SN=" + sb.toString();
533
534          testToRFC(test7);
535       }
536    }
537
538    /** Field i */
539    static int counter = 0;
540
541    /**
542     * Method test
543     *
544     * @param st
545     */

546    static void testToXML(String JavaDoc st) {
547
548       System.out.println("start " + counter++ + ": " + st);
549       System.out.println(" " + rfc2253toXMLdsig(st));
550       System.out.println("");
551    }
552
553    /**
554     * Method testToRFC
555     *
556     * @param st
557     */

558    static void testToRFC(String JavaDoc st) {
559
560       System.out.println("start " + counter++ + ": " + st);
561       System.out.println(" " + xmldsigtoRFC2253(st));
562       System.out.println("");
563    }
564 }
565
Popular Tags