View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with 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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.ws.security.components.crypto;
21  
22  import java.util.ArrayList;
23  
24  import org.apache.ws.security.util.RFC2253Parser;
25  
26  /**
27   * class for breaking up an X500 Name into it's component tokens, ala
28   * java.util.StringTokenizer. We need this class as some of the
29   * lightweight Java environment don't support classes like
30   * StringTokenizer.
31   */
32  public class X509NameTokenizer {
33  
34      private final java.util.List<String> tokens = new ArrayList<String>();
35      private int index = 0;
36  
37      public X509NameTokenizer(String dn) {
38         final String normalizedDN = RFC2253Parser.normalize(dn);
39         int i = 0;
40         int l = 0;
41         int k;
42         for (int j = 0; (k = normalizedDN.indexOf(',', j)) >= 0; j = k + 1) {
43            l += countQuotes(normalizedDN, j, k);
44            if ((k > 0) && (normalizedDN.charAt(k - 1) != '\\') && (l % 2) == 0) {
45               tokens.add(normalizedDN.substring(i, k).trim());
46               i = k + 1;
47               l = 0;
48            }
49         }
50         if (normalizedDN.trim().length() != 0) {
51             tokens.add(trim(normalizedDN.substring(i)));
52         }
53      }
54  
55      public boolean hasMoreTokens() {
56          return (index < tokens.size());
57      }
58  
59      public String nextToken() {
60          if (hasMoreTokens()) {
61              return (String) tokens.get(index++);
62          } else {
63              return "";
64          }
65      }
66  
67  
68      /**
69       * Returns the number of Quotation from i to j
70       *
71       * @param s
72       * @param i
73       * @param j
74       * @return number of quotes
75       */
76      private static int countQuotes(String s, int i, int j) {
77         int k = 0;
78         for (int l = i; l < j; l++) {
79            if (s.charAt(l) == '"') {
80               k++;
81            }
82         }
83         return k;
84      }
85  
86      /**
87       * Method trim
88       *
89       * @param str
90       * @return the string
91       */
92      private static String trim(String str) {
93         String trimed = str.trim();
94         int i = str.indexOf(trimed) + trimed.length();
95         if ((str.length() > i) 
96             && trimed.endsWith("\\")
97             && !trimed.endsWith("\\\\")
98             && (str.charAt(i) == ' ')) {
99           trimed = trimed + " ";
100        }
101        return trimed;
102     }
103 
104 }