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