/* * Copyright 2004 The Closure Compiler Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.javascript.jscomp; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import java.io.IOException; import java.util.Collection; import java.util.List; /** * Extracts messages and message comments from JS code. * *
Uses a special prefix (e.g. {@code MSG_}) to determine which variables
* are messages. Here are the recognized formats:
*
*
* var MSG_FOO = "foo";
* var MSG_FOO_HELP = "this message is used for foo";
*
*
*
* var MSG_BAR = function(a, b) {
* return a + " bar " + b;
* }
* var MSG_BAR_HELP = "the bar message";
*
*
*
This class enforces the policy that message variable names must be unique
* across all JS files.
*
*/
public class JsMessageExtractor {
private final JsMessage.Style style;
private final JsMessage.IdGenerator idGenerator;
private final CompilerOptions options;
public JsMessageExtractor(
JsMessage.IdGenerator idGenerator,
JsMessage.Style style) {
this(idGenerator, style, new CompilerOptions());
}
public JsMessageExtractor(
JsMessage.IdGenerator idGenerator,
JsMessage.Style style,
CompilerOptions options) {
this.idGenerator = idGenerator;
this.style = style;
this.options = options;
}
/**
* Visitor that collects messages.
*/
private class ExtractMessagesVisitor extends JsMessageVisitor {
// We use List here as we want to preserve insertion-order for found
// messages.
// Take into account that messages with the same id could be present in the
// result list. Message could have the same id only in case if they are
// unnamed and have the same text but located in different source files.
private final List