001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.util.asn1.x509;
019    
020    /**
021     * class for breaking up an X500 Name into it's component tokens, ala
022     * java.util.StringTokenizer. We need this class as some of the
023     * lightweight Java environment don't support classes like
024     * StringTokenizer.
025     */
026    public class X509NameTokenizer
027    {
028        private String          value;
029        private int             index;
030        private char            seperator;
031        private StringBuffer    buf = new StringBuffer();
032    
033        public X509NameTokenizer(
034            String  oid)
035        {
036            this(oid, ',');
037        }
038    
039        public X509NameTokenizer(
040            String  oid,
041            char    seperator)
042        {
043            this.value = oid;
044            this.index = -1;
045            this.seperator = seperator;
046        }
047    
048        public boolean hasMoreTokens()
049        {
050            return (index != value.length());
051        }
052    
053        public String nextToken()
054        {
055            if (index == value.length())
056            {
057                return null;
058            }
059    
060            int     end = index + 1;
061            boolean quoted = false;
062            boolean escaped = false;
063    
064            buf.setLength(0);
065    
066            while (end != value.length())
067            {
068                char    c = value.charAt(end);
069    
070                if (c == '"')
071                {
072                    if (!escaped)
073                    {
074                        quoted = !quoted;
075                    }
076                    else
077                    {
078                        buf.append(c);
079                    }
080                    escaped = false;
081                }
082                else
083                {
084                    if (escaped || quoted)
085                    {
086                        buf.append(c);
087                        escaped = false;
088                    }
089                    else if (c == '\\')
090                    {
091                        escaped = true;
092                    }
093                    else if (c == seperator)
094                    {
095                        break;
096                    }
097                    else
098                    {
099                        buf.append(c);
100                    }
101                }
102                end++;
103            }
104    
105            index = end;
106            return buf.toString().trim();
107        }
108    }