logo       

Re: pretty printing page output: msg#00115

java.enhydra.xmlc

Subject: Re: pretty printing page output

At 11:15 PM 9/17/2003 -0700, you wrote:
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.

I suspect this has to do with the way the JBoss class loader works. I bet a restart using a standalone version of Tomcat would work just fine. XMLCContext caches a reference to itself as a singleton in the servlet context object. I'm not sure how JBoss reloads apps, but it must not kill the WebappClassLoader since it is really part of the larger class loader. If the class loader doesn't die, then the XMLCContext might not be dying. What might be happening is that the static variable values are being retained. In this case, the following code would be a problem....

public static XMLCContext getContext(ServletContext servletContext) {
if (initialized == false) {
synchronized (synch) {
// Now that we are synched, check to see if we are really not yet initialized. If so, initialize.
XMLCContext context = (XMLCContext)servletContext.getAttribute(CONTEXT_ATTRIBUTE);
if (context == null) {
context = new XMLCContextInit().createContext(servletContext);
servletContext.setAttribute(CONTEXT_ATTRIBUTE, context);
}
initialized = true;
}
}
return (XMLCContext)servletContext.getAttribute(CONTEXT_ATTRIBUTE);
}

Notice the "initialized" static variable. If it is "true", then null check for the XMLCContext object in the servlet context will never be done. I'm attaching a modified source and compiled XMLCContext class in a zip file. Please let me know if this solves your problem. Note that I compiled this with j2sdk1.4.2_01. If this causes binary compatibility problems, you can try compiling it yourself.

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?

Hmm... That's really weird. Let me research this a bit and I'll get back to you.

Jake

Thanks,
-M@

_______________________________________________
XMLC mailing list
XMLC@xxxxxxxxxxx
http://www.enhydra.org/mailman/listinfo.cgi/xmlc

Attachment: XMLCContext_fix.zip
Description: Zip archive

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise