Collection of helper methods for dealing with
objects.
| Method from org.hibernate.util.PropertiesHelper Detail: |
public static String extractPropertyValue(String propertyName,
Properties properties) {
String value = properties.getProperty( propertyName );
if ( value == null ) {
return null;
}
value = value.trim();
if ( StringHelper.isEmpty( value ) ) {
return null;
}
return value;
}
Extract a property value by name from the given properties object.
Both null and empty string are viewed as the same, and return null. |
public static boolean getBoolean(String propertyName,
Properties properties) {
return getBoolean( propertyName, properties, false );
}
|
public static boolean getBoolean(String propertyName,
Properties properties,
boolean defaultValue) {
String value = extractPropertyValue( propertyName, properties );
return value == null ? defaultValue : Boolean.valueOf( value ).booleanValue();
}
Get a property value as a boolean.
First, the string value is extracted, and then Boolean#valueOf(String) is
used to determine the correct boolean value. |
public static int getInt(String propertyName,
Properties properties,
int defaultValue) {
String value = extractPropertyValue( propertyName, properties );
return value == null ? defaultValue : Integer.parseInt( value );
}
Get a property value as an int.
First, the string value is extracted, and then Integer#parseInt(String) is
used to determine the correct int value for any non-null property values. |
public static Integer getInteger(String propertyName,
Properties properties) {
String value = extractPropertyValue( propertyName, properties );
return value == null ? null : Integer.valueOf( value );
}
Get a property value as an Integer.
First, the string value is extracted, and then Integer#valueOf(String) is
used to determine the correct boolean value for any non-null property values. |
public static String getString(String propertyName,
Properties properties,
String defaultValue) {
String value = extractPropertyValue( propertyName, properties );
return value == null ? defaultValue : value;
}
Get a property value as a string. |
public static Properties maskOut(Properties props,
String key) {
Properties clone = ( Properties ) props.clone();
if ( clone.get( key ) != null ) {
clone.setProperty( key, "****" );
}
return clone;
}
replace a property by a starred version |
public static String resolvePlaceHolder(String property) {
if ( property.indexOf( PLACEHOLDER_START ) < 0 ) {
return property;
}
StringBuffer buff = new StringBuffer();
char[] chars = property.toCharArray();
for ( int pos = 0; pos < chars.length; pos++ ) {
if ( chars[pos] == '$" ) {
// peek ahead
if ( chars[pos+1] == '{" ) {
// we have a placeholder, spin forward till we find the end
String systemPropertyName = "";
int x = pos + 2;
for ( ; x < chars.length && chars[x] != '}"; x++ ) {
systemPropertyName += chars[x];
// if we reach the end of the string w/o finding the
// matching end, that is an exception
if ( x == chars.length - 1 ) {
throw new IllegalArgumentException( "unmatched placeholder start [" + property + "]" );
}
}
String systemProperty = extractFromSystem( systemPropertyName );
buff.append( systemProperty == null ? "" : systemProperty );
pos = x + 1;
// make sure spinning forward did not put us past the end of the buffer...
if ( pos >= chars.length ) {
break;
}
}
}
buff.append( chars[pos] );
}
String rtn = buff.toString();
return StringHelper.isEmpty( rtn ) ? null : rtn;
}
Handles interpolation processing for a single property. |
public static void resolvePlaceHolders(Properties properties) {
Iterator itr = properties.entrySet().iterator();
while ( itr.hasNext() ) {
final Map.Entry entry = ( Map.Entry ) itr.next();
final Object value = entry.getValue();
if ( value != null && String.class.isInstance( value ) ) {
final String resolved = resolvePlaceHolder( ( String ) value );
if ( !value.equals( resolved ) ) {
if ( resolved == null ) {
itr.remove();
}
else {
entry.setValue( resolved );
}
}
}
}
}
Handles interpolation processing for all entries in a properties object. |
public static Map toMap(String propertyName,
String delim,
Properties properties) {
Map map = new HashMap();
String value = extractPropertyValue( propertyName, properties );
if ( value != null ) {
StringTokenizer tokens = new StringTokenizer( value, delim );
while ( tokens.hasMoreTokens() ) {
map.put( tokens.nextToken(), tokens.hasMoreElements() ? tokens.nextToken() : "" );
}
}
return map;
}
Constructs a map from a property value.
The exact behavior here is largely dependant upon what is passed in as
the delimiter. |
public static String[] toStringArray(String stringForm,
String delim) {
// todo : move to StringHelper?
if ( stringForm != null ) {
return StringHelper.split( delim, stringForm );
}
else {
return ArrayHelper.EMPTY_STRING_ARRAY;
}
}
Convert a string to an array of strings. The assumption is that
the individual array elements are delimited in the source stringForm
param by the delim param. |
public static String[] toStringArray(String propertyName,
String delim,
Properties properties) {
return toStringArray( extractPropertyValue( propertyName, properties ), delim );
}
Get a property value as a string array. |