The styler task makes useful combinations of XSLT transformations easy to specify in an Ant build file. Like the built-in task style, styler can apply a single transformation to a set of XML files. But it can also:
handle multiple transformations, in parallel or pipelined.
enable transformations that split or merge files
process non-XML files, especially HTML (based on JTidy) and Simple Outline XML (SOX).
apply non-XSLT transformation, especially "regular fragmentations"
use any custom XMLReader or XMLFilter class to handle new file formats and transformation techniques. See the customisation page.
As an add-in task, styler must be installed by placing the styler.jar
file in the Ant lib directory. The following taskdef is then placed
in any build.xml file that uses styler:
<taskdef classname="au.com.Langdale.styler.StylerTask" name="styler"/>
The styler element has no attributes, everything is specified with nested elements as follows.
<styler> <fileset ...> ... </fileset> <input ...> <mapper .../> </input> <output ...> <mapper .../> </output> <transform ...> <mapper .../> </transform> ... <param ...> <mapper .../> </param> ... </styler>
The input, output, transform, and param elements each take an optional, nested mapper element (see core types) and the following attributes.
| Attribute | Description | Required |
|---|---|---|
| name | The name of a stylesheet parameter. Applies to param elements only. | Required for param elements. |
| type | The type of an input, output or transform file. Determines the parser, serializer or transformer type. If not present, the file extension is used as the type. | Optional. |
| value | The string value of a stylesheet parameter. Applies to param elements only and no mapping from the base file is performed. |
At least one of these attributes or a nested mapper is required. |
| file | A specific file to use as the input, output, transform or parameter value. No mapping from the base file is performed. | |
| dir |
Specifies a directory for mapping each base file to the input, output, transform, or parameter value. The dir attribute is used with a nested mapper element. If the mapper is omitted, an identity mapping is assumed. If a mapper is supplied but not a dir attribute, then the project base directory is used. Note that the fileset directory is not used when generating file names. |
The type attribute or, if omitted, the file name extension determines how each input and transform file is parsed and how each output file is generated. For transforms the type also selects the transformer implementation. The following table lists the built-in alternatives but these may be extended (see below).
| Type or File Extension | Parsing | Transforming | Generating |
|---|---|---|---|
|
html |
Parsed as HTML using JTidy to produce an XML equivalent. |
N/A
|
Generated as HTML with HTML output method. |
| htm | |||
| xhtml | Parsed as XML with Xerces or other default XML parser. | Generated as XML without XML declaration. | |
| text | Generated as plain text with default TEXT output method. | ||
| txt | |||
| ptl | Parsed a a Rational Rose UML model file using Xpetal and converted to an XML equivalent. |
N/A
|
|
| mdl | |||
| sox | Parsed as Simple Outline XML. | Interpreted as XSLT templates by Xalan. | |
| xslt | Parsed as XML with Xerces or other default XML parser. | Generated as XML with default XML output properties. | |
| frag | Interpreted as regular fragmentation rules by FragmentFilter. | ||
| other |
N/A
|
Processors to handle additional file and transform types may be added relatively easily if they have SAX interfaces. See the customisation page.
This example processes each XML file in the testdata directory to produce a file of the same name in the testout directory. Each file is transformed by the select_data.xslt stylesheet followed by the format_report.xslt stylesheet. Its often easier to design each stylesheet to do just one thing and chain them in this way.
<styler>
<fileset dir="testdata">
<include name="*.xml"/>
</fileset>
<transform file="select_data.xslt"/>
<transform file="format_report.xslt"/>
<output dir="testout"/>
</styler>
This example runs each stylesheet found in the skins directory to transform the same file, log.xml. A parameter, user-name, is passed to each stylesheet. The results are written as HTML in the views directory.
<styler>
<fileset dir="skins">
<include name="*.xslt"/>
</fileset>
<input file="log.xml"/>
<transform dir="skins"/>
<param name="user-name" value="${user}"/>
<output dir="results">
<mapper type="glob" from="*.xslt" to="*.html"/>
</output>
</styler>
This example merges each input file *Xml with a corresponding file *.extra producing a file in the merged directory with the same name as the input.
<styler>
<fileset dir=".">
<include name="*Xml"/>
</fileset>
<transform file="merge.xslt"/>
<param name="url-to-merge" dir="."> <mapper type="glob" from="*Xml" to="*Extra"/>
</param>
<output dir="merged"/>
</pipe>
The merge.xslt file might contain something like the following
in one of its templates:
<xsl:apply-templates select="document($url-to-merge)" />
This example parses HTML files, then repairs all site-relative URL's to point
at the new document directory. The document directory is passed by the task
to the stylesheet as a parameter, called base, in the form of an
URL.
<styler>
<fileset dir="site">
<include name="*.html"/>
</fileset>
<transform file="fix-base.xslt"/>
<param name="base" file="cleaned"/>
<output dir="cleaned"/>
</styler>
The fix-base stylesheet might contain a template something like the following:
<xsl:template match="@href|@src">
<xsl:attribute name="{name()}">
<xsl:if test="starts-with(.,'/')">
<xsl:value-of select="$base"/> </xsl:if>
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
The final example parses a Rational Rose model file, extracts modelling data, cross-references it, and encodes it in a standard schema language, RDF. The extension used for the input file tells styler to use the petal parser instead of a standard XML parser. (The stylesheets used here can be found at http://xmodel.sourceforge.net.)
<styler>
<fileset dir=".">
<include name="*Mdl"/>
</fileset>
<transform file="PetalClasses.xslt"/>
<transform file="CrossRef.xslt"/>
<transform file="Petal2RDF.xslt"/>
<output dir=".">
<mapper type="glob" from="*Mdl" to="*.rdf"/>
</output>
</styler>
The styler jar file provides a command line interface that is sometimes useful. This has the following syntax:
Java -cp styler.jar au.com.Langdale.styler.Pipe \ input-file transform-file1 transform-file2 ... output-file
The input-file and output-file follow the conventions
given above. Each transform-file must end in .xslt. A single pipeline
is run parsing the input file and generating the output file.
The task does not take into account whether the outputs are up-to-date with respect to the input files and stylesheets. The output files are unconditionally regenerated.
The type of the input file is deduced from its name whenever no type attribute is supplied. This is convenient although may go against the usual ant philosophy.