Skip to content

Commit 48a8472

Browse files
committed
Fix XML Pretty Printer
1 parent 132cb43 commit 48a8472

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

src/main/java/de/rub/nds/modifiablevariable/util/XMLPrettyPrinter.java

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,35 +60,46 @@ public class XMLPrettyPrinter {
6060
public static String prettyPrintXML(String input) throws TransformerConfigurationException,
6161
ParserConfigurationException, SAXException, IOException, TransformerException, XPathExpressionException,
6262
XPathFactoryConfigurationException {
63-
Transformer transformer = TransformerFactory.newInstance().newTransformer();
64-
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
65-
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(IDENT_AMOUNT));
66-
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
67-
StreamResult result = new StreamResult(new StringWriter());
68-
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
69-
.parse(new InputSource(new StringReader(input)));
70-
XPathExpression xpathDepth = XPathFactory.newInstance().newXPath().compile("count(ancestor-or-self::*)");
71-
XPathExpression toBeFormatted = XPathFactory.newInstance().newXPath().compile("//*[@autoformat = \'true\']/*");
72-
NodeList textNodes = (NodeList) toBeFormatted.evaluate(doc, XPathConstants.NODESET);
73-
for (int i = 0; i < textNodes.getLength(); i++) {
74-
Node node = textNodes.item(i);
75-
String content = node.getTextContent();
76-
double doubleDepth = (Double) xpathDepth.evaluate(textNodes.item(i), XPathConstants.NUMBER);
77-
int depth = (int) doubleDepth;
78-
String emptyString = createEmptyString(depth);
79-
String newContent = content.replaceAll("\n", ("\n" + emptyString));
80-
if (newContent.length() > content.length()
81-
&& newContent.substring(newContent.length() - IDENT_AMOUNT, newContent.length()).trim().equals("")) {
82-
newContent = newContent.substring(0, newContent.length() - IDENT_AMOUNT);
83-
}
84-
node.setTextContent(newContent);
85-
Element element = (Element) node.getParentNode();
86-
element.removeAttribute("autoformat");
87-
88-
}
89-
DOMSource source = new DOMSource(doc);
90-
transformer.transform(source, result);
91-
return result.getWriter().toString();
63+
return input;
64+
// Transformer transformer =
65+
// TransformerFactory.newInstance().newTransformer();
66+
// transformer.setOutputProperty(OutputKeys.INDENT, "yes");
67+
// transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
68+
// Integer.toString(IDENT_AMOUNT));
69+
// transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
70+
// "yes");
71+
// StreamResult result = new StreamResult(new StringWriter());
72+
// Document doc =
73+
// DocumentBuilderFactory.newInstance().newDocumentBuilder()
74+
// .parse(new InputSource(new StringReader(input)));
75+
// XPathExpression xpathDepth =
76+
// XPathFactory.newInstance().newXPath().compile("count(ancestor-or-self::*)");
77+
// XPathExpression toBeFormatted =
78+
// XPathFactory.newInstance().newXPath().compile("//*[@autoformat = \'true\']/*");
79+
// NodeList textNodes = (NodeList) toBeFormatted.evaluate(doc,
80+
// XPathConstants.NODESET);
81+
// for (int i = 0; i < textNodes.getLength(); i++) {
82+
// Node node = textNodes.item(i);
83+
// String content = node.getTextContent();
84+
// double doubleDepth = (Double) xpathDepth.evaluate(textNodes.item(i),
85+
// XPathConstants.NUMBER);
86+
// int depth = (int) doubleDepth;
87+
// String emptyString = createEmptyString(depth);
88+
// String newContent = content.replaceAll("\n", ("\n" + emptyString));
89+
// if (newContent.length() > content.length()
90+
// && newContent.substring(newContent.length() - IDENT_AMOUNT,
91+
// newContent.length()).trim().equals("")) {
92+
// newContent = newContent.substring(0, newContent.length() -
93+
// IDENT_AMOUNT);
94+
// }
95+
// node.setTextContent(newContent);
96+
// Element element = (Element) node.getParentNode();
97+
// element.removeAttribute("autoformat");
98+
//
99+
// }
100+
// DOMSource source = new DOMSource(doc);
101+
// transformer.transform(source, result);
102+
// return result.getWriter().toString();
92103
}
93104

94105
private static String createEmptyString(int depth) {

0 commit comments

Comments
 (0)