📄 basicproperties.java
字号:
if (separatorIndex > 0) {
return arg.substring(0, separatorIndex);
}
if ((arg.length() > 1) && (arg.startsWith("-"))) { // "-x" -> "arg=true"
return arg.substring(1);
}
throw new PropertiesException("Unable to identify key part in argument: " + arg);
}
/**
* Called by parseArgument to extract the value component from the current argument.
* By default, any value of the form 'hello world' will be returned as "hello world".
* A value of '' (two single quotes) will be returned as an empty string. An argument
* of the form "key=" will return null for its value and cause the key to be removed
* from the properties.
* @param arg The argument being processed.
* @return String The resultant value, may be null.
*/
protected String isolateValue(String arg) {
String value = null;
int separatorIndex = getSeparatorIndex(arg); // key=value or key:value
if (separatorIndex > 0) {
if (separatorIndex == (arg.length()-1)) {
return null;
}
value = arg.substring(separatorIndex+1);
} else {
if ((arg.length() > 1) && (arg.startsWith("-"))) { // "-x" -> "arg=true"
value = "true";
}
}
if (value != null) {
if ( (value.startsWith("'")) && (value.endsWith("'")) ) {
if (value.length() == 2) {
value = "";
} else {
// Replace the single quotes with double quote as this
// quoting character works the same on Windows and Unix.
value = "\"" + value.substring(1, value.length()-1) + "\"";
}
}
return value;
}
throw new PropertiesException("Unable to identify value part in argument: " + arg);
}
/**
* Called by parseArgument as a final step prior to actually storing the key=value pair.
* By default it simply returns true, which directs parseArgs to store the pair.
* An extending class could change this behavior to say look for something special
* such as an import directive and take different action.
* @param key The key string.
* @param value The value string.
* @return True if the key=value pair should be stored, false otherwise.
*/
protected boolean storableProperty(String key, String value) {
return true;
}
/**
* Called by parseArgs to set the next argument index. By default
* it simply returns the current index + 1.
* @return int If the returned value is negative or greater
* than or equal to args.length, parseArgs will stop parsing.
* Otherwise parseArgs will use the returned value as its
* next index.
*/
protected int nextArgIndex(String[] args, int argIndex) {
return argIndex+1;
}
/**
* Copy a data from standard Properties.
* @param source The Hashtable to copy from.
*/
public synchronized void copyProperties(BasicProperties source) {
for (Enumeration e = source.keys(); e.hasMoreElements(); ) {
String key = (String)e.nextElement();
super.put(key, source.getRawProperty(key));
}
}
/**
* Create a new PropertiesCollection from this one by coping those
* attributes which begin with a particular prefix string.
* The prefix of selected attributes is deleted when those
* keys are placed in the new collection.
* @param anArgPrefix The prefix string. Ex: "server."
*/
public synchronized BasicProperties extractSubset(String anArgPrefix) {
BasicProperties result = new BasicProperties();
for (Enumeration e = super.keys(); e.hasMoreElements(); ) {
String originalKey = (String) e.nextElement();
String newKey = null;
if (originalKey.startsWith(anArgPrefix)) {
newKey =
originalKey
.substring(anArgPrefix
.length()); // could be nothing left
} else {
newKey = ""; // this argument not in result
}
if (newKey.length() > 0) {
result.setProperty(newKey, getRawProperty(originalKey));
}
}
return result;
}
/**
* Get the object associated with a key.
* @param aKey Key for desired property.
* @return The object associated with this key or null if none exits.
*/
public Object get(String aKey) {
String testKey = (aKey.endsWith("!")) ? aKey.substring(0, aKey.length()) : aKey;
if (testKey.length() == 0) {
return null;
}
Object data = super.get(testKey);
if (data == null) {
data = super.get(testKey + "!" );
}
return data;
}
/**
* Set property value to specified object.
* @param aKey The key used to store the data. The key may contain strings of
* the form <b><tt>${key}</tt></b> which will be evaluated first.
* @param aValue The object to be stored.
* @return The previous value of the specified key, or null if it did not have one.
*/
public Object put(String aKey, Object aValue) {
String actualKey = doSubstitutions(aKey);
String testKey = (actualKey.endsWith("!")) ? actualKey.substring(0, actualKey.length()) : actualKey;
if (super.containsKey(testKey + "!")) {
throw new PropertiesException("Attempt to alter read only property:" + testKey);
}
return super.put(actualKey, aValue);
}
/**
* Override getProperty in base class so all occurances of
* the form <b><tt>${key}</tt></b> are replaced by their
* associated value.
* @param aKey Key for desired property.
* @return The keys value with substitutions done.
*/
public String getProperty(String aKey) {
return getProperty(doSubstitutions(aKey), null);
}
/**
* Set property value. If value is null the property (key and value) will be removed.
* @param aKey The key used to store the data. The key may contain strings of
* the form <b><tt>${key}</tt></b> which will be evaluated first.
* @param aValue The value to be stored, if null they property will be removed.
* @return The previous value of the specified key, or null if it did not have one.
*/
public Object setProperty(String aKey, String aValue) {
String actualKey = doSubstitutions(aKey);
String testKey = (actualKey.endsWith("!")) ? actualKey.substring(0, actualKey.length()) : actualKey;
if (super.containsKey(testKey + "!")) {
throw new PropertiesException("Attempt to alter read only property:" + testKey);
}
if (aValue == null) {
return super.remove(actualKey);
} else {
return super.put(actualKey, aValue);
}
}
/**
* Set property value only if its not set already.
* @param aKey The key used to store the data. The key may contain strings of
* the form <b><tt>${key}</tt></b> which will be evaluated first.
* @param value The value to be stored.
* @return Null if store was done, non-null indicates store not done and the
* returned value in the current properties value.
*/
public Object setPropertyIfNot(String aKey, String value) {
String current = getProperty(aKey);
if (current == null) {
return setProperty(aKey, value);
}
return current;
}
/**
* Fetch property value for key which may contain strings
* of the form <b><tt>${key}</tt></b>.
* @param aKey Key for desired property.
* @return The keys value with no substitutions done.
*/
public String getRawProperty(String aKey) {
Object data = super.get(aKey);
return (data != null) ? data.toString() : null;
}
/**
* Use this method to fetch a property ignoring case of key.
* @param aKey The key of the environment property.
* @return The key's value or null if not found.
*/
public String getPropertyIgnoreCase(String aKey) {
for (Enumeration e = super.keys(); e.hasMoreElements(); ) {
String key = (String) e.nextElement();
if (aKey.equalsIgnoreCase(key)) {
return getProperty(key);
}
}
return null;
}
/**
* Perform substitution when a value is fetched. Traps circular definitions,
* and calls valueFilter with value prior to returning it.
* @param aKey The property key.
* @param defaultValue Value to return if property not defined. May be null.
* If non null it will be passes to valueFilter first.
* @return The resultant value - could be null or empty.
* @throws PropertiesException if circular definition.
*/
public String getProperty(String aKey, String defaultValue) {
String testKey = (aKey.endsWith("!")) ? aKey.substring(0, aKey.length()) : aKey;
if (testKey.length() == 0) {
return null;
}
if (keyNames == null) {
keyNames = new Hashtable();
}
if (keyNames.put(testKey, "x") != null) { // value doesn't matter
throw new PropertiesException(
"Circular argument substitution with key: " + aKey);
}
Object data = super.get(testKey);
if (data == null) {
data = super.get(testKey + "!" );
}
String value = (data != null) ? data.toString() : null;
if (value != null) {
if (value.length() >= 4) { // shortest possible value: ${x}
value = doSubstitutions(value);
}
} else {
value = defaultValue;
}
if (value != null) {
value = valueFilter(aKey, value);
}
keyNames.remove(testKey);
return value;
}
/**
* Called by getProperty(key, default) to perform any post processing of the
* value string. By default, this method provides special processing on the value
* associated with any property whose key name has the string "path" as part of it
* (ex: "classpath", "sourcepath", "mypath"). When the value for such keys is fetched
* any occurance of '|' will be converted to a ':' on Unix systems and a ';' on
* Windows systems. Therefore to increase the direct reuse of your property files,
* always use a '|' as a separator and always assign a key name which has "path" as
* part of it.
* @param key The properties key.
* @param value The properties value.
* @return String New potentially altered value.
*/
protected String valueFilter(String key, String value) {
if (key.toLowerCase().indexOf("path") >= 0) { // convert separators to be correct for this system
String correctSeparator = System.getProperty("path.separator");
if (correctSeparator.equals(";")) {
value = value.replace('|', ';');
} else {
value = value.replace('|', ':');
}
}
return value;
}
/**
* Extract a string value and convert it to an integer.
* If there isn't one or there is a problem with the conversion,
* return the default value.
* @param aKey The key which will be used to fetch the attribute.
* @param aDefaultValue Specifies the default value for the int.
* @return int The result.
*/
public int getIntProperty(String aKey, int aDefaultValue) {
int result = aDefaultValue;
try {
result = Integer.parseInt(getProperty(aKey));
} catch (Exception e) {}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -