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

The following examples show how to use java.text.MessageFormat#toPattern() . 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: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test extended and built in formats.
 */
public void testExtendedAndBuiltInFormats() {
    Calendar cal = Calendar.getInstance();
    cal.set(2007, Calendar.JANUARY, 23, 18, 33, 05);
    Object[] args = new Object[] {"John Doe", cal.getTime(), new Double("12345.67")};
    String builtinsPattern = "DOB: {1,date,short} Salary: {2,number,currency}";
    String extendedPattern = "Name: {0,upper} ";
    String pattern = extendedPattern + builtinsPattern;

    HashSet<Locale> testLocales = new HashSet<Locale>();
    testLocales.addAll(Arrays.asList(DateFormat.getAvailableLocales()));
    testLocales.retainAll(Arrays.asList(NumberFormat.getAvailableLocales()));
    testLocales.add(null);

    for (Iterator<Locale> l = testLocales.iterator(); l.hasNext();) {
        Locale locale = l.next();
        MessageFormat builtins = createMessageFormat(builtinsPattern, locale);
        String expectedPattern = extendedPattern + builtins.toPattern();
        DateFormat df = null;
        NumberFormat nf = null;
        ExtendedMessageFormat emf = null;
        if (locale == null) {
            df = DateFormat.getDateInstance(DateFormat.SHORT);
            nf = NumberFormat.getCurrencyInstance();
            emf = new ExtendedMessageFormat(pattern, registry);
        } else {
            df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
            nf = NumberFormat.getCurrencyInstance(locale);
            emf = new ExtendedMessageFormat(pattern, locale, registry);
        }
        StringBuffer expected = new StringBuffer();
        expected.append("Name: ");
        expected.append(args[0].toString().toUpperCase());
        expected.append(" DOB: ");
        expected.append(df.format(args[1]));
        expected.append(" Salary: ");
        expected.append(nf.format(args[2]));
        assertPatternsEqual("pattern comparison for locale " + locale, expectedPattern, emf.toPattern());
        assertEquals(String.valueOf(locale), expected.toString(), emf.format(args));
    }
}
 
Example 2
Source File: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test extended and built in formats.
 */
@Test
public void testExtendedAndBuiltInFormats() {
    Calendar cal = Calendar.getInstance();
    cal.set(2007, Calendar.JANUARY, 23, 18, 33, 05);
    Object[] args = new Object[] {"John Doe", cal.getTime(), Double.valueOf("12345.67")};
    String builtinsPattern = "DOB: {1,date,short} Salary: {2,number,currency}";
    String extendedPattern = "Name: {0,upper} ";
    String pattern = extendedPattern + builtinsPattern;

    HashSet<Locale> testLocales = new HashSet<Locale>();
    testLocales.addAll(Arrays.asList(DateFormat.getAvailableLocales()));
    testLocales.retainAll(Arrays.asList(NumberFormat.getAvailableLocales()));
    testLocales.add(null);

    for (Locale locale : testLocales) {
        MessageFormat builtins = createMessageFormat(builtinsPattern, locale);
        String expectedPattern = extendedPattern + builtins.toPattern();
        DateFormat df = null;
        NumberFormat nf = null;
        ExtendedMessageFormat emf = null;
        if (locale == null) {
            df = DateFormat.getDateInstance(DateFormat.SHORT);
            nf = NumberFormat.getCurrencyInstance();
            emf = new ExtendedMessageFormat(pattern, registry);
        } else {
            df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
            nf = NumberFormat.getCurrencyInstance(locale);
            emf = new ExtendedMessageFormat(pattern, locale, registry);
        }
        StringBuilder expected = new StringBuilder();
        expected.append("Name: ");
        expected.append(args[0].toString().toUpperCase());
        expected.append(" DOB: ");
        expected.append(df.format(args[1]));
        expected.append(" Salary: ");
        expected.append(nf.format(args[2]));
        assertPatternsEqual("pattern comparison for locale " + locale, expectedPattern, emf.toPattern());
        assertEquals(String.valueOf(locale), expected.toString(), emf.format(args));
    }
}
 
Example 3
Source File: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test extended and built in formats.
 */
public void testExtendedAndBuiltInFormats() {
    Calendar cal = Calendar.getInstance();
    cal.set(2007, Calendar.JANUARY, 23, 18, 33, 05);
    Object[] args = new Object[] {"John Doe", cal.getTime(), new Double("12345.67")};
    String builtinsPattern = "DOB: {1,date,short} Salary: {2,number,currency}";
    String extendedPattern = "Name: {0,upper} ";
    String pattern = extendedPattern + builtinsPattern;

    HashSet<Locale> testLocales = new HashSet<Locale>();
    testLocales.addAll(Arrays.asList(DateFormat.getAvailableLocales()));
    testLocales.retainAll(Arrays.asList(NumberFormat.getAvailableLocales()));
    testLocales.add(null);

    for (Iterator<Locale> l = testLocales.iterator(); l.hasNext();) {
        Locale locale = l.next();
        MessageFormat builtins = createMessageFormat(builtinsPattern, locale);
        String expectedPattern = extendedPattern + builtins.toPattern();
        DateFormat df = null;
        NumberFormat nf = null;
        ExtendedMessageFormat emf = null;
        if (locale == null) {
            df = DateFormat.getDateInstance(DateFormat.SHORT);
            nf = NumberFormat.getCurrencyInstance();
            emf = new ExtendedMessageFormat(pattern, registry);
        } else {
            df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
            nf = NumberFormat.getCurrencyInstance(locale);
            emf = new ExtendedMessageFormat(pattern, locale, registry);
        }
        StringBuffer expected = new StringBuffer();
        expected.append("Name: ");
        expected.append(args[0].toString().toUpperCase());
        expected.append(" DOB: ");
        expected.append(df.format(args[1]));
        expected.append(" Salary: ");
        expected.append(nf.format(args[2]));
        assertPatternsEqual("pattern comparison for locale " + locale, expectedPattern, emf.toPattern());
        assertEquals(String.valueOf(locale), expected.toString(), emf.format(args));
    }
}
 
Example 4
Source File: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test extended and built in formats.
 */
@Test
public void testExtendedAndBuiltInFormats() {
    final Calendar cal = Calendar.getInstance();
    cal.set(2007, Calendar.JANUARY, 23, 18, 33, 05);
    final Object[] args = new Object[] {"John Doe", cal.getTime(), Double.valueOf("12345.67")};
    final String builtinsPattern = "DOB: {1,date,short} Salary: {2,number,currency}";
    final String extendedPattern = "Name: {0,upper} ";
    final String pattern = extendedPattern + builtinsPattern;

    final HashSet<Locale> testLocales = new HashSet<Locale>();
    testLocales.addAll(Arrays.asList(DateFormat.getAvailableLocales()));
    testLocales.retainAll(Arrays.asList(NumberFormat.getAvailableLocales()));
    testLocales.add(null);

    for (final Locale locale : testLocales) {
        final MessageFormat builtins = createMessageFormat(builtinsPattern, locale);
        final String expectedPattern = extendedPattern + builtins.toPattern();
        DateFormat df = null;
        NumberFormat nf = null;
        ExtendedMessageFormat emf = null;
        if (locale == null) {
            df = DateFormat.getDateInstance(DateFormat.SHORT);
            nf = NumberFormat.getCurrencyInstance();
            emf = new ExtendedMessageFormat(pattern, registry);
        } else {
            df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
            nf = NumberFormat.getCurrencyInstance(locale);
            emf = new ExtendedMessageFormat(pattern, locale, registry);
        }
        final StringBuilder expected = new StringBuilder();
        expected.append("Name: ");
        expected.append(args[0].toString().toUpperCase());
        expected.append(" DOB: ");
        expected.append(df.format(args[1]));
        expected.append(" Salary: ");
        expected.append(nf.format(args[2]));
        assertPatternsEqual("pattern comparison for locale " + locale, expectedPattern, emf.toPattern());
        assertEquals(String.valueOf(locale), expected.toString(), emf.format(args));
    }
}
 
Example 5
Source File: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test extended and built in formats.
 */
@Test
public void testExtendedAndBuiltInFormats() {
    Calendar cal = Calendar.getInstance();
    cal.set(2007, Calendar.JANUARY, 23, 18, 33, 05);
    Object[] args = new Object[] {"John Doe", cal.getTime(), Double.valueOf("12345.67")};
    String builtinsPattern = "DOB: {1,date,short} Salary: {2,number,currency}";
    String extendedPattern = "Name: {0,upper} ";
    String pattern = extendedPattern + builtinsPattern;

    HashSet<Locale> testLocales = new HashSet<Locale>();
    testLocales.addAll(Arrays.asList(DateFormat.getAvailableLocales()));
    testLocales.retainAll(Arrays.asList(NumberFormat.getAvailableLocales()));
    testLocales.add(null);

    for (Locale locale : testLocales) {
        MessageFormat builtins = createMessageFormat(builtinsPattern, locale);
        String expectedPattern = extendedPattern + builtins.toPattern();
        DateFormat df = null;
        NumberFormat nf = null;
        ExtendedMessageFormat emf = null;
        if (locale == null) {
            df = DateFormat.getDateInstance(DateFormat.SHORT);
            nf = NumberFormat.getCurrencyInstance();
            emf = new ExtendedMessageFormat(pattern, registry);
        } else {
            df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
            nf = NumberFormat.getCurrencyInstance(locale);
            emf = new ExtendedMessageFormat(pattern, locale, registry);
        }
        StringBuffer expected = new StringBuffer();
        expected.append("Name: ");
        expected.append(args[0].toString().toUpperCase());
        expected.append(" DOB: ");
        expected.append(df.format(args[1]));
        expected.append(" Salary: ");
        expected.append(nf.format(args[2]));
        assertPatternsEqual("pattern comparison for locale " + locale, expectedPattern, emf.toPattern());
        assertEquals(String.valueOf(locale), expected.toString(), emf.format(args));
    }
}