XML Filter ver. 1.7


This is a Java servlet filter (as per Servlet API 2.3). XMLFilter lets you to perform post-processing for your jsp pages. It is a generic Java filter that uses XSLT to provide final content from the given XML source and associated XSL stylesheet. We assume that XML source is an output from your jsp page. You have to provide for this filter a configuration file, where you can describe your XSL stylesheets and business rules how to proceed requests. Rules are parameterized by parameters from request's header (User-Agent and Accept). In other words each rule looks like: "if User-Agent is ... then use XML data with this XSL stylesheet" or "if client accepts such content-type then use XML data with this XSL stylesheet".
So all your jsp pages may generate XML data only and depending on incoming request XMLFilter can convert XML source into specific content (WML,HTML etc.) viewable for the device raised this request.

How to use it:

a) download xmlflt.jar and save it in WEB-INF/lib

b) describe this filter in web.xml. The name for initial parameter is config. Parameter's value describes a path to XML file where you will define your configuration (rules for transformation).
 


<filter>
  <filter-name>XMLFilter</filter-name>
  <filter-class>com.cj.xmlflt.XMLFilter</filter-class>
  <init-param>
    <param-name>config</param-name>
    <param-value>path_to_your_file</param-value>
  </init-param>
</filter>

c) describe a mapping for this filter in web.xml
 


<filter-mapping>
  <filter-name>XMLFilter</filter-name>
  <url-pattern>*.jsp</url-pattern>
</filter-mapping>

in this case filter will be on for the each .jsp file

And now when you invoke any .jsp page: http://your_host/your_page.jsp XMLFilter will perform post-processing for the given page.

Configuration file is XML file. You must define the following elements:
  - one or more XSL data source. Source could be local (e.g.: c:/mydata/myfile.xsl) or remote (e.g. : http://www.acme.com/source.xsl)
  - one or more business rules (how to select XSL source for the given request)
  - default response (XSL source name if all rules are failed).

Config => (+xslsource, +rule, default)

xslsource - defines xsl sheet. Could be local or remote. Attributes are: name - unique name. E.g.:

<xslsource name="first">c:/data/file1.xsl</xmlsource>

rule - defines a rule (condition, trigger). There are two forms:
   a) if user-agent header contains this string then use that XSL-source and that content-type for the output. E.g.: if user-agent header contains string MSIE than set content type to text/html and use the described XSL-source.

<rule agent="MSIE" xsl="first" type="text/html"/>

   a) if accept header contains this string then use that XSL-source and that content-type for the output. E.g.: if client accepts wap.wml than set content type to text/vnd.wap.wml and use XSL source with name second:

<rule accept="wap.wml" xsl="second" type="text/vnd.wap.wml"/>

Attributes are: accept - defines accept header, agent - defines user-agent header, xsl - xsl source name, type - defines content-type for output.

default - defines default transformation (if all conditions are failed). Attributes are: xsl - xsl source name, type - defines a content-type for output. E.g.:

<default xsl="xsl0" type="text/html"/>

Notes:

1. Filter uses JAXP for parsing XML files.

2. Config file can be saved anywhere on your server. You may use the full path for setting data file location.

   For downloading:
    Filter:  xmlflt.jar
    Sample of config file:  xmlflt.xml

 ©  Coldbeans     Comments?

See also JSOS - the largest collection of servlets and filters.

Also in JSOS: