1    package org.bouncycastle.asn1.x509;
2    
3    /**
4     * class for breaking up an X500 Name into it's component tokens, ala
5     * java.util.StringTokenizer. We need this class as some of the
6     * lightweight Java environment don't support classes like
7     * StringTokenizer.
8     */
9    public class X509NameTokenizer
10   {
11       private String          oid;
12       private int             index;
13       private StringBuffer    buf = new StringBuffer();
14   
15       public X509NameTokenizer(
16           String oid)
17       {
18           this.oid = oid;
19           this.index = -1;
20       }
21   
22       public boolean hasMoreTokens()
23       {
24           return (index != oid.length());
25       }
26   
27       public String nextToken()
28       {
29           if (index == oid.length())
30           {
31               return null;
32           }
33   
34           int     end = index + 1;
35           boolean quoted = false;
36           boolean escaped = false;
37   
38           buf.setLength(0);
39   
40           while (end != oid.length())
41           {
42               char    c = oid.charAt(end);
43   
44               if (c == '"')
45               {
46                   if (!escaped)
47                   {
48                       quoted = !quoted;
49                   }
50                   else
51                   {
52                       buf.append(c);
53                   }
54                   escaped = false;
55               }
56               else
57               {
58                   if (escaped || quoted)
59                   {
60                       buf.append(c);
61                       escaped = false;
62                   }
63                   else if (c == '\\')
64                   {
65                       escaped = true;
66                   }
67                   else if (c == ',')
68                   {
69                       break;
70                   }
71                   else
72                   {
73                       buf.append(c);
74                   }
75               }
76               end++;
77           }
78   
79           index = end;
80           return buf.toString().trim();
81       }
82   }
83