From 164d7871a134360432d4dbf9287da389c778c24e Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Fri, 12 Feb 2021 18:30:53 +0100 Subject: [PATCH] Improved debug --- src/main/java/pro/litvinovg/xml/Debug.java | 52 ++++++++++++++-------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/main/java/pro/litvinovg/xml/Debug.java b/src/main/java/pro/litvinovg/xml/Debug.java index 09f0bf6..e0930a3 100644 --- a/src/main/java/pro/litvinovg/xml/Debug.java +++ b/src/main/java/pro/litvinovg/xml/Debug.java @@ -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(""); + ps.print(" "); + ps.println(""); } }