Example usage for java.text MessageFormat MessageFormat

List of usage examples for java.text MessageFormat MessageFormat

Introduction

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

Prototype

public MessageFormat(String pattern) 

Source Link

Document

Constructs a MessageFormat for the default java.util.Locale.Category#FORMAT FORMAT locale and the specified pattern.

Usage

From source file:com.jslsolucoes.tagria.lib.util.TagUtil.java

public static String getLocalizedForLib(String key, JspContext jspContext, Object... args) {
    try {//from w  w w. j  a  v a 2  s  . c om
        MessageFormat messageFormat = new MessageFormat(
                ResourceBundle.getBundle("messages_tagrialib", locale(jspContext)).getString(key));
        return messageFormat.format(args);
    } catch (MissingResourceException e) {
        logger.error("could not find key resource", e);
        return '!' + key + '!';
    }
}

From source file:com.npower.dm.util.MessageResources.java

/**
 * Returns a text message after parametric replacement of the specified
 * parameter placeholders.  A null string result will be returned by
 * this method if no resource bundle has been configured.
 *
 * @param locale The requested message Locale, or <code>null</code>
 *  for the system default Locale//from   www .j  a  v  a  2 s .  c  om
 * @param key The message key to look up
 * @param args An array of replacement parameters for placeholders
 */
public String getMessage(Locale locale, String key, Object args[]) {

    // Cache MessageFormat instances as they are accessed
    if (locale == null) {
        locale = defaultLocale;
    }

    MessageFormat format = null;
    String formatKey = messageKey(locale, key);

    synchronized (formats) {
        format = (MessageFormat) formats.get(formatKey);
        if (format == null) {
            String formatString = getMessage(locale, key);

            if (formatString == null) {
                return returnNull ? null : ("???" + formatKey + "???");
            }

            format = new MessageFormat(escape(formatString));
            format.setLocale(locale);
            formats.put(formatKey, format);
        }

    }

    return format.format(args);
}

From source file:edu.emory.cci.aiw.cvrg.eureka.services.resource.PhenotypeResource.java

private void deleteFailed(List<String> phenotypesUsedIn, PhenotypeEntity proposition)
        throws HttpStatusException {
    String phenotypeList;//from  www.  j a v  a 2 s .c om
    int size = phenotypesUsedIn.size();
    if (size > 1) {
        List<String> subList = phenotypesUsedIn.subList(0, phenotypesUsedIn.size() - 1);
        phenotypeList = StringUtils.join(subList, ", ") + " and " + phenotypesUsedIn.get(size - 1);
    } else {
        phenotypeList = phenotypesUsedIn.get(0);
    }
    MessageFormat usedByOtherPhenotypes = new MessageFormat(
            messages.getString("phenotypeResource.delete.error.usedByOtherPhenotypes"));
    String msg = usedByOtherPhenotypes
            .format(new Object[] { proposition.getDisplayName(), phenotypesUsedIn.size(), phenotypeList });
    throw new HttpStatusException(Response.Status.PRECONDITION_FAILED, msg);
}

From source file:org.jahia.ajax.gwt.helper.StubHelper.java

/**
 * Initialize a map with all data needed to render the code editor.
 * /*from w w  w  .  j a  v  a  2  s . c o m*/
 * @param path
 *            path from where we are trying to open the code editor
 * @param isNew
 *            is this a new file or an existing one
 * @param nodeTypeName
 *            null or the node type associated with the file
 * @param fileType
 *            the type of file we are creating/editing
 * @return a RpcMap containing all information to display code editor
 * @throws GWTJahiaServiceException
 *             if something happened
 */

public RpcMap initializeCodeEditor(final String path, final boolean isNew, final String nodeTypeName,
        final String fileType, final String siteName, final Locale uiLocale, final JCRSessionWrapper session)
        throws GWTJahiaServiceException {
    RpcMap r = new RpcMap();

    String ntName = nodeTypeName == null && path != null ? detectNodeTypeName(path, session) : nodeTypeName;

    r.put("stubs", getCodeSnippets(fileType, "stub", ntName));

    List<GWTJahiaValueDisplayBean> snippetsByType;
    Map<String, List<GWTJahiaValueDisplayBean>> snippets = new LinkedHashMap<String, List<GWTJahiaValueDisplayBean>>();

    if (ntName != null) {
        GWTJahiaNodeType nodeType = contentDefinition.getNodeType(ntName, uiLocale);
        if (nodeType != null) {
            r.put("nodeType", nodeType);
            List<GWTJahiaItemDefinition> items = new ArrayList<GWTJahiaItemDefinition>(nodeType.getItems());
            items.addAll(nodeType.getInheritedItems());
            for (String snippetType : propertiesSnippetTypes) {
                snippetsByType = new ArrayList<GWTJahiaValueDisplayBean>();
                for (Map.Entry<String, String> propertySnippetEntry : getCodeSnippets(fileType, snippetType,
                        null).entrySet()) {
                    MessageFormat labelTemplate = null;
                    for (GWTJahiaItemDefinition definition : items) {
                        String defName = definition.getName();
                        if (!"*".equals(defName) && !definition.isNode() && !definition.isHidden()) {
                            String propertySnippet = StringUtils.replace(propertySnippetEntry.getValue(),
                                    "__value__", defName);
                            if (null == labelTemplate) {
                                labelTemplate = new MessageFormat(getLabelTemplate(fileType, snippetType,
                                        propertySnippetEntry.getKey(), uiLocale));
                            }
                            GWTJahiaValueDisplayBean displayBean = new GWTJahiaValueDisplayBean(propertySnippet,
                                    labelTemplate.format(new String[] { defName }));
                            displayBean.set("text", StringUtils
                                    .replace(StringUtils.replace(propertySnippet, "<", "&lt;"), ">", "&gt;"));
                            snippetsByType.add(displayBean);
                        }

                    }
                }
                snippets.put(snippetType, snippetsByType);
            }
        }
    }

    Map<String, Set<String>> availableResources = template.getAvailableResources(siteName);

    snippetsByType = fillResources(fileType, uiLocale, availableResources);
    if (!snippetsByType.isEmpty() || !snippets.isEmpty()) {
        r.put("availableResources", availableResources);
        snippets.put("resources", snippetsByType);
        r.put("snippets", snippets);
    }

    Map<String, Set<String>> availableModuleResources = new HashMap<String, Set<String>>();
    availableModuleResources.put("css", template.getAvailableResources(siteName, "css", ".css", false));
    availableModuleResources.put("javascript",
            template.getAvailableResources(siteName, "javascript", ".js", false));
    snippetsByType = fillResources(fileType, uiLocale, availableModuleResources);
    if (!snippetsByType.isEmpty() || !snippets.isEmpty()) {
        r.put("availableModuleResources", availableModuleResources);
        snippets.put("moduleResources", snippetsByType);
        r.put("snippets", snippets);
    }
    return r;
}

From source file:com.taobao.tddl.common.ConfigServerHelper.java

public static String getDBGroupsConfig(String appName) {
    if (appName == null || appName.length() == 0) {
        throw new IllegalStateException("appName");
    }//from  ww  w.j  av  a2  s .  c o  m
    String dataId = new MessageFormat(DATA_ID_DB_GROUP_KEYS).format(new Object[] { appName });
    return dataId;
}

From source file:org.apache.struts.util.MessageResources.java

/**
 * Returns a text message after parametric replacement of the specified
 * parameter placeholders.  A null string result will be returned by this
 * method if no resource bundle has been configured.
 *
 * @param locale The requested message Locale, or <code>null</code> for
 *               the system default Locale
 * @param key    The message key to look up
 * @param args   An array of replacement parameters for placeholders
 *///from  www.ja  v  a  2 s . c  o m
public String getMessage(Locale locale, String key, Object[] args) {
    // Cache MessageFormat instances as they are accessed
    if (locale == null) {
        locale = defaultLocale;
    }

    MessageFormat format = null;
    String formatKey = messageKey(locale, key);

    synchronized (formats) {
        format = (MessageFormat) formats.get(formatKey);

        if (format == null) {
            String formatString = getMessage(locale, key);

            if (formatString == null) {
                return returnNull ? null : ("???" + formatKey + "???");
            }

            format = new MessageFormat(escape(formatString));
            format.setLocale(locale);
            formats.put(formatKey, format);
        }
    }

    return format.format(args);
}

From source file:com.taobao.tddl.common.ConfigServerHelper.java

public static String getShardRuleConfig(String appName) {
    if (appName == null || appName.length() == 0) {
        throw new IllegalStateException("appName");
    }/*from  ww  w. j  a  v a  2 s. c om*/
    String dataId = new MessageFormat(DATA_ID_TDDL_SHARD_RULE).format(new Object[] { appName });
    return dataId;
}

From source file:org.pentaho.reporting.libraries.pensol.vfs.LocalFileModel.java

public void createFolder(final FileName file) throws FileSystemException {
    final String[] fileName = computeFileNames(file);

    if (fileName.length < 2) {
        throw new FileSystemException("Cannot create directory in the root.");
    }/*from w w  w . j a  v  a  2 s. c  o m*/

    final String[] parentPath = new String[fileName.length - 1];
    System.arraycopy(fileName, 0, parentPath, 0, parentPath.length);
    final FileInfo fileInfo = lookupNode(parentPath);
    if (fileInfo == null) {
        throw new FileSystemException("Cannot locate parent directory.");
    }

    try {
        final String solution = fileName[0];
        final String path = buildPath(fileName, 1, fileName.length - 1);
        final String name = fileName[fileName.length - 1];
        String description = getDescriptionEntries().get(file);
        if (description == null) {
            description = "";
        }
        final Configuration config = LibPensolBoot.getInstance().getGlobalConfig();
        final String urlMessage = config
                .getConfigProperty("org.pentaho.reporting.libraries.pensol.web.CreateNewFolder");
        final MessageFormat fmt = new MessageFormat(urlMessage);
        final String fullpath = fmt
                .format(new Object[] { URLEncoder.encode(solution, "UTF-8"), URLEncoder.encode(path, "UTF-8"),
                        URLEncoder.encode(name, "UTF-8"), URLEncoder.encode(description, "UTF-8") });

        URI uri;
        String baseUrl = url + fullpath;
        try {
            URIBuilder builder = new URIBuilder(baseUrl);
            logger.debug("Connecting to '" + baseUrl + '\'');
            if (username != null) {
                builder.setParameter("user", username);
            }
            if (password != null) {
                builder.setParameter("password", password);
            }
            uri = builder.build();
        } catch (URISyntaxException e) {
            throw new FileSystemException("Provided URL is invalid: " + baseUrl);
        }
        final HttpPost filePost = new HttpPost(uri);
        filePost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

        HttpResponse httpResponse = client.execute(filePost, context);
        final int lastStatus = httpResponse.getStatusLine().getStatusCode();
        if (lastStatus != HttpStatus.SC_OK) {
            throw new FileSystemException("Server error: HTTP status code " + lastStatus);
        }
        if (name == null) {
            throw new FileSystemException("Error creating folder: Empty name");
        }

        new FileInfo(fileInfo, name, description);
    } catch (FileSystemException fse) {
        throw fse;
    } catch (IOException ioe) {
        throw new FileSystemException("Failed", ioe);
    }
}

From source file:org.sakaiproject.tool.roster.FilteredParticipantListingBean.java

private String getFormattedMessage(String key, String[] params) {
    String rawString = LocaleUtil.getLocalizedString(FacesContext.getCurrentInstance(),
            ServicesBean.MESSAGE_BUNDLE, key);
    MessageFormat format = new MessageFormat(rawString);
    return format.format(params);
}

From source file:libepg.ts.packet.TsPacket.java

/**
 * transport_scrambling_control(?)/*from www .  j  a v a 2 s.co m*/
 *
 * @return
 * @throws IllegalStateException ????(03?)????
 */
public synchronized TRANSPORT_SCRAMBLING_CONTROL getTransport_scrambling_control()
        throws IllegalStateException {
    int temp;
    temp = ByteConverter.byteToInt(this.data.getData()[3]);
    temp = temp >>> 6;
    switch (temp) {
    case 0:
        return TsPacket.TRANSPORT_SCRAMBLING_CONTROL.NOT_SCRAMBLED;
    case 1:
        return TsPacket.TRANSPORT_SCRAMBLING_CONTROL.RESERVED;
    case 2:
        return TsPacket.TRANSPORT_SCRAMBLING_CONTROL.SCRAMBLED_BY_EVEN_KEY;
    case 3:
        return TsPacket.TRANSPORT_SCRAMBLING_CONTROL.SCRAMBLED_BY_ODD_KEY;
    }
    MessageFormat msg = new MessageFormat(
            "??????={0}");
    Object[] parameters = { temp };
    throw new IllegalStateException(msg.format(parameters));

}