Home | History | Annotate | Download | only in x509
      1 package org.bouncycastle.asn1.x509;
      2 
      3 /**
      4  * class for breaking up an X500 Name into it's component tokens, ala
      5  * java.util.StringTokenizer. We need this class as some of the
      6  * lightweight Java environment don't support classes like
      7  * StringTokenizer.
      8  */
      9 public class X509NameTokenizer
     10 {
     11     private String          value;
     12     private int             index;
     13     private char            seperator;
     14     private StringBuffer    buf = new StringBuffer();
     15 
     16     public X509NameTokenizer(
     17         String  oid)
     18     {
     19         this(oid, ',');
     20     }
     21 
     22     public X509NameTokenizer(
     23         String  oid,
     24         char    seperator)
     25     {
     26         this.value = oid;
     27         this.index = -1;
     28         this.seperator = seperator;
     29     }
     30 
     31     public boolean hasMoreTokens()
     32     {
     33         return (index != value.length());
     34     }
     35 
     36     public String nextToken()
     37     {
     38         if (index == value.length())
     39         {
     40             return null;
     41         }
     42 
     43         int     end = index + 1;
     44         boolean quoted = false;
     45         boolean escaped = false;
     46 
     47         buf.setLength(0);
     48 
     49         while (end != value.length())
     50         {
     51             char    c = value.charAt(end);
     52 
     53             if (c == '"')
     54             {
     55                 if (!escaped)
     56                 {
     57                     quoted = !quoted;
     58                 }
     59                 else
     60                 {
     61                     // BEGIN android-added
     62                     // copied from a newer version of BouncyCastle
     63                     if (c == '#' && buf.charAt(buf.length() - 1) == '=')
     64                     {
     65                         buf.append('\\');
     66                     }
     67                     else if (c == '+' && seperator != '+')
     68                     {
     69                         buf.append('\\');
     70                     }
     71                     // END android-added
     72                     buf.append(c);
     73                 }
     74                 escaped = false;
     75             }
     76             else
     77             {
     78                 if (escaped || quoted)
     79                 {
     80                     if (c == '#' && buf.charAt(buf.length() - 1) == '=')
     81                     {
     82                         buf.append('\\');
     83                     }
     84                     else if (c == '+' && seperator != '+')
     85                     {
     86                         buf.append('\\');
     87                     }
     88                     buf.append(c);
     89                     escaped = false;
     90                 }
     91                 else if (c == '\\')
     92                 {
     93                     escaped = true;
     94                 }
     95                 else if (c == seperator)
     96                 {
     97                     break;
     98                 }
     99                 else
    100                 {
    101                     buf.append(c);
    102                 }
    103             }
    104             end++;
    105         }
    106 
    107         index = end;
    108         return buf.toString().trim();
    109     }
    110 }