| 1 package org.apache.turbine.util.parser; 2 3 18 19 import java.beans.IndexedPropertyDescriptor ; 20 import java.beans.Introspector ; 21 import java.beans.PropertyDescriptor ; 22 23 import java.io.UnsupportedEncodingException ; 24 25 import java.lang.reflect.Method ; 26 27 import java.math.BigDecimal ; 28 29 import java.text.DateFormat ; 30 import java.text.ParseException ; 31 32 import java.util.Calendar ; 33 import java.util.Collections ; 34 import java.util.Date ; 35 import java.util.Enumeration ; 36 import java.util.GregorianCalendar ; 37 import java.util.HashMap ; 38 import java.util.Iterator ; 39 import java.util.Set ; 40 import java.util.Map ; 41 42 import org.apache.commons.lang.StringUtils; 43 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 47 import org.apache.torque.om.NumberKey; 48 import org.apache.torque.om.StringKey; 49 50 import org.apache.turbine.util.DateSelector; 51 import org.apache.turbine.util.TimeSelector; 52 import org.apache.turbine.util.pool.Recyclable; 53 import org.apache.turbine.util.pool.RecyclableSupport; 54 55 86 public class BaseValueParser 87 extends RecyclableSupport 88 implements ValueParser, Recyclable 89 { 90 91 private static Log log = LogFactory.getLog(BaseValueParser.class); 92 93 97 private Map parameters = new HashMap (); 98 99 100 private String characterEncoding = "US-ASCII"; 101 102 112 public static String convertAndTrim(String value) 113 { 114 return ParserUtils.convertAndTrim(value); 115 } 116 117 120 public BaseValueParser() 121 { 122 super(); 123 } 124 125 128 public BaseValueParser(String characterEncoding) 129 { 130 super(); 131 recycle(characterEncoding); 132 } 133 134 137 public void recycle() 138 { 139 recycle("US-ASCII"); 140 } 141 142 147 public void recycle(String characterEncoding) 148 { 149 setCharacterEncoding(characterEncoding); 150 if (!isDisposed()) 151 { 152 super.recycle(); 153 } 154 } 155 156 159 public void dispose() 160 { 161 clear(); 162 super.dispose(); 163 } 164 165 168 public void clear() 169 { 170 parameters.clear(); 171 } 172 173 176 public void setCharacterEncoding(String s) 177 { 178 characterEncoding = s; 179 } 180 181 184 public String getCharacterEncoding() 185 { 186 return characterEncoding; 187 } 188 189 195 public void add(String name, double value) 196 { 197 add(name, Double.toString(value)); 198 } 199 200 206 public void add(String name, int value) 207 { 208 add(name, Integer.toString(value)); 209 } 210 211 217 public void add(String name, Integer value) 218 { 219 add(name, value.toString()); 220 } 221 222 228 public void add(String name, long value) 229 { 230 add(name, Long.toString(value)); 231 } 232 233 239 public void add(String name, String value) 240 { 241 append(name, value); 242 } 243 244 252 public void add(String name, String [] value) 253 { 254 for (int i = 0 ; i < value.length; i++) 255 { 256 add(name, value[i]); 257 } 258 } 259 260 268 public void append(String name, String value) 269 { 270 String [] items = this.getStrings(name); 271 if (items == null) 272 { 273 items = new String [1]; 274 items[0] = value; 275 parameters.put(convert(name), items); 276 } 277 else 278 { 279 String [] newItems = new String [items.length + 1]; 280 System.arraycopy(items, 0, newItems, 0, items.length); 281 newItems[items.length] = value; 282 parameters.put(convert(name), newItems); 283 } 284 } 285 286 293 public Object remove(String name) 294 { 295 return parameters.remove(convert(name)); 296 } 297 298 306 public String convert(String value) 307 { 308 return ParserUtils.convertAndTrim(value); 309 } 310 311 319 public boolean containsKey(Object key) 320 { 321 return parameters.containsKey(convert((String )key)); 322 } 323 324 331 public boolean containsDateSelectorKeys(String key) 332 { 333 return (containsKey(key + DateSelector.DAY_SUFFIX) && 334 containsKey(key + DateSelector.MONTH_SUFFIX) && 335 containsKey(key + DateSelector.YEAR_SUFFIX)); 336 } 337 338 345 public boolean containsTimeSelectorKeys(String key) 346 { 347 return (containsKey(key + TimeSelector.HOUR_SUFFIX) && 348 containsKey(key + TimeSelector.MINUTE_SUFFIX) && 349 containsKey(key + TimeSelector.SECOND_SUFFIX)); 350 } 351 352 358 public Enumeration keys() 359 { 360 return Collections.enumeration(parameters.keySet()); 361 } 362 363 368 public Set keySet() 369 { 370 return parameters.keySet(); 371 } 372 373 378 public Object [] getKeys() 379 { 380 return parameters.keySet().toArray(); 381 } 382 383 391 public boolean getBoolean(String name, boolean defaultValue) 392 { 393 Boolean result = getBooleanObject(name); 394 return (result == null ? defaultValue : result.booleanValue()); 395 } 396 397 404 public boolean getBoolean(String name) 405 { 406 return getBoolean(name, false); 407 } 408 409 421 public Boolean getBooleanObject(String name) 422 { 423 Boolean result = null; 424 String value = getString(name); 425 if (StringUtils.isNotEmpty(value)) 426 { 427 if (value.equals("1") || 428 value.equalsIgnoreCase("true") || 429 value.equalsIgnoreCase("yes") || 430 value.equalsIgnoreCase("on")) 431 { 432 result = Boolean.TRUE; 433 } 434 else if (value.equals("0") || 435 value.equalsIgnoreCase("false") || 436 value.equalsIgnoreCase("no") || 437 value.equalsIgnoreCase("off")) 438 { 439 result = Boolean.FALSE; 440 } 441 else 442 { 443 logConvertionFailure(name, value, "Boolean"); 444 } 445 } 446 return result; 447 } 448 449 462 public Boolean getBooleanObject(String name, Boolean defaultValue) 463 { 464 Boolean result = getBooleanObject(name); 465 return (result==null ? defaultValue : result); 466 } 467 468 477 public Boolean getBool(String name, boolean defaultValue) 478 { 479 return getBooleanObject(name, new Boolean (defaultValue)); 480 } 481 482 490 public Boolean getBool(String name) 491 { 492 return getBooleanObject(name, Boolean.FALSE); 493 } 494 495 503 public double getDouble(String name, double defaultValue) 504 { 505 double result = defaultValue; 506 String value = getString(name); 507 if (StringUtils.isNotEmpty(value)) 508 { 509 try 510 { 511 result = Double.valueOf(value).doubleValue(); 512 } 513 catch (NumberFormatException e) 514 { 515 logConvertionFailure(name, value, "Double"); 516 } 517 } 518 return result; 519 } 520 521 528 public double getDouble(String name) 529 { 530 return getDouble(name, 0.0); 531 } 532 533 540 public double[] getDoubles(String name) 541 { 542 double[] result = null; 543 String value[] = getStrings(name); 544 if (value != null) 545 { 546 result = new double[value.length]; 547 for (int i = 0; i < value.length; i++) 548 { 549 if (StringUtils.isNotEmpty(value[i])) 550 { 551 try 552 { 553 result[i] = Double.parseDouble(value[i]); 554 } 555 catch (NumberFormatException e) 556 { 557 logConvertionFailure(name, value[i], "Double"); 558 } 559 } 560 } 561 } 562 return result; 563 } 564 565 573 public Double getDoubleObject(String name, Double defaultValue) 574 { 575 Double result = getDoubleObject(name); 576 return (result==null ? defaultValue : result); 577 } 578 579 586 public Double getDoubleObject(String name) 587 { 588 Double result = null; 589 String value = getString(name); 590 if (StringUtils.isNotEmpty(value)) 591 { 592 try 593 { 594 result = new Double (value); 595 } 596 catch(NumberFormatException e) 597 { 598 logConvertionFailure(name, value, "Double"); 599 } 600 } 601 return result; 602 } 603 604 611 public Double [] getDoubleObjects(String name) 612 { 613 Double [] result = null; 614 String value[] = getStrings(convert(name)); 615 if (value != null) 616 { 617 result = new Double [value.length]; 618 for (int i = 0; i < value.length; i++) 619 { 620 if (StringUtils.isNotEmpty(value[i])) 621 { 622 try 623 { 624 result[i] = Double.valueOf(value[i]); 625 } 626 catch (NumberFormatException e) 627 { 628 logConvertionFailure(name, value[i], "Double"); 629 } 630 } 631 } 632 } 633 return result; 634 } 635 636 644 public float getFloat(String name, float defaultValue) 645 { 646 float result = defaultValue; 647 String value = getString(name); 648 if (StringUtils.isNotEmpty(value)) 649 { 650 try 651 { 652 result = Float.valueOf(value).floatValue(); 653 } 654 catch (NumberFormatException e) 655 { 656 logConvertionFailure(name, value, "Float"); 657 } 658 } 659 return result; 660 } 661 662 669 public float getFloat(String name) 670 { 671 return getFloat(name, 0.0f); 672 } 673 674 681 public float[] getFloats(String name) 682 { 683 float[] result = null; 684 String value[] = getStrings(name); 685 if (value != null) 686 { 687 result = new float[value.length]; 688 for (int i = 0; i < value.length; i++) 689 { 690 if (StringUtils.isNotEmpty(value[i])) 691 { 692 try 693 { 694 result[i] = Float.parseFloat(value[i]); 695 } 696 catch (NumberFormatException e) 697 { 698 logConvertionFailure(name, value[i], "Float"); 699 } 700 } 701 } 702 } 703 return result; 704 } 705 706 714 public Float getFloatObject(String name, Float defaultValue) 715 { 716 Float result = getFloatObject(name); 717 return (result==null ? defaultValue : result); 718 } 719 720 727 public Float getFloatObject(String name) 728 { 729 Float result = null; 730 String value = getString(name); 731 if (StringUtils.isNotEmpty(value)) 732 { 733 try 734 { 735 result = new Float (value); 736 } 737 catch(NumberFormatException e) 738 { 739 logConvertionFailure(name, value, "Float"); 740 } 741 } 742 return result; 743 } 744 745 752 public Float [] getFloatObjects(String name) 753 { 754 Float [] result = null; 755 String value[] = getStrings(convert(name)); 756 if (value != null) 757 { 758 result = new Float [value.length]; 759 for (int i = 0; i < value.length; i++) 760 { 761 if (StringUtils.isNotEmpty(value[i])) 762 { 763 try 764 { 765 result[i] = Float.valueOf(value[i]); 766 } 767 catch (NumberFormatException e) 768 { 769 logConvertionFailure(name, value[i], "Float"); 770 } 771 } 772 } 773 } 774 return result; 775 } 776 777 785 public BigDecimal getBigDecimal(String name, BigDecimal defaultValue) 786 { 787 BigDecimal result = defaultValue; 788 String value = getString(name); 789 if (StringUtils.isNotEmpty(value)) 790 { 791 try 792 { 793 result = new BigDecimal (value); 794 } 795 catch (NumberFormatException e) 796 { 797 logConvertionFailure(name, value, "BigDecimal"); 798 } 799 } 800 return result; 801 } 802 803 810 public BigDecimal getBigDecimal(String name) 811 { 812 return getBigDecimal(name, new BigDecimal (0.0)); 813 } 814 815 822 public BigDecimal [] getBigDecimals(String name) 823 { 824 BigDecimal [] result = null; 825 String value[] = getStrings(name); 826 if (value != null) 827 { 828 result = new BigDecimal [value.length]; 829 for (int i = 0; i < value.length; i++) 830 { 831 if(StringUtils.isNotEmpty(value[i])) 832 { 833 try 834 { 835 result[i] = new BigDecimal (value[i]); 836 } 837 catch (NumberFormatException e) 838 { 839 logConvertionFailure(name, value[i], "BigDecimal"); 840 } 841 } 842 } 843 } 844 return result; 845 } 846 847 855 public int getInt(String name, int defaultValue) 856 { 857 int result = defaultValue; 858 String value = getString(name); 859 if (StringUtils.isNotEmpty(value)) 860 { 861 try 862 { 863 result = Integer.valueOf(value).intValue(); 864 } 865 catch (NumberFormatException e) 866 { 867 logConvertionFailure(name, value, "Integer"); 868 } 869 } 870 return result; 871 } 872 873 880 public int getInt(String name) 881 { 882 return getInt(name, 0); 883 } 884 885 894 public Integer getInteger(String name, int defaultValue) 895 { 896 return getIntObject(name, new Integer (defaultValue)); 897 } 898 899 |