<title>Servlet Filter with XTP -- Vary</title>

<p/>The draft Servlet 2.3 specification creates a new way to control
servlets: filters.  Filters can inspect and modify the request or
response before and after passing the request to the servlet.

<p/>This example uses a servlet filter to select a stylesheet Serif
(XTP) pages.  If the user adds the style=plain query, the page will
use a plain stylesheet.  Otherwise the page will use a fancy
stylesheet.

<p/>The filter itself just looks at the "style" parameter.  If it's
"plain", then the filter will set the <var/caucho.xsl.stylesheet/>
parameter to <var/plain.xsl/>.  Serif uses
<var/caucho.xsl.stylesheet/> to transform the page to HTML.

<example title="test.VaryFilter.java">
package test;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class VaryFilter implements Filter {
  private FilterConfig config;

  public void init(FilterConfig config)
  {
    this.config = config;
  }

  public void doFilter(ServletRequest request,
                       ServletResponse response,
                       FilterChain next)
    throws IOException, ServletException
  {
    HttpServletRequest req = (HttpServletRequest) request;

    String style = req.getParameter("style");

    if ("plain".equals(style))
      req.setAttribute("caucho.xsl.stylesheet", "plain.xsl");
    else
      req.setAttribute("caucho.xsl.stylesheet", "default.xsl");

    next.doFilter(request, response);
  }

  public void destroy()
  {
  }
}
</example>

<example title="web.xml">
&lt;web-app>
  &lt;filter-mapping url-pattern='*.xtp'
                  filter-name='test.VaryFilter'/>
&lt;/web-app>
</example>

<p/>The sample Serif page has a small section and some content.  It's
parsed as HTML before using the stylesheets.  The default stylesheet
will color the section header red.  The plain stylesheet leaves it as
black.

<example title="test.xtp">
&lt;title>A Sample Title&lt;/title>

&lt;section title="A Sample Section">

Some content in the sample section.

&lt;/section>
</example>

<p/>The example uses StyleScript as the style language.  You can
easily translate the example to use string XSL syntax.

<p/>Unknown tags are copied from the Serif page to the generated HTML
page unchanged.  So you can just add rules for tags you want to
change.  The example forces the background of the body to white and
formats a section with an H3 header colored red.

<example title="default.xsl">
$output(disable-output-escaping=>true);

*|@* &lt;&lt;
  $copy() &lt;&lt;
     $apply-templates(node()|@*);
  >>
>>

body &lt;&lt;
  &lt;body bgcolor=white>
    $apply-templates();
  &lt;/body>
>>

section &lt;&lt;
  &lt;h3>&lt;font color=red>$(@title)&lt;/font>&lt;/h3>
  $apply-templates();
>>
</example>

<p/>The plain stylesheet leaves the Serif page untouched except for
converting the section title to use plain H3.

<example title="plain.xsl">
$output(disable-output-escaping=>true);

*|@* &lt;&lt;
  $copy() &lt;&lt;
     $apply-templates(node()|@*);
  >>
>>

section &lt;&lt;
  &lt;h3>$(@title)&lt;/h3>
  $apply-templates();
>>
</example>
