FileDocCategorySizeDatePackage
PrettyPrintWriter.javaAPI DocExample5599Mon Jul 23 13:26:52 BST 2007org.apache.struts2.interceptor.debugging

PrettyPrintWriter

public class PrettyPrintWriter extends Object

Fields Summary
private final PrintWriter
writer
private final Stack
elementStack
private final char[]
lineIndenter
private boolean
tagInProgress
private int
depth
private boolean
readyForNewLine
private boolean
tagIsEmpty
private String
newLine
private static final char[]
NULL
private static final char[]
AMP
private static final char[]
LT
private static final char[]
GT
private static final char[]
SLASH_R
private static final char[]
QUOT
private static final char[]
APOS
private static final char[]
CLOSE
Constructors Summary
public PrettyPrintWriter(Writer writer, char[] lineIndenter, String newLine)


           
        this.writer = new PrintWriter(writer);
        this.lineIndenter = lineIndenter;
        this.newLine = newLine;
    
public PrettyPrintWriter(Writer writer, char[] lineIndenter)

        this(writer, lineIndenter, "\n");
    
public PrettyPrintWriter(Writer writer, String lineIndenter, String newLine)

        this(writer, lineIndenter.toCharArray(), newLine);
    
public PrettyPrintWriter(Writer writer, String lineIndenter)

        this(writer, lineIndenter.toCharArray());
    
public PrettyPrintWriter(Writer writer)

        this(writer, new char[]{' ", ' "});
    
Methods Summary
public voidaddAttribute(java.lang.String key, java.lang.String value)

        writer.write(' ");
        writer.write(key);
        writer.write('=");
        writer.write('\"");
        writeAttributeValue(writer, value);
        writer.write('\"");
    
public voidclose()

        writer.close();
    
public voidendNode()

        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();
        }
    
protected voidendOfLine()

        writer.write(newLine);
        for (int i = 0; i < depth; i++) {
            writer.write(lineIndenter);
        }
    
private voidfinishTag()

        if (tagInProgress) {
            writer.write('>");
        }
        tagInProgress = false;
        if (readyForNewLine) {
            endOfLine();
        }
        readyForNewLine = false;
        tagIsEmpty = false;
    
public voidflush()

        writer.flush();
    
public voidsetValue(java.lang.String text)

        readyForNewLine = false;
        tagIsEmpty = false;
        finishTag();

        writeText(writer, text);
    
public voidstartNode(java.lang.String name)

        tagIsEmpty = false;
        finishTag();
        writer.write('<");
        writer.write(name);
        elementStack.push(name);
        tagInProgress = true;
        depth++;
        readyForNewLine = true;
        tagIsEmpty = true;
    
protected voidwriteAttributeValue(java.io.PrintWriter writer, java.lang.String text)

        writeText(text);
    
protected voidwriteText(java.io.PrintWriter writer, java.lang.String text)

        writeText(text);
    
private voidwriteText(java.lang.String text)

        int length = text.length();
        for (int i = 0; i < length; i++) {
            char c = text.charAt(i);
            switch (c) {
                case '\0":
                    this.writer.write(NULL);
                    break;
                case '&":
                    this.writer.write(AMP);
                    break;
                case '<":
                    this.writer.write(LT);
                    break;
                case '>":
                    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);
            }
        }