Java XPath Expression merge(XPathExpression expression, InputStream... files)

Here you can find the source of merge(XPathExpression expression, InputStream... files)

Description

merge

License

Open Source License

Declaration

private static Document merge(XPathExpression expression, InputStream... files) throws Exception 

Method Source Code

//package com.java2s;
/*/*from   ww  w . jav a2 s  .co  m*/
 * Copyright (C) 2009-2015 SM2 SOFTWARE & SERVICES MANAGEMENT
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program has been created in the hope that it will be useful.
 * It is distributed WITHOUT ANY WARRANTY of any Kind,
 * without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 *
 * For more information, please contact SM2 Software & Services Management.
 * Mail: info@talaia-openppm.com
 * Web: http://www.talaia-openppm.com
 *
 * Module: utils
 * File: SessionFactoryUtil.java
 * Create User: javier.hernandez
 * Create Date: 06/03/2015 14:35:37
 */

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    private static Document merge(String expression, InputStream... files) throws Exception {

        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        XPathExpression compiledExpression = xpath.compile(expression);
        return merge(compiledExpression, files);
    }

    private static Document merge(XPathExpression expression, InputStream... files) throws Exception {

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setIgnoringElementContentWhitespace(true);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document base = docBuilder.parse(files[0]);

        Node results = (Node) expression.evaluate(base, XPathConstants.NODE);

        if (results == null) {
            throw new IOException(files[0] + ": expression does not evaluate to node");
        }

        for (int i = 1; i < files.length; i++) {

            Document merge = docBuilder.parse(files[i]);
            Node nextResults = (Node) expression.evaluate(merge, XPathConstants.NODE);

            while (nextResults.hasChildNodes()) {

                Node kid = nextResults.getFirstChild();
                nextResults.removeChild(kid);
                kid = base.importNode(kid, true);
                results.appendChild(kid);
            }
        }

        return base;
    }
}

Related

  1. isLegalXPath(final String xPath)
  2. isValidFilter(String xPathString)
  3. isXPathAbsolute(String path)
  4. loadFromXmlNode(String xmlFile, String prefix)
  5. loadNodeList(String file, String path)
  6. modelPathExpr()
  7. parseXMLValue(String xml, String xpathExpression)
  8. parseXPath(String expression, NamespaceContext nsContext)
  9. pathExists(XPath xpath, String expression, Object object)