page 182
tootomatic\index2.html . We use the Redirect extension whenever we need to generate an
HTML file for a section index or an individual panel.
9.5.5 Generating the Individual Panels
The masthead and footer of each panel are fairly straightforward; both use a predefined format and a series of links common to all pages on IBM sites. This is a perfect use for
named templates. We need to create certain HTML markup for the masthead of each HTML page, and we need to create more markup for the footer of each page. In addition, we need to
create the title bar at the top of each page and a navigation bar an area with Previous and Next links, among other things at the top and bottom of most pages. We use four templates,
cleverly named
dw-masthead
,
dw-title-bar
,
dw-nav-bar
, and
dw-footer
, to do this:
xsl:call-template name=dw-masthead xsl:call-template name=dw-title-bar
xsl:call-template name=dw-nav-bar xsl:with-param name=includeMain select=youBetcha
xsl:with-param name=sectionNumber select=sectionNumber xsl:with-param name=position select=pos
xsl:with-param name=last select=last xsl:with-param name=topOrBottom select=top
xsl:with-param name=oneOrTwo select=two xsl:call-template
-- Processing for the main body of the page goes here -- xsl:call-template name=dw-nav-bar
xsl:with-param name=includeMain select=youBetcha xsl:with-param name=sectionNumber select=sectionNumber
xsl:with-param name=position select=pos xsl:with-param name=last select=last
xsl:with-param name=topOrBottom select=bottom xsl:with-param name=oneOrTwo select=two
xsl:call-template xsl:call-template name=dw-footer
Of the four templates, only
dw-nav-bar
takes any parameters. Depending on the page were currently generating, we may or may not need the Main menu button we dont include this
button on the Main menu panel. We need the current section number so the navigation bar can create filenames for the links to the section menu, the previous panel, and the next panel.
The
position
parameter defines the position of this particular panel;
last
defines the position of the last panel. If
position
is 1, then the Previous button will be disabled. If
position
is equal to
last
, then the Next button will be disabled. The parameter
topOrBottom
defines whether this navigation bar is being created at the top or bottom of the panel we have to
name the images differently so the JavaScript mouseover effects work correctly. Finally, the
oneOrTwo
parameter determines whether this panel will have two navigation bars or just one. This is also necessary for the mouseover effects.
Now that weve built all these parts of the page, building the actual content of the panel is somewhat anticlimactic. We support a limited set of HTML tags the 20 or so most-used tags,
added sparingly as weve needed to add new functions to the tool, most of which are converted directly into their HTML equivalents.
9.5.6 Generating the PDF Files
Converting the XML document to an XSL Formatting Objects XSL-FO stream is fairly straightforward, as well. Our printed layout consists of the graphics and text from the tutorial,
combined with page numbers, headers, and footers to create high-quality printed output. We use the Apache XML Projects FOP Formatting Objects to PDF tool to do this.
page 183
When we invoke the PDF-generating templates with the
mode=generate-pdf
attribute, we pass in the
page-size
parameter to set the dimensions of the printed page. We generate PDFs with both letter-sized and A4-sized pages to support our customers around the world.
To create the PDF, we first create the output file of formatting objects, converting the various XML tags from our source document into the various formatting objects we need:
fo:block font-size=16pt line-height=19pt font-weight=bold space-after.optimum=12pt
Introduction to JavaServer Pages fo:block
fo:block space-after.optimum=6pt In todays environment, most web sites want to display dynamic content based on
the user and the session. Most content, such as images, text, and banner ads, is most
easily built with HTML editors. So we need to mix the static content of HTML files with directives for accessing or generating dynamic content.
fo:block fo:block space-after.optimum=6pt
JavaServer Pages meet this need. They provide server-side scripting support for generating web pages with combined
static and dynamic content. fo:block
Currently, the XSL:FO specification is a candidate recommendation at the World Wide Web Consortium W3C. Because future changes are likely, we wont discuss the formatting
objects themselves. It suffices to say that our stylesheet defines page layouts margins, running headers and footers, etc. and then creates a number of formatting objects inside
those page layouts. The FOP tool handles the details of calculating line, page, and column breaks, page references, and hyperlinks.
Once the file of formatting objects is created, we call an extension function to convert the formatting objects file into a PDF. Heres the exensions main code:
public static void buildPDFFileString foFilename, String pdfFilename {
try {
XMLReader parser = XMLReader Class.forNameorg.apache.xerces.parsers.SAXParser
.newInstance; Driver driver = new Driver;
driver.setRendererorg.apache.fop.render.pdf.PDFRenderer, Version.getVersion;
driver.addElementMappingorg.apache.fop.fo.StandardElementMapping; driver.addElementMappingorg.apache.fop.svg.SVGElementMapping;
driver.addPropertyListorg.apache.fop.fo.StandardPropertyListMapping; driver.addPropertyListorg.apache.fop.svg.SVGPropertyListMapping;
driver.setOutputStreamnew FileOutputStreampdfFilename; driver.buildFOTreeparser, new InputSourcefoFilename;
driver.format; driver.render;
}
The code merely creates the FOP
Driver
object, sets its various properties, and then tells it to render the formatting objects in a PDF file. The main difficulty here is in determining how
the various XML elements should be converted to formatting objects; once the conversion is done, we have a tool that generates high-quality printable output from our XML source files.
Best of all, this code uses open source tools exclusively.