Java Code Examples for java.text.MessageFormat#getFormatsByArgumentIndex()

The following examples show how to use java.text.MessageFormat#getFormatsByArgumentIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ErrorFactory.java    From alibaba-flink-connectors with Apache License 2.0 6 votes vote down vote up
/**
 * Parameter check when invoking method.
 *
 * @param method
 * @param args
 */
protected static void checkParam(Method method, Object[] args) {
	ErrCode errCode = method.getAnnotation(ErrCode.class);
	String errDetail = errCode.details();
	String errCause = errCode.cause();

	MessageFormat format1 = new MessageFormat(errDetail);
	MessageFormat format2 = new MessageFormat(errCause);

	if (args == null || args.length == 0) {
		if ((format1.getFormatsByArgumentIndex() != null && format1.getFormatsByArgumentIndex().length > 0)
			|| (format2.getFormatsByArgumentIndex() != null && format2.getFormatsByArgumentIndex().length > 0)) {
			throw new AssertionError("mismatched parameter length between "
				+ method.getName() + " and its annotation @ErrCode");
		}
	} else {
		if ((format1.getFormatsByArgumentIndex() != null && format1.getFormatsByArgumentIndex().length > args.length)
			|| format1.getFormatsByArgumentIndex() == null
			|| (format2.getFormatsByArgumentIndex() != null && format2.getFormatsByArgumentIndex().length > args.length)) {
			throw new AssertionError("mismatched parameter length between "
				+ method.getName() + " and its annotation @ErrCode");
		}
	}
}
 
Example 2
Source File: MessageUtils.java    From primecloud-controller with GNU General Public License v2.0 6 votes vote down vote up
/**
 * TODO: メソッドコメントを記述
 *
 * @param pattern
 * @param arguments
 * @return
 */
public static String format(String pattern, Object... arguments) {
    pattern = pattern.replace("'", "''");
    MessageFormat messageFormat = new MessageFormat(pattern);

    if (arguments != null && arguments.length > 0) {
        Format[] formats = messageFormat.getFormatsByArgumentIndex();
        for (int i = 0; i < arguments.length; i++) {
            if (formats.length <= i) {
                continue;
            }

            // 引数が数値でフォーマットの指定がない場合、数値をそのまま出力するために文字列に変換する
            if (arguments[i] != null && arguments[i] instanceof Number && formats[i] == null) {
                arguments[i] = arguments[i].toString();
            }
        }
    }

    return messageFormat.format(arguments);
}
 
Example 3
Source File: FilterEncoder.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Formats a filter and handles encoding of special characters in the value arguments using the
 * &lt;valueencoding&gt; rule as described in <a href="http://www.ietf.org/rfc/rfc4515.txt">RFC 4515</a>.
 * <p>
 * Example of filter template format: <code>(&amp;(cn={0})(uid={1}))</code>
 * 
 * @param filterTemplate the filter with placeholders
 * @param values the values to encode and substitute
 * @return the formatted filter with escaped values
 * @throws IllegalArgumentException if the number of values does not match the number of placeholders in the template
 */
public static String format( String filterTemplate, String... values )
{
    if ( values == null )
    {
        values = Strings.EMPTY_STRING_ARRAY;
    }

    MessageFormat mf = new MessageFormat( filterTemplate, Locale.ROOT );

    // check element count and argument count
    Format[] formats = mf.getFormatsByArgumentIndex();
    
    if ( formats.length != values.length )
    {
        throw new IllegalArgumentException( I18n.err( I18n.ERR_13300_BAD_PLACE_HOLDERS_NUMBER, filterTemplate, formats.length, values.length ) );
    }

    // encode arguments
    for ( int i = 0; i < values.length; i++ )
    {
        values[i] = encodeFilterValue( values[i] );
    }

    // format the filter
    return mf.format( values );
}
 
Example 4
Source File: MessageFormatsByArgumentIndex.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    Format[] subformats;

    MessageFormat format = new MessageFormat("{3, choice," + choicePattern + "}, {2}, {0}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, null);
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);

    format.setFormatByArgumentIndex(0, NumberFormat.getInstance());

    checkPattern(format.toPattern(), "{3,choice," + choicePattern + "}, {2}, {0,number}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, NumberFormat.getInstance());
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, NumberFormat.getInstance());

    format.setFormatsByArgumentIndex(subformats);

    checkPattern(format.toPattern(), "{3,choice," + choicePattern + "}, {2,number}, {0,choice," + choicePattern + "}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, NumberFormat.getInstance());
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, NumberFormat.getInstance());
    checkSubformat(subformats, 2, new ChoiceFormat(choicePattern));

}
 
Example 5
Source File: MessageFormatsByArgumentIndex.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    Format[] subformats;

    MessageFormat format = new MessageFormat("{3, choice," + choicePattern + "}, {2}, {0}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, null);
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);

    format.setFormatByArgumentIndex(0, NumberFormat.getInstance());

    checkPattern(format.toPattern(), "{3,choice," + choicePattern + "}, {2}, {0,number}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, NumberFormat.getInstance());
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, NumberFormat.getInstance());

    format.setFormatsByArgumentIndex(subformats);

    checkPattern(format.toPattern(), "{3,choice," + choicePattern + "}, {2,number}, {0,choice," + choicePattern + "}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, NumberFormat.getInstance());
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, NumberFormat.getInstance());
    checkSubformat(subformats, 2, new ChoiceFormat(choicePattern));

}
 
Example 6
Source File: MessageFormatsByArgumentIndex.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    Format[] subformats;

    MessageFormat format = new MessageFormat("{3, choice," + choicePattern + "}, {2}, {0}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, null);
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);

    format.setFormatByArgumentIndex(0, NumberFormat.getInstance());

    checkPattern(format.toPattern(), "{3,choice," + choicePattern + "}, {2}, {0,number}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, NumberFormat.getInstance());
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, NumberFormat.getInstance());

    format.setFormatsByArgumentIndex(subformats);

    checkPattern(format.toPattern(), "{3,choice," + choicePattern + "}, {2,number}, {0,choice," + choicePattern + "}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, NumberFormat.getInstance());
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, NumberFormat.getInstance());
    checkSubformat(subformats, 2, new ChoiceFormat(choicePattern));

}
 
Example 7
Source File: MessageFormatsByArgumentIndex.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    Format[] subformats;

    MessageFormat format = new MessageFormat("{3, choice," + choicePattern + "}, {2}, {0}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, null);
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);

    format.setFormatByArgumentIndex(0, NumberFormat.getInstance());

    checkPattern(format.toPattern(), "{3,choice," + choicePattern + "}, {2}, {0,number}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, NumberFormat.getInstance());
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, NumberFormat.getInstance());

    format.setFormatsByArgumentIndex(subformats);

    checkPattern(format.toPattern(), "{3,choice," + choicePattern + "}, {2,number}, {0,choice," + choicePattern + "}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, NumberFormat.getInstance());
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, NumberFormat.getInstance());
    checkSubformat(subformats, 2, new ChoiceFormat(choicePattern));

}
 
Example 8
Source File: MessageFormatsByArgumentIndex.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    Format[] subformats;

    MessageFormat format = new MessageFormat("{3, choice," + choicePattern + "}, {2}, {0}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, null);
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);

    format.setFormatByArgumentIndex(0, NumberFormat.getInstance());

    checkPattern(format.toPattern(), "{3,choice," + choicePattern + "}, {2}, {0,number}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, NumberFormat.getInstance());
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, null);
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, NumberFormat.getInstance());

    format.setFormatsByArgumentIndex(subformats);

    checkPattern(format.toPattern(), "{3,choice," + choicePattern + "}, {2,number}, {0,choice," + choicePattern + "}");

    subformats = format.getFormatsByArgumentIndex();
    checkSubformatLength(subformats, 4);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, null);
    checkSubformat(subformats, 2, NumberFormat.getInstance());
    checkSubformat(subformats, 3, new ChoiceFormat(choicePattern));

    subformats = format.getFormats();
    checkSubformatLength(subformats, 3);
    checkSubformat(subformats, 0, new ChoiceFormat(choicePattern));
    checkSubformat(subformats, 1, NumberFormat.getInstance());
    checkSubformat(subformats, 2, new ChoiceFormat(choicePattern));

}
 
Example 9
Source File: MessageFormatTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void test_setFormatByArgumentIndexILjava_text_Format() {
    // test for method setFormatByArgumentIndex(int, Format)
    MessageFormat f1 = (MessageFormat) format1.clone();
    f1.setFormatByArgumentIndex(0, DateFormat.getTimeInstance());
    f1.setFormatByArgumentIndex(4, new ChoiceFormat("1#few|2#ok|3#a lot"));

    // test with repeating formats and max argument index < max offset
    // compare getFormatsByArgumentIndex() results after calls to
    // setFormatByArgumentIndex()
    Format[] formats = f1.getFormatsByArgumentIndex();

    Format[] correctFormats = new Format[] { DateFormat.getTimeInstance(),
            new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(),
            NumberFormat.getCurrencyInstance(),
            new ChoiceFormat("1#few|2#ok|3#a lot") };

    assertEquals("Test1A:Returned wrong number of formats:",
            correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
        assertEquals("Test1B:wrong format for argument index " + i + ":",
                correctFormats[i], formats[i]);
    }

    // compare getFormats() results after calls to
    // setFormatByArgumentIndex()
    formats = f1.getFormats();

    correctFormats = new Format[] { NumberFormat.getCurrencyInstance(),
            DateFormat.getTimeInstance(), DateFormat.getTimeInstance(),
            new ChoiceFormat("1#few|2#ok|3#a lot"),
            new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(), };

    assertEquals("Test1C:Returned wrong number of formats:",
            correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
        assertEquals("Test1D:wrong format for pattern index " + i + ":",
                correctFormats[i], formats[i]);
    }

    // test setting argumentIndexes that are not used
    MessageFormat f2 = (MessageFormat) format2.clone();
    f2.setFormatByArgumentIndex(2, NumberFormat.getPercentInstance());
    f2.setFormatByArgumentIndex(4, DateFormat.getTimeInstance());

    formats = f2.getFormatsByArgumentIndex();
    correctFormats = format2.getFormatsByArgumentIndex();

    assertEquals("Test2A:Returned wrong number of formats:",
            correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
        assertEquals("Test2B:wrong format for argument index " + i + ":",
                correctFormats[i], formats[i]);
    }

    formats = f2.getFormats();
    correctFormats = format2.getFormats();

    assertEquals("Test2C:Returned wrong number of formats:",
            correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
        assertEquals("Test2D:wrong format for pattern index " + i + ":",
                correctFormats[i], formats[i]);
    }

    // test exceeding the argumentIndex number
    MessageFormat f3 = (MessageFormat) format3.clone();
    f3.setFormatByArgumentIndex(1, NumberFormat.getCurrencyInstance());

    formats = f3.getFormatsByArgumentIndex();
    assertEquals("Test3A:Returned wrong number of formats:", 0,
            formats.length);

    formats = f3.getFormats();
    assertEquals("Test3B:Returned wrong number of formats:", 0,
            formats.length);
}
 
Example 10
Source File: MessageFormatTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void test_setFormatsByArgumentIndex$Ljava_text_Format() {
    MessageFormat f1 = (MessageFormat) format1.clone();

    // test with repeating formats and max argument index < max offset
    // compare getFormatsByArgumentIndex() results after calls to
    // setFormatsByArgumentIndex(Format[])
    Format[] correctFormats = new Format[] { DateFormat.getTimeInstance(),
            new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(),
            NumberFormat.getCurrencyInstance(),
            new ChoiceFormat("1#few|2#ok|3#a lot") };

    f1.setFormatsByArgumentIndex(correctFormats);
    Format[] formats = f1.getFormatsByArgumentIndex();

    assertEquals("Test1A:Returned wrong number of formats:",
            correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
        assertEquals("Test1B:wrong format for argument index " + i + ":",
                correctFormats[i], formats[i]);
    }

    // compare getFormats() results after calls to
    // setFormatByArgumentIndex()
    formats = f1.getFormats();
    correctFormats = new Format[] { NumberFormat.getCurrencyInstance(),
            DateFormat.getTimeInstance(), DateFormat.getTimeInstance(),
            new ChoiceFormat("1#few|2#ok|3#a lot"),
            new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(), };

    assertEquals("Test1C:Returned wrong number of formats:",
            correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
        assertEquals("Test1D:wrong format for pattern index " + i + ":",
                correctFormats[i], formats[i]);
    }

    // test setting argumentIndexes that are not used
    MessageFormat f2 = (MessageFormat) format2.clone();
    Format[] inputFormats = new Format[] { DateFormat.getDateInstance(),
            new ChoiceFormat("0#off|1#on"),
            NumberFormat.getPercentInstance(),
            NumberFormat.getCurrencyInstance(),
            DateFormat.getTimeInstance(), null, null, null,
            DateFormat.getTimeInstance() };
    f2.setFormatsByArgumentIndex(inputFormats);

    formats = f2.getFormatsByArgumentIndex();
    correctFormats = format2.getFormatsByArgumentIndex();

    assertEquals("Test2A:Returned wrong number of formats:",
            correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
        assertEquals("Test2B:wrong format for argument index " + i + ":",
                correctFormats[i], formats[i]);
    }

    formats = f2.getFormats();
    correctFormats = new Format[] { NumberFormat.getCurrencyInstance(),
            DateFormat.getTimeInstance(), DateFormat.getDateInstance(),
            null, new ChoiceFormat("0#off|1#on"),
            DateFormat.getDateInstance() };

    assertEquals("Test2C:Returned wrong number of formats:",
            correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
        assertEquals("Test2D:wrong format for pattern index " + i + ":",
                correctFormats[i], formats[i]);
    }

    // test exceeding the argumentIndex number
    MessageFormat f3 = (MessageFormat) format3.clone();
    f3.setFormatsByArgumentIndex(inputFormats);

    formats = f3.getFormatsByArgumentIndex();
    assertEquals("Test3A:Returned wrong number of formats:", 0,
            formats.length);

    formats = f3.getFormats();
    assertEquals("Test3B:Returned wrong number of formats:", 0,
            formats.length);

}