My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...









[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians


luni, 15 iunie 2015

[OmniFaces utilities 2.1] Convenience method to return a node list matching given XPath expression

[OmniFaces utilities] The getNodeList() method return a node list matching given XPath expression.


Before reading this post, please read about Xml#createDocument().

This example was practically "ripped up" from OmniFaces 2.1, WebXml class, which is used to parses the /WEB-INF/web.xml and all /META-INF/web-fragment files found in the classpath and offers methods to obtain information from them which is not available by the standard Servlet API.

import org.omnifaces.util.Xml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
private static final String WEB_XML = "/WEB-INF/web.xml";

// the following elements will be resolved
private static final String XPATH_WELCOME_FILE = "welcome-file-list/welcome-file";
private static final String XPATH_EXCEPTION_TYPE = "error-page/exception-type";
private static final String XPATH_SECURITY_CONSTRAINT = "security-constraint";
ServletContext servletContext = (ServletContext) Faces.getExternalContext().getContext();
List<URL> urls = new ArrayList<>();
try {
    Document document = Xml.createDocument(urls);
    // DOM root available in memory
    Element root = document.getDocumentElement();
    // XPath
    XPath xpath = XPathFactory.newInstance().newXPath();
    // resolve XPATH_WELCOME_FILE
    NodeList welcomeFileList = getNodeList(root, xpath, XPATH_WELCOME_FILE);

    NodeList exceptionTypes = getNodeList(root, xpath, XPATH_EXCEPTION_TYPE);
    NodeList constraints = getNodeList(root, xpath, XPATH_SECURITY_CONSTRAINT);

} catch (...) {
    // ...

Niciun comentariu :

Trimiteți un comentariu


Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors