1 19 20 package org.netbeans.modules.j2ee.sun.validation.util; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.lang.reflect.Constructor ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.Method ; 27 import java.net.URL ; 28 import java.text.MessageFormat ; 29 30 import org.netbeans.modules.j2ee.sun.validation.util.BundleReader; 31 32 39 public class Utils { 40 41 private final String GET_PREFIX = "get"; 43 44 45 public Utils() { 46 } 47 48 49 57 public String getName(String absoluteName, int delimiter) { 58 if(null == absoluteName){ 59 return absoluteName; 60 } 61 int index = absoluteName.lastIndexOf(delimiter); 62 if( index != -1) { 63 index = index + 1; 64 return absoluteName.substring(index); 65 } else { 66 return absoluteName; 67 } 68 } 69 70 71 80 public String getParentName(String absoluteName, int delimiter) { 81 if(null == absoluteName){ 82 return absoluteName; 83 } 84 int endIndex = absoluteName.lastIndexOf(delimiter); 85 if(endIndex != -1){ 86 if(0 == endIndex){ 87 return null; 88 } else { 89 return absoluteName.substring(0, endIndex); 90 } 91 } else { 92 return null; 93 } 94 } 95 96 97 103 public String upperCaseFirstLetter(String string) 104 { 105 if(string == null || string.length() <= 0){ 106 return string; 107 } 108 return string.substring(0, 1).toUpperCase() + string.substring(1); 109 } 110 111 112 119 public Object createObject(String type) { 120 Object object = null; 121 try { 122 Class classObject = Class.forName(type); 123 object = classObject.newInstance(); 124 } catch (InstantiationException e) { 125 System.out.println(e); 126 } catch (IllegalAccessException e) { 127 System.out.println(e); 128 } catch (ClassNotFoundException e) { 129 System.out.println(e); 130 } 131 return object; 132 } 133 134 135 143 public Object createObject(Class classObject) { 144 Object object = null; 145 try { 146 object = classObject.newInstance(); 147 } catch (InstantiationException e) { 148 System.out.println(e); 149 } catch (IllegalAccessException e) { 150 System.out.println(e); 151 } 152 return object; 153 } 154 155 156 166 public Object createObject(Constructor constructor, 167 Object [] arguments) { 168 Object object = null; 170 171 try { 172 object = constructor.newInstance(arguments); 173 return object; 175 } catch (InstantiationException e) { 176 System.out.println(e); 177 } catch (IllegalAccessException e) { 178 System.out.println(e); 179 } catch (IllegalArgumentException e) { 180 System.out.println(e); 181 } catch (InvocationTargetException e) { 182 System.out.println(e); 183 } 184 return object; 185 } 186 187 188 198 public Constructor getConstructor(String type, 199 Class [] argumentClass){ 200 Constructor constructor = null; 201 Class classObject = getClass(type); 202 203 try { 204 constructor = classObject.getConstructor(argumentClass); 205 } catch (NoSuchMethodException e) { 206 System.out.println(e); 207 } 208 return constructor; 209 } 210 211 212 224 public Constructor getConstructor(Class classObject, 225 Class [] argumentClass){ 226 Constructor constructor = null; 227 228 try { 229 constructor = classObject.getConstructor(argumentClass); 230 } catch (NoSuchMethodException e) { 231 System.out.println(e); 232 } 233 return constructor; 234 } 235 236 237 243 public Class getClass(Object object){ 244 Class classObject = null; 245 246 classObject = object.getClass(); 247 return classObject; 249 } 250 251 252 258 public Class getClass(String type){ 259 Class classObject = null; 260 261 try { 262 classObject = Class.forName(type); 263 } catch (ClassNotFoundException e) { 264 System.out.println(e); 265 } 266 return classObject; 267 } 268 269 270 281 public Method getMethod(String type, String methodName, 282 Class [] argumentClass){ 283 Method method = null; 284 Class classObject = getClass(type); 285 286 try { 287 method = classObject.getMethod(methodName, argumentClass); 288 } catch (NoSuchMethodException e) { 289 System.out.println(e); 290 } 291 return method; 292 } 293 294 295 307 public Method getMethod(Class classObject, String methodName, 308 Class [] argumentClass){ 309 Method method = null; 310 311 try { 312 method = classObject.getMethod(methodName, argumentClass); 313 } catch (NoSuchMethodException e) { 314 System.out.println(e); 315 } 316 return method; 317 } 318 319 320 330 public Object invoke(Object object, Method method, 331 Object [] arguments) { 332 Object result = null; 333 try { 334 result = method.invoke(object, arguments); 335 } catch (IllegalAccessException e) { 336 System.out.println(e); 337 } catch (InvocationTargetException e) { 338 System.out.println(e); 339 } 340 return result; 341 } 342 343 344 353 public Method getMethod(String type, String methodName){ 354 Method method = null; 355 Class classObject = getClass(type); 356 357 try { 358 method = classObject.getMethod(methodName, null); 359 } catch (NoSuchMethodException e) { 360 System.out.println(e); 361 } 362 return method; 363 } 364 365 366 376 public Method getMethod(Class classObject, 377 String methodName){ 378 Method method = null; 379 380 try { 381 method = classObject.getMethod(methodName, null); 382 } catch (NoSuchMethodException e) { 383 System.out.println(e); 384 } 385 return method; 386 } 387 388 389 397 public Object invoke(Object object, Method method) { 398 Object result = null; 399 try { 400 result = method.invoke(object, null); 401 } catch (IllegalAccessException e) { 402 System.out.println(e); 403 } catch (InvocationTargetException e) { 404 System.out.println(e); 405 } 406 return result; 407 } 408 409 410 417 public String eleminateHypen(String string){ 418 if(!(string == null || string.length() <= 0)){ 419 int index = string.indexOf('-'); 420 while(index != -1){ 421 if(index == 0){ 422 string = string.substring(1); 423 } else { 424 if(index == (string.length() - 1)){ 425 string = string.substring(0,string.length()-1); 426 } else { 427 string = string.substring(0,index) + 428 upperCaseFirstLetter(string.substring(index + 1)); 429 } 430 } 431 index = string.indexOf('-'); 432 } 433 } 434 return string; 435 } 436 437 438 446 public String methodNameFromBeanName(String elementName, 447 String prefix){ 448 if((null == elementName) || (null == prefix) || 449 (prefix.length() <= 0 )){ 450 return elementName; 451 } 452 String methodName = upperCaseFirstLetter(elementName); 453 return methodName = prefix + methodName; 454 } 455 456 457 465 public String methodNameFromDtdName(String elementName, 466 String prefix){ 467 return methodNameFromBeanName(eleminateHypen(elementName), prefix); 468 } 469 470 471 482 public Object getElement(String elementName, Object object){ 483 if((null == object) || (null == elementName) || 487 (elementName.length() <= 0)){ 488 return null; 489 } 490 491 String methodName = 492 methodNameFromDtdName(elementName, GET_PREFIX); Method getMethod = null; 494 getMethod = getMethod(getClass(object), methodName); 495 return invoke(object, getMethod); 496 } 497 498 499 510 public Object [] getElements(String elementName, Object object){ 511 return (Object []) getElement(elementName, object); 512 } 513 514 515 528 public Object getElement(String elementName, int index, 529 Object object){ 530 if((null == object) || (null == elementName) || 534 (elementName.length() <= 0) || (index < 0)){ 535 return null; 536 } 537 538 String methodName = 539 methodNameFromDtdName(elementName, GET_PREFIX); Class [] argumentTypes = new Class [] {int.class}; 541 Method getMethod = null; 542 getMethod = getMethod(getClass(object), methodName, 543 argumentTypes); 544 545 Integer in = new Integer (index); 546 Object [] argumentValues = new Object [] {in}; 547 return invoke(object, getMethod, argumentValues); 548 } 549 550 551 563 public Object getElement(String elementName, Object object, 564 String prefix){ 565 if((null == object) || (null == elementName) || 569 (elementName.length() <= 0)){ 570 return null; 571 } 572 573 String returnValue = null; 574 String methodName = 575 methodNameFromDtdName(elementName, prefix); 576 Class classObject = getClass(object); 577 Method method = getMethod(classObject, methodName); 578 return (Integer ) invoke(object, method); 579 } 580 581 582 590 public String getIndexedName(String name, int index){ 591 if(name != null) { 592 String format = 593 BundleReader.getValue("Indexed_Name_Format"); Object [] arguments = new Object []{name, String.valueOf(index)}; 595 name = MessageFormat.format(format, arguments); 596 } 597 return name; 598 } 599 600 601 604 public URL getUrlObject(String relativePath){ 605 Class cl = getClass(); 606 ClassLoader classLoader = cl.getClassLoader(); 607 return classLoader.getResource(relativePath); 608 } 609 610 611 619 public InputStream getInputStream(String relavtiveFilePath){ 620 InputStream inputStream = null; 621 URL url = null; 622 if(relavtiveFilePath != null){ 623 url = getUrlObject(relavtiveFilePath); 624 if(url != null) { 625 try { 626 inputStream = url.openStream(); 627 } catch (IOException exception){ 628 System.out.println(exception.getMessage()); 629 } 630 } else { 631 String format = 632 BundleReader.getValue("Error_does_not_exists"); Object [] arguments = 634 new Object []{"File", relavtiveFilePath}; System.out.println(MessageFormat.format(format, arguments)); 636 } 637 } 638 return inputStream; 639 } 640 641 642 649 public boolean fileExists(String relativePath){ 650 boolean returnValue = false; 651 InputStream inputStream = getInputStream(relativePath); 652 if(inputStream != null){ 653 returnValue = true; 654 } 655 return returnValue; 656 } 657 658 } 659 | Popular Tags |