1 23 24 package com.sun.enterprise.tools.common.validation.util; 25 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.lang.reflect.Constructor ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.lang.reflect.Method ; 31 import java.net.URL ; 32 import java.text.MessageFormat ; 33 34 import com.sun.enterprise.tools.common.validation.util.BundleReader; 35 36 43 public class Utils { 44 45 private final String GET_PREFIX = "get"; 47 48 49 public Utils() { 50 } 51 52 53 61 public String getName(String absoluteName, int delimiter) { 62 if(null == absoluteName){ 63 return absoluteName; 64 } 65 int index = absoluteName.lastIndexOf(delimiter); 66 if( index != -1) { 67 index = index + 1; 68 return absoluteName.substring(index); 69 } else { 70 return absoluteName; 71 } 72 } 73 74 75 84 public String getParentName(String absoluteName, int delimiter) { 85 if(null == absoluteName){ 86 return absoluteName; 87 } 88 int endIndex = absoluteName.lastIndexOf(delimiter); 89 if(endIndex != -1){ 90 if(0 == endIndex){ 91 return null; 92 } else { 93 return absoluteName.substring(0, endIndex); 94 } 95 } else { 96 return null; 97 } 98 } 99 100 101 107 public String upperCaseFirstLetter(String string) 108 { 109 if(string == null || string.length() <= 0){ 110 return string; 111 } 112 return string.substring(0, 1).toUpperCase() + string.substring(1); 113 } 114 115 116 123 public Object createObject(String type) { 124 Object object = null; 125 try { 126 Class classObject = Class.forName(type); 127 object = classObject.newInstance(); 128 } catch (InstantiationException e) { 129 System.out.println(e); 130 } catch (IllegalAccessException e) { 131 System.out.println(e); 132 } catch (ClassNotFoundException e) { 133 System.out.println(e); 134 } 135 return object; 136 } 137 138 139 147 public Object createObject(Class classObject) { 148 Object object = null; 149 try { 150 object = classObject.newInstance(); 151 } catch (InstantiationException e) { 152 System.out.println(e); 153 } catch (IllegalAccessException e) { 154 System.out.println(e); 155 } 156 return object; 157 } 158 159 160 170 public Object createObject(Constructor constructor, 171 Object [] arguments) { 172 Object object = null; 174 175 try { 176 object = constructor.newInstance(arguments); 177 return object; 179 } catch (InstantiationException e) { 180 System.out.println(e); 181 } catch (IllegalAccessException e) { 182 System.out.println(e); 183 } catch (IllegalArgumentException e) { 184 System.out.println(e); 185 } catch (InvocationTargetException e) { 186 System.out.println(e); 187 } 188 return object; 189 } 190 191 192 202 public Constructor getConstructor(String type, 203 Class [] argumentClass){ 204 Constructor constructor = null; 205 Class classObject = getClass(type); 206 207 try { 208 constructor = classObject.getConstructor(argumentClass); 209 } catch (NoSuchMethodException e) { 210 System.out.println(e); 211 } 212 return constructor; 213 } 214 215 216 228 public Constructor getConstructor(Class classObject, 229 Class [] argumentClass){ 230 Constructor constructor = null; 231 232 try { 233 constructor = classObject.getConstructor(argumentClass); 234 } catch (NoSuchMethodException e) { 235 System.out.println(e); 236 } 237 return constructor; 238 } 239 240 241 247 public Class getClass(Object object){ 248 Class classObject = null; 249 250 classObject = object.getClass(); 251 return classObject; 253 } 254 255 256 262 public Class getClass(String type){ 263 Class classObject = null; 264 265 try { 266 classObject = Class.forName(type); 267 } catch (ClassNotFoundException e) { 268 System.out.println(e); 269 } 270 return classObject; 271 } 272 273 274 285 public Method getMethod(String type, String methodName, 286 Class [] argumentClass){ 287 Method method = null; 288 Class classObject = getClass(type); 289 290 try { 291 method = classObject.getMethod(methodName, argumentClass); 292 } catch (NoSuchMethodException e) { 293 System.out.println(e); 294 } 295 return method; 296 } 297 298 299 311 public Method getMethod(Class classObject, String methodName, 312 Class [] argumentClass){ 313 Method method = null; 314 315 try { 316 method = classObject.getMethod(methodName, argumentClass); 317 } catch (NoSuchMethodException e) { 318 System.out.println(e); 319 } 320 return method; 321 } 322 323 324 334 public Object invoke(Object object, Method method, 335 Object [] arguments) { 336 Object result = null; 337 try { 338 result = method.invoke(object, arguments); 339 } catch (IllegalAccessException e) { 340 System.out.println(e); 341 } catch (InvocationTargetException e) { 342 System.out.println(e); 343 } 344 return result; 345 } 346 347 348 357 public Method getMethod(String type, String methodName){ 358 Method method = null; 359 Class classObject = getClass(type); 360 361 try { 362 method = classObject.getMethod(methodName, null); 363 } catch (NoSuchMethodException e) { 364 System.out.println(e); 365 } 366 return method; 367 } 368 369 370 380 public Method getMethod(Class classObject, 381 String methodName){ 382 Method method = null; 383 384 try { 385 method = classObject.getMethod(methodName, null); 386 } catch (NoSuchMethodException e) { 387 System.out.println(e); 388 } 389 return method; 390 } 391 392 393 401 public Object invoke(Object object, Method method) { 402 Object result = null; 403 try { 404 result = method.invoke(object, null); 405 } catch (IllegalAccessException e) { 406 System.out.println(e); 407 } catch (InvocationTargetException e) { 408 System.out.println(e); 409 } 410 return result; 411 } 412 413 414 421 public String eleminateHypen(String string){ 422 if(!(string == null || string.length() <= 0)){ 423 int index = string.indexOf('-'); 424 while(index != -1){ 425 if(index == 0){ 426 string = string.substring(1); 427 } else { 428 if(index == (string.length() - 1)){ 429 string = string.substring(0,string.length()-1); 430 } else { 431 string = string.substring(0,index) + 432 upperCaseFirstLetter(string.substring(index + 1)); 433 } 434 } 435 index = string.indexOf('-'); 436 } 437 } 438 return string; 439 } 440 441 442 450 public String methodNameFromBeanName(String elementName, 451 String prefix){ 452 if((null == elementName) || (null == prefix) || 453 (prefix.length() <= 0 )){ 454 return elementName; 455 } 456 String methodName = upperCaseFirstLetter(elementName); 457 return methodName = prefix + methodName; 458 } 459 460 461 469 public String methodNameFromDtdName(String elementName, 470 String prefix){ 471 return methodNameFromBeanName(eleminateHypen(elementName), prefix); 472 } 473 474 475 486 public Object getElement(String elementName, Object object){ 487 if((null == object) || (null == elementName) || 491 (elementName.length() <= 0)){ 492 return null; 493 } 494 495 String methodName = 496 methodNameFromDtdName(elementName, GET_PREFIX); Method getMethod = null; 498 getMethod = getMethod(getClass(object), methodName); 499 return invoke(object, getMethod); 500 } 501 502 503 514 public Object [] getElements(String elementName, Object object){ 515 return (Object []) getElement(elementName, object); 516 } 517 518 519 532 public Object getElement(String elementName, int index, 533 Object object){ 534 if((null == object) || (null == elementName) || 538 (elementName.length() <= 0) || (index < 0)){ 539 return null; 540 } 541 542 String methodName = 543 methodNameFromDtdName(elementName, GET_PREFIX); Class [] argumentTypes = new Class [] {int.class}; 545 Method getMethod = null; 546 getMethod = getMethod(getClass(object), methodName, 547 argumentTypes); 548 549 Integer in = new Integer (index); 550 Object [] argumentValues = new Object [] {in}; 551 return invoke(object, getMethod, argumentValues); 552 } 553 554 555 567 public Object getElement(String elementName, Object object, 568 String prefix){ 569 if((null == object) || (null == elementName) || 573 (elementName.length() <= 0)){ 574 return null; 575 } 576 577 String returnValue = null; 578 String methodName = 579 methodNameFromDtdName(elementName, prefix); 580 Class classObject = getClass(object); 581 Method method = getMethod(classObject, methodName); 582 return (Integer ) invoke(object, method); 583 } 584 585 586 594 public String getIndexedName(String name, int index){ 595 if(name != null) { 596 String format = 597 BundleReader.getValue("Indexed_Name_Format"); Object [] arguments = new Object []{name, String.valueOf(index)}; 599 name = MessageFormat.format(format, arguments); 600 } 601 return name; 602 } 603 604 605 608 public URL getUrlObject(String relativePath){ 609 Class cl = getClass(); 610 ClassLoader classLoader = cl.getClassLoader(); 611 return classLoader.getResource(relativePath); 612 } 613 614 615 623 public InputStream getInputStream(String relavtiveFilePath){ 624 InputStream inputStream = null; 625 URL url = null; 626 if(relavtiveFilePath != null){ 627 url = getUrlObject(relavtiveFilePath); 628 if(url != null) { 629 try { 630 inputStream = url.openStream(); 631 } catch (IOException exception){ 632 System.out.println(exception.getMessage()); 633 } 634 } else { 635 String format = 636 BundleReader.getValue("Error_does_not_exists"); Object [] arguments = 638 new Object []{"File", relavtiveFilePath}; System.out.println(MessageFormat.format(format, arguments)); 640 } 641 } 642 return inputStream; 643 } 644 645 646 653 public boolean fileExists(String relativePath){ 654 boolean returnValue = false; 655 InputStream inputStream = getInputStream(relativePath); 656 if(inputStream != null){ 657 returnValue = true; 658 } 659 return returnValue; 660 } 661 662 } 663 | Popular Tags |