PrettyPrintWriterpublic 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 void | addAttribute(java.lang.String key, java.lang.String value)
writer.write(' ");
writer.write(key);
writer.write('=");
writer.write('\"");
writeAttributeValue(writer, value);
writer.write('\"");
| public void | close()
writer.close();
| 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();
}
| protected void | endOfLine()
writer.write(newLine);
for (int i = 0; i < depth; i++) {
writer.write(lineIndenter);
}
| private void | finishTag()
if (tagInProgress) {
writer.write('>");
}
tagInProgress = false;
if (readyForNewLine) {
endOfLine();
}
readyForNewLine = false;
tagIsEmpty = false;
| public void | flush()
writer.flush();
| public void | setValue(java.lang.String text)
readyForNewLine = false;
tagIsEmpty = false;
finishTag();
writeText(writer, text);
| public void | startNode(java.lang.String name)
tagIsEmpty = false;
finishTag();
writer.write('<");
writer.write(name);
elementStack.push(name);
tagInProgress = true;
depth++;
readyForNewLine = true;
tagIsEmpty = true;
| protected void | writeAttributeValue(java.io.PrintWriter writer, java.lang.String text)
writeText(text);
| protected void | writeText(java.io.PrintWriter writer, java.lang.String text)
writeText(text);
| private void | writeText(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);
}
}
|
|