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            seperator;
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    seperator)
25    {
26        this.value = oid;
27        this.index = -1;
28        this.seperator = seperator;
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                else
60                {
61                    buf.append(c);
62                }
63                escaped = false;
64            }
65            else
66            {
67                if (escaped || quoted)
68                {
69                    if (c == '#' && buf.charAt(buf.length() - 1) == '=')
70                    {
71                        buf.append('\\');
72                    }
73                    else if (c == '+' && seperator != '+')
74                    {
75                        buf.append('\\');
76                    }
77                    buf.append(c);
78                    escaped = false;
79                }
80                else if (c == '\\')
81                {
82                    escaped = true;
83                }
84                else if (c == seperator)
85                {
86                    break;
87                }
88                else
89                {
90                    buf.append(c);
91                }
92            }
93            end++;
94        }
95
96        index = end;
97        return buf.toString().trim();
98    }
99}
100