Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2004 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.ImmutableList;
20 import com.google.common.collect.Lists;
21
22 import java.io.IOException;
23 import java.util.Collection;
24 import java.util.List;
25
26
27 /**
28 * Extracts messages and message comments from JS code.
29 *
30 * <p> Uses a special prefix (e.g. {@code MSG_}) to determine which variables
31 * are messages. Here are the recognized formats:
32 *
33 * <code>
34 * var MSG_FOO = "foo";
35 * var MSG_FOO_HELP = "this message is used for foo";
36 * </code>
37 *
38 * <code>
39 * var MSG_BAR = function(a, b) {
40 * return a + " bar " + b;
41 * }
42 * var MSG_BAR_HELP = "the bar message";
43 * </code>
44 *
45 * <p>This class enforces the policy that message variable names must be unique
46 * across all JS files.
47 *
48 */
49 public class JsMessageExtractor {
50
51 private final JsMessage.Style style;
52 private final JsMessage.IdGenerator idGenerator;
53 private final CompilerOptions options;
54
55 public JsMessageExtractor(
56 JsMessage.IdGenerator idGenerator,
57 JsMessage.Style style) {
58 this(idGenerator, style, new CompilerOptions());
59 }
60
61 public JsMessageExtractor(
62 JsMessage.IdGenerator idGenerator,
63 JsMessage.Style style,
64 CompilerOptions options) {
65 this.idGenerator = idGenerator;
66 this.style = style;
67 this.options = options;
68 }
69
70 /**
71 * Visitor that collects messages.
72 */
73 private class ExtractMessagesVisitor extends JsMessageVisitor {
74 // We use List here as we want to preserve insertion-order for found
75 // messages.
76 // Take into account that messages with the same id could be present in the
77 // result list. Message could have the same id only in case if they are
78 // unnamed and have the same text but located in different source files.
79 private final List<JsMessage> messages = Lists.newLinkedList();
80
81 private ExtractMessagesVisitor(AbstractCompiler compiler) {
82 super(compiler, true, style, idGenerator);
83 }
84
85 @Override
86 void processJsMessage(JsMessage message,
87 JsMessageDefinition definition) {
88 if (!message.isExternal()) {
89 messages.add(message);
90 }
91 }
92
93 /**
94 * Returns extracted messages.
95 *
96 * @return collection of JsMessage objects that was found in js sources.
97 */
98 public Collection<JsMessage> getMessages() {
99 return messages;
100 }
101 }
102
103 /**
104 * Extracts JS messages from JavaScript code.
105 */
106 public Collection<JsMessage> extractMessages(SourceFile... inputs)
107 throws IOException {
108 return extractMessages(ImmutableList.copyOf(inputs));
109 }
110
111
112 /**
113 * Extracts JS messages from JavaScript code.
114 *
115 * @param inputs the JavaScript source code inputs
116 * @return the extracted messages collection
117 * @throws IOException if there is a problem reading the JS code
118 * @throws RuntimeException if there are problems parsing the JS code or the
119 * JS messages, or if two messages have the same key
120 */
121 public <T extends SourceFile> Collection<JsMessage> extractMessages(
122 Iterable<T> inputs) throws IOException {
123
124 Compiler compiler = new Compiler();
125 compiler.init(
126 ImmutableList.<SourceFile>of(),
127 Lists.newArrayList(inputs),
128 options);
129 compiler.parseInputs();
130
131 ExtractMessagesVisitor extractCompilerPass =
132 new ExtractMessagesVisitor(compiler);
133 if (compiler.getErrors().length == 0) {
134 extractCompilerPass.process(null, compiler.getRoot());
135 }
136
137 JSError[] errors = compiler.getErrors();
138 // Check for errors.
139 if (errors.length > 0) {
140 StringBuilder msg = new StringBuilder("JSCompiler errors\n");
141 MessageFormatter formatter = new LightweightMessageFormatter(compiler);
142 for (JSError e : errors) {
143 msg.append(formatter.formatError(e));
144 }
145 throw new RuntimeException(msg.toString());
146 }
147
148 return extractCompilerPass.getMessages();
149 }
150 }