KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > x509 > X509NameTokenizer


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

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

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