KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > x509 > X509NameTokenizer


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

17
18 package org.apache.geronimo.util.asn1.x509;
19
20 /**
21  * class for breaking up an X500 Name into it's component tokens, ala
22  * java.util.StringTokenizer. We need this class as some of the
23  * lightweight Java environment don't support classes like
24  * StringTokenizer.
25  */

26 public class X509NameTokenizer
27 {
28     private String JavaDoc value;
29     private int index;
30     private char seperator;
31     private StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
32
33     public X509NameTokenizer(
34         String JavaDoc oid)
35     {
36         this(oid, ',');
37     }
38
39     public X509NameTokenizer(
40         String JavaDoc oid,
41         char seperator)
42     {
43         this.value = oid;
44         this.index = -1;
45         this.seperator = seperator;
46     }
47
48     public boolean hasMoreTokens()
49     {
50         return (index != value.length());
51     }
52
53     public String JavaDoc nextToken()
54     {
55         if (index == value.length())
56         {
57             return null;
58         }
59
60         int end = index + 1;
61         boolean quoted = false;
62         boolean escaped = false;
63
64         buf.setLength(0);
65
66         while (end != value.length())
67         {
68             char c = value.charAt(end);
69
70             if (c == '"')
71             {
72                 if (!escaped)
73                 {
74                     quoted = !quoted;
75                 }
76                 else
77                 {
78                     buf.append(c);
79                 }
80                 escaped = false;
81             }
82             else
83             {
84                 if (escaped || quoted)
85                 {
86                     buf.append(c);
87                     escaped = false;
88                 }
89                 else if (c == '\\')
90                 {
91                     escaped = true;
92                 }
93                 else if (c == seperator)
94                 {
95                     break;
96                 }
97                 else
98                 {
99                     buf.append(c);
100                 }
101             }
102             end++;
103         }
104
105         index = end;
106         return buf.toString().trim();
107     }
108 }
109
Popular Tags