Debian Codegraph @

src/com/google/javascript/jscomp/ReplaceMessagesForChrome.java

raw symbols SCIP
1 /*
2 * Copyright 2012 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.common.collect.Lists;
20 import com.google.javascript.jscomp.JsMessage.PlaceholderReference;
21 import com.google.javascript.rhino.IR;
22 import com.google.javascript.rhino.Node;
23 import com.google.javascript.rhino.Token;
24
25 import java.util.Collections;
26 import java.util.List;
27
28
29 /**
30 * Replaces user-visible messages with appropriate calls to
31 * chrome.i18n.getMessage. The first argument to getMessage is the id of the
32 * message, as a string. If the message contains placeholders, the second
33 * argument is an array of the values being used for the placeholders, in the
34 * order they appear in the source code.
35 *
36 */
37 class ReplaceMessagesForChrome extends JsMessageVisitor {
38
39 ReplaceMessagesForChrome(AbstractCompiler compiler,
40 JsMessage.IdGenerator idGenerator,
41 boolean checkDuplicatedMessages, JsMessage.Style style) {
42
43 super(compiler, checkDuplicatedMessages, style, idGenerator);
44 }
45
46 private static Node getChromeI18nGetMessageNode(String messageId) {
47 Node chromeI18n = IR.getprop(IR.name("chrome"), IR.string("i18n"));
48 Node getMessage = IR.getprop(chromeI18n, IR.string("getMessage"));
49 return IR.call(getMessage, IR.string(messageId));
50 }
51
52 @Override
53 protected void processJsMessage(
54 JsMessage message, JsMessageDefinition definition) {
55 try {
56 Node msgNode = definition.getMessageNode();
57 Node newValue = getNewValueNode(msgNode, message);
58 newValue.copyInformationFromForTree(msgNode);
59
60 definition.getMessageParentNode().replaceChild(msgNode, newValue);
61 compiler.reportCodeChange();
62 } catch (MalformedException e) {
63 compiler.report(JSError.make(message.getSourceName(), e.getNode(),
64 MESSAGE_TREE_MALFORMED, e.getMessage()));
65 }
66 }
67
68 private Node getNewValueNode(Node origNode, JsMessage message)
69 throws MalformedException {
70 Node newValueNode = getChromeI18nGetMessageNode(message.getId());
71
72 if (!message.placeholders().isEmpty()) {
73 Node placeholderValues = origNode.getLastChild();
74 checkNode(placeholderValues, Token.OBJECTLIT);
75
76 // Output the placeholders, sorted alphabetically by placeholder name,
77 // regardless of what order they appear in the original message.
78 List<String> placeholderNames = Lists.newArrayList();
79 for (CharSequence cs : message.parts()) {
80 if (cs instanceof PlaceholderReference) {
81 String placeholderName = ((PlaceholderReference) cs).getName();
82 placeholderNames.add(placeholderName);
83 }
84 }
85 Collections.sort(placeholderNames);
86
87 Node placeholderValueArray = IR.arraylit();
88 for (String name : placeholderNames) {
89 Node value = getPlaceholderValue(placeholderValues, name);
90 if (value == null) {
91 throw new MalformedException(
92 "No value was provided for placeholder " + name,
93 origNode);
94 }
95 placeholderValueArray.addChildToBack(value);
96 }
97 newValueNode.addChildToBack(placeholderValueArray);
98 }
99
100 newValueNode.copyInformationFromForTree(origNode);
101 return newValueNode;
102 }
103
104 private Node getPlaceholderValue(
105 Node placeholderValues, String placeholderName) {
106 for (Node key : placeholderValues.children()) {
107 if (key.getString().equals(placeholderName)) {
108 return key.getFirstChild().cloneTree();
109 }
110 }
111 return null;
112 }
113 }