001package org.apache.commons.ssl.org.bouncycastle.asn1.x500.style;
002
003/**
004 * class for breaking up an X500 Name into it's component tokens, ala
005 * java.util.StringTokenizer. We need this class as some of the
006 * lightweight Java environment don't support classes like
007 * StringTokenizer.
008 */
009public class X500NameTokenizer
010{
011    private String          value;
012    private int             index;
013    private char            separator;
014    private StringBuffer    buf = new StringBuffer();
015
016    public X500NameTokenizer(
017        String  oid)
018    {
019        this(oid, ',');
020    }
021    
022    public X500NameTokenizer(
023        String  oid,
024        char    separator)
025    {
026        this.value = oid;
027        this.index = -1;
028        this.separator = separator;
029    }
030
031    public boolean hasMoreTokens()
032    {
033        return (index != value.length());
034    }
035
036    public String nextToken()
037    {
038        if (index == value.length())
039        {
040            return null;
041        }
042
043        int     end = index + 1;
044        boolean quoted = false;
045        boolean escaped = false;
046
047        buf.setLength(0);
048
049        while (end != value.length())
050        {
051            char    c = value.charAt(end);
052
053            if (c == '"')
054            {
055                if (!escaped)
056                {
057                    quoted = !quoted;
058                }
059                buf.append(c);
060                escaped = false;
061            }
062            else
063            {
064                if (escaped || quoted)
065                {
066                    buf.append(c);
067                    escaped = false;
068                }
069                else if (c == '\\')
070                {
071                    buf.append(c);
072                    escaped = true;
073                }
074                else if (c == separator)
075                {
076                    break;
077                }
078                else
079                {
080                    buf.append(c);
081                }
082            }
083            end++;
084        }
085
086        index = end;
087
088        return buf.toString();
089    }
090}