class ProcessClosurePrimitives {
/**
* Processes a call to goog.setCssNameMapping(). Either the argument to
* goog.setCssNameMapping() is valid, in which case it will be used to create
* a CssRenamingMap for the compiler of this CompilerPass, or it is invalid
* and a JSCompiler error will be reported.
* @see #visit(NodeTraversal, Node, Node)
*/
private void processSetCssNameMapping(NodeTraversal t, Node n, Node parent) {
Node left = n.getFirstChild();
Node arg = left.getNext();
if (verifyArgument(t, left, arg, Token.OBJECTLIT)) {
// Translate OBJECTLIT into SubstitutionMap. All keys and
// values must be strings, or an error will be thrown.
final Map<String, String> cssNames = Maps.newHashMap();
JSError error = null;
for (Node key = arg.getFirstChild(); key != null;
key = key.getNext()) {
Node value = key.getFirstChild();
if (key.getType() != Token.STRING
|| value == null
|| value.getType() != Token.STRING) {
error = t.makeError(n,
NON_STRING_PASSED_TO_SET_CSS_NAME_MAPPING_ERROR);
}
if (error != null) {
compiler.report(error);
break;
}
cssNames.put(key.getString(), value.getString());
}
// If there were no errors, create a CssRenamingMap from cssNames, update
// the compiler to use it and remove the call to goog.setCssNameMapping().
if (error == null) {
CssRenamingMap cssRenamingMap = new CssRenamingMap() {
public String get(String value) {
if (cssNames.containsKey(value)) {
return cssNames.get(value);
} else {
return value;
}
}
};
compiler.setCssRenamingMap(cssRenamingMap);
parent.getParent().removeChild(parent);
compiler.reportCodeChange();
}
}
}
}