1 package com.ca.commons.naming; 2 3 import com.ca.commons.cbutil.CBParse; 4 import javax.naming.InvalidNameException ; 5 6 10 11 public class NameUtility 12 { 13 20 21 public static int next(String searchMe, int startpos, char c) 22 { 23 if (c=='\\') return -1; 25 if (startpos < 0 || startpos > searchMe.length()) return -1; 27 int escape=-1, quotes=-1, nextC=-1; 28 29 while (true) 30 { 31 if (escape<startpos) escape = searchMe.indexOf('\\', startpos); 32 if (quotes<startpos) quotes = searchMe.indexOf('"', startpos); 33 if (nextC<startpos) nextC = searchMe.indexOf(c, startpos); 34 35 if (escape==-1 && quotes==-1) 37 return nextC; 38 39 if ( ((escape == -1) || (nextC < escape)) && ((quotes == -1) || (nextC <= quotes)) ) 43 return nextC; 44 45 47 if (quotes == -1 || (escape != -1 && escape<quotes)) 48 { 49 startpos = escape+2; } 51 else { 53 boolean escaped = true; 55 while (escaped) 56 { 57 quotes = searchMe.indexOf('"', quotes+1); if (quotes == -1) return -1; 60 int backcheck = quotes-1; while (searchMe.charAt(backcheck--) == '\\') escaped = !escaped; 63 64 escaped = !escaped; 65 } 66 67 startpos = quotes+1; 68 } 69 } 70 } 71 72 73 77 78 public static String unescape(String string) 79 throws InvalidNameException 80 { 81 return unescape(string, false); 82 } 83 84 88 89 public static String removeEscapedUTF(String utfString) 90 throws InvalidNameException 91 { 92 try 93 { 94 boolean foundUTF = false; 95 int safeLen = utfString.length()-1; 96 int pos = utfString.indexOf('\\'); 97 98 while ( pos > -1 && pos < safeLen) 99 { 100 char c = utfString.charAt(pos+1); 101 if ("01234567890ABCDEFabcdef".indexOf(c) > 0) 102 { 103 foundUTF = true; 104 char c2 = utfString.charAt(pos+2); 105 if ("01234567890ABCDEFabcdef".indexOf(c2) == -1) 106 throw new InvalidNameException ("second char of escaped hex couplet wasn't hex; was '" + c2 + "'"); 107 108 char utf8 = (char)Integer.parseInt("" + c + c2, 16); 109 utfString = utfString.substring(0,pos) + utf8 + utfString.substring(pos+3); 110 pos = utfString.indexOf('\\',pos+1); 111 } 112 else 113 { 114 pos = utfString.indexOf('\\',pos+1); } 116 } 117 118 if (foundUTF) { utfString = new String (utfString.getBytes("ISO-8859-1"), "UTF8"); 121 } 122 } 123 catch (Exception e) 124 { 125 e.printStackTrace(); 126 throw new InvalidNameException ("unable to parse rdn val: '" + utfString + "' - raw error was: " + e.toString()); 127 } 128 129 return utfString; 130 } 131 132 133 156 157 158 public static String unescape(String string, boolean jndiHack) 159 throws InvalidNameException 160 { 161 int len = string.length(); 162 if (len == 0) return string; 163 164 if (string.charAt(0)=='\"') 165 { 166 if (string.charAt(string.length()-1)!='\"') throw new InvalidNameException ("RDN.unescape(): invalid rdn fragment '" + ((string==null)?"<null>":string) + "'"); 168 169 string = string.substring(1,string.length()-1); 170 } 171 else 172 { 173 string = handleEscapedCharacters(string); 174 } 175 return string; 176 } 177 178 179 185 186 private static String handleEscapedCharacters(String string) 187 throws InvalidNameException 188 { 189 if (string.indexOf('\\') == -1) 190 return string; 191 192 boolean hasUTF8 = false; int pos; 195 StringBuffer buffy = new StringBuffer (string); 196 197 try 198 { 199 pos = string.indexOf("\\"); 200 while ( pos > -1) 201 { 202 if (pos == buffy.length()-1) { 204 buffy.setCharAt(pos, ' '); } 206 else 207 { 208 209 char c = buffy.charAt(pos+1); 210 211 if (("\",=+<>#;\\ ".indexOf(c)) >= 0) 212 { 213 buffy.deleteCharAt(pos); } 215 else if (("0123456789abcdefABCDEF").indexOf(c) >= 0) 216 { 217 hasUTF8 = true; pos += 2; } 220 else 221 { 222 throw new InvalidNameException ("illegal escaped character '" + c + "' in name: '" + string + "' (NameUtility:handleEscapedCharacters() )."); 223 } 224 } 225 pos = buffy.toString().indexOf("\\",pos+1); } 227 } 228 catch (StringIndexOutOfBoundsException e) 229 { 230 throw new InvalidNameException ("unparsable string '" + string + "' in NameUtility"); 231 } 232 233 if (hasUTF8) 234 return removeEscapedUTF(buffy.toString()); 235 else 236 return buffy.toString(); 237 } 238 239 240 241 246 public static String trimQuotes(String string) 247 { 248 int pos = string.indexOf('\"'); 249 int pos2 = string.lastIndexOf('\"'); 250 if (pos == -1 || pos == pos2) 251 System.out.println("RDN.trimQuotes(): rare error parsing rdn fragment: " + string); else 253 string = string.substring(0,pos) + string.substring(pos+1,pos2) + string.substring(pos2+1); 254 return string; 255 } 256 257 269 protected static String cleanupSlashes(String string) 270 { 271 int pos = 0; 272 int next3group; 273 int next4group; 274 while (pos > -1) 275 { 276 next4group = string.indexOf("\\\\\\\\",pos); 277 next3group = string.indexOf("\\\\\\",pos); 278 279 if (next3group == -1) { 281 pos = -1; 282 } 283 else if ((next4group==-1) || (next3group<next4group)) { 285 string = string.substring(0,next3group) + string.substring(next3group+1); 286 pos = next3group + 2; 287 } 288 else { 290 string = string.substring(0,next4group) + string.substring(next4group+2); 291 pos = next4group + 2; 292 } 293 } 294 return string; 295 } 296 297 302 303 public static String escape(String string) 304 { 305 if (string == null || string.length() == 0) 306 return string; 307 308 StringBuffer buffy = new StringBuffer (string); 309 310 buffy = CBParse.replaceAllBufferChar(buffy, '\\',"\\\\"); 311 buffy = CBParse.replaceAllBufferChar(buffy, ',',"\\,"); 312 buffy = CBParse.replaceAllBufferChar(buffy, '=',"\\="); 313 buffy = CBParse.replaceAllBufferChar(buffy, '+',"\\+"); 314 buffy = CBParse.replaceAllBufferChar(buffy, '<',"\\<"); 315 buffy = CBParse.replaceAllBufferChar(buffy, '>',"\\>"); 316 buffy = CBParse.replaceAllBufferChar(buffy, '#',"\\#"); 317 buffy = CBParse.replaceAllBufferChar(buffy, ';',"\\;"); 318 buffy = CBParse.replaceAllBufferChar(buffy, '\"',"\\\""); 319 320 if (buffy.charAt(buffy.length()-1) == ' ') { 322 buffy.setCharAt(buffy.length()-1, '\\'); 323 buffy.append(' '); 324 } 325 string = buffy.toString(); 327 328 return buffy.toString(); 329 } 330 331 332 340 341 public static String checkEndSpaces(String ldapDNString) 342 { 343 352 int finalPos = ldapDNString.length() - 1; 353 int pos = finalPos; 354 355 while (ldapDNString.charAt(pos) == '\\') { 357 pos--; 358 } 359 360 int numSlashes = finalPos - pos; 361 362 if (numSlashes%4 == 2) 363 { 364 return ldapDNString + " "; 365 } 366 367 return ldapDNString; 368 } 369 370 371 } | Popular Tags |