Re: pretty printing page output

Matthew Hixson <[email protected]> Wed, 17 Sep 2003 23:15:34 -0700
Newsgroups gmane.comp.java.enhydra.xmlc
Message-ID <[email protected]>
On Wednesday, September 17, 2003, at 09:50  PM, Jacob Kjome wrote:

> At 02:21 PM 9/15/2003 -0700, you wrote:
>> Is there a way to format the resulting HTML from the toDocument() 
>> call into a more easily readable form?  Looks like there's not a 
>> single newline in the entire output.  That's great for a production 
>> system, but it's a bit hard on the eyes trying to debug HTML issues.
>
>
> You shouldn't be using toDocument() anyway.  It doesn't provide an 
> opportunity to add formatting options.  Are you using XMLC in a 
> servlet environment?  Then use....
>
> XMLCContext#writeDOM(HttpServletRequest, HttpServletResponse, 
> OutputOptions, XMLObject)
>
> The OutputOptions provide the magic.  Use 
> OutputOptions#setPrettyPrinting(true)
>
> Check out the XMLC-2.2 or 2.2.1 release in the tomcat example for code 
> using XMLCContext.
>
> Alternatively, you can just create a new DOMFormatter, pass it 
> OutputOptions (with pretty printing as described above), and then use 
> one of the various DOMFormatter#write() methods.
>
> Make sure to check out the Javadoc for XMLC (comes in a zip file 
> within the 2.2 and 2.2.1 releases) as it will show you all the options 
> I've mentioned above.

I went with the DOMFormatter because the XMLCContext was giving me 
grief when reloading my webapp in Tomcat.  I'm running Tomcat from 
within JBoss.  For some reason it is not noticing changes to my servlet 
so I have to touch web.xml.  This redeploys the servlet, but something 
happens to the context because from that point on 
XMLCContext.getContext(this) will return null.  To get around this I 
have to restart Tomcat/JBoss.
   The DOMFormatter does not suffer from this problem so I'm using it 
for now.  Only thing is that I've made use of OutputOptions and I still 
see no change in the source output.

		try{
			response.setContentType("text/html");
			OutputOptions output = new OutputOptions();
			output.setPrettyPrinting(true);
			output.setFormat(OutputOptions.FORMAT_HTML);
			output.setIndentSize(2);
			DOMFormatter df = new DOMFormatter();
			df.setOutputOptions(output);
			ServletOutputStream sos = response.getOutputStream();
			df.write(page, sos);
		}catch(IOException izzy){
			izzy.printStackTrace();
		}

Anything else I need to do to get it formatted?
   Thanks,
    -M@