luni, 15 iunie 2015

[OmniFaces utilities 2.1] Create a single XML Document based on given URLs representing XML documents


[OmniFaces utilities] The createDocument() method creates a single XML Document based on given URLs representing XML documents. All those XML documents are merged into a single root element named root.

Method:
Usage:

The Xml#createDocument() is based on Java, DocumentBuilderFactory, DocumentBuilder and Document API.
Let's suppose that you want to obtain the DOM of the web.xml file. For this, you can use the Xml#createDocument(), as below:

import org.omnifaces.util.Xml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
...
private static final String WEB_XML = "/WEB-INF/web.xml";
...
ServletContext servletContext = (ServletContext) Faces.getExternalContext().getContext();
List<URL> urls = new ArrayList<>();
urls.add(servletContext.getResource(WEB_XML));
      
try {
    Document document = Xml.createDocument(urls);
    // DOM available in memory
    // DOM root, Element root = document.getDocumentElement();
} catch (SAXException ex) {
    // ...
}

Let's suppose that you want to obtain the DOM of available faces-config.xml files. For this, you can use the Xml#createDocument(), as below:

import org.omnifaces.util.Xml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
...
private static final String APP_FACES_CONFIG_XML = "/WEB-INF/faces-config.xml";
private static final String LIB_FACES_CONFIG_XML = "META-INF/faces-config.xml";
...
ServletContext servletContext = (ServletContext) Faces.getExternalContext().getContext();
List<URL> urls = new ArrayList<>();
urls.add(servletContext.getResource(APP_FACES_CONFIG_XML));
urls.addAll(Collections.list(Thread.currentThread().getContextClassLoader().getResources(LIB_FACES_CONFIG_XML)));
            
try {
    Document document = Xml.createDocument(urls);
    // DOM available in memory
    // DOM root, Element root = document.getDocumentElement();
} catch (SAXException ex) {
    // ...
}

Sample of faces-config.xml that was merged:

D:/wildfly/standalone/deployments/Test_1-1.0-SNAPSHOT.war/WEB-INF/faces-config.xml

jar:file:/D:/wildfly/modules/system/layers/base/org/jboss/as/jsf-injection/main/wildfly-jsf-injection-8.2.0.Final.jar!/META-INF/faces-config.xml

vfs:/D:/wildfly/standalone/deployments/MoveComponent_1-1.0-SNAPSHOT.war/WEB-INF/lib/omnifaces-2.1.jar/META-INF/faces-config.xml

Niciun comentariu:

Trimiteți un comentariu