Monday, February 27, 2006

xtags v1.0 parse custom JSP tag using Dom4J Document object parameter

Background:
Trying to parse a Dom4J XML Document using <xtags:parse> in order to iterate through it (XSLT style) inside JSP code.

Problem Description:
The JSP code has an inline Dom4J Document object, however it cannot be passed as a parameter to xtags:parse
eg. when trying:
<%
org.dom4j.Document xmlDoc = ...;
%>
<xtags:parse reader="<%= xmlDoc %>" />

the following error message is occuring:
Explicit cast needed to convert org.dom4j.Document to java.io.Reader._jspx_th_xtags_parse_0.setReader( xmlDoc);

according to the xtags:parse v1.0 API documentation, this is occuring because the reader parameter of xtags:parse requires an object of type java.io.Reader so the question is how to get a java.io.Reader from an org.dom4j.Document (The xtags:parse v1.0 documentation shows how to parse a URL, an XML file, a java.io.Reader etc, but doesn't have an example for an org.dom4j.Document parameter).

Problem Solution:
Kind of straight-forward and ugly but:
<%
org.dom4j.Document xmlDoc = ...;
%>
<xtags:parse>
<%= xmlDoc.asXML() %>
</xtags:parse>
It would have been nicer to have a parameter to the tag that accepts an org.dom4j.Document however the performance doesn't seem to be an issue (ie. with the writing of the XML straight to the output stream).

Monday, February 20, 2006

Dom4j SAXParseException performing XSLT with JSTL 1.0 transform tag

Background:
Trying to perform an XSL Transformation of a Dom4J Document in some JSP. Receiving SAXParseException.

Problem Description:
The Following Code:
Document searchResults = ...

<c:import url="/servlet/test.xsl" var="stylesheet" />

<x:transform xml="${
searchResults}" xslt="${stylesheet}">
<x:param name="currentUrlNoQueryString" value="<%=request.getRequestURI()%>"/>
<x:param name="queryStringNoSortType" value="<%=queryStringNoSortType%>"/>
<x:param name="selectedSortType" value='<%=request.getParameter("selectedSortType")%>'/>
</x:transform>

is producing:
javax.xml.transform.TransformerException: org.xml.sax.SAXParseException: Document root element is missing.


Problem Solution:
The solution was that my path to the XSL file (ie. "/servlet/test.xsl") was not being resolved properly.

ie. I changed it explicitly to:
<c:import url="http://servername/servlet/test.xsl" var="stylesheet" />

and it worked. I Guess the error message wasn't intuitive enough for me at first (a lot of other forum posts eg. this one and this one seem to suggest the same error message can also be caused by invalid ie. non UTF characters at the beginning of the XML file).

As a side-note, I was unable to find a JSTL 1.0 API specification anywhere. The JSTL 1.1 API Specification for the x:transform tag deprecates the xml attribute and the 1.0 specification doesn't yet support the doc attribute.

Thursday, February 16, 2006

Image Thumbnail (16*16px) Icons for common file types

Seems like I am constantly having to find icons for common file types. This page will serve as a reference to find them when needed:

Text (.txt) MimeType: text/plain

WinZip (.zip) MimeType: application/zip

Adobe (.pdf) MimeType: application/pdf

Macromedia Flash SWF (.swf) MimeType: application/zip

Microsoft Word (.doc) MimeType: application/msword

Excel (.xls) MimeType: application/excel

Microsoft Visio (.vsd) MimeType: application/x-visio

Microsoft PowerPoint (.ppt) MimeType: application/powerpoint

GIF Image (.gif) MimeType: image/gif

JPEG Image (.jpg, .jpeg) MimeType: image/jpeg

MPEG Video (.mpeg) MimeType: video/mpeg

BMP Image (.bmp) MimeType: image/bmp

PNG Image (.png) MimeType: image/png

Executable (.exe) MimeType: application/octet-stream

Tuesday, February 07, 2006

Printing server-side JSP scriptlet code to output HTML conditionally from a custom JSP tag

Background:
The Java Web Application (WAR) that I am developing uses a content management system (Fatwire CMS) that executes all JSP code during the publish process and spits the resultant HTML out into static files in the published output WAR.

Problem Description
I needed to be able to conditionally either execute JSP code or write the JSP to the output.

Problem Solution:
The JSP bodycontent attribute of the tag config in the web.xml can be set to either 'JSP' or 'tagdependent' (as per this article) ie:

<bodycontent>tagdependent</bodycontent>


When bodycontent is set to 'JSP' in the web.xml, the following JSP code:
<%= "a" + "b" %>

outputs to the result stream:
ab

Wraps any 'dynamic' server side processing JSP code that is to
be deployed to the application server without being parsed by
the Fatwire publication process.

Also, allows evaluation of the code in preview mode of Fatwire.

however, when bodycontent is set to 'tagdependent' in the web.xml, the same JSP code outputs:
<%= "a" + "b" %>

The question now is how to conditionally determine whether to set bodycontent to 'JSP' or 'tagdependent'

Resolution: Was unable to find a JSP API call that dynamically modified the bodycontent property of the custom tag. As a work-around, implemented a custom version of the Fatwire tag render:sattelitepage (containing if/else logic that either parsed the page in preview mode using ics.ReadPage() or otherwise printed out the include contents if in publish mode)

Note: bodycontent should be set to 'tagdependent' not 'tagdependant' (that typo in the web.xml caused this error for me: jasper.error.bad.bodycontent.type)
Using a custom JSP tag, the conditional
saaj.jar fr