001/*
002 * Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025package org.jdrupes.mdoclet.internal.doclets.toolkit;
026
027import javax.lang.model.element.Element;
028import javax.tools.Diagnostic;
029import javax.tools.FileObject;
030
031import com.sun.source.util.DocTreePath;
032import jdk.javadoc.doclet.Reporter;
033
034import static javax.tools.Diagnostic.Kind.*;
035
036/**
037 * Provides standardized access to the diagnostic reporting facilities
038 * for a doclet.
039 *
040 * Messages are specified by resource keys to be found in the doclet's
041 * {@link Resources resources}.  Values can be substituted into the
042 * strings obtained from the resource files.
043 *
044 * Messages are reported to the doclet's {@link Reporter reporter}.
045 */
046public class Messages {
047    private final BaseConfiguration configuration;
048    private final Resources resources;
049    private final Reporter reporter;
050
051    /**
052     * Creates a {@code Messages} object to provide standardized access to
053     * the doclet's diagnostic reporting mechanisms.
054     *
055     * @param configuration the doclet's configuration, used to access the doclet's
056     *                      reporter, and additional methods and state used to
057     *                      filter out messages, if any, which should be suppressed.
058     * @param resources     resources for console messages and exceptions
059     */
060    public Messages(BaseConfiguration configuration, Resources resources) {
061        this.configuration = configuration;
062        this.resources = resources;
063        reporter = configuration.getReporter();
064    }
065
066    /**
067     * Returns the resources being used when generating messages.
068     *
069     * @return the resources
070     */
071    public Resources getResources() {
072        return resources;
073    }
074
075    // ***** Errors *****
076
077    /**
078     * Reports an error message to the doclet's reporter.
079     *
080     * @param key  the name of a resource containing the message to be printed
081     * @param args optional arguments to be replaced in the message
082     */
083    public void error(String key, Object... args) {
084        report(ERROR, resources.getText(key, args));
085    }
086
087    /**
088     * Reports an error message to the doclet's reporter.
089     *
090     * @param path a path identifying the position to be included with the message
091     * @param key  the name of a resource containing the message to be printed
092     * @param args optional arguments to be replaced in the message
093     */
094    public void error(DocTreePath path, String key, Object... args) {
095        report(ERROR, path, resources.getText(key, args));
096    }
097
098    /**
099     * Reports an error message to the doclet's reporter.
100     *
101     * @param path  a path identifying the position to be included with the message
102     * @param start the start of a range of characters to be associated with the message
103     * @param pos   the position to be associated with the message
104     * @param end   the end of a range of characters to be associated with the message
105     * @param key   the name of a resource containing the message to be printed
106     * @param args  optional arguments to be replaced in the message
107     */
108    public void error(DocTreePath path, int start, int pos, int end, String key,
109            Object... args) {
110        report(ERROR, path, start, pos, end, resources.getText(key, args));
111    }
112
113    /**
114     * Reports an error message to the doclet's reporter.
115     *
116     * @param fo    the file object to be associated with the message
117     * @param start the start of a range of characters to be associated with the message
118     * @param pos   the position to be associated with the message
119     * @param end   the end of a range of characters to be associated with the message
120     * @param key   the name of a resource containing the message to be printed
121     * @param args  optional arguments to be replaced in the message
122     */
123    public void error(FileObject fo, int start, int pos, int end, String key,
124            Object... args) {
125        report(ERROR, fo, start, pos, end, resources.getText(key, args));
126    }
127
128    /**
129     * Reports an error message to the doclet's reporter.
130     *
131     * @param e    an element identifying the position to be included with the message
132     * @param key  the name of a resource containing the message to be printed
133     * @param args optional arguments to be replaced in the message
134     */
135    public void error(Element e, String key, Object... args) {
136        report(ERROR, e, resources.getText(key, args));
137    }
138
139    // ***** Warnings *****
140
141    /**
142     * Reports a warning message to the doclet's reporter.
143     *
144     * @param key  the name of a resource containing the message to be printed
145     * @param args optional arguments to be replaced in the message
146     */
147    public void warning(String key, Object... args) {
148        report(WARNING, resources.getText(key, args));
149    }
150
151    /**
152     * Reports a warning message to the doclet's reporter.
153     *
154     * @param path a path identifying the position to be included with the message
155     * @param key  the name of a resource containing the message to be printed
156     * @param args optional arguments to be replaced in the message
157     */
158    public void warning(DocTreePath path, String key, Object... args) {
159        report(WARNING, path, resources.getText(key, args));
160    }
161
162    /**
163     * Reports a warning message to the doclet's reporter.
164     *
165     * @param path  a path identifying the position to be included with the message
166     * @param start the start of a range of characters to be associated with the message
167     * @param pos   the position to be associated with the message
168     * @param end   the end of a range of characters to be associated with the message
169     * @param key   the name of a resource containing the message to be printed
170     * @param args  optional arguments to be replaced in the message
171     */
172    public void warning(DocTreePath path, int start, int pos, int end,
173            String key, Object... args) {
174        report(WARNING, path, start, pos, end, resources.getText(key, args));
175    }
176
177    /**
178     * Reports a warning message to the doclet's reporter.
179     *
180     * @param e    an element identifying the position to be included with the message
181     * @param key  the name of a resource containing the message to be printed
182     * @param args optional arguments to be replaced in the message
183     */
184    public void warning(Element e, String key, Object... args) {
185        report(WARNING, e, resources.getText(key, args));
186    }
187
188    /**
189     * Reports a warning message to the doclet's reporter.
190     *
191     * @param fo    the file object to be associated with the message
192     * @param start the start of a range of characters to be associated with the message
193     * @param pos   the position to be associated with the message
194     * @param end   the end of a range of characters to be associated with the message
195     * @param key   the name of a resource containing the message to be printed
196     * @param args  optional arguments to be replaced in the message
197     */
198    public void warning(FileObject fo, int start, int pos, int end, String key,
199            Object... args) {
200        report(WARNING, fo, start, pos, end, resources.getText(key, args));
201    }
202
203    // ***** Notices *****
204
205    /**
206     * Reports an informational notice to the doclet's reporter.
207     * The message is written directly to the reporter's diagnostic stream.
208     *
209     * @param key  the name of a resource containing the message to be printed
210     * @param args optional arguments to be replaced in the message
211     */
212    public void notice(String key, Object... args) {
213        if (!configuration.getOptions().quiet()) {
214            // Note: we do not use report(NOTE, ...) which would prefix the
215            // output with "Note:"
216            reporter.getDiagnosticWriter()
217                .println(resources.getText(key, args));
218        }
219    }
220
221    // ***** Internal support *****
222
223    private void report(Diagnostic.Kind k, String msg) {
224        reporter.print(k, msg);
225    }
226
227    private void report(Diagnostic.Kind k, DocTreePath p, String msg) {
228        reporter.print(k, p, msg);
229    }
230
231    private void report(Diagnostic.Kind k, Element e, String msg) {
232        reporter.print(k, e, msg);
233    }
234
235    private void report(Diagnostic.Kind k, FileObject fo, int start, int pos,
236            int end, String msg) {
237        reporter.print(k, fo, start, pos, end, msg);
238    }
239
240    private void report(Diagnostic.Kind k, DocTreePath path, int start, int pos,
241            int end, String msg) {
242        reporter.print(k, path, start, pos, end, msg);
243    }
244}