1 package javax.naming.ldap; 2 3 import java.util.List ; 4 import java.util.ArrayList ; 5 6 import javax.naming.InvalidNameException ; 7 8 11 final class Rfc2253Parser { 12 13 private final String name; private final char[] chars; private final int len; private int cur = 0; 18 21 Rfc2253Parser(String name) { 22 this.name = name; 23 len = name.length(); 24 chars = name.toCharArray(); 25 } 26 27 30 32 List parseDn() throws InvalidNameException { 33 cur = 0; 34 35 38 ArrayList rdns = 39 new ArrayList (len / 3 + 10); 41 if (len == 0) { 42 return rdns; 43 } 44 45 rdns.add(doParse(new Rdn ())); 46 while (cur < len) { 47 if (chars[cur] == ',' || chars[cur] == ';') { 48 ++cur; 49 rdns.add(0, doParse(new Rdn ())); 50 } else { 51 throw new InvalidNameException ("Invalid name: " + name); 52 } 53 } 54 return rdns; 55 } 56 57 60 Rdn parseRdn() throws InvalidNameException { 61 return parseRdn(new Rdn ()); 62 } 63 64 67 Rdn parseRdn(Rdn rdn) throws InvalidNameException { 68 rdn = doParse(rdn); 69 if (cur < len) { 70 throw new InvalidNameException ("Invalid RDN: " + name); 71 } 72 return rdn; 73 } 74 75 79 private Rdn doParse(Rdn rdn) throws InvalidNameException { 80 81 while (cur < len) { 82 consumeWhitespace(); 83 String attrType = parseAttrType(); 84 consumeWhitespace(); 85 if (cur >= len || chars[cur] != '=') { 86 throw new InvalidNameException ("Invalid name: " + name); 87 } 88 ++cur; consumeWhitespace(); 90 String value = parseAttrValue(); 91 consumeWhitespace(); 92 93 rdn.put(attrType, Rdn.unescapeValue(value)); 94 if (cur >= len || chars[cur] != '+') { 95 break; 96 } 97 ++cur; } 99 rdn.sort(); 100 return rdn; 101 } 102 103 110 private String parseAttrType() throws InvalidNameException { 111 112 final int beg = cur; 113 while (cur < len) { 114 char c = chars[cur]; 115 if (Character.isLetterOrDigit(c) || 116 c == '.' || 117 c == '-' || 118 c == ' ') { 119 ++cur; 120 } else { 121 break; 122 } 123 } 124 while ((cur > beg) && (chars[cur - 1] == ' ')) { 126 --cur; 127 } 128 129 if (beg == cur) { 130 throw new InvalidNameException ("Invalid name: " + name); 131 } 132 return new String (chars, beg, cur - beg); 133 } 134 135 139 private String parseAttrValue() throws InvalidNameException { 140 141 if (cur < len && chars[cur] == '#') { 142 return parseBinaryAttrValue(); 143 } else if (cur < len && chars[cur] == '"') { 144 return parseQuotedAttrValue(); 145 } else { 146 return parseStringAttrValue(); 147 } 148 } 149 150 private String parseBinaryAttrValue() throws InvalidNameException { 151 final int beg = cur; 152 ++cur; while ((cur < len) && 154 Character.isLetterOrDigit(chars[cur])) { 155 ++cur; 156 } 157 return new String (chars, beg, cur - beg); 158 } 159 160 private String parseQuotedAttrValue() throws InvalidNameException { 161 162 final int beg = cur; 163 ++cur; 165 while ((cur < len) && chars[cur] != '"') { 166 if (chars[cur] == '\\') { 167 ++cur; } 169 ++cur; 170 } 171 if (cur >= len) { throw new InvalidNameException ("Invalid name: " + name); 173 } 174 ++cur; 176 return new String (chars, beg, cur - beg); 177 } 178 179 private String parseStringAttrValue() throws InvalidNameException { 180 181 final int beg = cur; 182 int esc = -1; 184 while ((cur < len) && !atTerminator()) { 185 if (chars[cur] == '\\') { 186 ++cur; esc = cur; 188 } 189 ++cur; 190 } 191 if (cur > len) { throw new InvalidNameException ("Invalid name: " + name); 193 } 194 195 int end; 197 for (end = cur; end > beg; end--) { 198 if (!isWhitespace(chars[end - 1]) || (esc == end - 1)) { 199 break; 200 } 201 } 202 return new String (chars, beg, end - beg); 203 } 204 205 private void consumeWhitespace() { 206 while ((cur < len) && isWhitespace(chars[cur])) { 207 ++cur; 208 } 209 } 210 211 215 private boolean atTerminator() { 216 return (cur < len && 217 (chars[cur] == ',' || 218 chars[cur] == ';' || 219 chars[cur] == '+')); 220 } 221 222 225 private static boolean isWhitespace(char c) { 226 return (c == ' ' || c == '\r'); 227 } 228 } 229 | Popular Tags |