<commit_before>/*******************************************************************************
 * Copyright (c) 2012 Oak Ridge National Laboratory.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.alarm.beast;

import java.util.regex.Pattern;

import org.eclipse.osgi.util.NLS;

/** Formatter for alarm system annunciations
 *
 *  <p>Used by the alarm server to create the annunciation,
 *  also used by the GUI to display what the annunciation
 *  should have been.
 *
 *  @author <NAME>
 *  @author <NAME> - ability to force custom formatting of message even when it doesn't start with '*'
 */
public class AnnunciationFormatter
{
    final private static Pattern priority_pattern = Pattern.compile(Messages.PriorityAnnunciationPattern);

    /** Determine if the format describes a 'priority' message
     *  @param format Format
     *  @return <code>true</code> for priority message
     */
    public static boolean hasPriority(final String format)
    {
        return priority_pattern.matcher(format).matches();
    }

    /** Create message for annunciation
     *  @param format Format to use, either the plain description or a format
     *                that starts with '*' and may then include {0} for the severity
     *                and {1} for the value
     *  @param severity Alarm severity
     *  @param value Alarm value
     *  @param forceCustomFormat Custom-format the message even when format does not start
     *           with '*' (instead of using the default message formatting)
     *  @return Annunciation text
     */
    public static String format(String format, final String severity, String value, boolean forceCustomFormat)
    {
        // Priority flag at start of format?
        format = format.trim();
        boolean priority = format.startsWith(Messages.PriorityAnnunciationPrefix);
        if (priority)
            format = format.substring(Messages.PriorityAnnunciationPrefix.length()).trim();

        String message;
        boolean customFormat = format.startsWith(Messages.FormattedAnnunciationPrefix);
        // Custom format ?
        if (customFormat || forceCustomFormat)
        {
            if (customFormat)
                format = format.substring(Messages.FormattedAnnunciationPrefix.length()).trim();

            // Priority flag at start of custom format?
            if (format.startsWith(Messages.PriorityAnnunciationPrefix))
            {
                priority = true;
                format = format.substring(Messages.PriorityAnnunciationPrefix.length()).trim();
            }
            if (value == null)
                value = "null"; //$NON-NLS-1$
            // Use custom format
            message = NLS.bind(format, severity, value);
        }
        else// Use default format
            message = NLS.bind(Messages.AnnunciationFmt, severity, format);

        message = message.trim();
        // Re-apply priority flag as needed
        if (priority)
            message = Messages.PriorityAnnunciationPrefix + message;

        return message;<commit_msg>#1770 formatting the alarm description to resolve regression errors<commit_after>/*******************************************************************************
 * Copyright (c) 2012 Oak Ridge National Laboratory.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.alarm.beast;

import java.util.regex.Pattern;

import org.eclipse.osgi.util.NLS;

/** Formatter for alarm system annunciations
 *
 *  <p>Used by the alarm server to create the annunciation,
 *  also used by the GUI to display what the annunciation
 *  should have been.
 *
 *  @author <NAME>
 *  @author <NAME> - ability to force custom formatting of message even when it doesn't start with '*'
 */
public class AnnunciationFormatter
{
    final private static Pattern priority_pattern = Pattern.compile(Messages.PriorityAnnunciationPattern);

    /** Determine if the format describes a 'priority' message
     *  @param format Format
     *  @return <code>true</code> for priority message
     */
    public static boolean hasPriority(final String format)
    {
        return priority_pattern.matcher(format).matches();
    }

    /** Create message for annunciation
     *  @param format Format to use, either the plain description or a format
     *                that starts with '*' and may then include {0} for the severity
     *                and {1} for the value
     *  @param severity Alarm severity
     *  @param value Alarm value
     *  @param forceCustomFormat Custom-format the message even when format does not start
     *           with '*' (instead of using the default message formatting)
     *  @return Annunciation text
     */
    public static String format(String format, final String severity, String value, boolean forceCustomFormat)
    {
        // Priority flag at start of format?
        format = format.trim();
        boolean priority = format.startsWith(Messages.PriorityAnnunciationPrefix);
        if (priority)
            format = format.substring(Messages.PriorityAnnunciationPrefix.length()).trim();

        String message;
        boolean customFormat = format.startsWith(Messages.FormattedAnnunciationPrefix);
        // Custom format ?
        if (customFormat || forceCustomFormat)
        {
            if (customFormat)
                format = format.substring(Messages.FormattedAnnunciationPrefix.length()).trim();

            // Priority flag at start of custom format?
            if (format.startsWith(Messages.PriorityAnnunciationPrefix))
            {
                priority = true;
                format = format.substring(Messages.PriorityAnnunciationPrefix.length()).trim();
            }
            if (value == null)
                value = "null"; //$NON-NLS-1$
            // Use custom format
            format = format.replace("{", "'{'");
            format = format.replace("}", "'}'");
            format = format.replaceAll("'\\{'([\\d]+)'\\}'", "{$1}");
            message = NLS.bind(format, severity, value);
        }
        else// Use default format
            message = NLS.bind(Messages.AnnunciationFmt, severity, format);

        message = message.trim();
        // Re-apply priority flag as needed
        if (priority)
            message = Messages.PriorityAnnunciationPrefix + message;

        return message;
{'fraction_non_alphanumeric': 0.1312066974595843, 'fraction_numerical': 0.003897228637413395, 'mean_word_length': 2.6856382978723405, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5223123', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1618, 'n_words': 558}