Improved debug

This commit is contained in:
Georgy Litvinov 2021-02-12 18:30:53 +01:00
parent 0dd5fd9c05
commit 164d7871a1

View file

@ -24,12 +24,17 @@ import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.io.StringWriter;
import java.io.Writer;
import com.sun.star.uno.XComponentContext;
public class Debug {
static int number = 0;
public static void printNode(Node node){
Document document = node.getOwnerDocument();
DOMImplementationLS domImplLS = (DOMImplementationLS) document
@ -43,49 +48,60 @@ public class Debug {
System.out.println(ste);
}
}
public static void prettyPrintXml(Node node) {
printXMLNode(node, 0, true);
public static int prettyPrintXml(Node node) {
PrintStream ps;
try {
ps = new PrintStream(new File ("debug_" + number + ".txt"));
printXMLNode(node, 0, true, ps);
ps.close();
int echo = number;
number++;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return number - 1;
}
public static void printXMLNode(Node node, int depth, boolean printText) {
public static void printXMLNode(Node node, int depth, boolean printText, PrintStream ps) {
if (node == null) {
return;
}
for (int i = 0; i < depth; i++)
System.out.print(" ");
ps.print(" ");
if (node.getNodeType() == Node.TEXT_NODE) {
if (printText) {
System.out.println(node.getNodeValue());
ps.println(node.getNodeValue());
} else {
System.out.println("text");
ps.println("text");
}
return;
}
System.out.print('<');
System.out.print(node.getNodeName());
ps.print('<');
ps.print(node.getNodeName());
NamedNodeMap attrs = node.getAttributes();
if (attrs != null) {
for (int i = 0; i < attrs.getLength(); i++) {
Node attr = attrs.item(i);
System.out.print(' ');
System.out.print(attr.getNodeName());
System.out.print("=\"");
System.out.print(attr.getNodeValue());
System.out.print('"');
ps.print(' ');
ps.print(attr.getNodeName());
ps.print("=\"");
ps.print(attr.getNodeValue());
ps.print('"');
}
}
NodeList children = node.getChildNodes();
if (children == null || children.getLength() == 0)
System.out.println("/>");
ps.println("/>");
else {
System.out.println('>');
ps.println('>');
int len = children.getLength();
for (int i = 0; i < len; i++) {
printXMLNode(children.item(i), depth + 1, printText);
printXMLNode(children.item(i), depth + 1, printText, ps);
}
for (int i = 0; i < depth; i++)
System.out.print(" ");
System.out.println("</" + node.getNodeName() + ">");
ps.print(" ");
ps.println("</" + node.getNodeName() + ">");
}
}