📄 basicproperties.java
字号:
return result;
}
/**
* Store an int as a string with the specified key.
* @param aKey The key which will be used to store the attribute.
* @param aValue The int value.
*/
public int setIntProperty(String aKey, int aValue) {
setProperty(aKey, Integer.toString(aValue));
return aValue;
}
/**
* Extract a string value ("true" or "false") and convert it to
* a boolean. 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 boolean.
* @return boolean The result.
*/
public boolean getBooleanProperty(String aKey, boolean aDefaultValue) {
boolean result = aDefaultValue;
try {
String value = getProperty(aKey);
result = value.equalsIgnoreCase("true");
} catch (Exception e) {}
return result;
}
/**
* Store a boolean as a string ("true" or "false") with the specified key.
* @param aKey The key which will be used to store the attribute.
* @param aValue The boolean value.
*/
public void setBooleanProperty(String aKey, boolean aValue) {
setProperty(aKey, (aValue) ? "true" : "false");
}
/**
* Change key string associated with existing value.
* @param existintKey The current key.
* @param newKey The new key.
* @return Non null is former value of object associated with new key.
* Null indicates that either the existing key didn't exist or there
* was no former value associated with the new key. i.e. null => success.
*/
public Object renameKey(String existingKey, String newKey) {
Object value = super.remove(doSubstitutions(existingKey));
if (value != null) {
return super.put(doSubstitutions(newKey), value);
}
return null;
}
/**
* Replace all substrings of the form ${xxx} with the property value
* using the key xxx. Calls doSubstitutions(anInputString, false).
* @param anInputString The input string - may be null.
* @return The resultant line with all substitutions done or null if input string was.
*/
public String doSubstitutions(String anInputString) {
return doSubstitutions(anInputString, false);
}
/**
* Replace all substrings of the form ${xxx} with the property value
* using the key xxx. If the key is all caps then the property is
* considered to be a system property.
* @param anInputString The input string - may be null.
* @param allowUndefined If true, undefined strings will remain as is,
* if false, an exception will be thrown.
* @return The resultant line with all substitutions done or null if input string was.
*/
public String doSubstitutions(String anInputString, boolean allowUndefined) {
if (anInputString == null) {
return null;
}
StringBuffer result = new StringBuffer();
int si = 0; // source index
int oi = 0; // opening index
int ci = 0; // closing index
do {
oi = anInputString.indexOf("${", si);
ci = anInputString.indexOf('}', si);
if (oi > si) { // xxxxxx${key}
result.append(anInputString.substring(si, oi));
si = oi;
}
if ((oi == si) && (ci > oi + 2)) { // ${key}xxxxx
String key = anInputString.substring(oi + 2, ci);
// Try our properties first as this allows the user
// to override system or environment setting
String value = getProperty(key, null);
// If we didn't find the property and its key is all uppercase
// them check if its a Java or environment property
if ((value == null) && key.equals(key.toUpperCase())) {
value = getEnvironmentProperty(key);
}
if (value == null) {
if (allowUndefined) {
value = "${" + key + "}";
} else {
throw new PropertiesException("Unable to get property value for key: " + key);
}
}
if (oi > si) {
result.append(anInputString.substring(si, oi));
}
result.append(value);
si = ci + 1;
} else {
if (oi == -1) { // xxxxxxxxx
result.append(anInputString.substring(si, anInputString.length()));
si = anInputString.length();
} else { // xxxxxx${xxxxxx
result.append(anInputString.substring(si, oi + 2));
si = oi + 2;
}
}
} while (si < anInputString.length());
return result.toString();
}
/**
* Fetch environment property by looking calling System.getProperty.
* @param key The key of the desired property.
* @return The resultant property if it exists or null.
*/
protected String getEnvironmentProperty(String key) {
String value = System.getProperty(key.toLowerCase());
return value;
}
/**
* Add properties from Reader. Explicitly handled so as to enable
* handling of import=<file> directive. Blank lines as well as
* those beginning with a '#' character (comments) are ignored.
* @param reader The buffered reader to read from.
* to catch circular imports.
* @throws IOException if anything goes wrong.
*/
protected void addFromReader(Reader reader) throws IOException {
String line = null;
String key = null;
String value = null;
do {
line = getOneLine(reader);
if (line != null) {
line = line.trim();
if (line.length() == 0) {
continue; // empty line
}
if (line.startsWith("#") || line.startsWith("!")) {
continue; // comment line
}
parseArgument(line);
}
} while (line != null);
}
/**
* Get a logical line. Any physical line ending in '\' is considered
* to continue on the next line.
* @param reader The input reader to read.
* @return The resultant logical line which may have been constructed
* from one or more physical lines.
* @throws IOException if anything goes wrong.
*/
protected String getOneLine(Reader reader) throws IOException {
StringBuffer sb = null;
String line = null;
boolean continued;
do {
continued = false;
try {
line = readLine(reader);
if (line != null) {
line = line.trim();
// If we already have something going ignore blank lines and comments
if ((sb != null)
&& ((line.length() == 0) ||
(line.startsWith("#") || line.startsWith("!")))) {
continued = true;
continue;
}
continued = line.endsWith("\\");
if (continued) { // delete the ending slash
line = line.substring(0, line.length() - 1);
}
if (sb == null) {
sb = new StringBuffer();
}
sb.append(line);
}
} catch (EOFException eof) {
continued = false;
}
} while (continued);
return (sb == null) ? null : sb.toString();
}
/**
* Read one line from the Reader. A line may be terminated
* by a single CR or LF, or the pair CR LF.
* @param aReader The Reader to read characters from.
* @return Next physical line.
* @throws IOException if anything goes wrong.
*/
protected String readLine(Reader aReader) throws IOException {
StringBuffer sb = new StringBuffer();
boolean done = false;
while (!done) {
int result = aReader.read();
if (result == -1) {
if (sb.length() > 0) {
break;
}
throw new EOFException();
} else {
char ch = (char)result;
if (ch == '\n') { // LF
if (CRState) {
CRState = false;
continue;
}
break;
} else {
if (ch == '\r') {
CRState = true;
break;
} else {
sb.append(ch);
CRState = false;
}
}
}
}
return sb.toString();
}
/**
* List properties to provided PrintStream.
* Output will be in sorted key sequence.
* If a value is null, it will appear as "key=".
* @param out The print stream.
*/
public void list(PrintStream out) {
for (Enumeration e = sortedKeys(); e.hasMoreElements(); ) {
String key = (String) e.nextElement();
String value = getProperty(key);
if (value != null) {
out.println(key + "=" + value);
} else {
out.println(key + "=");
}
}
}
/**
* Create a String[] for the properties with one key=value pair per array entry.
* If a value is null, it will appear as "key=".
* @return The resultant String[].
*/
public String[] toStringArray() {
String[] result = new String[super.size()];
int i = 0;
for (Enumeration e = sortedKeys(); e.hasMoreElements(); ) {
String key = (String) e.nextElement();
String value = getProperty(key);
if (value != null) {
result[i++] = key + "=" + value;
} else {
result[i++] = key + "=";
}
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -