X500NameTokenizer.java revision e1142c149e244797ce73b0e7fad40816e447a817
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                else
60                {
61                    if (c == '#' && buf.charAt(buf.length() - 1) == '=')
62                    {
63                        buf.append('\\');
64                    }
65                    else if (c == '+' && separator != '+')
66                    {
67                        buf.append('\\');
68                    }
69                    buf.append(c);
70                }
71                escaped = false;
72            }
73            else
74            {
75                if (escaped || quoted)
76                {
77                    if (c == '#' && buf.charAt(buf.length() - 1) == '=')
78                    {
79                        buf.append('\\');
80                    }
81                    else if (c == '+' && separator != '+')
82                    {
83                        buf.append('\\');
84                    }
85                    buf.append(c);
86                    escaped = false;
87                }
88                else if (c == '\\')
89                {
90                    escaped = true;
91                }
92                else if (c == separator)
93                {
94                    break;
95                }
96                else
97                {
98                    buf.append(c);
99                }
100            }
101            end++;
102        }
103
104        index = end;
105        return buf.toString().trim();
106    }
107}
108