All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.asn1.x500.style.X500NameTokenizer Maven / Gradle / Ivy

Go to download

The Long Term Stable (LTS) Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains the JCA/JCE provider and low-level API for the BC LTS version 2.73.4 for Java 8 and later.

There is a newer version: 2.73.6
Show newest version
package org.bouncycastle.asn1.x500.style;

/**
 * class for breaking up an X500 Name into it's component tokens, ala
 * java.util.StringTokenizer. We need this class as some of the
 * lightweight Java environment don't support classes like
 * StringTokenizer.
 */
public class X500NameTokenizer
{
    private final String value;
    private final char separator;

    private int index;

    public X500NameTokenizer(String oid)
    {
        this(oid, ',');
    }

    public X500NameTokenizer(String oid, char separator)
    {
        if (oid == null)
        {
            throw new NullPointerException();
        }
        if (separator == '"' || separator == '\\')
        {
            throw new IllegalArgumentException("reserved separator character");
        }

        this.value = oid;
        this.separator = separator;
        this.index = oid.length() < 1 ? 0 : -1;
    }

    public boolean hasMoreTokens()
    {
        return index < value.length();
    }

    public String nextToken()
    {
        if (index >= value.length())
        {
            return null;
        }

        boolean quoted = false;
        boolean escaped = false;

        int beginIndex = index + 1;
        while (++index < value.length())
        {
            char c = value.charAt(index);

            if (escaped)
            {
                escaped = false;
            }
            else if (c == '"')
            {
                quoted = !quoted;
            }
            else if (quoted)
            {
            }
            else if (c == '\\')
            {
                escaped = true;
            }
            else if (c == separator)
            {
                return value.substring(beginIndex, index);
            }
        }

        if (escaped || quoted)
        {
            throw new IllegalArgumentException("badly formatted directory string");
        }

        return value.substring(beginIndex, index);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy