View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.asn1.x509;
20  
21  /**
22   * class for breaking up an X500 Name into it's component tokens, ala
23   * java.util.StringTokenizer. We need this class as some of the
24   * lightweight Java environment don't support classes like
25   * StringTokenizer.
26   */
27  public class X509NameTokenizer
28  {
29      private String          value;
30      private int             index;
31      private char            seperator;
32      private StringBuffer    buf = new StringBuffer();
33  
34      public X509NameTokenizer(
35          String  oid)
36      {
37          this(oid, ',');
38      }
39  
40      public X509NameTokenizer(
41          String  oid,
42          char    seperator)
43      {
44          this.value = oid;
45          this.index = -1;
46          this.seperator = seperator;
47      }
48  
49      public boolean hasMoreTokens()
50      {
51          return (index != value.length());
52      }
53  
54      public String nextToken()
55      {
56          if (index == value.length())
57          {
58              return null;
59          }
60  
61          int     end = index + 1;
62          boolean quoted = false;
63          boolean escaped = false;
64  
65          buf.setLength(0);
66  
67          while (end != value.length())
68          {
69              char    c = value.charAt(end);
70  
71              if (c == '"')
72              {
73                  if (!escaped)
74                  {
75                      quoted = !quoted;
76                  }
77                  else
78                  {
79                      buf.append(c);
80                  }
81                  escaped = false;
82              }
83              else
84              {
85                  if (escaped || quoted)
86                  {
87                      buf.append(c);
88                      escaped = false;
89                  }
90                  else if (c == '\\')
91                  {
92                      escaped = true;
93                  }
94                  else if (c == seperator)
95                  {
96                      break;
97                  }
98                  else
99                  {
100                     buf.append(c);
101                 }
102             }
103             end++;
104         }
105 
106         index = end;
107         return buf.toString().trim();
108     }
109 }