📄 messageformat.java
字号:
return formatInternal(arguments, appendBuf, fp, null); } private StringBuffer formatInternal (Object arguments[], StringBuffer appendBuf, FieldPosition fp, FormatCharacterIterator output_iterator) { appendBuf.append(leader); if (output_iterator != null) output_iterator.append(leader); for (int i = 0; i < elements.length; ++i) { Object thisArg = null; boolean unavailable = false; if (arguments == null || elements[i].argNumber >= arguments.length) unavailable = true; else thisArg = arguments[elements[i].argNumber]; AttributedCharacterIterator iterator = null; Format formatter = null; if (fp != null && i == fp.getField() && fp.getFieldAttribute() == Field.ARGUMENT) fp.setBeginIndex(appendBuf.length()); if (unavailable) appendBuf.append("{" + elements[i].argNumber + "}"); else { if (elements[i].setFormat != null) formatter = elements[i].setFormat; else if (elements[i].format != null) { if (elements[i].formatClass != null && ! elements[i].formatClass.isInstance(thisArg)) throw new IllegalArgumentException("Wrong format class"); formatter = elements[i].format; } else if (thisArg instanceof Number) formatter = NumberFormat.getInstance(locale); else if (thisArg instanceof Date) formatter = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale); else appendBuf.append(thisArg); } if (fp != null && fp.getField() == i && fp.getFieldAttribute() == Field.ARGUMENT) fp.setEndIndex(appendBuf.length()); if (formatter != null) { // Special-case ChoiceFormat. if (formatter instanceof ChoiceFormat) { StringBuffer buf = new StringBuffer (); formatter.format(thisArg, buf, fp); MessageFormat mf = new MessageFormat (); mf.setLocale(locale); mf.applyPattern(buf.toString()); mf.format(arguments, appendBuf, fp); } else { if (output_iterator != null) iterator = formatter.formatToCharacterIterator(thisArg); else formatter.format(thisArg, appendBuf, fp); } elements[i].format = formatter; } if (output_iterator != null) { HashMap hash_argument = new HashMap(); int position = output_iterator.getEndIndex(); hash_argument.put (MessageFormat.Field.ARGUMENT, new Integer(elements[i].argNumber)); if (iterator != null) { output_iterator.append(iterator); output_iterator.addAttributes(hash_argument, position, output_iterator.getEndIndex()); } else output_iterator.append(thisArg.toString(), hash_argument); output_iterator.append(elements[i].trailer); } appendBuf.append(elements[i].trailer); } return appendBuf; } /** * Returns the pattern with the formatted objects. The first argument * must be a array of Objects. * This is equivalent to format((Object[]) objectArray, appendBuf, fpos) * * @param objectArray The object array to be formatted. * @param appendBuf The StringBuffer where the text is appened. * @param fpos A FieldPosition object (it is ignored). */ public final StringBuffer format (Object objectArray, StringBuffer appendBuf, FieldPosition fpos) { return format ((Object[])objectArray, appendBuf, fpos); } /** * Returns an array with the Formats for * the arguments. */ public Format[] getFormats () { Format[] f = new Format[elements.length]; for (int i = elements.length - 1; i >= 0; --i) f[i] = elements[i].setFormat; return f; } /** * Returns the locale. */ public Locale getLocale () { return locale; } /** * Overrides Format.hashCode() */ public int hashCode () { // FIXME: not a very good hash. return pattern.hashCode() + locale.hashCode(); } private MessageFormat () { } /** * Creates a new MessageFormat object with * the specified pattern * * @param pattern The Pattern */ public MessageFormat(String pattern) { this(pattern, Locale.getDefault()); } /** * Creates a new MessageFormat object with * the specified pattern * * @param pattern The Pattern * @param locale The Locale to use * * @since 1.4 */ public MessageFormat(String pattern, Locale locale) { this.locale = locale; applyPattern (pattern); } /** * Parse a string <code>sourceStr</code> against the pattern specified * to the MessageFormat constructor. * * @param sourceStr the string to be parsed. * @param pos the current parse position (and eventually the error position). * @return the array of parsed objects sorted according to their argument number * in the pattern. */ public Object[] parse (String sourceStr, ParsePosition pos) { // Check initial text. int index = pos.getIndex(); if (! sourceStr.startsWith(leader, index)) { pos.setErrorIndex(index); return null; } index += leader.length(); Vector results = new Vector (elements.length, 1); // Now check each format. for (int i = 0; i < elements.length; ++i) { Format formatter = null; if (elements[i].setFormat != null) formatter = elements[i].setFormat; else if (elements[i].format != null) formatter = elements[i].format; Object value = null; if (formatter instanceof ChoiceFormat) { // We must special-case a ChoiceFormat because it might // have recursive formatting. ChoiceFormat cf = (ChoiceFormat) formatter; String[] formats = (String[]) cf.getFormats(); double[] limits = (double[]) cf.getLimits(); MessageFormat subfmt = new MessageFormat (); subfmt.setLocale(locale); ParsePosition subpos = new ParsePosition (index); int j; for (j = 0; value == null && j < limits.length; ++j) { subfmt.applyPattern(formats[j]); subpos.setIndex(index); value = subfmt.parse(sourceStr, subpos); } if (value != null) { index = subpos.getIndex(); value = new Double (limits[j]); } } else if (formatter != null) { pos.setIndex(index); value = formatter.parseObject(sourceStr, pos); if (value != null) index = pos.getIndex(); } else { // We have a String format. This can lose in a number // of ways, but we give it a shot. int next_index; if (elements[i].trailer.length() > 0) next_index = sourceStr.indexOf(elements[i].trailer, index); else next_index = sourceStr.length(); if (next_index == -1) { pos.setErrorIndex(index); return null; } value = sourceStr.substring(index, next_index); index = next_index; } if (value == null || ! sourceStr.startsWith(elements[i].trailer, index)) { pos.setErrorIndex(index); return null; } if (elements[i].argNumber >= results.size()) results.setSize(elements[i].argNumber + 1); results.setElementAt(value, elements[i].argNumber); index += elements[i].trailer.length(); } Object[] r = new Object[results.size()]; results.copyInto(r); return r; } public Object[] parse (String sourceStr) throws ParseException { ParsePosition pp = new ParsePosition (0); Object[] r = parse (sourceStr, pp); if (r == null) throw new ParseException ("couldn't parse string", pp.getErrorIndex()); return r; } public Object parseObject (String sourceStr, ParsePosition pos) { return parse (sourceStr, pos); } /** * Sets the format for the argument at an specified * index. * * @param variableNum The index. * @param newFormat The Format object. */ public void setFormat (int variableNum, Format newFormat) { elements[variableNum].setFormat = newFormat; } /** * Sets the formats for the arguments. * * @param newFormats An array of Format objects. */ public void setFormats (Format[] newFormats) { if (newFormats.length < elements.length) throw new IllegalArgumentException("Not enough format objects"); int len = Math.min(newFormats.length, elements.length); for (int i = 0; i < len; ++i) elements[i].setFormat = newFormats[i]; } /** * Sets the locale. * * @param loc A Locale */ public void setLocale (Locale loc) { locale = loc; if (elements != null) { for (int i = 0; i < elements.length; ++i) elements[i].setLocale(loc); } } /** * Returns the pattern. */ public String toPattern () { return pattern; } /** * Return the formatters used sorted by argument index. It uses the * internal table to fill in this array: if a format has been * set using <code>setFormat</code> or <code>setFormatByArgumentIndex</code> * then it returns it at the right index. If not it uses the detected * formatters during a <code>format</code> call. If nothing is known * about that argument index it just puts null at that position. * To get useful informations you may have to call <code>format</code> * at least once. * * @return an array of formatters sorted by argument index. */ public Format[] getFormatsByArgumentIndex() { int argNumMax = 0; // First, find the greatest argument number. for (int i=0;i<elements.length;i++) if (elements[i].argNumber > argNumMax) argNumMax = elements[i].argNumber; Format[] formats = new Format[argNumMax]; for (int i=0;i<elements.length;i++) { if (elements[i].setFormat != null) formats[elements[i].argNumber] = elements[i].setFormat; else if (elements[i].format != null) formats[elements[i].argNumber] = elements[i].format; } return formats; } /** * Set the format to used using the argument index number. * * @param argumentIndex the argument index. * @param newFormat the format to use for this argument. */ public void setFormatByArgumentIndex(int argumentIndex, Format newFormat) { for (int i=0;i<elements.length;i++) { if (elements[i].argNumber == argumentIndex) elements[i].setFormat = newFormat; } } /** * Set the format for argument using a specified array of formatters * which is sorted according to the argument index. If the number of * elements in the array is fewer than the number of arguments only * the arguments specified by the array are touched. * * @param newFormats array containing the new formats to set. * * @throws NullPointerException if newFormats is null */ public void setFormatsByArgumentIndex(Format[] newFormats) { for (int i=0;i<newFormats.length;i++) { // Nothing better than that can exist here. setFormatByArgumentIndex(i, newFormats[i]); } } // The pattern string. private String pattern; // The locale. private Locale locale; // Variables. private MessageFormatElement[] elements; // Leader text. private String leader;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -