public class ReplaceVariableLiteralsInBinaryContent
extends java.lang.Object
byte[] content = "The US$ ${amount} billion bilateral trade target between ${country1} and ${country2} set for this year will not be reached.".getBytes(StandardCharsets.UTF_8);
HashMap<String, String> nameValueMap = new HashMap<String, String>();
nameValueMap.put("amount", "15");
nameValueMap.put("country1", "Cambodia");
nameValueMap.put("country2", "Thailand");
ReplaceVariableLiteralsInBinaryContent replaceVariableLiteralsInBinaryContent = new ReplaceVariableLiteralsInBinaryContent(content, nameValueMap);
boolean allOk = replaceVariableLiteralsInBinaryContent.execute();
System.out.println(new String(replaceVariableLiteralsInBinaryContent.getResultContent(), StandardCharsets.UTF_8)); // The US$ 15 billion bilateral trade target between Cambodia and Thailand set for this year will not be reached.
System.out.println("successfulReplaces = " + replaceVariableLiteralsInBinaryContent.getNumSuccessfulReplaces());
System.out.println("failedReplaces = " + replaceVariableLiteralsInBinaryContent.getNumFailedReplaces());
System.out.println("allOk = " + allOk);
if (!allOk) {
for (String contentVarName : replaceVariableLiteralsInBinaryContent.getFailedReplaceVariablesList()) {
System.out.println("- Failed to replace var " + new String(replaceVariableLiteralsInBinaryContent.getLeftSidePattern(), StandardCharsets.UTF_8) + contentVarName + new String(replaceVariableLiteralsInBinaryContent.getRightSidePattern(), StandardCharsets.UTF_8) + " in content");
}
}
| Modifier and Type | Field and Description |
|---|---|
static byte[] |
DEFAULT_LEFT_SIDE_PATTERN
The default left side binary pattern of the literals.
|
static byte[] |
DEFAULT_RIGHT_SIDE_PATTERN
The default right side binary pattern of the literals.
|
| Constructor and Description |
|---|
ReplaceVariableLiteralsInBinaryContent(byte[] 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 the binary 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.
|
byte[] |
getLeftSidePattern()
Get the left side binary pattern of the literals.
|
int |
getNumFailedReplaces()
Get the number of failed replaces.
|
int |
getNumSuccessfulReplaces()
Get the number of successful replaces.
|
byte[] |
getResultContent()
Get the result content.
|
byte[] |
getRightSidePattern()
Get the right side binary 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(byte[] leftSidePattern)
Set the left side binary pattern of the literals.
|
void |
setRightSidePattern(byte[] rightSidePattern)
Set the right side binary pattern of the literals.
|
public static final byte[] DEFAULT_LEFT_SIDE_PATTERN
public static final byte[] DEFAULT_RIGHT_SIDE_PATTERN
public ReplaceVariableLiteralsInBinaryContent(byte[] content,
java.util.Map<java.lang.String,java.lang.String> nameValueMap)
content - the binary 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(byte[] leftSidePattern)
leftSidePattern - the left side binary pattern of the literals.java.lang.IllegalArgumentException - if the left side binary pattern has zero lengthDEFAULT_LEFT_SIDE_PATTERNpublic byte[] getLeftSidePattern()
public void setRightSidePattern(byte[] rightSidePattern)
rightSidePattern - the right side binary pattern of the literals.java.lang.IllegalArgumentException - if the right side pattern has zero lengthDEFAULT_RIGHT_SIDE_PATTERNpublic byte[] getRightSidePattern()
public boolean execute()
public byte[] getResultContent()
public int getNumSuccessfulReplaces()
public java.util.List<ReplaceVariableLiteralsSuccessListEntry> getSuccessfulReplaceList()
public int getNumFailedReplaces()
public java.util.List<java.lang.String> getFailedReplaceVariablesList()