package org.apache.struts2.interceptor.debugging; import java.io.PrintWriter; import java.io.Writer; import java.util.Stack; /** * A simple writer that outputs XML in a pretty-printed indented stream. * *

By default, the chars

& < > " ' \r are escaped and replaced with a suitable XML entity. * To alter this behavior, override the the {@link #writeText(com.thoughtworks.xstream.core.util.QuickWriter, String)} * and {@link #writeAttributeValue(com.thoughtworks.xstream.core.util.QuickWriter, String)} methods.

* *

This code was taken from the XStream project under the BSD license.

* */ public class PrettyPrintWriter { private final PrintWriter writer; private final Stack elementStack = new Stack(); private final char[] lineIndenter; private boolean tagInProgress; private int depth; private boolean readyForNewLine; private boolean tagIsEmpty; private String newLine; private static final char[] NULL = "�".toCharArray(); private static final char[] AMP = "&".toCharArray(); private static final char[] LT = "<".toCharArray(); private static final char[] GT = ">".toCharArray(); private static final char[] SLASH_R = " ".toCharArray(); private static final char[] QUOT = """.toCharArray(); private static final char[] APOS = "'".toCharArray(); private static final char[] CLOSE = "': this.writer.write(GT); break; case '"': this.writer.write(QUOT); break; case '\'': this.writer.write(APOS); break; case '\r': this.writer.write(SLASH_R); break; default: this.writer.write(c); } } } public void endNode() { depth--; if (tagIsEmpty) { writer.write('/'); readyForNewLine = false; finishTag(); elementStack.pop(); } else { finishTag(); writer.write(CLOSE); writer.write((String)elementStack.pop()); writer.write('>'); } readyForNewLine = true; if (depth == 0 ) { writer.flush(); } } private void finishTag() { if (tagInProgress) { writer.write('>'); } tagInProgress = false; if (readyForNewLine) { endOfLine(); } readyForNewLine = false; tagIsEmpty = false; } protected void endOfLine() { writer.write(newLine); for (int i = 0; i < depth; i++) { writer.write(lineIndenter); } } public void flush() { writer.flush(); } public void close() { writer.close(); } }