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