| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 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 |
|
| 72 |
|
| 73 |
private class ExtractMessagesVisitor extends JsMessageVisitor { |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 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 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public Collection<JsMessage> getMessages() { |
| 99 |
return messages; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public Collection<JsMessage> extractMessages(SourceFile... inputs) |
| 107 |
throws IOException { |
| 108 |
return extractMessages(ImmutableList.copyOf(inputs)); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 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 |
|
| 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 |
} |