@Override public void processMeta(XmlMetadata file) { if (controlDoc == null) { // some action and } else { // real work here if (this.xpathExp == null) { // make this an assertion test } else { Document testDoc = getDocFragment(xpathExp, file); if (testDoc != null) { if (isdifferent(testDoc, controlDoc)) { // process for print formating (identify specific) LOG.debug("XML differences found"); } else { // remove this from list of things to report. LOG.debug("Remove from list. XML has default settings"); LinkedList<AbstractDecoration> dList = new LinkedList<AbstractDecoration>(file.getDecorations()); try { file.getDecorations().remove(dList.removeLast()); } catch (NoSuchElementException ne) { LOG.debug("Nothing removed. Decoration list is empty."); } } } } } return; }
/** * Extract an XML element. * * @param xpathExp * @param file * @return */ private Document getDocFragment(String xpathExp, XmlMetadata file) { Document testDoc = null; try { XPath xpath = XPathFactory.newInstance().newXPath(); Node node = (Node) xpath.evaluate(xpathExp, file.getParsedDocument(), XPathConstants.NODE); if (node != null) { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(buffer)); String testStr = buffer.toString(); LOG.debug("src string: " + testStr); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); testDoc = dBuilder.parse(new InputSource(new ByteArrayInputStream(testStr.getBytes("utf-8")))); } } catch (XPathExpressionException xee) { LOG.error("Invalid XPath expression: " + xpathExp); } catch (Exception e) { LOG.error("Error extracting XML content: ", e); } return testDoc; }