1 21 22 package org.apache.derby.iapi.util; 23 24 import org.apache.derby.iapi.reference.Attribute; 25 import org.apache.derby.iapi.reference.SQLState; 26 import org.apache.derby.iapi.reference.Property; 27 import org.apache.derby.iapi.error.StandardException; 28 import java.io.IOException ; 29 import java.io.StringReader ; 30 import java.util.Vector ; 31 import java.util.HashSet ; 32 import java.util.Properties ; 33 34 58 public abstract class IdUtil 59 { 60 64 public static String delimitId(String id) 65 { 66 StringBuffer quotedBuffer = new StringBuffer (); 67 quotedBuffer.append('\"'); 68 char[] charArray = id.toCharArray(); 69 70 for (int ix = 0; ix < charArray.length; ix++){ 71 char currentChar = charArray[ix]; 72 quotedBuffer.append(currentChar); 73 if (currentChar == '\"') 74 quotedBuffer.append('\"'); 75 } 76 quotedBuffer.append('\"'); 77 78 return quotedBuffer.toString(); 79 } 80 81 86 public static String mkQualifiedName(String id1, 87 String id2) 88 { 89 if( null == id1) 90 return delimitId(id2); 91 return 92 delimitId(id1) + 93 "." + 94 delimitId(id2); 95 } 96 97 100 public static String mkQualifiedName(String [] ids) 101 { 102 StringBuffer sb = new StringBuffer (); 103 for (int ix=0; ix < ids.length; ix++) 104 { 105 if (ix!=0) sb.append("."); 106 sb.append(delimitId(ids[ix])); 107 } 108 return sb.toString(); 109 } 110 111 120 public static String [] parseQualifiedName(String s, boolean normalizeToUpper) 121 throws StandardException 122 { 123 StringReader r = new StringReader (s); 124 String [] qName = parseQualifiedName(r, normalizeToUpper); 125 verifyEmpty(r); 126 return qName; 127 } 128 129 139 public static String [] parseQualifiedName(StringReader r, boolean normalizeToUpper) 140 throws StandardException 141 { 142 Vector v = new Vector (); 143 while (true) 144 { 145 String thisId = parseId(r,true, normalizeToUpper); 146 v.addElement(thisId); 147 int dot; 148 149 try { 150 r.mark(0); 151 dot = r.read(); 152 if (dot != '.') 153 { 154 if (dot!=-1) r.reset(); 155 break; 156 } 157 } 158 159 catch (IOException ioe){ 160 throw StandardException.newException(SQLState.ID_PARSE_ERROR,ioe); 161 } 162 } 163 String [] result = new String [v.size()]; 164 v.copyInto(result); 165 return result; 166 } 167 168 178 public static String parseId(String s) 179 throws StandardException 180 { 181 StringReader r = new StringReader (s); 182 String id = parseId(r,true, true); 183 verifyEmpty(r); 184 return id; 185 } 186 187 200 public static String parseId(StringReader r, boolean normalize, boolean normalizeToUpper) 201 throws StandardException 202 { 203 try { 204 r.mark(0); 205 int c = r.read(); 206 if (c == -1) throw StandardException.newException(SQLState.ID_PARSE_ERROR); 208 r.reset(); 209 if (c == '"') 210 return parseQId(r,normalize); 211 else 212 return parseUnQId(r,normalize, normalizeToUpper); 213 } 214 215 catch (IOException ioe){ 216 throw StandardException.newException(SQLState.ID_PARSE_ERROR,ioe); 217 } 218 } 219 220 private static String parseUnQId(StringReader r, boolean normalize, boolean normalizeToUpper) 221 throws IOException ,StandardException 222 { 223 StringBuffer b = new StringBuffer (); 224 int c; 225 boolean first; 226 for(first = true; ; first=false) 228 { 229 r.mark(0); 230 if (idChar(first,c=r.read())) 231 b.append((char)c); 232 else 233 break; 234 } 235 if (c != -1) r.reset(); 236 237 if (normalize) 238 return normalizeToUpper ? StringUtil.SQLToUpperCase(b.toString()) : StringUtil.SQLToLowerCase(b.toString()); 239 else 240 return b.toString(); 241 } 242 243 244 private static boolean idChar(boolean first,int c) 245 { 246 if (((c>='a' && c<='z') || (c>='A' && c<='Z')) || 247 (!first &&(c>='0' && c<='9')) || (!first &&c =='_') ) 248 return true; 249 else if (Character.isLetter((char) c)) 250 return true; 251 else if (!first && Character.isDigit((char) c)) 252 return true; 253 return false; 254 } 255 private static String parseQId(StringReader r,boolean normalize) 256 throws IOException ,StandardException 257 { 258 StringBuffer b = new StringBuffer (); 259 int c = r.read(); 260 if (c != '"') throw StandardException.newException(SQLState.ID_PARSE_ERROR); 261 while (true) 262 { 263 c=r.read(); 264 if (c == '"') 265 { 266 r.mark(0); 267 int c2 = r.read(); 268 if (c2 != '"') 269 { 270 if (c2!=-1)r.reset(); 271 break; 272 } 273 } 274 else if (c == -1) 275 throw StandardException.newException(SQLState.ID_PARSE_ERROR); 276 277 b.append((char)c); 278 } 279 280 if (b.length() == 0) throw StandardException.newException(SQLState.ID_PARSE_ERROR); 282 283 if (normalize) 284 return b.toString(); 285 else 286 return delimitId(b.toString()); } 288 289 private static void verifyEmpty(java.io.Reader r) 290 throws StandardException 291 { 292 try { 293 if (r.read() != -1) 294 throw StandardException.newException(SQLState.ID_PARSE_ERROR); 295 } 296 297 catch (IOException ioe){ 298 throw StandardException.newException(SQLState.ID_PARSE_ERROR,ioe); 299 } 300 } 301 302 public static final int DBCP_SCHEMA_NAME = 0; 303 304 public static final int DBCP_SQL_JAR_NAME = 1; 305 306 326 public static String [][] parseDbClassPath(String input, boolean normalizeToUpper) 327 throws StandardException 328 { 329 if (input.length() == 0) 331 return new String [0][]; 332 333 Vector v = new Vector (); 334 java.io.StringReader r = new java.io.StringReader (input); 335 while (true) 337 { 338 try { 339 String [] thisQName = IdUtil.parseQualifiedName(r, normalizeToUpper); 340 if (thisQName.length != 2) 341 throw StandardException.newException(SQLState.DB_CLASS_PATH_PARSE_ERROR,input); 342 343 v.addElement(thisQName); 344 int delim = r.read(); 345 if (delim != ':') 346 { 347 if (delim!=-1) 348 throw StandardException.newException(SQLState.DB_CLASS_PATH_PARSE_ERROR,input); 349 break; 350 } 351 } 352 353 catch (StandardException se){ 354 if (se.getMessageId().equals(SQLState.ID_PARSE_ERROR)) 355 throw StandardException.newException(SQLState.DB_CLASS_PATH_PARSE_ERROR, 356 se,input); 357 else 358 throw se; 359 } 360 361 catch (IOException ioe){ 362 throw StandardException.newException(SQLState.DB_CLASS_PATH_PARSE_ERROR,ioe,input); 363 } 364 } 365 String [][] result = new String [v.size()][]; 366 v.copyInto(result); 367 return result; 368 } 369 370 371 374 375 376 383 public static String [] parseIdList(String p) 384 throws StandardException 385 { 386 if (p==null) return null; 387 StringReader r = new StringReader (p); 388 String [] result = parseIdList(r, true); 389 verifyListEmpty(r); 390 return result; 391 } 392 393 394 402 private static String [] parseIdList(StringReader r, boolean normalize) 403 throws StandardException 404 { 405 Vector v = new Vector (); 406 while (true) 407 { 408 int delim; 409 try { 410 String thisId = IdUtil.parseId(r,normalize, true); 411 v.addElement(thisId); 412 r.mark(0); 413 delim = r.read(); 414 if (delim != ',') 415 { 416 if (delim!=-1) r.reset(); 417 break; 418 } 419 } 420 421 catch (StandardException se){ 422 if (se.getMessageId().equals(SQLState.ID_LIST_PARSE_ERROR)) 423 throw StandardException.newException(SQLState.ID_LIST_PARSE_ERROR,se); 424 else 425 throw se; 426 } 427 428 catch (IOException ioe){ 429 throw StandardException.newException(SQLState.ID_LIST_PARSE_ERROR,ioe); 430 } 431 } 432 if (v.size() == 0) return null; 433 String [] result = new String [v.size()]; 434 v.copyInto(result); 435 return result; 436 } 437 438 445 public static String intersect(String [] l1, String [] l2) 446 { 447 if (l1 == null || l2 == null) return null; 448 HashSet h = new HashSet (); 449 for(int ix=0;ix<l2.length;ix++) h.add(l2[ix]); 450 Vector v = new Vector (); 451 for(int ix=0;ix<l1.length;ix++) if (h.contains(l1[ix])) v.addElement(l1[ix]); 452 return vectorToIdList(v,true); 453 } 454 455 462 private static String vectorToIdList(Vector v,boolean normal) 463 { 464 if (v.size() == 0) return null; 465 String [] a = new String [v.size()]; 466 v.copyInto(a); 467 if (normal) 468 return mkIdList(a); 469 else 470 return mkIdListAsEntered(a); 471 } 472 473 478 public static String getUserAuthorizationId(String userName) throws StandardException 479 { 480 try { 481 return parseId(userName); 482 } 483 catch (StandardException se) { 484 throw StandardException.newException(SQLState.AUTH_INVALID_USER_NAME, userName); 485 } 486 } 487 488 493 public static String getUserNameFromURLProps(Properties params) 494 { 495 String userName = params.getProperty(Attribute.USERNAME_ATTR, 496 Property.DEFAULT_USER_NAME); 497 if (userName.equals("")) 498 userName = Property.DEFAULT_USER_NAME; 499 500 return userName; 501 } 502 503 509 public static String dups(String [] l) 510 { 511 if (l == null) return null; 512 HashSet h = new HashSet (); 513 Vector v = new Vector (); 514 for(int ix=0;ix<l.length;ix++) 515 { 516 if (!h.contains(l[ix])) 517 h.add(l[ix]); 518 else 519 v.addElement(l[ix]); 520 } 521 return vectorToIdList(v,true); 522 } 523 524 529 public static String pruneDups(String l) throws StandardException 530 { 531 if (l == null) return null; 532 String [] normal_a = parseIdList(l); 533 StringReader r = new StringReader (l); 534 String [] external_a = parseIdList(r,false); 535 HashSet h = new HashSet (); 536 Vector v = new Vector (); 537 for(int ix=0;ix<normal_a.length;ix++) 538 { 539 if (!h.contains(normal_a[ix])) 540 { 541 h.add(normal_a[ix]); 542 v.addElement(external_a[ix]); 543 } 544 } 545 return vectorToIdList(v,false); 546 } 547 548 552 public static String mkIdList(String [] ids) 553 { 554 StringBuffer sb = new StringBuffer (); 555 for (int ix=0;ix<ids.length; ix++) 556 { 557 if (ix != 0) sb.append(","); 558 sb.append(IdUtil.delimitId(ids[ix])); 559 } 560 return sb.toString(); 561 } 562 563 566 private static String mkIdListAsEntered(String [] externalIds ) 567 { 568 StringBuffer sb = new StringBuffer (); 569 for (int ix=0;ix<externalIds.length; ix++) 570 { 571 if (ix != 0) sb.append(","); 572 sb.append(externalIds[ix]); 573 } 574 return sb.toString(); 575 } 576 577 private static void verifyListEmpty(StringReader r) 578 throws StandardException 579 { 580 try { 581 if (r.read() != -1) 582 throw StandardException.newException(SQLState.ID_LIST_PARSE_ERROR); 583 } 584 585 catch (IOException ioe){ 586 throw StandardException.newException(SQLState.ID_LIST_PARSE_ERROR,ioe); 587 } 588 589 590 } 591 592 598 public static boolean idOnList(String id, String list) 599 throws StandardException 600 { 601 if (list==null) return false; 602 String [] list_a = parseIdList(list); 603 for (int ix=0; ix < list_a.length; ix++) 604 if (id.equals(list_a[ix])) return true; 605 return false; 606 } 607 608 619 public static String deleteId(String id, String list) 620 throws StandardException 621 { 622 if (list==null) return null; 623 Vector v = new Vector (); 624 StringReader r = new StringReader (list); 625 String [] enteredList_a = parseIdList(r,false); 626 for (int ix=0; ix < enteredList_a.length; ix++) 632 if (!id.equals(IdUtil.parseId(enteredList_a[ix]))) 633 v.addElement(enteredList_a[ix]); 634 if (v.size() == 0) 635 return null; 636 else 637 return vectorToIdList(v,false); 638 } 639 640 641 646 public static String appendId(String id, String list) 647 throws StandardException 648 { 649 if (list==null) 650 return id; 651 else 652 return list+","+id; 653 } 654 } 655 | Popular Tags |