Exercises

On Day 14, “JSP Tag Libraries,” you developed a custom tag library for use with your Web application. Today, you will write an XSLT stylesheet that will transform a Tag Library Descriptor (TLD) document into an HTML page for viewing.

Listing 17.18 shows the agency.tld file from Day 14 that you will transform into HTML for display by a Web browser.

Listing 17.18. Full Text of agency.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>agency</short-name>
  <tag>
    <name>option</name>
    <tag-class>webagency.OptionTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
      <name>selected</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      <type>java.lang.Object</type>
    </attribute>
    <attribute>
      <name>option</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      <type>java.lang.String</type>
    </attribute>
  </tag>
  <tag>
    <name>useAgency</name>
    <tag-class>webagency.UseAgencyTag</tag-class>
    <body-content>empty</body-content>
    <variable>
      <name-given>agency</name-given>
      <variable-class>agency.Agency</variable-class>
      <declare>true</declare>
      <scope>AT_BEGIN</scope>
    </variable>
  </tag>
  <tag>
    <name>useApplicant</name>
    <tag-class>webagency.UseApplicantTag</tag-class>
    <body-content>empty</body-content>
    <variable>
      <name-given>app</name-given>
      <variable-class>agency.Register</variable-class>
      <declare>true</declare>
      <scope>AT_BEGIN</scope>
    </variable>
    <attribute>
      <name>login</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
  <tag>
    <name>useCustomer</name>
    <tag-class>webagency.UseCustomerTag</tag-class>
    <body-content>empty</body-content>
    <variable>
      <name-given>cust</name-given>
      <variable-class>agency.Advertise</variable-class>
      <declare>true</declare>
      <scope>AT_BEGIN</scope>
    </variable>
    <attribute>
      <name>login</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
  <tag>
    <name>useJob</name>
    <tag-class>webagency.UseJobTag</tag-class>
    <body-content>empty</body-content>
    <variable>
      <name-given>job</name-given>
      <variable-class>agency.AdvertiseJob</variable-class>
      <declare>true</declare>
      <scope>AT_BEGIN</scope>
    </variable>
    <attribute>
      <name>ref</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>customer</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
</taglib>

To display this TLD document, you will need to write an XSLT stylesheet that defines rules for the following transformations:

  • Put the tag library name in an <H1> element.

  • Define a <TABLE border="1"> element to contain all the tags.

  • Highlight each tag name using a <STRONG> element.

  • Put each tag in a row in the table and put the tag name, body content, a list of the attributes and a list of the variables each in its own cell.

Listing 17.19 shows a suitable HTML page containing one row that meets the previously listed requirements.

Listing 17.19. Transformed HTML Output
<HTML>
  <HEAD>
  <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <TITLE>agency TLD</TITLE>
  </HEAD>
  <BODY>
    <H1>Tag Library Name: agency</H1>
    <TABLE border="1">
      <TR align="left" valign="top">
        <TH colspan="4">Tag</TH>
      </TR>
      <TR align="left" valign="top">
        <TH>Name</TH><TH>Body</TH><TH>Attributes</TH><TH>Variables</TH>
      </TR>
      <TR align="left" valign="top">
        <TD><STRONG>option</STRONG></TD><TD>empty</TD><TD>selected<BR>option<BR>
      </TD><TD></TD>
    </TR>
    </TABLE>
  </BODY>
</HTML>

To get you started, there is an example TLD (agency.tld) and an empty XSLT stylesheet (tld.xsl) in the Day17/exercise/XML directory on the accompanying Web site. Also included in the exercise/src directory is a simple Java program called Transform that can be used to apply your XSLT stylesheet to an XML document.

To test your XSLT stylesheet run the command

> java -classpath classes Transform XML/tld.xsl XML/agency.tld

and study the HTML output. You can also use the following command to save the output to a file:

> java -classpath classes Transform XML/tld.xsl XML/agency.tld XML/agency.html

You can view the file XML/agency.html in your browser.

Alternatively use the supplied asant build files and enter:

asant run

and browse the output file XML/agency.html as before.

Finally, Figure 17.4 shows a screen snapshot of how your transformed HTML page should appear in a Web browser.

Figure 17.4. Applying tld.xsl to agency.tld.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.76.237