de.infinityloop.util.xslt
Class ExtensionFunctions

java.lang.Object
  extended by de.infinityloop.util.xslt.ExtensionFunctions

public class ExtensionFunctions
extends java.lang.Object

This class encapsulates XSLT extension function implementations for some often-needed handy utilities.

Except when noted otherwise for a function, the general usage pattern is as follows:

  1. Create a namespace prefix mapping for the namespace in the <xsl:stylesheet> element, e.g.:

    xmlns:ilext="java:de.infinityloop.util.xslt.ExtensionFunctions" (for Saxon) or xmlns:ilext="xalan://de.infinityloop.util.xslt.ExtensionFunctions" (for Xalan)

  2. In your XSLT, call the static member function:

    ilext:functionName( ...parameters if any... )

  3. Add the prefix to the list of excluded prefixes in the result in the <xsl:stylesheet> element:

    exclude-result-prefixes="ilext ...other prefixes..."

Example:

<xsl:value-of select="ilext:URLToLocal(@href)" />

The above converts the value of the href attribute, which is supposed to be a URI reference, to local file naming convention.

Since:
upCast 5.4 / downCast 1.7
Author:
Christian Roth

Constructor Summary
ExtensionFunctions()
           
 
Method Summary
static java.lang.String addLengths(java.lang.String length1, java.lang.String length2, java.lang.String resultDimension)
          Adds two absolute length values, even if using different dimensions.
static java.lang.String localToURL(java.lang.String local)
          Returns the URL representation of the passed local filename.
static java.lang.String makeValidXML_ID(java.lang.String src)
          Transform a passed string in such a way that the result conforms to the rules for an XML 1.0 'Name' (as used e.g. for element names or xml:id).
static java.lang.String makeValidXMLName(java.lang.String src)
          Transform a passed string in such a way that the result conforms to the rules for an XML 1.0 'Name' (as used e.g. for element names or xml:id).
static java.lang.String multiplyLength(java.lang.String length, double factor, java.lang.String resultDimension)
          Multiply an absolute length by some factor.
static java.lang.String subtractLengths(java.lang.String length1, java.lang.String length2, java.lang.String resultDimension)
          Subtracts two absolute length values, even if using different dimensions.
static java.lang.String URLToLocal(java.lang.String url)
          Returns the local file name representation of the passed URL, if it's a file: URL; returns the normalized URL otherwise.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExtensionFunctions

public ExtensionFunctions()
Method Detail

localToURL

public static java.lang.String localToURL(java.lang.String local)
Returns the URL representation of the passed local filename. If you pass in a string that is already in URL fromat, it is returned normalized.

Parameters:
local - the file name in local format
Returns:
the file name in URL format

URLToLocal

public static java.lang.String URLToLocal(java.lang.String url)
Returns the local file name representation of the passed URL, if it's a file: URL; returns the normalized URL otherwise.

Parameters:
url - the file name in URL format
Returns:
the file name in local naming convention for the platform it originates seemingly from

addLengths

public static java.lang.String addLengths(java.lang.String length1,
                                          java.lang.String length2,
                                          java.lang.String resultDimension)
Adds two absolute length values, even if using different dimensions. The result dimension must also be specified.

Parameters:
length1 - the first length, e.g. "2mm"
length2 - the second length to add, e.g. "3.67in"
resultDimension - the dimension the result should be returned in, e.g. "cm"
Returns:
the resulting length as String

subtractLengths

public static java.lang.String subtractLengths(java.lang.String length1,
                                               java.lang.String length2,
                                               java.lang.String resultDimension)
Subtracts two absolute length values, even if using different dimensions. The result dimension must also be specified.

Parameters:
length1 - the first length, e.g. "2mm"
length2 - the length to subtract from length1, e.g. "3.67in"
resultDimension - the dimension the result should be returned in, e.g. "cm"
Returns:
the resulting length as String

multiplyLength

public static java.lang.String multiplyLength(java.lang.String length,
                                              double factor,
                                              java.lang.String resultDimension)
Multiply an absolute length by some factor. The result dimension must also be specified.

Parameters:
length - the first length, e.g. "2mm"
factor - the factor to multiply the length with, e.g. "2.54"
resultDimension - the dimension the result should be returned in, e.g. "cm"
Returns:
the resulting length as String

makeValidXMLName

public static java.lang.String makeValidXMLName(java.lang.String src)
Transform a passed string in such a way that the result conforms to the rules for an XML 1.0 'Name' (as used e.g. for element names or xml:id). Invalid characters are replaced by '-' if within the name, and prepended by an additional '_' when at the beginning. Also note that a starting colon ':' will not be allowed by this function, as it has special meaning in XML Namespaces. Therefore, consider the passed string as only the local name of an element.

Important Note: This method does not guarantee that for different source strings the output will be different as well! For example, both 1elem and _1elem will be converted to _1elem: The first will be modified by prepending a '_', while the second is already a valid XML Name and therefore passed unchanged.

Parameters:
src - the source string
Returns:
a string that matches the XML 1.0 Name production.

makeValidXML_ID

public static java.lang.String makeValidXML_ID(java.lang.String src)
Transform a passed string in such a way that the result conforms to the rules for an XML 1.0 'Name' (as used e.g. for element names or xml:id). Invalid characters are replaced by '-' if within the name, and prepended by an additional '_' when at the beginning. This is essentially the same method as makeValidXMLName(), but additionally allows the colon character ':' .

Important Note: This method does not guarantee that for different source strings the output will be different as well! For example, both 1elem and _1elem will be converted to _1elem: The first will be modified by prepending a '_', while the second is already a valid XML Name and therefore passed unchanged.

Parameters:
src - the source string
Returns:
a string that matches the XML 1.0 Name production.