1package org.bouncycastle.asn1.x500.style;
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 */
9class X500NameTokenizer
10{
11    private String          value;
12    private int             index;
13    private char            separator;
14    private StringBuffer    buf = new StringBuffer();
15
16    public X500NameTokenizer(
17        String  oid)
18    {
19        this(oid, ',');
20    }
21
22    public X500NameTokenizer(
23        String  oid,
24        char    separator)
25    {
26        this.value = oid;
27        this.index = -1;
28        this.separator = separator;
29    }
30
31    public boolean hasMoreTokens()
32    {
33        return (index != value.length());
34    }
35
36    public String nextToken()
37    {
38        if (index == value.length())
39        {
40            return null;
41        }
42
43        int     end = index + 1;
44        boolean quoted = false;
45        boolean escaped = false;
46
47        buf.setLength(0);
48
49        while (end != value.length())
50        {
51            char    c = value.charAt(end);
52
53            if (c == '"')
54            {
55                if (!escaped)
56                {
57                    quoted = !quoted;
58                }
59                buf.append(c);
60                escaped = false;
61            }
62            else
63            {
64                if (escaped || quoted)
65                {
66                    buf.append(c);
67                    escaped = false;
68                }
69                else if (c == '\\')
70                {
71                    buf.append(c);
72                    escaped = true;
73                }
74                else if (c == separator)
75                {
76                    break;
77                }
78                else
79                {
80                    buf.append(c);
81                }
82            }
83            end++;
84        }
85
86        index = end;
87
88        return buf.toString();
89    }
90}
91