Java String Format getRequriedArgumentCount(MessageFormat msgFormat)

Here you can find the source of getRequriedArgumentCount(MessageFormat msgFormat)

Description

Returns the number of required arguments for the given MessageFormat.

License

Open Source License

Parameter

Parameter Description
msgFormat The MessageFormat to be analyzed

Return

the number of required arguments for the given MessageFormat

Declaration

public static int getRequriedArgumentCount(MessageFormat msgFormat) 

Method Source Code


//package com.java2s;
/*//w ww .ja  v a2s .  c  om
 * Copyright 2015 (C) Tom Parker <thpr@users.sourceforge.net>
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.text.ChoiceFormat;
import java.text.Format;
import java.text.MessageFormat;

public class Main {
    /**
     * Returns the number of required arguments for the given MessageFormat.
     * This will analyze all paths of any choices embedded in the format to
     * ensure that any arguments they may call are also considered.
     * 
     * @param msgFormat
     *            The MessageFormat to be analyzed
     * @return the number of required arguments for the given MessageFormat
     */
    public static int getRequriedArgumentCount(MessageFormat msgFormat) {
        Format[] formats = msgFormat.getFormatsByArgumentIndex();
        int required = formats.length;
        MessageFormat mf = new MessageFormat("");
        for (Format fmt : formats) {
            if (fmt instanceof java.text.ChoiceFormat) {
                mf.applyPattern(((ChoiceFormat) fmt).toPattern());
                required = Math.max(required, getRequriedArgumentCount(mf));
            }
        }
        return required;
    }
}

Related

  1. getFormattedString(String key, Object arg)
  2. getFormattedString(String p_bundleName, String p_key, Locale p_locale, Object[] p_arguments)
  3. getFormatValue(String value, Object[] args)
  4. getImageWriter(ImageTypeSpecifier imageType, String imageFormatName)
  5. getImageWriter(String formatName)
  6. getStylingHyphenFormat(String cssProperties)
  7. getTextAsFormattedLines(String text, int lineLength)
  8. getWriterByFormat(final String format)
  9. isDurationFormatPattern(String formatPattern)