com.pelzer.util
Class XMLPrettyPrintOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by com.pelzer.util.XMLPrettyPrintOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class XMLPrettyPrintOutputStream
extends OutputStream

As you pass in non-pretty xml, this will write out 'pretty' xml as best it can, but without validating the underlying XML in any way (as using the xerces code would require). This class may result in munged XML if whitespace was significant to it in some way. In general, this class reads an entire line of the incoming text, then parses it when a newline or stream close command comes in. If the line is empty (only whitespace) then the line is dropped. If the line starts with a '<' (after removing whitespace) then the system indents that line based on its perceived current depth.


Constructor Summary
XMLPrettyPrintOutputStream(OutputStream target)
           
 
Method Summary
 void close()
          Closes the underlying target stream.
 void flush()
          Flushes the target
static String getPrettyPrinted(String xml)
          Takes a given XML as a string and does a pretty print.
 void write(byte[] b)
           
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLPrettyPrintOutputStream

public XMLPrettyPrintOutputStream(OutputStream target)
Method Detail

write

public void write(int b)
           throws IOException
Specified by:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] b)
           throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

flush

public void flush()
           throws IOException
Flushes the target

Specified by:
flush in interface Flushable
Overrides:
flush in class OutputStream
Throws:
IOException

close

public void close()
           throws IOException
Closes the underlying target stream.

Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException

getPrettyPrinted

public static String getPrettyPrinted(String xml)
Takes a given XML as a string and does a pretty print. Internally creates streams, so if you already have a stream, it'll be more efficient to use this class as it was designed. This is mostly used for testing.



Copyright © 2012. All Rights Reserved.