📄 messageformat.java
字号:
lastOffset = offsets[i]; int argumentNumber = argumentNumbers[i]; if (arguments == null || argumentNumber >= arguments.length) { result.append("{" + argumentNumber + "}"); continue; } // int argRecursion = ((recursionProtection >> (argumentNumber*2)) & 0x3); if (false) { // if (argRecursion == 3){ // prevent loop!!! result.append('\uFFFD'); } else { Object obj = arguments[argumentNumber]; String arg = null; Format subFormatter = null; if (obj == null) { arg = "null"; } else if (formats[i] != null) { subFormatter = formats[i]; if (subFormatter instanceof ChoiceFormat) { arg = formats[i].format(obj); if (arg.indexOf('{') >= 0) { subFormatter = new MessageFormat(arg, locale); obj = arguments; arg = null; } } } else if (obj instanceof Number) { // format number if can subFormatter = NumberFormat.getInstance(locale); } else if (obj instanceof Date) { // format a Date if can subFormatter = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT, locale);//fix } else if (obj instanceof String) { arg = (String) obj; } else { arg = obj.toString(); if (arg == null) arg = "null"; } // At this point we are in two states, either subFormatter // is non-null indicating we should format obj using it, // or arg is non-null and we should use it as the value. if (characterIterators != null) { // If characterIterators is non-null, it indicates we need // to get the CharacterIterator from the child formatter. if (last != result.length()) { characterIterators.add( createAttributedCharacterIterator(result.substring (last))); last = result.length(); } if (subFormatter != null) { AttributedCharacterIterator subIterator = subFormatter.formatToCharacterIterator(obj); append(result, subIterator); if (last != result.length()) { characterIterators.add( createAttributedCharacterIterator( subIterator, Field.ARGUMENT, new Integer(argumentNumber))); last = result.length(); } arg = null; } if (arg != null && arg.length() > 0) { result.append(arg); characterIterators.add( createAttributedCharacterIterator( arg, Field.ARGUMENT, new Integer(argumentNumber))); last = result.length(); } } else { if (subFormatter != null) { arg = subFormatter.format(obj); } last = result.length(); result.append(arg); if (i == 0 && fp != null && Field.ARGUMENT.equals( fp.getFieldAttribute())) { fp.setBeginIndex(last); fp.setEndIndex(result.length()); } last = result.length(); } } } result.append(pattern.substring(lastOffset, pattern.length())); if (characterIterators != null && last != result.length()) { characterIterators.add(createAttributedCharacterIterator( result.substring(last))); } return result; } /** * Convenience method to append all the characters in * <code>iterator</code> to the StringBuffer <code>result</code>. */ private void append(StringBuffer result, CharacterIterator iterator) { if (iterator.first() != CharacterIterator.DONE) { char aChar; result.append(iterator.first()); while ((aChar = iterator.next()) != CharacterIterator.DONE) { result.append(aChar); } } } private static final String[] typeList = {"", "", "number", "", "date", "", "time", "", "choice"}; private static final String[] modifierList = {"", "", "currency", "", "percent", "", "integer"}; private static final String[] dateModifierList = {"", "", "short", "", "medium", "", "long", "", "full"}; private void makeFormat(int position, int offsetNumber, StringBuffer[] segments) { // get the argument number int argumentNumber; try { argumentNumber = Integer.parseInt(segments[1].toString()); // always unlocalized! } catch (NumberFormatException e) { throw new IllegalArgumentException("can't parse argument number " + segments[1]); } if (argumentNumber < 0) { throw new IllegalArgumentException("negative argument number " + argumentNumber); } // resize format information arrays if necessary if (offsetNumber >= formats.length) { int newLength = formats.length * 2; Format[] newFormats = new Format[newLength]; int[] newOffsets = new int[newLength]; int[] newArgumentNumbers = new int[newLength]; System.arraycopy(formats, 0, newFormats, 0, maxOffset + 1); System.arraycopy(offsets, 0, newOffsets, 0, maxOffset + 1); System.arraycopy(argumentNumbers, 0, newArgumentNumbers, 0, maxOffset + 1); formats = newFormats; offsets = newOffsets; argumentNumbers = newArgumentNumbers; } int oldMaxOffset = maxOffset; maxOffset = offsetNumber; offsets[offsetNumber] = segments[0].length(); argumentNumbers[offsetNumber] = argumentNumber; // now get the format Format newFormat = null; switch (findKeyword(segments[2].toString(), typeList)) { case 0: break; case 1: case 2:// number switch (findKeyword(segments[3].toString(), modifierList)) { case 0: // default; newFormat = NumberFormat.getInstance(locale); break; case 1: case 2:// currency newFormat = NumberFormat.getCurrencyInstance(locale); break; case 3: case 4:// percent newFormat = NumberFormat.getPercentInstance(locale); break; case 5: case 6:// integer newFormat = NumberFormat.getIntegerInstance(locale); break; default: // pattern newFormat = new DecimalFormat(segments[3].toString(), new DecimalFormatSymbols(locale)); break; } break; case 3: case 4: // date switch (findKeyword(segments[3].toString(), dateModifierList)) { case 0: // default newFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, locale); break; case 1: case 2: // short newFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale); break; case 3: case 4: // medium newFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, locale); break; case 5: case 6: // long newFormat = DateFormat.getDateInstance(DateFormat.LONG, locale); break; case 7: case 8: // full newFormat = DateFormat.getDateInstance(DateFormat.FULL, locale); break; default: newFormat = new SimpleDateFormat(segments[3].toString(), locale); break; } break; case 5: case 6:// time switch (findKeyword(segments[3].toString(), dateModifierList)) { case 0: // default newFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale); break; case 1: case 2: // short newFormat = DateFormat.getTimeInstance(DateFormat.SHORT, locale); break; case 3: case 4: // medium newFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale); break; case 5: case 6: // long newFormat = DateFormat.getTimeInstance(DateFormat.LONG, locale); break; case 7: case 8: // full newFormat = DateFormat.getTimeInstance(DateFormat.FULL, locale); break; default: newFormat = new SimpleDateFormat(segments[3].toString(), locale); break; } break; case 7: case 8:// choice try { newFormat = new ChoiceFormat(segments[3].toString()); } catch (Exception e) { maxOffset = oldMaxOffset; throw new IllegalArgumentException( "Choice Pattern incorrect"); } break; default: maxOffset = oldMaxOffset; throw new IllegalArgumentException("unknown format type at "); } formats[offsetNumber] = newFormat; segments[1].setLength(0); // throw away other segments segments[2].setLength(0); segments[3].setLength(0); } private static final int findKeyword(String s, String[] list) { s = s.trim().toLowerCase(); for (int i = 0; i < list.length; ++i) { if (s.equals(list[i])) return i; } return -1; } private static final void copyAndFixQuotes( String source, int start, int end, StringBuffer target) { for (int i = start; i < end; ++i) { char ch = source.charAt(i); if (ch == '{') { target.append("'{'"); } else if (ch == '}') { target.append("'}'"); } else if (ch == '\'') { target.append("''"); } else { target.append(ch); } } } /** * After reading an object from the input stream, do a simple verification * to maintain class invariants. * @throws InvalidObjectException if the objects read from the stream is invalid. */ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); boolean isValid = maxOffset >= -1 && formats.length > maxOffset && offsets.length > maxOffset && argumentNumbers.length > maxOffset; if (isValid) { int lastOffset = pattern.length() + 1; for (int i = maxOffset; i >= 0; --i) { if ((offsets[i] < 0) || (offsets[i] > lastOffset)) { isValid = false; break; } else { lastOffset = offsets[i]; } } } if (!isValid) { throw new InvalidObjectException("Could not reconstruct MessageFormat from corrupt stream."); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -