Code with Finding: |
class ReplaceMessages {
/**
* Updates the descendants of a FUNCTION node to represent a message's value.
* <p>
* The tree looks something like:
* <pre>
* function
* |-- name
* |-- lp
* | |-- name <arg1>
* | -- name <arg2>
* -- block
* |
* --return
* |
* --add
* |-- string foo
* -- name <arg1>
* </pre>
*
* @param message a message
* @param functionNode the message's original FUNCTION value node
*
* @throws MalformedException if the passed node's subtree structure is
* not as expected
*/
private void updateFunctionNode(JsMessage message, Node functionNode)
throws MalformedException {
checkNode(functionNode, Token.FUNCTION);
Node nameNode = functionNode.getFirstChild();
checkNode(nameNode, Token.NAME);
Node argListNode = nameNode.getNext();
checkNode(argListNode, Token.LP);
Node oldBlockNode = argListNode.getNext();
checkNode(oldBlockNode, Token.BLOCK);
Iterator<CharSequence> iterator = message.parts().iterator();
Node valueNode = iterator.hasNext()
? constructAddOrStringNode(iterator, argListNode)
: Node.newString("");
Node newBlockNode = new Node(Token.BLOCK,
new Node(Token.RETURN, valueNode));
functionNode.replaceChild(oldBlockNode, newBlockNode);
}
}
|