Method from org.apache.commons.collections.ExtendedProperties Detail: |
public void addProperty(String key,
Object value) {
if (value instanceof String) {
String str = (String) value;
if (str.indexOf(PropertiesTokenizer.DELIMITER) > 0) {
// token contains commas, so must be split apart then added
PropertiesTokenizer tokenizer = new PropertiesTokenizer(str);
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
addPropertyInternal(key, unescape(token));
}
} else {
// token contains no commas, so can be simply added
addPropertyInternal(key, unescape(str));
}
} else {
addPropertyInternal(key, value);
}
// Adding a property connotes initialization
isInitialized = true;
}
Add a property to the configuration. If it already
exists then the value stated here will be added
to the configuration entry. For example, if
resource.loader = file
is already present in the configuration and you
addProperty("resource.loader", "classpath")
Then you will end up with a Vector like the
following:
["file", "classpath"] |
public void clearProperty(String key) {
if (containsKey(key)) {
// we also need to rebuild the keysAsListed or else
// things get *very* confusing
for (int i = 0; i < keysAsListed.size(); i++) {
if (( keysAsListed.get(i)).equals(key)) {
keysAsListed.remove(i);
break;
}
}
remove(key);
}
}
Clear a property in the configuration. |
public void combine(ExtendedProperties props) {
for (Iterator it = props.getKeys(); it.hasNext();) {
String key = (String) it.next();
setProperty(key, props.get(key));
}
}
|
public static ExtendedProperties convertProperties(Properties props) {
ExtendedProperties c = new ExtendedProperties();
for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
String s = (String) e.nextElement();
c.setProperty(s, props.getProperty(s));
}
return c;
}
Convert a standard properties class into a configuration class.
NOTE: From Commons Collections 3.2 this method will pick up
any default parent Properties of the specified input object. |
public void display() {
Iterator i = getKeys();
while (i.hasNext()) {
String key = (String) i.next();
Object value = get(key);
System.out.println(key + " = > " + value);
}
}
Display the configuration for debugging purposes to System.out. |
public boolean getBoolean(String key) {
Boolean b = getBoolean(key, null);
if (b != null) {
return b.booleanValue();
} else {
throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object");
}
}
Get a boolean associated with the given configuration key. |
public boolean getBoolean(String key,
boolean defaultValue) {
return getBoolean(key, new Boolean(defaultValue)).booleanValue();
}
Get a boolean associated with the given configuration key. |
public Boolean getBoolean(String key,
Boolean defaultValue) {
Object value = get(key);
if (value instanceof Boolean) {
return (Boolean) value;
} else if (value instanceof String) {
String s = testBoolean((String) value);
Boolean b = new Boolean(s);
put(key, b);
return b;
} else if (value == null) {
if (defaults != null) {
return defaults.getBoolean(key, defaultValue);
} else {
return defaultValue;
}
} else {
throw new ClassCastException('\'' + key + "' doesn't map to a Boolean object");
}
}
Get a boolean associated with the given configuration key. |
public byte getByte(String key) {
Byte b = getByte(key, null);
if (b != null) {
return b.byteValue();
} else {
throw new NoSuchElementException('\'' + key + " doesn't map to an existing object");
}
}
Get a byte associated with the given configuration key. |
public byte getByte(String key,
byte defaultValue) {
return getByte(key, new Byte(defaultValue)).byteValue();
}
Get a byte associated with the given configuration key. |
public Byte getByte(String key,
Byte defaultValue) {
Object value = get(key);
if (value instanceof Byte) {
return (Byte) value;
} else if (value instanceof String) {
Byte b = new Byte((String) value);
put(key, b);
return b;
} else if (value == null) {
if (defaults != null) {
return defaults.getByte(key, defaultValue);
} else {
return defaultValue;
}
} else {
throw new ClassCastException('\'' + key + "' doesn't map to a Byte object");
}
}
Get a byte associated with the given configuration key. |
public double getDouble(String key) {
Double d = getDouble(key, null);
if (d != null) {
return d.doubleValue();
} else {
throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object");
}
}
Get a double associated with the given configuration key. |
public double getDouble(String key,
double defaultValue) {
return getDouble(key, new Double(defaultValue)).doubleValue();
}
Get a double associated with the given configuration key. |
public Double getDouble(String key,
Double defaultValue) {
Object value = get(key);
if (value instanceof Double) {
return (Double) value;
} else if (value instanceof String) {
Double d = new Double((String) value);
put(key, d);
return d;
} else if (value == null) {
if (defaults != null) {
return defaults.getDouble(key, defaultValue);
} else {
return defaultValue;
}
} else {
throw new ClassCastException('\'' + key + "' doesn't map to a Double object");
}
}
Get a double associated with the given configuration key. |
public float getFloat(String key) {
Float f = getFloat(key, null);
if (f != null) {
return f.floatValue();
} else {
throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object");
}
}
Get a float associated with the given configuration key. |
public float getFloat(String key,
float defaultValue) {
return getFloat(key, new Float(defaultValue)).floatValue();
}
Get a float associated with the given configuration key. |
public Float getFloat(String key,
Float defaultValue) {
Object value = get(key);
if (value instanceof Float) {
return (Float) value;
} else if (value instanceof String) {
Float f = new Float((String) value);
put(key, f);
return f;
} else if (value == null) {
if (defaults != null) {
return defaults.getFloat(key, defaultValue);
} else {
return defaultValue;
}
} else {
throw new ClassCastException('\'' + key + "' doesn't map to a Float object");
}
}
Get a float associated with the given configuration key. |
public String getInclude() {
return include;
}
Gets the property value for including other properties files.
By default it is "include". |
public int getInt(String name) {
return getInteger(name);
}
The purpose of this method is to get the configuration resource
with the given name as an integer. |
public int getInt(String name,
int def) {
return getInteger(name, def);
}
The purpose of this method is to get the configuration resource
with the given name as an integer, or a default value. |
public int getInteger(String key) {
Integer i = getInteger(key, null);
if (i != null) {
return i.intValue();
} else {
throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object");
}
}
Get a int associated with the given configuration key. |
public int getInteger(String key,
int defaultValue) {
Integer i = getInteger(key, null);
if (i == null) {
return defaultValue;
}
return i.intValue();
}
Get a int associated with the given configuration key. |
public Integer getInteger(String key,
Integer defaultValue) {
Object value = get(key);
if (value instanceof Integer) {
return (Integer) value;
} else if (value instanceof String) {
Integer i = new Integer((String) value);
put(key, i);
return i;
} else if (value == null) {
if (defaults != null) {
return defaults.getInteger(key, defaultValue);
} else {
return defaultValue;
}
} else {
throw new ClassCastException('\'' + key + "' doesn't map to a Integer object");
}
}
Get a int associated with the given configuration key. |
public Iterator getKeys() {
return keysAsListed.iterator();
}
Get the list of the keys contained in the configuration
repository. |
public Iterator getKeys(String prefix) {
Iterator keys = getKeys();
ArrayList matchingKeys = new ArrayList();
while (keys.hasNext()) {
Object key = keys.next();
if (key instanceof String && ((String) key).startsWith(prefix)) {
matchingKeys.add(key);
}
}
return matchingKeys.iterator();
}
Get the list of the keys contained in the configuration
repository that match the specified prefix. |
public List getList(String key) {
return getList(key, null);
}
Get a List of strings associated with the given configuration key.
The list is a copy of the internal data of this object, and as
such you may alter it freely. |
public List getList(String key,
List defaultValue) {
Object value = get(key);
if (value instanceof List) {
return new ArrayList((List) value);
} else if (value instanceof String) {
List values = new ArrayList(1);
values.add(value);
put(key, values);
return values;
} else if (value == null) {
if (defaults != null) {
return defaults.getList(key, defaultValue);
} else {
return ((defaultValue == null) ? new ArrayList() : defaultValue);
}
} else {
throw new ClassCastException('\'' + key + "' doesn't map to a List object");
}
}
Get a List of strings associated with the given configuration key.
The list is a copy of the internal data of this object, and as
such you may alter it freely. |
public long getLong(String key) {
Long l = getLong(key, null);
if (l != null) {
return l.longValue();
} else {
throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object");
}
}
Get a long associated with the given configuration key. |
public long getLong(String key,
long defaultValue) {
return getLong(key, new Long(defaultValue)).longValue();
}
Get a long associated with the given configuration key. |
public Long getLong(String key,
Long defaultValue) {
Object value = get(key);
if (value instanceof Long) {
return (Long) value;
} else if (value instanceof String) {
Long l = new Long((String) value);
put(key, l);
return l;
} else if (value == null) {
if (defaults != null) {
return defaults.getLong(key, defaultValue);
} else {
return defaultValue;
}
} else {
throw new ClassCastException('\'' + key + "' doesn't map to a Long object");
}
}
Get a long associated with the given configuration key. |
public Properties getProperties(String key) {
return getProperties(key, new Properties());
}
Get a list of properties associated with the given
configuration key. |
public Properties getProperties(String key,
Properties defaults) {
/*
* Grab an array of the tokens for this key.
*/
String[] tokens = getStringArray(key);
// Each token is of the form 'key=value'.
Properties props = new Properties(defaults);
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i];
int equalSign = token.indexOf('=');
if (equalSign > 0) {
String pkey = token.substring(0, equalSign).trim();
String pvalue = token.substring(equalSign + 1).trim();
props.put(pkey, pvalue);
} else {
throw new IllegalArgumentException('\'' + token + "' does not contain " + "an equals sign");
}
}
return props;
}
Get a list of properties associated with the given
configuration key. |
public Object getProperty(String key) {
// first, try to get from the 'user value' store
Object obj = this.get(key);
if (obj == null) {
// if there isn't a value there, get it from the
// defaults if we have them
if (defaults != null) {
obj = defaults.get(key);
}
}
return obj;
}
Gets a property from the configuration. |
public short getShort(String key) {
Short s = getShort(key, null);
if (s != null) {
return s.shortValue();
} else {
throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object");
}
}
Get a short associated with the given configuration key. |
public short getShort(String key,
short defaultValue) {
return getShort(key, new Short(defaultValue)).shortValue();
}
Get a short associated with the given configuration key. |
public Short getShort(String key,
Short defaultValue) {
Object value = get(key);
if (value instanceof Short) {
return (Short) value;
} else if (value instanceof String) {
Short s = new Short((String) value);
put(key, s);
return s;
} else if (value == null) {
if (defaults != null) {
return defaults.getShort(key, defaultValue);
} else {
return defaultValue;
}
} else {
throw new ClassCastException('\'' + key + "' doesn't map to a Short object");
}
}
Get a short associated with the given configuration key. |
public String getString(String key) {
return getString(key, null);
}
Get a string associated with the given configuration key. |
public String getString(String key,
String defaultValue) {
Object value = get(key);
if (value instanceof String) {
return interpolate((String) value);
} else if (value == null) {
if (defaults != null) {
return interpolate(defaults.getString(key, defaultValue));
} else {
return interpolate(defaultValue);
}
} else if (value instanceof List) {
return interpolate((String) ((List) value).get(0));
} else {
throw new ClassCastException('\'' + key + "' doesn't map to a String object");
}
}
Get a string associated with the given configuration key. |
public String[] getStringArray(String key) {
Object value = get(key);
List values;
if (value instanceof String) {
values = new Vector(1);
values.add(value);
} else if (value instanceof List) {
values = (List) value;
} else if (value == null) {
if (defaults != null) {
return defaults.getStringArray(key);
} else {
return new String[0];
}
} else {
throw new ClassCastException('\'' + key + "' doesn't map to a String/List object");
}
String[] tokens = new String[values.size()];
for (int i = 0; i < tokens.length; i++) {
tokens[i] = (String) values.get(i);
}
return tokens;
}
Get an array of strings associated with the given configuration
key. |
public Vector getVector(String key) {
return getVector(key, null);
}
Get a Vector of strings associated with the given configuration
key. |
public Vector getVector(String key,
Vector defaultValue) {
Object value = get(key);
if (value instanceof List) {
return new Vector((List) value);
} else if (value instanceof String) {
Vector values = new Vector(1);
values.add(value);
put(key, values);
return values;
} else if (value == null) {
if (defaults != null) {
return defaults.getVector(key, defaultValue);
} else {
return ((defaultValue == null) ? new Vector() : defaultValue);
}
} else {
throw new ClassCastException('\'' + key + "' doesn't map to a Vector object");
}
}
Get a Vector of strings associated with the given configuration key.
The list is a copy of the internal data of this object, and as
such you may alter it freely. |
protected String interpolate(String base) {
// COPIED from [configuration] 2003-12-29
return (interpolateHelper(base, null));
}
Interpolate key names to handle ${key} stuff |
protected String interpolateHelper(String base,
List priorVariables) {
// COPIED from [configuration] 2003-12-29
if (base == null) {
return null;
}
// on the first call initialize priorVariables
// and add base as the first element
if (priorVariables == null) {
priorVariables = new ArrayList();
priorVariables.add(base);
}
int begin = -1;
int end = -1;
int prec = 0 - END_TOKEN.length();
String variable = null;
StringBuffer result = new StringBuffer();
// FIXME: we should probably allow the escaping of the start token
while (((begin = base.indexOf(START_TOKEN, prec + END_TOKEN.length())) > -1)
&& ((end = base.indexOf(END_TOKEN, begin)) > -1)) {
result.append(base.substring(prec + END_TOKEN.length(), begin));
variable = base.substring(begin + START_TOKEN.length(), end);
// if we've got a loop, create a useful exception message and throw
if (priorVariables.contains(variable)) {
String initialBase = priorVariables.remove(0).toString();
priorVariables.add(variable);
StringBuffer priorVariableSb = new StringBuffer();
// create a nice trace of interpolated variables like so:
// var1- >var2- >var3
for (Iterator it = priorVariables.iterator(); it.hasNext();) {
priorVariableSb.append(it.next());
if (it.hasNext()) {
priorVariableSb.append("- >");
}
}
throw new IllegalStateException(
"infinite loop in property interpolation of " + initialBase + ": " + priorVariableSb.toString());
}
// otherwise, add this variable to the interpolation list.
else {
priorVariables.add(variable);
}
//QUESTION: getProperty or getPropertyDirect
Object value = getProperty(variable);
if (value != null) {
result.append(interpolateHelper(value.toString(), priorVariables));
// pop the interpolated variable off the stack
// this maintains priorVariables correctness for
// properties with multiple interpolations, e.g.
// prop.name=${some.other.prop1}/blahblah/${some.other.prop2}
priorVariables.remove(priorVariables.size() - 1);
} else if (defaults != null && defaults.getString(variable, null) != null) {
result.append(defaults.getString(variable));
} else {
//variable not defined - so put it back in the value
result.append(START_TOKEN).append(variable).append(END_TOKEN);
}
prec = end;
}
result.append(base.substring(prec + END_TOKEN.length(), base.length()));
return result.toString();
}
Recursive handler for multiple levels of interpolation.
When called the first time, priorVariables should be null. |
public boolean isInitialized() {
return isInitialized;
}
Indicate to client code whether property
resources have been initialized or not. |
public void load(InputStream input) throws IOException {
load(input, null);
}
Load the properties from the given input stream. |
public synchronized void load(InputStream input,
String enc) throws IOException {
PropertiesReader reader = null;
if (enc != null) {
try {
reader = new PropertiesReader(new InputStreamReader(input, enc));
} catch (UnsupportedEncodingException ex) {
// Another try coming up....
}
}
if (reader == null) {
try {
reader = new PropertiesReader(new InputStreamReader(input, "8859_1"));
} catch (UnsupportedEncodingException ex) {
// ISO8859-1 support is required on java platforms but....
// If it's not supported, use the system default encoding
reader = new PropertiesReader(new InputStreamReader(input));
}
}
try {
while (true) {
String line = reader.readProperty();
if (line == null) {
return; // EOF
}
int equalSign = line.indexOf('=');
if (equalSign > 0) {
String key = line.substring(0, equalSign).trim();
String value = line.substring(equalSign + 1).trim();
// Configure produces lines like this ... just ignore them
if ("".equals(value)) {
continue;
}
if (getInclude() != null && key.equalsIgnoreCase(getInclude())) {
// Recursively load properties files.
File file = null;
if (value.startsWith(fileSeparator)) {
// We have an absolute path so we'll use this
file = new File(value);
} else {
// We have a relative path, and we have two
// possible forms here. If we have the "./" form
// then just strip that off first before continuing.
if (value.startsWith("." + fileSeparator)) {
value = value.substring(2);
}
file = new File(basePath + value);
}
if (file != null && file.exists() && file.canRead()) {
load(new FileInputStream(file));
}
} else {
addProperty(key, value);
}
}
}
} finally {
// Loading is initializing
isInitialized = true;
}
}
Load the properties from the given input stream
and using the specified encoding. |
public synchronized void save(OutputStream output,
String header) throws IOException {
if (output == null) {
return;
}
PrintWriter theWrtr = new PrintWriter(output);
if (header != null) {
theWrtr.println(header);
}
Enumeration theKeys = keys();
while (theKeys.hasMoreElements()) {
String key = (String) theKeys.nextElement();
Object value = get(key);
if (value != null) {
if (value instanceof String) {
StringBuffer currentOutput = new StringBuffer();
currentOutput.append(key);
currentOutput.append("=");
currentOutput.append(escape((String) value));
theWrtr.println(currentOutput.toString());
} else if (value instanceof List) {
List values = (List) value;
for (Iterator it = values.iterator(); it.hasNext(); ) {
String currentElement = (String) it.next();
StringBuffer currentOutput = new StringBuffer();
currentOutput.append(key);
currentOutput.append("=");
currentOutput.append(escape(currentElement));
theWrtr.println(currentOutput.toString());
}
}
}
theWrtr.println();
theWrtr.flush();
}
}
Save the properties to the given output stream.
The stream is not closed, but it is flushed. |
public void setInclude(String inc) {
include = inc;
}
Sets the property value for including other properties files.
By default it is "include". |
public void setProperty(String key,
Object value) {
clearProperty(key);
addProperty(key, value);
}
Set a property, this will replace any previously
set values. Set values is implicitly a call
to clearProperty(key), addProperty(key,value). |
public ExtendedProperties subset(String prefix) {
ExtendedProperties c = new ExtendedProperties();
Iterator keys = getKeys();
boolean validSubset = false;
while (keys.hasNext()) {
Object key = keys.next();
if (key instanceof String && ((String) key).startsWith(prefix)) {
if (!validSubset) {
validSubset = true;
}
/*
* Check to make sure that c.subset(prefix) doesn't
* blow up when there is only a single property
* with the key prefix. This is not a useful
* subset but it is a valid subset.
*/
String newKey = null;
if (((String) key).length() == prefix.length()) {
newKey = prefix;
} else {
newKey = ((String) key).substring(prefix.length() + 1);
}
/*
* use addPropertyDirect() - this will plug the data as
* is into the Map, but will also do the right thing
* re key accounting
*/
c.addPropertyDirect(newKey, get(key));
}
}
if (validSubset) {
return c;
} else {
return null;
}
}
Create an ExtendedProperties object that is a subset
of this one. Take into account duplicate keys
by using the setProperty() in ExtendedProperties. |
public String testBoolean(String value) {
String s = value.toLowerCase();
if (s.equals("true") || s.equals("on") || s.equals("yes")) {
return "true";
} else if (s.equals("false") || s.equals("off") || s.equals("no")) {
return "false";
} else {
return null;
}
}
Test whether the string represent by value maps to a boolean
value or not. We will allow true , on ,
and yes for a true boolean value, and
false , off , and no for
false boolean values. Case of value to test for
boolean status is ignored. |