1 64 65 package com.jcorporate.expresso.core.misc.upload; 66 67 import java.beans.IndexedPropertyDescriptor ; 68 import java.beans.Introspector ; 69 import java.beans.PropertyDescriptor ; 70 import java.io.UnsupportedEncodingException ; 71 import java.lang.reflect.Method ; 72 import java.math.BigDecimal ; 73 import java.util.Enumeration ; 74 import java.util.Hashtable ; 75 76 77 80 108 public class BaseValueParser 109 implements ValueParser { 110 111 114 protected Hashtable parameters = new Hashtable (); 115 116 119 private String characterEncoding = "US-ASCII"; 120 121 130 public static String convertAndTrim(String value) { 131 return value.trim(); 132 } 133 134 137 public BaseValueParser() { 138 super(); 139 } 140 141 144 public BaseValueParser(String newEncoding) { 145 super(); 146 characterEncoding = newEncoding; 147 } 148 149 152 public void dispose() { 153 clear(); 154 } 155 156 159 public void clear() { 160 parameters.clear(); 161 } 162 163 166 public void setCharacterEncoding(String s) { 167 characterEncoding = s; 168 } 169 170 173 public String getCharacterEncoding() { 174 return characterEncoding; 175 } 176 177 183 public void add(String name, double value) { 184 add(name, Double.toString(value)); 185 } 186 187 193 public void add(String name, int value) { 194 add(name, Integer.toString(value)); 195 } 196 197 203 public void add(String name, Integer value) { 204 add(name, value.toString()); 205 } 206 207 213 public void add(String name, long value) { 214 add(name, Long.toString(value)); 215 } 216 217 223 public void add(String name, String value) { 224 append(name, value); 225 } 226 227 235 public void append(String name, String value) { 236 String [] items = this.getStrings(name); 237 238 if (items == null) { 239 items = new String [1]; 240 items[0] = value; 241 parameters.put(convert(name), items); 242 } else { 243 String [] newItems = new String [items.length + 1]; 244 System.arraycopy(items, 0, newItems, 0, items.length); 245 newItems[items.length] = value; 246 parameters.put(convert(name), newItems); 247 } 248 } 249 250 257 public Object remove(String name) { 258 return parameters.remove(convert(name)); 259 } 260 261 269 public String convert(String value) { 270 return convertAndTrim(value); 271 } 272 273 281 public boolean containsKey(Object key) { 282 return parameters.containsKey(convert((String ) key)); 283 } 284 285 296 public Enumeration keys() { 297 return parameters.keys(); 298 } 299 300 309 public Object [] getKeys() { 310 return parameters.keySet().toArray(); 311 } 312 313 321 public boolean getBoolean(String name, boolean defaultValue) { 322 boolean value = defaultValue; 323 Object object = parameters.get(convert(name)); 324 325 if (object != null) { 326 String tmp = getString(name); 327 328 if (tmp.equalsIgnoreCase("1") || tmp.equalsIgnoreCase("true") || 329 tmp.equalsIgnoreCase("on")) { 330 value = true; 331 } 332 if (tmp.equalsIgnoreCase("0") || tmp.equalsIgnoreCase("false")) { 333 value = false; 334 } 335 } 336 337 return value; 338 } 339 340 347 public boolean getBoolean(String name) { 348 return getBoolean(name, false); 349 } 350 351 359 public Boolean getBool(String name, boolean defaultValue) { 360 if (getBoolean(name, defaultValue)) { 361 return Boolean.TRUE; 362 } else { 363 return Boolean.FALSE; 364 } 365 } 366 367 374 public Boolean getBool(String name) { 375 return getBool(name, false); 376 } 377 378 386 public double getDouble(String name, double defaultValue) { 387 double value = defaultValue; 388 389 try { 390 Object object = parameters.get(convert(name)); 391 392 if (object != null) { 393 value = Double.valueOf(((String []) object)[0]).doubleValue(); 394 } 395 } catch (NumberFormatException exception) { 396 } 397 398 return value; 399 } 400 401 408 public double getDouble(String name) { 409 return getDouble(name, 0.0); 410 } 411 412 420 public float getFloat(String name, float defaultValue) { 421 float value = defaultValue; 422 423 try { 424 Object object = parameters.get(convert(name)); 425 426 if (object != null) { 427 value = Float.valueOf(((String []) object)[0]).floatValue(); 428 } 429 } catch (NumberFormatException exception) { 430 } 431 432 return value; 433 } 434 435 442 public float getFloat(String name) { 443 return getFloat(name, 0.0f); 444 } 445 446 454 public BigDecimal getBigDecimal(String name, BigDecimal defaultValue) { 455 BigDecimal value = defaultValue; 456 457 try { 458 Object object = parameters.get(convert(name)); 459 460 if (object != null) { 461 value = new BigDecimal (((String []) object)[0]); 462 } 463 } catch (NumberFormatException exception) { 464 } 465 466 return value; 467 } 468 469 476 public BigDecimal getBigDecimal(String name) { 477 return getBigDecimal(name, new BigDecimal (0.0)); 478 } 479 480 487 public BigDecimal [] getBigDecimals(String name) { 488 BigDecimal [] value = null; 489 Object object = getStrings(convert(name)); 490 491 if (object != null) { 492 String [] temp = (String []) object; 493 value = new BigDecimal [temp.length]; 494 495 for (int i = 0; i < temp.length; i++) { 496 value[i] = new BigDecimal (temp[i]); 497 } 498 } 499 500 return value; 501 } 502 503 511 public int getInt(String name, int defaultValue) { 512 int value = defaultValue; 513 514 try { 515 Object object = parameters.get(convert(name)); 516 517 if (object != null) { 518 value = Integer.valueOf(((String []) object)[0]).intValue(); 519 } 520 } catch (NumberFormatException exception) { 521 } 522 523 return value; 524 } 525 526 533 public int getInt(String name) { 534 return getInt(name, 0); 535 } 536 537 545 public Integer getInteger(String name, int defaultValue) { 546 return new Integer (getInt(name, defaultValue)); 547 } 548 549 558 public Integer getInteger(String name, Integer def) { 559 return new Integer (getInt(name, def.intValue())); 560 } 561 562 569 public Integer getInteger(String name) { 570 return new Integer (getInt(name, 0)); 571 } 572 573 580 public int[] getInts(String name) { 581 int[] value = null; 582 Object object = getStrings(convert(name)); 583 584 if (object != null) { 585 String [] temp = (String []) object; 586 value = new int[temp.length]; 587 588 for (int i = 0; i < temp.length; i++) { 589 value[i] = Integer.parseInt(temp[i]); 590 } 591 } 592 593 return value; 594 } 595 596 603 public Integer [] getIntegers(String name) { 604 Integer [] value = null; 605 Object object = getStrings(convert(name)); 606 607 if (object != null) { 608 String [] temp = (String []) object; 609 value = new Integer [temp.length]; 610 611 for (int i = 0; i < temp.length; i++) { 612 value[i] = Integer.valueOf(temp[i]); 613 } 614 } 615 616 return value; 617 } 618 619 627 public long getLong(String name, long defaultValue) { 628 long value = defaultValue; 629 630 try { 631 Object object = parameters.get(convert(name)); 632 633 if (object != null) { 634 value = Long.valueOf(((String []) object)[0]).longValue(); 635 } 636 } catch (NumberFormatException exception) { 637 } 638 639 return value; 640 } 641 642 649 public long getLong(String name) { 650 return getLong(name, 0); 651 } 652 653 660 public long[] getLongs(String name) { 661 long[] value = null; 662 Object object = getStrings(convert(name)); 663 664 if (object != null) { 665 String [] temp = (String []) object; 666 value = new long[temp.length]; 667 668 for (int i = 0; i < temp.length; i++) { 669 value[i] = Long.parseLong(temp[i]); 670 } 671 } 672 673 return value; 674 } 675 676 683 public Long [] getLongObjects(String name) { 684 Long [] value = null; 685 Object object = getStrings(convert(name)); 686 687 if (object != null) { 688 String [] temp = (String []) object; 689 value = new Long [temp.length]; 690 691 for (int i = 0; i < temp.length; i++) { 692 value[i] = Long.valueOf(temp[i]); 693 } 694 } 695 696 return value; 697 } 698 699 707 public byte getByte(String name, byte defaultValue) { 708 byte value = defaultValue; 709 710 try { 711 Object object = parameters.get(convert(name)); 712 713 if (object != null) { 714 value = Byte.valueOf(((String []) object)[0]).byteValue(); 715 } 716 } catch (NumberFormatException exception) { 717 } 718 719 return value; 720 } 721 722 729 public byte getByte(String name) { 730 return getByte(name, (byte) 0); 731 } 732 733 741 public byte[] getBytes(String name) 742 throws UnsupportedEncodingException { 743 String tempStr = getString(name); 744 745 if (tempStr != null) { 746 return tempStr.getBytes(characterEncoding); 747 } 748 749 return null; 750 } 751 752 759 public String getString(String name) { 760 try { 761 String value = null; 762 Object object = parameters.get(convert(name)); 763 764 if (object != null) { 765 value = ((String []) object)[0]; 766 } 767 if (value == null || value.equals("null")) { 768 return null; 769 } 770 771 return value; 772 } catch (ClassCastException e) { 773 return null; 774 } 775 } 776 777 789 public String get(String name) { 790 return getString(name); 791 } 792 793 801 public String getString(String name, String defaultValue) { 802 String value = getString(name); 803 804 if (value == null || value.length() == 0 || value.equals("null")) { 805 return defaultValue; 806 } else { 807 return value; 808 } 809 } 810 811 820 public void setString(String name, String value) { 821 if (value != null) { 822 parameters.put(convert(name), new String []{value}); 823 } 824 } 825 826 833 public String [] getStrings(String name) { 834 String [] value = null; 835 Object object = parameters.get(convert(name)); 836 837 if (object != null) { 838 if (object instanceof String []) { 839 value = (String []) object; 840 } 841 } 842 843 return value; 844 } 845 846 854 public String [] getStrings(String name, String [] defaultValue) { 855 String [] value = getStrings(name); 856 857 if (value == null || value.length == 0) { 858 return defaultValue; 859 } else { 860 return value; 861 } 862 } 863 864 873 public void setStrings(String name, String [] values) { 874 if (values != null) { 875 parameters.put(convert(name), values); 876 } 877 } 878 879 886 public Object getObject(String name) { 887 try { 888 Object value = null; 889 Object object = parameters.get(convert(name)); 890 891 if (object != null) { 892 value = ((Object []) object)[0]; 893 } 894 895 return value; 896 } catch (ClassCastException e) { 897 return null; 898 } 899 } 900 901 908 public Object [] getObjects(String name) { 909 try { 910 return (Object []) parameters.get(convert(name)); 911 } catch (ClassCastException e) { 912 return null; 913 } 914 } 915 916 924 public void setProperties(Object bean) 925 throws Exception { 926 Class beanClass = bean.getClass(); 927 PropertyDescriptor [] props = Introspector.getBeanInfo(beanClass).getPropertyDescriptors(); 928 929 for (int i = 0; i < props.length; i++) { 930 String propname = props[i].getName(); 931 Method setter = props[i].getWriteMethod(); 932 933 if (setter != null && (containsKey(propname))) { 934 setProperty(bean, props[i]); 935 } 936 } 937 } 938 939 945 public String toString() { 946 StringBuffer sb = new StringBuffer (); 947 948 for (Enumeration e = parameters.keys(); e.hasMoreElements();) { 949 String name = (String ) e.nextElement(); 950 951 try { 952 sb.append("{"); 953 sb.append(name); 954 sb.append("="); 955 956 String [] params = this.getStrings(name); 957 958 if (params.length <= 1) { 959 sb.append(params[0]); 960 } else { 961 for (int i = 0; i < params.length; i++) { 962 if (i != 0) { 963 sb.append(", "); 964 } 965 966 sb.append('[').append(params[i]).append(']'); 967 } 968 } 969 970 sb.append("}\n"); 971 } catch (Exception ee) { 972 try { 973 sb.append("{"); 974 sb.append(name); 975 sb.append("="); 976 sb.append("ERROR?"); 977 sb.append("}\n"); 978 } catch (Exception eee) { 979 } 980 } 981 } 982 983 return sb.toString(); 984 } 985 986 997 private void setProperty(Object bean, PropertyDescriptor prop) 998 throws Exception { 999 if (prop instanceof IndexedPropertyDescriptor ) { 1000 throw new Exception (prop.getName() + 1001 " is an indexed property (not supported)"); 1002 } 1003 1004 Method setter = prop.getWriteMethod(); 1005 1006 if (setter == null) { 1007 throw new Exception (prop.getName() + " is a read only property"); 1008 } 1009 1010 Class propclass = prop.getPropertyType(); 1011 Object [] args = {null}; 1012 1013 if (propclass == String .class) { 1014 args[0] = getString(prop.getName()); 1015 } else if (propclass == Integer .class || propclass == Integer.TYPE) { 1016 args[0] = getInteger(prop.getName()); 1017 } else if (propclass == Long .class || propclass == Long.TYPE) { 1018 args[0] = new Long (getLong(prop.getName())); 1019 } else if (propclass == Boolean .class || propclass == Boolean.TYPE) { 1020 args[0] = getBool(prop.getName()); 1021 } else if (propclass == Double .class || propclass == Double.TYPE) { 1022 args[0] = new Double (getDouble(prop.getName())); 1023 } else if (propclass == String [].class) { 1024 args[0] = getStrings(prop.getName()); 1025 } else if (propclass == Object .class) { 1026 args[0] = getObject(prop.getName()); 1027 } else if (propclass == int[].class) { 1028 args[0] = getInts(prop.getName()); 1029 } else if (propclass == Integer [].class) { 1030 args[0] = getIntegers(prop.getName()); 1031 } else { 1032 throw new Exception ("property " + prop.getName() + 1033 " is of unsupported type " + 1034 propclass.toString()); 1035 } 1036 1037 setter.invoke(bean, args); 1038 } 1039} | Popular Tags |