1 19 package org.java.plugin.util; 20 21 import java.util.Iterator ; 22 import java.util.Properties ; 23 24 29 public class ExtendedProperties extends Properties { 30 private static final long serialVersionUID = 8904709563073950956L; 31 32 35 public ExtendedProperties() { 36 super(); 37 } 38 39 42 public ExtendedProperties(Properties defs) { 43 super(defs); 44 } 45 46 66 public String getProperty(String key) { 67 String result = super.getProperty(key); 68 return (result == null) ? null : expandValue(result); 69 } 70 71 74 public String getProperty(String key, String defaultValue) { 75 String result = getProperty(key); 76 return (result == null) ? expandValue(defaultValue) : result; 77 } 78 79 84 public ExtendedProperties getSubset(final String prefix) { 85 return getSubset(prefix, ""); } 87 88 94 public ExtendedProperties getSubset(final String prefix, 95 final String newPrefix) { 96 ExtendedProperties result = new ExtendedProperties(); 97 for (Iterator it = keySet().iterator(); it.hasNext();) { 98 String key = it.next().toString(); 99 if (!key.startsWith(prefix) || key.equals(prefix)) { 100 continue; 101 } 102 result.put(key.substring(prefix.length()) + newPrefix, 103 getProperty(key)); 104 } 105 return result; 106 } 107 108 private String expandValue(final String value) { 109 if ((value == null) || (value.length() < 4)) { 110 return value; 111 } 112 StringBuffer result = new StringBuffer (value.length()); 113 result.append(value); 114 int p1 = result.indexOf("${"); int p2 = result.indexOf("}", p1 + 2); while ((p1 >= 0) && (p2 > p1)) { 117 String paramName = result.substring(p1 + 2, p2); 118 String paramValue = getProperty(paramName); 119 if (paramValue != null) { 120 result.replace(p1, p2 + 1, paramValue); 121 p1 += paramValue.length(); 122 } else { 123 p1 = p2 + 1; 124 } 125 p1 = result.indexOf("${", p1); p2 = result.indexOf("}", p1 + 2); } 128 return result.toString(); 129 } 130 } 131 | Popular Tags |