X509NameTokenizer.java revision e1142c149e244797ce73b0e7fad40816e447a817
1package 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 * @deprecated use X500NameTokenizer 9 */ 10public class X509NameTokenizer 11{ 12 private String value; 13 private int index; 14 private char separator; 15 private StringBuffer buf = new StringBuffer(); 16 17 public X509NameTokenizer( 18 String oid) 19 { 20 this(oid, ','); 21 } 22 23 public X509NameTokenizer( 24 String oid, 25 char separator) 26 { 27 this.value = oid; 28 this.index = -1; 29 this.separator = separator; 30 } 31 32 public boolean hasMoreTokens() 33 { 34 return (index != value.length()); 35 } 36 37 public String nextToken() 38 { 39 if (index == value.length()) 40 { 41 return null; 42 } 43 44 int end = index + 1; 45 boolean quoted = false; 46 boolean escaped = false; 47 48 buf.setLength(0); 49 50 while (end != value.length()) 51 { 52 char c = value.charAt(end); 53 54 if (c == '"') 55 { 56 if (!escaped) 57 { 58 quoted = !quoted; 59 } 60 else 61 { 62 if (c == '#' && buf.charAt(buf.length() - 1) == '=') 63 { 64 buf.append('\\'); 65 } 66 else if (c == '+' && separator != '+') 67 { 68 buf.append('\\'); 69 } 70 buf.append(c); 71 } 72 escaped = false; 73 } 74 else 75 { 76 if (escaped || quoted) 77 { 78 if (c == '#' && buf.charAt(buf.length() - 1) == '=') 79 { 80 buf.append('\\'); 81 } 82 else if (c == '+' && separator != '+') 83 { 84 buf.append('\\'); 85 } 86 buf.append(c); 87 escaped = false; 88 } 89 else if (c == '\\') 90 { 91 escaped = true; 92 } 93 else if (c == separator) 94 { 95 break; 96 } 97 else 98 { 99 // BEGIN android-added 100 // copied from a newer version of BouncyCastle 101 if (c == '#' && buf.charAt(buf.length() - 1) == '=') 102 { 103 buf.append('\\'); 104 } 105 else if (c == '+' && separator != '+') 106 { 107 buf.append('\\'); 108 } 109 // END android-added 110 buf.append(c); 111 } 112 } 113 end++; 114 } 115 116 index = end; 117 return buf.toString().trim(); 118 } 119} 120