Example usage for java.text MessageFormat format

List of usage examples for java.text MessageFormat format

Introduction

In this page you can find the example usage for java.text MessageFormat format.

Prototype

public final String format(Object obj) 

Source Link

Document

Formats an object to produce a string.

Usage

From source file:MessageFormatApp.java

public static void main(String args[]) {
    String pattern = "The time is {0,time} and ";
    pattern += "your lucky number is {1,number}.";
    MessageFormat format = new MessageFormat(pattern);
    Object objects[] = { new Date(), new Integer((int) (Math.random() * 1000)) };
    String formattedOutput = format.format(objects);
    System.out.println(formattedOutput);
}

From source file:MessageFormatReuse.java

public static void main(String args[]) {
    String pattern = "{0}K was deleted on {1}.";
    MessageFormat formatter = new MessageFormat(pattern);

    Double kb = new Double(3.5);
    Date today = new Date();
    Object[] arguments = { kb, today };

    formatter.setLocale(Locale.US);
    System.out.println(formatter.format(arguments));
}

From source file:MainClass.java

public static void main(String[] argv) {
    String pattern = "{0}K was deleted on {1}.";
    MessageFormat formatter = new MessageFormat(pattern);

    Double kb = new Double(3.5);
    Date today = new Date();
    Object[] arguments = { kb, today };

    formatter.setLocale(Locale.US);
    System.out.println(formatter.format(arguments));

    formatter.setLocale(Locale.FRANCE);
    System.out.println(formatter.format(arguments));

    pattern = "On {1}, {0}K was deleted.";
    formatter.applyPattern(pattern);/*  w ww.j av a2 s. c  o m*/
    System.out.println(formatter.format(arguments));

    formatter.setLocale(Locale.US);
    System.out.println(formatter.format(arguments));
}

From source file:com.clustercontrol.util.Messages.java

/**
 * Returns the formatted message for the given key in the resource bundle.
 * //from w w w  .j  a  v a 2  s. c  om
 * @param key
 *            the resource name
 * @param args
 *            the message arguments
 * @param locale
 * @return the string
 */
public static String getString(String key, Object[] args, Locale locale) {
    MessageFormat messageFormat = new MessageFormat(getString(key, key, locale));
    return messageFormat.format(args);
}

From source file:at.ac.tuwien.infosys.jcloudscale.datastore.util.URLUtil.java

private static String formatMessage(String message, Object... args) {
    MessageFormat messageFormat = new MessageFormat(message);
    return messageFormat.format(args);
}

From source file:com.pactera.edg.am.metamanager.extractor.util.GenSqlUtil.java

/**
 * ?SQL//from  w w w  . j  a v a 2  s.co m
 * 
 * @throws IOException
 *             XML?
 */
private static Map<String, String> readSQL(String location) throws IOException {
    Map<String, String> sqls = new HashMap<String, String>();
    MessageFormat mf = new MessageFormat(Constants.SQL_STORE_PATH);
    String file = mf.format(new Object[] { location });

    InputStream in = null;
    Dom4jReader reader = null;
    try {
        in = GenSqlUtil.class.getClassLoader().getResourceAsStream(file);
        reader = new Dom4jReader();
        reader.initDocument(in);
        List<?> elements = reader.selectNodes(Constants.SQL_FLAG);
        for (int i = 0; i < elements.size(); i++) {
            Element element = (Element) elements.get(i);
            sqls.put(element.attributeValue("id"), element.getTextTrim());
        }
    } finally {
        if (reader != null) {
            reader.close();
        }
        if (in != null) {
            in.close();
        }
    }
    return sqls;
}

From source file:it.cnr.icar.eric.common.security.KeyToolStripped.java

/** 
  * Generate a public/private key pair.//  w w w.  j av  a2  s  .  c o  m
  *
  * @throws Exception
  */
public static void generateKeyPair(KeyStore keyStore, char[] storePass, String alias, char[] keyPass,
        String dname, String keyAlg, int validity) throws Exception {
    int keySize = 1024;
    if (keyStore.containsAlias(alias)) {
        MessageFormat messageformat = new MessageFormat("Key pair not generated, alias <alias> already exists");
        Object[] aobj = { alias };
        throw new Exception(messageformat.format(((Object) (aobj))));
    }

    String sigAlg = null;

    if (keyAlg.equalsIgnoreCase("DSA")) {
        sigAlg = "SHA1WithDSA";
    } else if (keyAlg.equalsIgnoreCase("RSA")) {
        sigAlg = "MD5WithRSA";
    } else {
        throw new Exception("Cannot derive signature algorithm");
    }

    //Must specify provider "SunRsaSign" otherwise it gets some weird NSS specific provider
    //when running in AppServer EE.
    CertAndKeyGen certandkeygen = new CertAndKeyGen(keyAlg, sigAlg);
    X500Name x500name;

    if (dname == null) {
        throw new Exception("Key pair not generated, dname is null.");
    } else {
        x500name = new X500Name(dname);
    }

    certandkeygen.generate(keySize);

    PrivateKey privatekey = certandkeygen.getPrivateKey();
    X509Certificate[] ax509certificate = new X509Certificate[1];
    ax509certificate[0] = certandkeygen.getSelfCertificate(x500name, validity * 24 * 60 * 60);

    keyStore.setKeyEntry(alias, privatekey, keyPass, ax509certificate);
}

From source file:net.sf.ginp.util.GinpUtil.java

/**
 * Get an Internationalized Message encoded with a Message Format.
 * @param key the message key//from   ww  w . ja  v a2s  . c  o  m
 * @param args keys for messageformat
 * @return the i18n message
 */
public static String message(final String key, final Object[] args) {
    String format = bundle.getString(key);
    MessageFormat msg = new MessageFormat(format);
    String formatted = msg.format(args);

    return formatted;
}

From source file:org.alfresco.extension.scripting.javascript.JavascriptHelper.java

public static String getMessage(String messageId, Object[] args) {
    Context cx = Context.getCurrentContext();
    Locale locale = cx == null ? Locale.getDefault() : cx.getLocale();

    // ResourceBundle does cacheing.
    ResourceBundle rb = ResourceBundle.getBundle("org.alfresco.extension.scripting.javascript.Messages",
            locale);//from w w  w .jav a 2  s . c o  m

    String formatString;
    try {
        formatString = rb.getString(messageId);
    } catch (java.util.MissingResourceException mre) {
        throw new RuntimeException("no message resource found for message property " + messageId);
    }

    if (args == null) {
        return formatString;
    } else {
        MessageFormat formatter = new MessageFormat(formatString);
        return formatter.format(args);
    }
}

From source file:com.icesoft.faces.utils.MessageUtils.java

public static FacesMessage getMessage(FacesContext facesContext, String messageId, Object params[]) {
    String messageInfo[] = new String[2];

    Locale locale = facesContext.getViewRoot().getLocale();
    String bundleName = facesContext.getApplication().getMessageBundle();
    //see if the message has been overridden by the application
    if (bundleName != null) {
        try {//from   www . j  a  v  a  2  s  .  c  o m
            loadMessageInfo(bundleName, locale, messageId, messageInfo);
        } catch (Exception e) {
            if (log.isWarnEnabled())
                log.warn(e + ", using " + ICE_MESSAGES_BUNDLE);
        }
    }

    //TODO Use defered evaluation of the parameters, like how
    // JSF 1.2's javax.faces.component.MessageFactory.
    // BindingFacesMessage does. ICE-2290.

    //if not overridden then check in Icefaces message bundle.
    if (messageInfo[SUMMARY] == null && messageInfo[DETAIL] == null) {
        loadMessageInfo(ICE_MESSAGES_BUNDLE, locale, messageId, messageInfo);
    }
    if (params != null) {
        MessageFormat format;
        for (int i = 0; i < messageInfo.length; i++) {
            if (messageInfo[i] != null) {
                format = new MessageFormat(messageInfo[i], locale);
                messageInfo[i] = format.format(params);
            }
        }
    }
    return new FacesMessage(messageInfo[SUMMARY], messageInfo[DETAIL]);
}