/** * Formats the message of the exception. * * @param params the parameters for the exception message * @return the formatted message */ public String getMessage(Object... params) { Preconditions.checkArgument(mMessage.getFormats().length == params.length, "The message takes " + mMessage.getFormats().length + " arguments, but is given " + params.length); // MessageFormat is not thread-safe, so guard it synchronized (mMessage) { return mMessage.format(params); } }
@Override public String render(String template, StatementContext ctx) { MessageFormat msgFormat = new MessageFormat(template); validateKeys(ctx.getAttributes().keySet(), msgFormat.getFormats().length); Object[] args = ctx.getAttributes() .entrySet() .stream() .map(x -> new AbstractMap.SimpleImmutableEntry<>(Integer.valueOf(x.getKey()), x.getValue())) .sorted(Comparator.comparingInt(AbstractMap.SimpleImmutableEntry::getKey)) .map(AbstractMap.SimpleImmutableEntry::getValue) .toArray(Object[]::new); return msgFormat.format(args); }
protected Message getMessage(final String msgPattern, final Object[] args, final Throwable aThrowable) { try { final MessageFormat format = new MessageFormat(msgPattern); final Format[] formats = format.getFormats(); if (formats != null && formats.length > 0) { return new MessageFormatMessage(locale, msgPattern, args); } } catch (final Exception ignored) { // Obviously, the message is not a proper pattern for MessageFormat. } try { if (MSG_PATTERN.matcher(msgPattern).find()) { return new StringFormattedMessage(locale, msgPattern, args); } } catch (final Exception ignored) { // Also not properly formatted. } return new ParameterizedMessage(msgPattern, args, aThrowable); }
Format[] formats = messageFormat.getFormats(); for (int i = 0; i < formats.length; i++) { Format format = formats[i];
continue; final int expected = message.getFormats().length; if (argumentCount != expected) { final String suffix = ARGUMENT_COUNT_PREFIX + expected;
/** * Returns the formats used for the format elements in the * previously set pattern string. * The order of formats in the returned array corresponds to * the order of format elements in the pattern string. * <p> * Since the order of format elements in a pattern string often * changes during localization, it's generally better to use the * {@link #getFormatsByArgumentIndex()} * method, which assumes an order of formats corresponding to the * order of elements in the <code>arguments</code> array passed to * the <code>format</code> methods or the result array returned by * the <code>parse</code> methods. * * This method is only supported when exclusively numbers are used for * argument names. Otherwise an IllegalArgumentException is thrown. * * @return the formats used for the format elements in the pattern * @throws IllegalArgumentException if this format uses named arguments * @stable ICU 3.0 */ public Format[] getFormats() { return messageFormat.getFormats(); }
/** * Returns the formats used for the format elements in the * previously set pattern string. * The order of formats in the returned array corresponds to * the order of format elements in the pattern string. * <p> * Since the order of format elements in a pattern string often * changes during localization, it's generally better to use the * {@link #getFormatsByArgumentIndex()} * method, which assumes an order of formats corresponding to the * order of elements in the <code>arguments</code> array passed to * the <code>format</code> methods or the result array returned by * the <code>parse</code> methods. * * This method is only supported when exclusively numbers are used for * argument names. Otherwise an IllegalArgumentException is thrown. * * @return the formats used for the format elements in the pattern * @throws IllegalArgumentException if this format uses named arguments * @stable ICU 3.0 */ public Format[] getFormats() { return messageFormat.getFormats(); }
/** * Formats the message of the exception. * * @param params the parameters for the exception message * @return the formatted message */ public String getMessage(Object... params) { Preconditions.checkArgument(mMessage.getFormats().length == params.length, "The message takes " + mMessage.getFormats().length + " arguments, but is given " + params.length); // MessageFormat is not thread-safe, so guard it synchronized (mMessage) { return mMessage.format(params); } }
public class Format { public static void main(String argv[]) { MessageFormat mf = new MessageFormat("The time is: {0, time, HH:mm}"); TimeZone tz = TimeZone.getTimeZone("GMT"); Object [] formats = mf.getFormats(); for (int i = 0; i < formats.length; i++) { if (formats[i] instanceof SimpleDateFormat) { ((SimpleDateFormat)formats[i]).setTimeZone(tz); } } Date date = new Date(); Object [] args = {date}; System.out.println(mf.format(args)); } }
public MessageFormat getFormat(String pattern) { MessageFormat format = formats.get(pattern); if (format == null) { format = new MessageFormat(pattern,locale); // lets try to make sure any internal DateFormats use UTC: Format[] subFormats = format.getFormats(); if (subFormats != null) { for (Format subFormat : subFormats) { if (subFormat instanceof DateFormat) { DateFormat subDateFormat = (DateFormat)subFormat; subDateFormat.setTimeZone(TZ_UTC); } } } formats.put(pattern, format); } return format; }
public MessageFormat getFormat(String pattern) { MessageFormat format = formats.get(pattern); if (format == null) { format = new MessageFormat(pattern,locale); // lets try to make sure any internal DateFormats use UTC: Format[] subFormats = format.getFormats(); if (subFormats != null) { for (Format subFormat : subFormats) { if (subFormat instanceof DateFormat) { DateFormat subDateFormat = (DateFormat)subFormat; subDateFormat.setTimeZone(TZ_UTC); } } } formats.put(pattern, format); } return format; }
public VarFormatter(String format, Locale l) { logger.trace("new format: {}", format); this.format = format; locale = l; List<String> formats = new ArrayList<>(); // Convert the pattern to a MessageFormat which is compiled and be reused String pattern = findVariables(new StringBuilder(), format, 0, formats).toString(); mf = new MessageFormat(pattern, l); for(int i = 0; i < mf.getFormats().length; i++) { mf.setFormat(i, resolveFormat(formats.get(i))); } if (mapper.size() != 0) { mapper.keySet().stream().reduce((i,j) -> { if (i.getClass() != j.getClass()) { throw new IllegalArgumentException("Can't mix indexed with object resolution"); } else { return j; } }); } }
public static String getUniqueName( final DocumentBundle bundle, final String pattern ) { if ( bundle == null ) { throw new NullPointerException(); } if ( pattern == null ) { throw new NullPointerException(); } final MessageFormat message = new MessageFormat( pattern ); final Object[] objects = { "" }; final String plain = message.format( objects ); if ( bundle.isEntryExists( plain ) == false ) { return plain; } final Format[] formats = message.getFormats(); if ( formats.length == 0 ) { // there is no variation in this name. return null; } int count = 1; while ( count < 2000000 ) { objects[ 0 ] = String.valueOf( count ); final String testFile = message.format( objects ); if ( bundle.isEntryExists( testFile ) == false ) { return testFile; } count += 1; } // If you have more than 2 million entries, you would hate me to test for the two billion entries, wont you? throw new IllegalStateException(); }
protected String formatWithTimeZone( String template, Object[] arguments, Locale locale, TimeZone timezone) { MessageFormat mf = new MessageFormat(" "); mf.setLocale(locale); mf.applyPattern(template); if (!timezone.equals(TimeZone.getDefault())) { Format[] formats = mf.getFormats(); for (int i = 0; i < formats.length; i++) { if (formats[i] instanceof DateFormat) { DateFormat temp = (DateFormat) formats[i]; temp.setTimeZone(timezone); mf.setFormat(i,temp); } } } return mf.format(arguments); }
protected String formatWithTimeZone( String template, Object[] arguments, Locale locale, TimeZone timezone) { MessageFormat mf = new MessageFormat(" "); mf.setLocale(locale); mf.applyPattern(template); if (!timezone.equals(TimeZone.getDefault())) { Format[] formats = mf.getFormats(); for (int i = 0; i < formats.length; i++) { if (formats[i] instanceof DateFormat) { DateFormat temp = (DateFormat) formats[i]; temp.setTimeZone(timezone); mf.setFormat(i,temp); } } } return mf.format(arguments); }
protected String formatWithTimeZone( String template, Object[] arguments, Locale locale, TimeZone timezone) { MessageFormat mf = new MessageFormat(" "); mf.setLocale(locale); mf.applyPattern(template); if (!timezone.equals(TimeZone.getDefault())) { Format[] formats = mf.getFormats(); for (int i = 0; i < formats.length; i++) { if (formats[i] instanceof DateFormat) { DateFormat temp = (DateFormat) formats[i]; temp.setTimeZone(timezone); mf.setFormat(i,temp); } } } return mf.format(arguments); }
protected String formatWithTimeZone( String template, Object[] arguments, Locale locale, TimeZone timezone) { MessageFormat mf = new MessageFormat(" "); mf.setLocale(locale); mf.applyPattern(template); if (!timezone.equals(TimeZone.getDefault())) { Format[] formats = mf.getFormats(); for (int i = 0; i < formats.length; i++) { if (formats[i] instanceof DateFormat) { DateFormat temp = (DateFormat) formats[i]; temp.setTimeZone(timezone); mf.setFormat(i,temp); } } } return mf.format(arguments); }
protected Message getMessage(final String msgPattern, final Object[] args, final Throwable aThrowable) { try { final MessageFormat format = new MessageFormat(msgPattern); final Format[] formats = format.getFormats(); if (formats != null && formats.length > 0) { return new MessageFormatMessage(locale, msgPattern, args); } } catch (final Exception ignored) { // Obviously, the message is not a proper pattern for MessageFormat. } try { if (MSG_PATTERN.matcher(msgPattern).find()) { return new StringFormattedMessage(locale, msgPattern, args); } } catch (final Exception ignored) { // Also not properly formatted. } return new ParameterizedMessage(msgPattern, args, aThrowable); }
private void wrapNestedFormatters(java.text.MessageFormat mfmt) { // Update nested formatters created by Java MessageFormat // with ICU versions, so FieldPosition / AttributedText will // use ICU formatter's definition, such as com.ibm.icu.text.NumberFormat.INTEGER_FIELD // Replacing nested formatter may change the pattern string // originally used. For example, "{0,integer} files" is replaced // with "{0} files". We preserve the original pattern. savedPattern = mfmt.toPattern(); Format[] subfmts = mfmt.getFormats(); for (int i = 0; i < subfmts.length; i++) { if (subfmts[i] instanceof java.text.DateFormat) { subfmts[i] = new DateFormat((java.text.DateFormat)subfmts[i]); } else if (subfmts[i] instanceof java.text.NumberFormat) { subfmts[i] = new NumberFormat((java.text.NumberFormat)subfmts[i]); } } mfmt.setFormats(subfmts); }
private void wrapNestedFormatters(java.text.MessageFormat mfmt) { // Update nested formatters created by Java MessageFormat // with ICU versions, so FieldPosition / AttributedText will // use ICU formatter's definition, such as com.ibm.icu.text.NumberFormat.INTEGER_FIELD // Replacing nested formatter may change the pattern string // originally used. For example, "{0,integer} files" is replaced // with "{0} files". We preserve the original pattern. savedPattern = mfmt.toPattern(); Format[] subfmts = mfmt.getFormats(); for (int i = 0; i < subfmts.length; i++) { if (subfmts[i] instanceof java.text.DateFormat) { subfmts[i] = new DateFormat((java.text.DateFormat)subfmts[i]); } else if (subfmts[i] instanceof java.text.NumberFormat) { subfmts[i] = new NumberFormat((java.text.NumberFormat)subfmts[i]); } } mfmt.setFormats(subfmts); }