All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wildfly.swarm.plugin.repository.PomUtils Maven / Gradle / Ivy

There is a newer version: 86
Show newest version
package org.wildfly.swarm.plugin.repository;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * Author: Michal Szynkiewicz, [email protected]
 * Date: 3/17/17
 * Time: 11:04 PM
 */
class PomUtils {
    private PomUtils() {
    }

    static XmlToString extract(File xmlFile, String xpathLocator) {
        try {
            NodeList nodeList = extractNodes(xmlFile, xpathLocator);
            return new XmlToString(nodeList);
        } catch (IOException | SAXException | ParserConfigurationException | XPathExpressionException e) {
            System.err.println("Error extracting dependencies from xmlFile " + xmlFile);
            e.printStackTrace();
            System.exit(1);
        }
        return null;
    }

    private static NodeList extractNodes(File xmlFile, String xpathLocator) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile(xpathLocator);
        return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    }

    static class XmlToString {
        private final NodeList nodeList;

        private final Set expressionsToSkip = new HashSet<>();

        XmlToString(NodeList nodeList) {
            this.nodeList = nodeList;
        }

        XmlToString skipping(String... expression) {
            expressionsToSkip.addAll(Arrays.asList(expression));
            return this;
        }

        String asString() throws TransformerException {
            StringBuilder result = new StringBuilder();
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

            for (int i = 0; i < nodeList.getLength(); ++i) {
                StringWriter writer = new StringWriter();
                StreamResult streamResult = new StreamResult(writer);
                DOMSource source = new DOMSource();
                source.setNode(nodeList.item(i));
                transformer.transform(source, streamResult);
                String element = writer.toString();
                if (expressionsToSkip.stream().noneMatch(element::contains)) {
                    result.append(element).append("\n");
                }
            }

            return result.toString();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy