Package com.kre8orz.i18n.processor

Source Code of com.kre8orz.i18n.processor.I18NProcessorMessages

/* Copyright Tom Valine 2002,2014 All Rights Reserved. ****************************************************************/
package com.kre8orz.i18n.processor;

import com.kre8orz.i18n.I18N;
import com.kre8orz.i18n.annotation.I18NMessage;
import com.kre8orz.i18n.annotation.I18NMessages;
import com.kre8orz.i18n.annotation.I18NResourceBundle;
import java.util.Locale;
import javax.annotation.processing.Messager;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.tools.Diagnostic;
import javax.tools.Diagnostic.Kind;

/**
* Internationalization and string constant factory used to access messages that are generated by the processor. The
* underlying property files are generated automatically by running the I18NProcessor with catalog class generation
* disabled, after the main compilation step. Do not create property files to support this nor directly modify any
* resource bundles generated from this source. Required changes should be made by modifying the <tt>I18NMessage</tt>
* annotations in this source file.
*
* @author  Tom Valine (thomas.valine@gmail.com)
*/
@I18NResourceBundle(bundleName = "pm" /*NOI18N*/, packageName = "com.kre8orz.i18n.processor.r" /*NOI18N*/)
class I18NProcessorMessages {

    //~ Instance fields ************************************************************************************************

    private final Messager _host;
    private I18N _i18n;
    private final String _key;
//J-
    @I18NMessages({
        @I18NMessage(value = "Cannot open bundle."),
        @I18NMessage(locale = "es_ES", value = "No se puede abrir el recurso.")
    })
    static final String CANNOT_OPEN_BUNDLE = "M0001";

    @I18NMessages({
        @I18NMessage(value = "Cannot open source file."),
        @I18NMessage(locale = "es_ES", value = "No se puede abrir la fuente.")
    })
    static final String CANNOT_OPEN_SOURCE = "M0002";

    @I18NMessages({
        @I18NMessage(value = "There was an error setting {0} to {1}."),
        @I18NMessage(locale = "es_ES", value = "Hubo un error al establecer la opci\\u00F3n {0} a {1}.")
    })
    static final String ERROR_SETTING_OPTION = "M0003";

    @I18NMessages({
        @I18NMessage(value = "The {0} option was set to {1}."),
        @I18NMessage(locale = "es_ES", value = "La opci\\u00F3n {0} se establece en {1}.")
    })
    static final String OPTION_SET_TO = "M0004";

    @I18NMessages({
        @I18NMessage(value = "Only string constants may be I18N annotation targets. Ignoring."),
        @I18NMessage(locale = "es_ES", value = "S\\u00F3lo las constantes de cadena pueden ser objeto de anotaci\\u00F3n I18N.  Haciendo caso omiso.")
    })
    static final String STRING_CONSTANT_REQUIRED = "M0005";
//J+
    private final Locale _locale;
    private final Diagnostic.Kind _verbosity;

    //~ Constructors ***************************************************************************************************

    /**
     * Creates a new I18NProcessorMessages object which will generate localized messages using the supplied locale.
     *
     * @param  host       The messenger host to which messages are sent if they are not filtered by the verbosity
     *                    setting.
     * @param  locale     The locale used to select the message language.
     * @param  verbosity  The verbosity level. Messages having a lower urgency will not be sent to the processor host's
     *                    messenger.
     */
    public I18NProcessorMessages(Messager host, Locale locale, Kind verbosity) {
        _locale = locale;
        _host = host;
        _verbosity = verbosity;
        _key = I18NProcessorOptions.getKey();
    }

    //~ Methods ********************************************************************************************************

    /**
     * Retrieves, formats and records a localized message.
     *
     * @param   kind  The kind of message to record.
     * @param   key   The message key used to retrieve the message from the localized resource bundle.
     * @param   args  The formatting parameters.
     *
     * @return  The localized message or null if the message is filtered due to verbosity settings.
     *
     * @see     com.kre8orz.i18n.I18N
     * @see     javax.annotation.processing.Messager
     */
    public String format(Kind kind, String key, Object... args) {
        String msg = null;

        if (_canRecordMessagesOfType(kind)) {
            msg = _i18n().get(key, args);
            _host.printMessage(kind, msg);
        }
        return msg;
    }

    /**
     * Retrieves, formats and records a localized message.
     *
     * @param   kind     The kind of message to record.
     * @param   key      The message key used to retrieve the message from the localized resource bundle.
     * @param   element  The element associated with the message.
     * @param   args     The formatting parameters.
     *
     * @return  The localized message or null if the message is filtered due to verbosity settings.
     *
     * @see     com.kre8orz.i18n.I18N
     * @see     javax.annotation.processing.Messager
     */
    public String format(Kind kind, String key, Element element, Object... args) {
        String msg = null;

        if (_canRecordMessagesOfType(kind)) {
            msg = _i18n().get(key, args);
            _host.printMessage(kind, msg, element);
        }
        return msg;
    }

    /**
     * Retrieves, formats and records a localized message.
     *
     * @param   kind     The kind of message to record.
     * @param   key      The message key used to retrieve the message from the localized resource bundle.
     * @param   element  The element associated with the message.
     * @param   aMirror  The annotation mirror associated with the message.
     * @param   args     The formatting parameters.
     *
     * @return  The localized message or null if the message is filtered due to verbosity settings.
     *
     * @see     com.kre8orz.i18n.I18N
     * @see     javax.annotation.processing.Messager
     */
    public String format(Kind kind, String key, Element element, AnnotationMirror aMirror, Object... args) {
        String msg = null;

        if (_canRecordMessagesOfType(kind)) {
            msg = _i18n().get(key, args);
            _host.printMessage(kind, msg, element, aMirror);
        }
        return msg;
    }

    /**
     * Retrieves, formats and records a localized message.
     *
     * @param   kind     The kind of message to record.
     * @param   key      The message key used to retrieve the message from the localized resource bundle.
     * @param   element  The element associated with the message.
     * @param   aMirror  The annotation mirror associated with the message.
     * @param   aValue   The annotation value associated with the message.
     * @param   args     The formatting parameters.
     *
     * @return  The localized message or null if the message is filtered due to verbosity settings.
     *
     * @see     com.kre8orz.i18n.I18N
     * @see     javax.annotation.processing.Messager
     */
    public String format(Kind kind, String key, Element element, AnnotationMirror aMirror, AnnotationValue aValue,
        Object... args) {
        String msg = null;

        if (_canRecordMessagesOfType(kind)) {
            msg = _i18n().get(key, args);
            _host.printMessage(kind, msg, element, aMirror, aValue);
        }
        return msg;
    }

    /**
     * Retrieves and records a localized message.
     *
     * @param   kind  The kind of message to record.
     * @param   key   The message key used to retrieve the message from the localized resource bundle.
     *
     * @return  The localized message or null if the message is filtered due to verbosity settings.
     *
     * @see     com.kre8orz.i18n.I18N
     * @see     javax.annotation.processing.Messager
     */
    public String record(Kind kind, String key) {
        String msg = null;

        if (_canRecordMessagesOfType(kind)) {
            msg = _i18n().get(key);
            _host.printMessage(kind, msg);
        }
        return msg;
    }

    /**
     * Retrieves and records a localized message.
     *
     * @param   kind     The kind of message to record.
     * @param   key      The message key used to retrieve the message from the localized resource bundle.
     * @param   element  The element associated with the message.
     *
     * @return  The localized message or null if the message is filtered due to verbosity settings.
     *
     * @see     com.kre8orz.i18n.I18N
     * @see     javax.annotation.processing.Messager
     */
    public String record(Kind kind, String key, Element element) {
        String msg = null;

        if (_canRecordMessagesOfType(kind)) {
            msg = _i18n().get(key);
            _host.printMessage(kind, msg, element);
        }
        return msg;
    }

    /**
     * Retrieves and records a localized message.
     *
     * @param   kind     The kind of message to record.
     * @param   key      The message key used to retrieve the message from the localized resource bundle.
     * @param   element  The element associated with the message.
     * @param   aMirror  The annotation mirror associated with the message.
     *
     * @return  The localized message or null if the message is filtered due to verbosity settings.
     *
     * @see     com.kre8orz.i18n.I18N
     * @see     javax.annotation.processing.Messager
     */
    public String record(Kind kind, String key, Element element, AnnotationMirror aMirror) {
        String msg = null;

        if (_canRecordMessagesOfType(kind)) {
            msg = _i18n().get(key);
            _host.printMessage(kind, msg, element, aMirror);
        }
        return msg;
    }

    /**
     * Retrieves and records a localized message.
     *
     * @param   kind     The kind of message to record.
     * @param   key      The message key used to retrieve the message from the localized resource bundle.
     * @param   element  The element associated with the message.
     * @param   aMirror  The annotation mirror associated with the message.
     * @param   aValue   The annotation value associated with the message.
     *
     * @return  The localized message or null if the message is filtered due to verbosity settings.
     *
     * @see     com.kre8orz.i18n.I18N
     * @see     javax.annotation.processing.Messager
     */
    public String record(Kind kind, String key, Element element, AnnotationMirror aMirror, AnnotationValue aValue) {
        String msg = null;

        if (_canRecordMessagesOfType(kind)) {
            msg = _i18n().get(key);
            _host.printMessage(kind, msg, element, aMirror, aValue);
        }
        return msg;
    }

    /* Indicates whether or not a message should be recorded. */
    private boolean _canRecordMessagesOfType(Kind kind) {
        return kind.ordinal() <= _verbosity.ordinal();
    }

    private synchronized I18N _i18n() {
        if (_i18n == null) {
            _i18n = new I18N("com/kre8orz/i18n/processor/r/pm" /*NOI18N*/, _locale, _key);
        }
        return _i18n;
    }
}
/* Copyright Tom Valine 2002,2014 All Rights Reserved. ****************************************************************/ 
TOP

Related Classes of com.kre8orz.i18n.processor.I18NProcessorMessages

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.