public class ReplaceVariableLiteralsInContent
extends java.lang.Object
String content = new String("The US$ ${amount} billion bilateral trade target between ${country1} and ${country2} set for this year will not be reached."); HashMap<String, String> nameValueMap = new HashMap<String, String>(); nameValueMap.put("amount", "15"); nameValueMap.put("country1", "Cambodia"); nameValueMap.put("country2", "Thailand"); ReplaceVariableLiteralsInContent replaceVariableLiteralsInContent = new ReplaceVariableLiteralsInContent(content, nameValueMap); boolean allOk = replaceVariableLiteralsInContent.execute(); System.out.println(replaceVariableLiteralsInContent.getResultContent()); // The US$ 15 billion bilateral trade target between Cambodia and Thailand set for this year will not be reached. System.out.println("successfulReplaces = " + replaceVariableLiteralsInContent.getNumSuccessfulReplaces()); System.out.println("failedReplaces = " + replaceVariableLiteralsInContent.getNumFailedReplaces()); System.out.println("allOk = " + allOk); if (!allOk) { for (String contentVarName : replaceVariableLiteralsInContent.getFailedReplaceVariablesList()) { System.out.println("- Failed to replace var " + replaceVariableLiteralsInContent.getLeftSidePattern() + contentVarName + replaceVariableLiteralsInContent.getRightSidePattern() + " in content"); } }
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_LEFT_SIDE_PATTERN
The default left side pattern of the literals.
|
static java.lang.String |
DEFAULT_RIGHT_SIDE_PATTERN
The default right side pattern of the literals.
|
Constructor and Description |
---|
ReplaceVariableLiteralsInContent(java.lang.String content,
java.util.Map<java.lang.String,java.lang.String> nameValueMap)
Create a new instance.
|
Modifier and Type | Method and Description |
---|---|
boolean |
execute()
Search and replace the variable literals in content.
|
java.util.List<java.lang.String> |
getFailedReplaceVariablesList()
Get a list of literal variable names whose name was not found in the name/value map.
|
java.lang.String |
getLeftSidePattern()
Get the left side pattern of the literals.
|
int |
getNumFailedReplaces()
Get the number of failed replaces.
|
int |
getNumSuccessfulReplaces()
Get the number of successful replaces.
|
java.lang.String |
getResultContent()
Get the result content.
|
java.lang.String |
getRightSidePattern()
Get the right side pattern of the literals.
|
java.util.List<ReplaceVariableLiteralsSuccessListEntry> |
getSuccessfulReplaceList()
Get a list of all successful replaced variables, inclusive the replaced value and the number of replaces.
|
void |
setLeftSidePattern(java.lang.String leftSidePattern)
Set the left side pattern of the literals.
|
void |
setRightSidePattern(java.lang.String rightSidePattern)
Set the right side pattern of the literals.
|
public static final java.lang.String DEFAULT_LEFT_SIDE_PATTERN
public static final java.lang.String DEFAULT_RIGHT_SIDE_PATTERN
public ReplaceVariableLiteralsInContent(java.lang.String content, java.util.Map<java.lang.String,java.lang.String> nameValueMap)
content
- the content in which the variable literals are replaced.nameValueMap
- a map of key = variable name, value = variable valuejava.lang.IllegalArgumentException
- if the nameValueMap contains a formal invalid variable nameUtils.isFormalValidVariableName(java.lang.String)
public void setLeftSidePattern(java.lang.String leftSidePattern)
leftSidePattern
- the left side pattern of the literals.java.lang.IllegalArgumentException
- if the left side pattern is an empty stringDEFAULT_LEFT_SIDE_PATTERN
public java.lang.String getLeftSidePattern()
public void setRightSidePattern(java.lang.String rightSidePattern)
rightSidePattern
- the right side pattern of the literals.java.lang.IllegalArgumentException
- if the right side pattern is an empty stringDEFAULT_RIGHT_SIDE_PATTERN
public java.lang.String getRightSidePattern()
public boolean execute()
public java.lang.String getResultContent()
public int getNumSuccessfulReplaces()
public java.util.List<ReplaceVariableLiteralsSuccessListEntry> getSuccessfulReplaceList()
public int getNumFailedReplaces()
public java.util.List<java.lang.String> getFailedReplaceVariablesList()