Code with Misuse: |
class ReplaceMessages {
/**
* Creates a parse tree corresponding to the remaining message parts in
* an iteration. The result will contain only STRING nodes, NAME nodes
* (corresponding to placeholder references), and/or ADD nodes used to
* combine the other two types.
*
* @param partsIterator an iterator over message parts
* @param argListNode an LP node whose children are valid placeholder names
* @return the root of the constructed parse tree
*
* @throws MalformedException if {@code partsIterator} contains a
* placeholder reference that does not correspond to a valid argument in
* the arg list
*/
private Node constructAddOrStringNode(Iterator<CharSequence> partsIterator,
Node argListNode)
throws MalformedException {
CharSequence part = partsIterator.next();
Node partNode = null;
if (part instanceof JsMessage.PlaceholderReference) {
JsMessage.PlaceholderReference phRef =
(JsMessage.PlaceholderReference) part;
for (Node node : argListNode.children()) {
if (node.getType() == Token.NAME) {
String arg = node.getString();
// We ignore the case here because the transconsole only supports
// uppercase placeholder names, but function arguments in javascript
// code can have mixed case.
if (arg.equalsIgnoreCase(phRef.getName())) {
partNode = Node.newString(Token.NAME, arg);
}
}
}
if (partNode == null) {
throw new MalformedException(
"Unrecognized message placeholder referenced: " + phRef.getName(),
argListNode);
}
} else {
// The part is just a string literal.
partNode = Node.newString(part.toString());
}
if (partsIterator.hasNext()) {
return new Node(Token.ADD, partNode,
constructAddOrStringNode(partsIterator, argListNode));
} else {
return partNode;
}
}
}
|