private static String trimPomXml()

in src/main/java/org/apache/openejb/tools/release/maven/pom/PomParser.java [88:134]


    private static String trimPomXml(final String rawXml) throws IOException, ParserConfigurationException, SAXException, TransformerException {

        /*
         * Read the xml into a dom and strip out the parts we do not need
         */
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        final DocumentBuilder builder = factory.newDocumentBuilder();

        final Document document = builder.parse(IO.read(rawXml.trim()));
        final Element documentElement = document.getDocumentElement();

        /*
         * Strip off the namespace.  Some people use it, some do not.  JAXB is incapable of
         * being flexible on this so we normalize everything to not have the namespace.
         */
        final NamedNodeMap attributes = documentElement.getAttributes();
        while (attributes.getLength() > 0) {
            final Node item = attributes.item(0);
            attributes.removeNamedItem(item.getNodeName());
        }

        /*
         * These are the only elements we care about
         */
        final Predicate<Node> wanted = element("parent")
                .or(element("groupId"))
                .or(element("artifactId"))
                .or(element("version"))
                .or(element("properties"))
                .or(element("dependencies"));

        /*
         * Remove anything but the wanted elements
         */
        nodes(documentElement).stream()
                .filter(wanted.negate())
                .forEach(documentElement::removeChild);

        /*
         * Long-winded boilerplate code to write the DOM back out as xml
         */
        final TransformerFactory tf = TransformerFactory.newInstance();
        final Transformer trans = tf.newTransformer();
        final StringWriter sw = new StringWriter();
        trans.transform(new DOMSource(document), new StreamResult(sw));
        return sw.toString();
    }