public class StringReplacer extends Object
The default is to replace ${var} with
var
from a Map instance. But there are other ways possible by
using different Pattern and StringReplacer.ReplaceSource implementations.
In order to replace a string with the ${var} pattern using Java System Properties use:
String str = StringReplacer.replace ( "Hello ${user.name}!", System.getProperties () );
Replacing a variables in a string with bean like references use:
class User
{
private String name;
public String getName ()
{
return name;
};
User ( String name )
{
this.name = name;
}
}
Map<String, User> userMap = Collections.singletonMap ( "user", new User ( "Arthur Dent" ) );
String str = StringReplacer.replace ( "Hello ${user.name}!", userMap );
| Modifier and Type | Class and Description |
|---|---|
static interface |
StringReplacer.ReplaceSource |
| Modifier and Type | Field and Description |
|---|---|
static Pattern |
DEFAULT_PATTERN
The default pattern:
${var} |
| Constructor and Description |
|---|
StringReplacer() |
| Modifier and Type | Method and Description |
|---|---|
static StringReplacer.ReplaceSource |
newBeansSource(Map<?,?> properties)
Create a new ReplaceSource that handles bean references
|
static StringReplacer.ReplaceSource |
newExtendedSource(Map<?,?> properties)
Create a new ReplaceSource for Map sources which allows additional
operators.
|
static StringReplacer.ReplaceSource |
newSource(Map<?,?> properties)
Create a new ReplaceSource for Map sources
|
static String |
replace(String string,
Map<?,?> properties)
Replace with the default pattern of
${var} |
static String |
replace(String string,
StringReplacer.ReplaceSource replaceSource,
Pattern pattern)
Replace variables in a string
Actually calls replace ( string, replcaeSource, pattern, true ). |
static String |
replace(String string,
StringReplacer.ReplaceSource replaceSource,
Pattern pattern,
boolean nested)
Replace variables in a string
|
public static final Pattern DEFAULT_PATTERN
${var}public static String replace(String string, Map<?,?> properties)
${var}string - the string which should be processed, may be nullproperties - the properties used for replacingpublic static StringReplacer.ReplaceSource newSource(Map<?,?> properties)
The source will not replace elements that are not found in the map.
properties - the Map acting as a sourcepublic static StringReplacer.ReplaceSource newExtendedSource(Map<?,?> properties)
properties - the Map acting as a sourceExtendedPropertiesReplacerpublic static StringReplacer.ReplaceSource newBeansSource(Map<?,?> properties)
properties - the properties to use for replacingpublic static String replace(String string, StringReplacer.ReplaceSource replaceSource, Pattern pattern, boolean nested)
string - the string to processreplaceSource - the source of the replacementspattern - the pattern for detecting variablesnested - true if the replacement process should honor
nested replacementsnull if the input string was
nullpublic static String replace(String string, StringReplacer.ReplaceSource replaceSource, Pattern pattern)
replace ( string, replcaeSource, pattern, true ).Copyright © 2016 Eclipse NeoSCADA Project. All rights reserved.