
uk.org.retep.util.string.PropertyExpansion Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2010, Peter T Mount
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the retep.org.uk nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package uk.org.retep.util.string;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.prefs.Preferences;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.concurrent.ThreadSafe;
/**
* A simple set of methods that will replace any instances of ${name} within a
* string with the appropriate value within a Map.
*
* @author peter
*/
@ThreadSafe
public class PropertyExpansion
{
private static Pattern PATTERN = Pattern.compile( "(\\$\\{([^\\}]*?)\\})" );
private PropertyExpansion()
{
}
/**
* Expand any references within the String with values contained in a map
* @param source Source string
* @param map Map to expand
* @return String with values expanded
*/
public static String expandMap( final String source, final Map, ?> map )
{
final StringBuffer sb = new StringBuffer();
final Matcher match = PATTERN.matcher( source );
while( match.find() )
{
String name = match.group( 2 );
match.appendReplacement( sb, String.valueOf( map.get( name ) ) );
}
match.appendTail( sb );
return sb.toString();
}
private static Map newMap( final Map map )
{
if( map instanceof LinkedHashMap )
{
return new LinkedHashMap();
}
if( map instanceof ConcurrentHashMap )
{
return new ConcurrentHashMap();
}
return new HashMap();
}
/**
* Expand any references within the values of a Map with values contained in
* a second map.
*
* The new map will be a HashMap, unless the source was one of LinkedHashMap
* or ConcurrentHashMap, when the map will be of the same type.
*
* @param Class of key
* @param Class of values
* @param source Map who's values to expand
* @param map Map to expand
* @return new Map containing the expanded values
*/
public static Map expandMap( final Map source,
final Map, ?> map )
{
final Map newMap = newMap( source );
if( map == null )
{
// just copy the values over
for( Map.Entry entry : source.entrySet() )
{
newMap.put( entry.getKey(), entry.getValue().toString() );
}
}
else
{
for( Map.Entry entry : source.entrySet() )
{
newMap.put( entry.getKey(),
expandMap( entry.getValue().toString(),
map ) );
}
}
return newMap;
}
/**
* Expand any references within the String with values contained in the
* JVM's system properties (ie those set by -D in the command line)
* @param source Source string
* @return String with values expanded
*/
public static String expandSystemProperties( final String source )
{
return expandMap( source, System.getProperties() );
}
/**
* Expand any references within the String with values contained in the
* JVM's system properties (ie those set by -D in the command line)
* @param source Source string
* @param Class of key
* @param Class of values
* @return String with values expanded
*/
public static Map expandSystemProperties(
final Map source )
{
return expandMap( source, System.getProperties() );
}
public static String expandPreferences( final String source,
final Preferences preferences )
{
StringBuffer sb = new StringBuffer();
Matcher match = PATTERN.matcher( source );
while( match.find() )
{
String name = match.group( 2 );
match.appendReplacement( sb, preferences.get( name,
"??" + name + "??" ) );
}
match.appendTail( sb );
return sb.toString();
}
public static Map expandPreferences(
final Map source,
final Preferences preferences )
{
final Map newMap = newMap( source );
for( Map.Entry entry : source.entrySet() )
{
newMap.put( entry.getKey(), expandPreferences( String.valueOf(
entry.getValue() ),
preferences ) );
}
return newMap;
}
}