1 package org.apache.velocity.runtime.configuration; 2 3 58 59 import java.io.IOException ; 60 import java.io.File ; 61 import java.io.FileInputStream ; 62 import java.io.InputStream ; 63 import java.io.InputStreamReader ; 64 import java.io.LineNumberReader ; 65 import java.io.OutputStream ; 66 import java.io.PrintWriter ; 67 import java.io.Reader ; 68 69 import java.util.ArrayList ; 70 import java.util.Enumeration ; 71 import java.util.Hashtable ; 72 import java.util.Iterator ; 73 import java.util.NoSuchElementException ; 74 import java.util.Properties ; 75 import java.util.StringTokenizer ; 76 import java.util.Vector ; 77 78 import org.apache.commons.collections.ExtendedProperties; 79 80 174 public class Configuration extends Hashtable 175 { 176 private ExtendedProperties deprecationCrutch = new ExtendedProperties(); 183 184 185 188 private Configuration defaults; 189 190 196 protected String file; 197 198 202 protected String basePath; 203 204 207 protected String fileSeparator = System.getProperty("file.separator"); 208 209 212 protected boolean isInitialized = false; 213 214 218 protected static String include = "include"; 219 220 226 protected ArrayList keysAsListed = new ArrayList (); 227 228 234 class PropertiesReader extends LineNumberReader 235 { 236 241 public PropertiesReader(Reader reader) 242 { 243 super(reader); 244 } 245 246 252 public String readProperty() throws IOException 253 { 254 StringBuffer buffer = new StringBuffer (); 255 256 try 257 { 258 while (true) 259 { 260 String line = readLine().trim(); 261 if ((line.length() != 0) && (line.charAt(0) != '#')) 262 { 263 if (line.endsWith("\\")) 264 { 265 line = line.substring(0, line.length() - 1); 266 buffer.append(line); 267 } 268 else 269 { 270 buffer.append(line); 271 break; 272 } 273 } 274 } 275 } 276 catch (NullPointerException e) 277 { 278 return null; 279 } 280 281 return buffer.toString(); 282 } 283 } 284 285 290 class PropertiesTokenizer extends StringTokenizer 291 { 292 295 static final String DELIMITER = ","; 296 297 302 public PropertiesTokenizer(String string) 303 { 304 super(string, DELIMITER); 305 } 306 307 312 public boolean hasMoreTokens() 313 { 314 return super.hasMoreTokens(); 315 } 316 317 322 public String nextToken() 323 { 324 StringBuffer buffer = new StringBuffer (); 325 326 while (hasMoreTokens()) 327 { 328 String token = super.nextToken(); 329 if (token.endsWith("\\")) 330 { 331 buffer.append(token.substring(0, token.length() - 1)); 332 buffer.append(DELIMITER); 333 } 334 else 335 { 336 buffer.append(token); 337 break; 338 } 339 } 340 341 return buffer.toString().trim(); 342 } 343 } 344 345 348 public Configuration () 349 { 350 super(); 351 } 352 353 360 public Configuration (String file) throws IOException 361 { 362 this(file,null); 363 } 364 365 372 public Configuration (String file, String defaultFile) 373 throws IOException 374 { 375 this.file = file; 376 377 basePath = new File (file).getAbsolutePath(); 378 basePath = basePath.substring(0, basePath.lastIndexOf(fileSeparator) + 1); 379 380 this.load(new FileInputStream (file)); 381 382 if (defaultFile != null) 383 { 384 defaults = new Configuration(defaultFile); 385 } 386 } 387 388 394 private void init( Configuration exp ) throws IOException 395 { 396 isInitialized = true; 397 } 398 399 403 public boolean isInitialized() 404 { 405 return isInitialized; 406 } 407 408 414 public String getInclude() 415 { 416 return Configuration.include; 417 } 418 419 425 public void setInclude(String inc) 426 { 427 Configuration.include = inc; 428 } 429 430 436 public synchronized void load(InputStream input) 437 throws IOException 438 { 439 PropertiesReader reader = 440 new PropertiesReader(new InputStreamReader (input)); 441 442 try 443 { 444 while (true) 445 { 446 String line = reader.readProperty(); 447 int equalSign = line.indexOf('='); 448 449 if (equalSign > 0) 450 { 451 String key = line.substring(0, equalSign).trim(); 452 String value = line.substring(equalSign + 1).trim(); 453 454 458 if ("".equals(value)) 459 continue; 460 461 if (getInclude() != null && 462 key.equalsIgnoreCase(getInclude())) 463 { 464 467 File file = null; 468 469 if (value.startsWith(fileSeparator)) 470 { 471 475 file = new File (value); 476 } 477 else 478 { 479 485 if (value.startsWith("." + fileSeparator)) 486 { 487 value = value.substring(2); 488 } 489 490 file = new File (basePath + value); 491 } 492 493 if (file != null && file.exists() && file.canRead()) 494 { 495 load ( new FileInputStream (file)); 496 } 497 } 498 else 499 { 500 addProperty(key,value); 501 } 503 } 504 } 505 } 506 catch (NullPointerException e) 507 { 508 511 return; 512 } 513 } 514 515 522 public Object getProperty( String key) 523 { 524 527 Object o = this.get(key); 528 529 if ( o == null) 530 { 531 535 if (defaults != null) 536 { 537 o = defaults.get(key); 538 } 539 } 540 541 return o; 542 } 543 544 563 public void addProperty(String key, Object token) 565 { 566 567 deprecationCrutch.addProperty( key, token ); 570 571 Object o = this.get(key); 572 573 585 586 if (o instanceof String ) 587 { 588 Vector v = new Vector (2); 589 v.addElement(o); 590 v.addElement(token); 591 put(key, v); 592 } 593 else if (o instanceof Vector ) 594 { 595 ((Vector ) o).addElement(token); 596 } 597 else 598 { 599 613 if (token instanceof String && 614 ((String )token).indexOf(PropertiesTokenizer.DELIMITER) > 0) 615 { 616 PropertiesTokenizer tokenizer = 617 new PropertiesTokenizer((String )token); 618 619 while (tokenizer.hasMoreTokens()) 620 { 621 String value = tokenizer.nextToken(); 622 623 628 addStringProperty(key,value); 629 } 630 } 631 else 632 { 633 642 643 646 647 if( !containsKey( key ) ) 648 { 649 keysAsListed.add(key); 650 } 651 652 655 put(key, token); 656 } 657 } 658 } 659 660 661 670 private void addStringProperty(String key, String token) 671 { 672 Object o = this.get(key); 673 674 686 687 692 693 if (o instanceof String ) 694 { 695 Vector v = new Vector (2); 696 v.addElement(o); 697 v.addElement(token); 698 put(key, v); 699 } 700 else if (o instanceof Vector ) 701 { 702 ((Vector ) o).addElement(token); 703 } 704 else 705 { 706 if( !containsKey( key ) ) 707 { 708 keysAsListed.add(key); 709 } 710 711 put( key, token); 712 } 713 } 714 715 723 public void setProperty(String key, Object value) 724 { 725 clearProperty(key); 726 addProperty(key,value); 727 } 728 729 736 public synchronized void save(OutputStream output, 737 String Header) 738 throws IOException 739 { 740 if(output != null) 741 { 742 PrintWriter theWrtr = new PrintWriter (output); 743 if(Header != null) 744 { 745 theWrtr.println(Header); 746 } 747 Enumeration theKeys = keys(); 748 while(theKeys.hasMoreElements()) 749 { 750 String key = (String ) theKeys.nextElement(); 751 Object value = get((Object ) key); 752 if(value != null) 753 { 754 if(value instanceof String ) 755 { 756 StringBuffer currentOutput = new StringBuffer (); 757 currentOutput.append(key); 758 currentOutput.append("="); 759 currentOutput.append((String ) value); 760 theWrtr.println(currentOutput.toString()); 761 } 762 else if(value instanceof Vector ) 763 { 764 Vector values = (Vector ) value; 765 Enumeration valuesEnum = values.elements(); 766 while(valuesEnum.hasMoreElements()) 767 { 768 String currentElement = 769 (String ) valuesEnum.nextElement(); 770 StringBuffer currentOutput = new StringBuffer (); 771 currentOutput.append(key); 772 currentOutput.append("="); 773 currentOutput.append(currentElement); 774 theWrtr.println(currentOutput.toString()); 775 } 776 } 777 } 778 theWrtr.println(); 779 theWrtr.flush(); 780 } 781 } 782 } 783 784 791 public void combine (Configuration c) 792 { 793 for (Iterator i = c.getKeys() ; i.hasNext() ;) 794 { 795 String key = (String ) i.next(); 796 setProperty( key, c.get(key) ); 798 } 799 } 800 801 806 public void clearProperty(String key) 807 { 808 deprecationCrutch.clearProperty( key ); 811 812 if (containsKey(key)) 813 { 814 818 819 for(int i = 0; i < keysAsListed.size(); i++) 820 { 821 if ( ( (String ) keysAsListed.get(i)).equals( key ) ) 822 { 823 keysAsListed.remove(i); 824 break; 825 } 826 } 827 828 remove(key); 829 } 830 } 831 832 838 public Iterator getKeys() 839 { 840 return keysAsListed.iterator(); 841 } 842 843 850 public Iterator getKeys(String prefix) 851 { 852 Iterator keys = getKeys(); 853 ArrayList matchingKeys = new ArrayList (); 854 855 while( keys.hasNext() ) 856 { 857 Object key = keys.next(); 858 859 if( key instanceof String && ((String ) key).startsWith(prefix) ) 860 { 861 matchingKeys.add(key); 862 } 863 } 864 return matchingKeys.iterator(); 865 } 866 867 874 public Configuration subset(String prefix) 875 { 876 Configuration c = new Configuration(); 877 Iterator keys = getKeys(); 878 boolean validSubset = false; 879 880 while( keys.hasNext() ) 881 { 882 Object key = keys.next(); 883 884 if( key instanceof String && ((String ) key).startsWith(prefix) ) 885 { 886 if (!validSubset) 887 { 888 validSubset = true; 889 } 890 891 String newKey = null; 892 893 899 if ( ((String )key).length() == prefix.length()) 900 { 901 newKey = prefix; 902 } 903 else 904 { 905 newKey = ((String )key).substring(prefix.length() + 1); 906 } 907 908 915 916 c.setProperty(newKey, get(key)); 917 } 918 } 919 920 if (validSubset) 921 { 922 return c; 923 } 924 else 925 { 926 return null; 927 } 928 } 929 930 934 public void display() 935 { 936 Iterator i = getKeys(); 937 938 while (i.hasNext()) 939 { 940 String key = (String ) i.next(); 941 Object value = get(key); 942 System.out.println(key + " => " + value); 943 } 944 } 945 946 954 public String getString(String key) 955 { 956 return getString(key, null); 957 } 958 959 969 public String getString(String key, 970 String defaultValue) 971 { 972 Object value = get(key); 973 974 if (value instanceof String ) 975 { 976 return (String ) value; 977 } 978 else if (value == null) 979 { 980 if (defaults != null) 981 { 982 return defaults.getString(key, defaultValue); 983 } 984 else 985 { 986 return defaultValue; 987 } 988 } 989 else if (value instanceof Vector ) 990 { 991 return (String ) ((Vector ) value).get(0); 992 } 993 else 994 { 995 throw new ClassCastException ( 996 '\'' + key + "' doesn't map to a String object"); 997 } 998 } 999 1000 1011 public Properties getProperties(String key) 1012 { 1013 return getProperties(key, new Properties ()); 1014 } 1015 1016 1027 public Properties getProperties(String key, 1028 Properties defaults) 1029 { 1030 1033 String [] tokens = getStringArray(key); 1034 1035 1038 Properties props = new Properties (defaults); 1039 for (int i = 0; i < tokens.length; i++) 1040 { 1041 String token = tokens[i]; 1042 int equalSign = token.indexOf('='); 1043 if (equalSign > 0) 1044 { 1045 String pkey = token.substring(0, equalSign).trim(); 1046 String pvalue = token.substring(equalSign + 1).trim(); 1047 props.put(pkey, pvalue); 1048 } 1049 else 1050 { 1051 throw new IllegalArgumentException ('\'' + token + 1052 "' does not contain " + 1053 "an equals sign"); 1054 } 1055 } 1056 return props; 1057 } 1058 1059 1068 public String [] getStringArray(String key) 1069 { 1070 Object value = get(key); 1071 1072 Vector vector; 1074 if (value instanceof String ) 1075 { 1076 vector = new Vector (1); 1077 vector.addElement(value); 1078 } 1079 else if (value instanceof Vector ) 1080 { 1081 vector = (Vector )value; 1082 } 1083 else if (value == null) 1084 { 1085 if (defaults != null) 1086 { 1087 return defaults.getStringArray(key); 1088 } 1089 else 1090 { 1091 return new String [0]; 1092 } 1093 } 1094 else 1095 { 1096 throw new ClassCastException ( 1097 '\'' + key + "' doesn't map to a String/Vector object"); 1098 } 1099 1100 String [] tokens = new String [vector.size()]; 1101 for (int i = 0; i < tokens.length; i++) 1102 { 1103 tokens[i] = (String )vector.elementAt(i); 1104 } 1105 1106 return tokens; 1107 } 1108 1109 1118 public Vector getVector(String key) 1119 { 1120 return getVector(key, null); 1121 } 1122 1123 1133 public Vector getVector(String key, 1134 Vector defaultValue) 1135 { 1136 Object value = get(key); 1137 1138 if (value instanceof Vector ) 1139 { 1140 return (Vector ) value; 1141 } 1142 else if (value instanceof String ) 1143 { 1144 Vector v = new Vector (1); 1145 v.addElement((String ) value); 1146 put(key, v); 1147 return v; 1148 } 1149 else if (value == null) 1150 { 1151 if (defaults != null) 1152 { 1153 return defaults.getVector(key, defaultValue); 1154 } 1155 else 1156 { 1157 return ((defaultValue == null) ? 1158 new Vector () : defaultValue); 1159 } 1160 } 1161 else 1162 { 1163 throw new ClassCastException ( 1164 '\'' + key + "' doesn't map to a Vector object"); 1165 } 1166 } 1167 1168 1178 public boolean getBoolean(String key) 1179 { 1180 Boolean b = getBoolean(key, (Boolean ) null); 1181 if (b != null) 1182 { 1183 return b.booleanValue(); 1184 } 1185 else 1186 { 1187 throw new NoSuchElementException ( 1188 '\'' + key + "' doesn't map to an existing object"); 1189 } 1190 } 1191 1192 1201 public boolean getBoolean(String key, boolean defaultValue) 1202 { 1203 return getBoolean(key, new Boolean (defaultValue)).booleanValue(); 1204 } 1205 1206 1216 public Boolean getBoolean(String key, Boolean defaultValue) 1217 { 1218 1219 Object value = get(key); 1220 1221 if (value instanceof Boolean ) 1222 { 1223 return (Boolean ) value; 1224 } 1225 else if (value instanceof String ) 1226 { 1227 String s = testBoolean((String )value); 1228 Boolean b = new Boolean (s); 1229 put(key, b); 1230 return b; 1231 } 1232 else if (value == null) 1233 { 1234 if (defaults != null) 1235 { 1236 return defaults.getBoolean(key, defaultValue); 1237 } 1238 else 1239 { 1240 return defaultValue; 1241 } 1242 } 1243 else 1244 { 1245 throw new ClassCastException ( 1246 '\'' + key + "' doesn't map to a Boolean object"); 1247 } 1248 } 1249 1250 1262 public String testBoolean(String value) 1263 { 1264 String s = ((String )value).toLowerCase(); 1265 1266 if (s.equals("true") || s.equals("on") || s.equals("yes")) 1267 { 1268 return "true"; 1269 } 1270 else if (s.equals("false") || s.equals("off") || s.equals("no")) 1271 { 1272 return "false"; 1273 } 1274 else 1275 { 1276 return null; 1277 } 1278 } 1279 1280 1292 public byte getByte(String key) 1293 { 1294 Byte b = getByte(key, null); 1295 if (b != null) 1296 { 1297 return b.byteValue(); 1298 } 1299 else 1300 { 1301 throw new NoSuchElementException ( 1302 '\'' + key + " doesn't map to an existing object"); 1303 } 1304 } 1305 1306 1317 public byte getByte(String key, 1318 byte defaultValue) 1319 { 1320 return getByte(key, new Byte (defaultValue)).byteValue(); 1321 } 1322 1323 1335 public Byte getByte(String key, 1336 Byte defaultValue) 1337 { 1338 Object value = get(key); 1339 1340 if (value instanceof Byte ) 1341 { 1342 return (Byte ) value; 1343 } 1344 else if (value instanceof String ) 1345 { 1346 Byte b = new Byte ((String ) value); 1347 put(key, b); 1348 return b; 1349 } 1350 else if (value == null) 1351 { 1352 if (defaults != null) 1353 { 1354 return defaults.getByte(key, defaultValue); 1355 } 1356 else 1357 { 1358 return defaultValue; 1359 } 1360 } 1361 else 1362 { 1363 throw new ClassCastException ( 1364 '\'' + key + "' doesn't map to a Byte object"); 1365 } 1366 } 1367 1368 1380 public short getShort(String key) 1381 { 1382 Short s = getShort(key, null); 1383 if (s != null) 1384 { 1385 return s.shortValue(); 1386 } 1387 else 1388 { 1389 throw new NoSuchElementException ( 1390 '\'' + key + "' doesn't map to an existing object"); 1391 } 1392 } 1393 1394 1405 public short getShort(String key, 1406 short defaultValue) 1407 { 1408 return getShort(key, new Short (defaultValue)).shortValue(); 1409 } 1410 1411 1423 public Short getShort(String key, 1424 Short defaultValue) 1425 { 1426 Object value = get(key); 1427 1428 if (value instanceof Short ) 1429 { 1430 return (Short ) value; 1431 } 1432 else if (value instanceof String ) 1433 { 1434 Short s = new Short ((String ) value); 1435 put(key, s); 1436 return s; 1437 } 1438 else if (value == null) 1439 { 1440 if (defaults != null) 1441 { 1442 return defaults.getShort(key, defaultValue); 1443 } 1444 else 1445 { 1446 return defaultValue; 1447 } 1448 } 1449 else 1450 { 1451 throw new ClassCastException ( 1452 '\'' + key + "' doesn't map to a Short object"); 1453 } 1454 } 1455 1456 1463 public int getInt(String name) 1464 { 1465 return getInteger(name); 1466 } 1467 1468 1476 public int getInt(String name, 1477 int def) 1478 { 1479 return getInteger(name, def); 1480 } 1481 1482 1494 public int getInteger(String key) 1495 { 1496 Integer i = getInteger(key, null); 1497 if (i != null) 1498 { 1499 return i.intValue(); 1500 } 1501 else 1502 { 1503 throw new NoSuchElementException ( 1504 '\'' + key + "' doesn't map to an existing object"); 1505 } 1506 } 1507 1508 1519 public int getInteger(String key, 1520 int defaultValue) 1521 { 1522 Integer i = getInteger(key, null); 1523 1524 if (i == null) 1525 { 1526 return defaultValue; 1527 } 1528 1529 return i.intValue(); 1530 } 1531 1532 1533 1545 public Integer getInteger(String key, 1546 Integer defaultValue) 1547 { 1548 Object value = get(key); 1549 1550 if (value instanceof Integer ) 1551 { 1552 return (Integer ) value; 1553 } 1554 else if (value instanceof String ) 1555 { 1556 Integer i = new Integer ((String ) value); 1557 put(key, i); 1558 return i; 1559 } 1560 else if (value == null) 1561 { 1562 if (defaults != null) 1563 { 1564 return defaults.getInteger(key, defaultValue); 1565 } 1566 else 1567 { 1568 return defaultValue; 1569 } 1570 } 1571 else 1572 { 1573 throw new ClassCastException ( 1574 '\'' + key + "' doesn't map to a Integer object"); 1575 } 1576 } 1577 1578 1590 public long getLong(String key) 1591 { 1592 Long l = getLong(key, null); 1593 if (l != null) 1594 { 1595 return l.longValue(); 1596 } 1597 else 1598 { 1599 throw new NoSuchElementException ( 1600 '\'' + key + "' doesn't map to an existing object"); 1601 } 1602 } 1603 1604 1615 public long getLong(String key, 1616 long defaultValue) 1617 { 1618 return getLong(key, new Long (defaultValue)).longValue(); 1619 } 1620 1621 1633 public Long getLong(String key, 1634 Long defaultValue) 1635 { 1636 Object value = get(key); 1637 1638 if (value instanceof Long ) 1639 { 1640 return (Long ) value; 1641 } 1642 else if (value instanceof String ) 1643 { 1644 Long l = new Long ((String ) value); 1645 put(key, l); 1646 return l; 1647 } 1648 else if (value == null) 1649 { 1650 if (defaults != null) 1651 { 1652 return defaults.getLong(key, defaultValue); 1653 } 1654 else 1655 { 1656 return defaultValue; 1657 } 1658 } 1659 else 1660 { 1661 throw new ClassCastException ( 1662 '\'' + key + "' doesn't map to a Long object"); 1663 } 1664 } 1665 1666 1678 public float getFloat(String key) 1679 { 1680 Float f = getFloat(key, null); 1681 if (f != null) 1682 { 1683 return f.floatValue(); 1684 } 1685 else 1686 { 1687 throw new NoSuchElementException ( 1688 '\'' + key + "' doesn't map to an existing object"); 1689 } 1690 } 1691 1692 1703 public float getFloat(String key, 1704 float defaultValue) 1705 { 1706 return getFloat(key, new Float (defaultValue)).floatValue(); 1707 } 1708 1709 1721 public Float getFloat(String key, 1722 Float defaultValue) 1723 { 1724 Object value = get(key); 1725 1726 if (value instanceof Float ) 1727 { 1728 return (Float ) value; 1729 } 1730 else if (value instanceof String ) 1731 { 1732 Float f = new Float ((String ) value); 1733 put(key, f); 1734 return f; 1735 } 1736 else if (value == null) 1737 { 1738 if (defaults != null) 1739 { 1740 return defaults.getFloat(key, defaultValue); 1741 } 1742 else 1743 { 1744 return defaultValue; 1745 } 1746 } 1747 else 1748 { 1749 throw new ClassCastException ( 1750 '\'' + key + "' doesn't map to a Float object"); 1751 } 1752 } 1753 1754 1766 public double getDouble(String key) 1767 { 1768 Double d = getDouble(key, null); 1769 if (d != null) 1770 { 1771 return d.doubleValue(); 1772 } 1773 else 1774 { 1775 throw new NoSuchElementException ( 1776 '\'' + key + "' doesn't map to an existing object"); 1777 } 1778 } 1779 1780 1791 public double getDouble(String key, 1792 double defaultValue) 1793 { 1794 return getDouble(key, new Double (defaultValue)).doubleValue(); 1795 } 1796 1797 1809 public Double getDouble(String key, 1810 Double defaultValue) 1811 { 1812 Object value = get(key); 1813 1814 if (value instanceof Double ) 1815 { 1816 return (Double ) value; 1817 } 1818 else if (value instanceof String ) 1819 { 1820 Double d = new Double ((String ) value); 1821 put(key, d); 1822 return d; 1823 } 1824 else if (value == null) 1825 { 1826 if (defaults != null) 1827 { 1828 return defaults.getDouble(key, defaultValue); 1829 } 1830 else 1831 { 1832 return defaultValue; 1833 } 1834 } 1835 else 1836 { 1837 throw new ClassCastException ( 1838 '\'' + key + "' doesn't map to a Double object"); 1839 } 1840 } 1841 1842 1852 public static Configuration convertProperties(Properties p) 1853 { 1854 Configuration c = new Configuration(); 1855 1856 for (Enumeration e = p.keys(); e.hasMoreElements() ; ) 1857 { 1858 String s = (String ) e.nextElement(); 1859 c.setProperty(s, p.getProperty(s)); 1860 } 1861 1862 return c; 1863 } 1864 1865 1879 public ExtendedProperties getExtendedProperties() 1880 { 1881 return deprecationCrutch; 1882 } 1883 1884 1885} 1886 | Popular Tags |