枚举faces-config.xml中定义的resource-bundle

时间:2015-01-14 08:52:43

标签: jsf-2 resourcebundle faces-config

我正在使用特定于mojarra的代码:

public static Map<String, ResourceBundle> getBundleMap()
{
    Locale locale = Faces.getLocale();

    ApplicationAssociate associate = ApplicationAssociate.getCurrentInstance();
    Map<String, ApplicationResourceBundle> resourceBundles = associate.getResourceBundles();

    Map<String, ResourceBundle> map = new HashMap<>(resourceBundles.size());
    for(Entry<String, ApplicationResourceBundle> entry : resourceBundles.entrySet())
    {
        String name = entry.getKey();
        ResourceBundle bundle = entry.getValue().getResourceBundle(locale);

        map.put(name, bundle);
    }

    return map;
}

我希望有一种与实现无关的方法来获取此地图。

我应该解析应用程序和库中定义的每个faces-config.xml吗?这不是重塑轮子吗?

Map<String, String>,其中key = /faces-config/application/resource-bundle/varvalue = /faces-config/application/resource-bundle/base-name就足够了。

感谢。

1 个答案:

答案 0 :(得分:3)

  

我想要一种与实现无关的方法来获取此地图。

可以理解的。


  

我应该解析应用程序和库中定义的每个faces-config.xml吗?

是。 JSF API中没有此功能。


  

这不是重新发明轮子吗?

是的,当然。但是,您可以尝试将其转换为OmniFaces,它已经为/WEB-INF/web.xml/META-INF/web-fragment.xml提供了类似的实用工具类,WebXml


  

一个Map<String, String>,其中key = /faces-config/application/resource-bundle/var和value = /faces-config/application/resource-bundle/base-name就足够了。

以下是使用JAXP( cough )的启动示例:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(false);
factory.setExpandEntityReferences(false);

DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
document.appendChild(document.createElement("all-faces-configs"));

List<URL> facesConfigURLs = new ArrayList<>();
facesConfigURLs.add(FacesContext.getCurrentInstance().getExternalContext().getResource("/WEB-INF/faces-config.xml"));
facesConfigURLs.addAll(Collections.list(Thread.currentThread().getContextClassLoader().getResources("META-INF/faces-config.xml")));

for (URL facesConfigURL : facesConfigURLs) {
    URLConnection connection = facesConfigURL.openConnection();
    connection.setUseCaches(false);

    try (InputStream input = connection.getInputStream()) {
        NodeList children = builder.parse(input).getDocumentElement().getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            document.getDocumentElement().appendChild(document.importNode(children.item(i), true));
        }
    }
}

Map<String, String> resourceBundles = new HashMap<>();
Element allFacesConfigs = document.getDocumentElement();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList resourceBundleNodes = (NodeList) xpath.compile("application/resource-bundle").evaluate(allFacesConfigs, XPathConstants.NODESET);

for (int i = 0; i < resourceBundleNodes.getLength(); i++) {
    Node resourceBundleNode = resourceBundleNodes.item(i);
    String var = xpath.compile("var").evaluate(resourceBundleNode).trim();
    String baseName = xpath.compile("base-name").evaluate(resourceBundleNode).trim();
    resourceBundles.put(var, baseName);
}