@@ -47,36 +47,25 @@ public static String prettyPrintXML(String input) throws TransformerConfiguratio
4747 transformer .setOutputProperty (OutputKeys .INDENT , "yes" );
4848 transformer .setOutputProperty ("{http://xml.apache.org/xslt}indent-amount" , Integer .toString (IDENT_AMOUNT ));
4949 transformer .setOutputProperty (OutputKeys .OMIT_XML_DECLARATION , "yes" );
50-
5150 StreamResult result = new StreamResult (new StringWriter ());
5251 Document doc = DocumentBuilderFactory .newInstance ().newDocumentBuilder ()
5352 .parse (new InputSource (new StringReader (input )));
54-
55- // XPath for selecting all text contents
56- XPathExpression xpath = XPathFactory .newInstance ().newXPath ().compile ("//*[text()]/*" );
57- // this is better but it does not work, because the default java xpath
58- // transformer does not support XPath 2.0
59- // XPathExpression xpath =
60- // XPathFactory.newInstance().newXPath().compile("//*[text()[matches(.,'^[0-9A-F ]*$')]]");
61- // XPath for counting the number of ancestors of a current element
53+ XPathExpression xpath = XPathFactory .newInstance ().newXPath ().compile ("//*[count(./*) = 0]" );
6254 XPathExpression xpathDepth = XPathFactory .newInstance ().newXPath ().compile ("count(ancestor-or-self::*)" );
6355
6456 NodeList textNodes = (NodeList ) xpath .evaluate (doc , XPathConstants .NODESET );
6557
6658 for (int i = 0 ; i < textNodes .getLength (); i ++) {
6759 String content = textNodes .item (i ).getTextContent ();
68- System .out .println (textNodes .item (i ).getTextContent ());
6960 double doubleDepth = (Double ) xpathDepth .evaluate (textNodes .item (i ), XPathConstants .NUMBER );
7061 int depth = (int ) doubleDepth ;
7162 String emptyString = createEmptyString (depth );
72- System .out .println (depth );
7363 String newContent = content .replaceAll ("\n " , ("\n " + emptyString ));
74- // remove last white space elements from the text content to align
75- // the closing tag
7664 if (newContent .length () > content .length ()) {
7765 newContent = newContent .substring (0 , newContent .length () - IDENT_AMOUNT );
7866 }
7967 textNodes .item (i ).setTextContent (newContent );
68+
8069 }
8170
8271 DOMSource source = new DOMSource (doc );
0 commit comments