XSP technology: An introduction, part 2
Last time, we
saw the steps involved in creating an XSP application. But how does XSP code
look? Well, let's walk through one complete example, following the steps we
outlined in our last XML tip.
First, let's look
at an XML file that could be created by the content folks:
<?xml version="1.0"?>
<?cocoon-process type="xsp"?>
<?cocoon-process type="xslt"?>
<?xml-stylesheet href="currentDate.xsl" type="text/xsl"?>
<xsp:page
language="java"
xmlns:xsp=http://www.apache.org/1999/XSP/Core
xmlns:samplens="http://myurl.com/Sample">
<page title="Print the Date">
<p>
Today's date is
<samplens:current-date format="MM/DD/YYYY"/>
</p>
</page>
</xsp:page>
You can see
that the first two tags of note are the cocoon-process ones. This is like
Servlet chaining, in that the file will be processed in order of the
cocoon-process tags. The first one specifies that the file is to be processed
as an XSP document, and the second one specifies that it is to be transformed
using XSLT. The actual stylesheet used is identified within
<?xml-stylesheet?>.
Next is
<xsp:page>, which is the document root element. This also contains the
language type as well as the taglib to be used.
Lastly, we have
the body of the page itself, which prints out today's date and uses a custom
tag from the taglib to generate that date.
Next time,
we'll look at the taglib, which contains our custom definitions!
XSP technology: An introduction, part 3
The XML
document from our last Java TechMail must now be converted to an XSP document
for compilation into the producer. Therefore, all custom tags used from any
taglibs must now be resolved.
Here is the
taglib we used for our example (which, by the way, is a stylesheet itself!):
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl=http://www.w3.org/XSL/Transform/1.0
xmlns:xsp=http://www.apache.org/1999/XSP/Core
xmlns:samplens="http://myurl.com/DTD/XSP/Sample">
<xsl:template match="xsp:page">
<xsp:page>
<xsl:copy>
<xsl:apply-templates select="@*"/>
</xsl:copy>
<xsp:structure>
<xsp:include>java.util.Date</xsp:include>
<xsp:include>java.text.SimpleDateFormat</xsp:include>
</xsp:structure>
<xsp:logic>
private static String formatDate(Date date, String format) {
if((format == null)||(format.length() == 0)) {
format = "MM/DD/YYYY";
}
return (new SimpleDateFormat(format)).format(date);
}
</xsp:logic>
<xsl:apply-templates/>
</xsp:page>
</xsl:template>
<xsl:template match="samplens:current-date">
<xsp:expr>
formatDate(new Date(), "<xsl value-of select="@format"/>")
</xsp:expr>
</xsl:template>
<xsl:template match="@*|node()" priority="-1">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Since we have
already discussed in an earlier tip how taglibs work, we won't go into this
much. The main thing to note here is that the custom tag will be replaced with
XSP tags, because the resultant document must be valid XSP.
After
conversion, our XSP document is now ready to be called by a client, and it
looks like this:
<?xml version="1.0"?>
<?cocoon-process type="xsp"?>
<?cocoon-process type="xslt"?>
<?xml-stylesheet href="currentDate.xsl" type="text/xsl"?>
<xsp:page
language="java"
xmlns:xsp=http://www.apache.org/1999/XSP/Core
xmlns:samplens="http://myurl.com/DTD/XSP/Sample">
<page title="Print the Date">
<p>
Today's date is
<xsp:expr>
formatDate(new Date(), "MM/DD/YYYY")
</xsp:expr>
</p>
</page>
</xsp:page>
Next time, we go even
further!Pa
XSP technology: An introduction, part 4
So now we have
our XSP file! When the client first requests this page, the XSP is compiled to
a producer. After the first call, all subsequent requests will go directly to
the already-compiled producer.
Let's say that
a client has now requested our XSP. The producer will evaluate and execute all
of the code contained within our file and then generate an XML document as the
output. In our case, the document will look like:
<?xml version="1.0"?>
<?cocoon-process type="xslt"?>
<?xml-stylesheet href="currentDate.xsl" type="text/xsl"?>
<page title="Print the Date">
<p>Today's date is 01/12/2001</p>
</page>
As you can see,
the actual value of the date is now a static value within the XML document. Also,
the only cocoon-process tag left is the one that specifies the presentation
stylesheet to be used for conversion to HTML.
This output is
passed to the XSLT processor, which uses the XSL document specified within the
XML document to change the output to
<html>
<head><title>Print the Date</title></head>
<body>
<h3 style="color:navy; text-align:center">Print the Date</h3>
<p>Today's date is 01/12/2001</p>
</body>
</html>
Done! Using this
technology, a developmental team can completely separate logical areas in such
a way that changes to one segment don't necessitate changes to another. The
developers can alter the taglib code and never have to touch the XSP or XML
file that the content team is responsible for. Some developers can take the
separation a step further and use bean code within the taglib. Using this
technique, a change to the bean code even prevents changes to the taglib!
We wind up our
discussion in the next Java TechMail.
XSP technology: An introduction, part 5
XSP gives us
several advantages over conventional Java-enhanced HTML. Each of these packages
is automatically imported into XSP:
java.io.*;
java.util.*;
org.w3c.dom.*;
org.xml.sax.*;
javax.servlet.*;
javax.servlet.http.*;
org.apache.cocoon.parser.*;
org.apache.cocoon.producer.*;
org.apache.cocoon.framework.*;
org.apache.cocoon.processor.xsp.*;
As such,
explicit declarations of these packages within xsp:include are not necessary.
Also, you'll note
from our example in the May 3, 2001 Java TechMail that when we generated the
value for the date, no explicit type casting was needed. This is because
<xsp:expr> automatically converts all Java types into strings for us!
Other rules and
features worthy of note include how to handle characters such as
"<" or "&" within a <xsp:logic> node. These
characters have certain functional properties within XML parsers and therefore
must be represented as < and & respectively. However, there are
workarounds to performing these substitutions manually.
If you are looking for a highly manageable methodology for the maintenance of a large production Web application, XSP could be the technology you have been waiting for! Keep in mind, however, that XSP is still relatively new, and the learning curve for mastering the technology could be considerable. If you are considering using XSP for a project with a tight deadline, it may be advisable to contemplate Java Server Page (JSP) technology as an alternative. More information is available at the Apache XSP Processor page.