public class ExtensionFunctions
extends java.lang.Object
Except when noted otherwise for a function, the general usage pattern is as follows:
<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)
ilext:functionName( ...parameters if any... )
<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.
Constructor and Description |
---|
ExtensionFunctions() |
Modifier and Type | Method and Description |
---|---|
static double |
absolutizeLength(java.lang.String length,
java.lang.String fontsize,
java.lang.String referenceValue,
java.lang.String resultDimension)
Converts and absolutizes the passed length specification to the given dimension.
|
static double |
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 |
calcCALSTablegrid(java.lang.String xml) |
static java.lang.String |
calcTableColgroup(java.lang.String tableXMLCode)
Returns a serialized colspec element mirroring the column widths of the passed table code.
|
static double |
convertLength(java.lang.String length,
java.lang.String resultDimension)
Converts the passed absolute length specification to the given dimension.
|
static java.lang.String |
emptyString(boolean whatever)
Returns an empty string
|
static java.lang.String |
emptyString(java.lang.String whatever)
Returns an empty string
|
static java.lang.String |
FileCalcAbsolutePath(java.lang.String basePath,
java.lang.String relativePath)
Deprecated.
use XPath 2 function
resolve-uri() instead |
static java.lang.String |
FileCalcRelativePath(java.lang.String basePath,
java.lang.String destPath)
Deprecated.
use
relativizeUri(String, String) instead |
static boolean |
fileExists(java.lang.String abspath)
Tests if a file or folder exists already in the file system.
|
static boolean |
FileExists(java.lang.String name)
Deprecated.
use
fileExists(String) instead |
static java.lang.String |
FileGetBasename(java.lang.String name)
Deprecated.
use
getPathComponent(String, String) instead |
static java.lang.String |
FileGetName(java.lang.String name)
Deprecated.
use
getPathComponent(String, String) instead |
static java.lang.String |
FileGetPathOnly(java.lang.String name)
Deprecated.
use
getPathComponent(String, String) instead |
static java.lang.String |
filterCSSExcluding(java.lang.String src,
java.util.List filter)
Filter the passed string of CSS property definitions so that it contains all properties not matching one
of the elements in the Sequence of filter string.
|
static java.lang.String |
filterCSSIncluding(java.lang.String src,
java.util.List filter)
Filter the passed string of CSS property definitions so that it contains all properties matching one
of the elements in the Sequence of filter string.
|
static java.lang.String |
generateUniqueResultURL(java.lang.String actualURL)
This function mangles the passed URL so that it appears as a unique URL to XSLT, but at the same time makes sure
that the destination that is written to is the same.
|
static java.lang.String |
generateUUID()
Generate a UUID using Java's
UUID class by evaluating UUID.randomUUID().toString().intern() |
static java.lang.String |
getPathComponent(java.lang.String path,
java.lang.String component)
Retrieves the specified component of the passed file path.
|
static java.lang.String |
getRealmValue(java.lang.String pipelineInstanceId,
java.lang.String realm,
java.lang.String name,
java.lang.String dflt)
This method retrieves the value of a variable in a specified realm.
|
static int |
getUnicodeFromFontchar(java.lang.String pipelineInstanceId,
java.lang.String fontFamily,
int index)
This method tries to retrieve the Unicode codepoint equivalent of character
index in font fontFamily . |
static int |
getUnicodeFromFontchar(java.lang.String pipelineUID,
java.lang.String fontFamily,
java.lang.String string)
This method tries to retrieve the Unicode codepoint equivalent of character
index in font fontFamily . |
static java.lang.String |
getUpcastNodeId(org.w3c.dom.Node contextNode)
This method retrieves an attribute on the passed element, where the attribute can also be an Upcast-synthesized attribute (like css[oc]:*, uci:node-id).
|
static java.lang.String |
getVersion()
retrieves version information
|
static java.lang.String |
localToURL(java.lang.String local)
Deprecated.
use
getPathComponent(String, String) instead |
static java.lang.String |
logCustom(java.lang.String pipelineInstanceID,
java.lang.String level,
int messagecode,
java.lang.String message)
This function lets you generate your own, attributed log message within the currently executing module (or pipeline, of no current module exists).
|
static void |
main(java.lang.String[] args)
Extension function test method
|
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 double |
multiplyLength(java.lang.String length,
double factor,
java.lang.String resultDimension)
Multiply an absolute length by some factor.
|
static java.lang.String |
parseCSSDeclaration(java.lang.String css)
Parses the passed CSS style declaration (i.e. a property/value list) into its individual properties/values and returns it as an XML fragment.
|
static org.w3c.dom.DocumentFragment |
parseCSSDeclarationToDocFrag(java.lang.String css)
Parses the passed CSS style declaration (i.e. a property/value list) into its individual properties/values and returns it as an XML fragment.
|
static java.lang.String |
parseCSSStylesheet(java.lang.String css)
Parses the passed CSS stylesheet into its individual rules and sub-components and returns it as an XML document serialized to a string.
|
static java.lang.String |
parseDate(java.lang.String date,
java.lang.String format)
parses the (beginning of the) string into a xs:dateTime compliant lexical representation.
|
static java.lang.String |
parseDate(java.lang.String date,
java.lang.String format,
java.lang.String languageTag)
parses the (beginning of the) string into a xs:dateTime compliant lexical representation.
|
static int |
parseInteger(java.lang.String numberstring,
int radix)
This method parses a string into an integer using the specified numbering base.
|
static org.w3c.dom.DocumentFragment |
parsePersonname(java.lang.String s)
SAXON EXTENSION function: parse a string into a personname document fragment.
|
static org.w3c.dom.DocumentFragment |
parseTagSoup(java.lang.String src)
This function uses the tagsoup parser ( http://home.ccil.org/~cowan/tagsoup/ ) to tidy up incoming HTML code and returns a well-formed XML document or fragment.
|
static org.w3c.dom.DocumentFragment |
parseTagSoup(java.lang.String pid,
java.lang.String src)
This function uses the tagsoup parser ( http://home.ccil.org/~cowan/tagsoup/ ) to tidy up incoming HTML code and returns a well-formed XML document or fragment.
|
static java.lang.Double |
random()
Generate a random numnber between 0 and 1.
|
static java.lang.String |
relativizeUri(java.lang.String absUri,
java.lang.String baseUri)
Returns the relative path from the base path to the destination path in URL format.
|
static void |
setRealmValue(java.lang.String pipelineInstanceId,
java.lang.String realm,
java.lang.String name,
java.lang.String value)
This method sets a value in the specified realm.
|
static double |
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 |
toHexString(long value)
This method converts the passed long value to its corresponding representation as a string of hexadecimal
digits.
|
static java.lang.String |
toHexString(long value,
int length)
This method converts the passed long value to its corresponding representation as a string of hexadecimal
digits.
|
static java.lang.String |
url2xml(java.lang.String url)
This method parses a passed URL into its constituents and returns a string of XML source code that encapsulates
the various parts into appropriate elements.
|
static java.lang.String |
URLToLocal(java.lang.String url)
Deprecated.
use
getPathComponent(String, String) instead |
static boolean |
writeLineToFile(java.lang.String url,
java.lang.String line,
boolean append)
Writes the passed String followed by the platform-specific new line character to the specified file, optionally appending the data (instead of replacing any existing data).
|
static boolean |
writeLineToFile(java.lang.String url,
java.lang.String line,
boolean append,
java.lang.String encoding)
Writes the passed String followed by the platform-specific new line character to the specified file, optionally appending the data (instead of replacing any existing data).
|
static boolean |
writeStringToFile(java.lang.String url,
java.lang.String data,
boolean append)
Writes the passed String to the specified file, optionally appending the data (instead of replacing any existing data).
|
static boolean |
writeStringToFile(java.lang.String url,
java.lang.String data,
boolean append,
java.lang.String encoding)
Writes the passed String to the specified file, optionally appending the data (instead of replacing any existing data).
|
public static java.lang.String emptyString(java.lang.String whatever)
whatever
- an arbitrary stringpublic static java.lang.String emptyString(boolean whatever)
whatever
- an arbitrary boolean value@Deprecated public static java.lang.String localToURL(java.lang.String local)
getPathComponent(String, String)
insteadlocal
- the file name in local format@Deprecated public static java.lang.String URLToLocal(java.lang.String url)
getPathComponent(String, String)
insteadurl
- the file name in URL format@Deprecated public static boolean FileExists(java.lang.String name)
fileExists(String)
insteadname
- the file to test for in URL formatpublic static boolean fileExists(java.lang.String abspath)
abspath
- the absolute path to the file or folder to test for@Deprecated public static java.lang.String FileGetBasename(java.lang.String name)
getPathComponent(String, String)
insteadname
- the absolute file name in either URL or local notationpublic static java.lang.String getPathComponent(java.lang.String path, java.lang.String component)
path
- a string to be interpreted as file pathcomponent
- one of: LOCAL | URL | LOCALPATH | URLPATH | LOCALNAME | URLNAME | LOCALEXTENSION | URLEXTENSION | LOCALBASENAME | URLBASENAME | LOCALBASENAMEPATH | URLBASENAMEPATH@Deprecated public static java.lang.String FileGetName(java.lang.String name)
getPathComponent(String, String)
insteadname
- the absolute file name in either URL or local notation@Deprecated public static java.lang.String FileGetPathOnly(java.lang.String name)
getPathComponent(String, String)
insteadname
- the absolute file name in either URL or local notation@Deprecated public static java.lang.String FileCalcAbsolutePath(java.lang.String basePath, java.lang.String relativePath)
resolve-uri()
insteadFileCalcAbsolutePath( "/usr/test/dir/", "../" )
returns "/usr/test/"
(incl.
trailing folder separator).basePath
- the basepath where relativePath should be interpreted relative torelativePath
- the relative path to make absolute with respect to the base path. If it is already absolute, it
is returned unaltered.@Deprecated public static java.lang.String FileCalcRelativePath(java.lang.String basePath, java.lang.String destPath)
relativizeUri(String, String)
insteadbasePath
- the path where we start navigationdestPath
- the path where we want to bepublic static java.lang.String relativizeUri(java.lang.String absUri, java.lang.String baseUri)
baseUri
- the base path to make the absUri relative toabsUri
- the path where we want to bepublic static java.lang.String logCustom(java.lang.String pipelineInstanceID, java.lang.String level, int messagecode, java.lang.String message)
pipelineInstanceID
- the pipeline's instance id. The pipeline's instance id can be retrieved using ${pipeline:PipelineInstanceId}
, which you then should pass to the XSLT processor as parameter of type xs:string.level
- the message level, one of: TRACE | DETAIL | VERBOSE | DEBUG | INFO | WARN | ERROR | FATALmessagecode
- the custom message code (should be greater than or equal to 0)message
- the message to issuepublic static double addLengths(java.lang.String length1, java.lang.String length2, java.lang.String resultDimension)
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"public static double convertLength(java.lang.String length, java.lang.String resultDimension)
length
- the first length, e.g. "2mm"resultDimension
- the dimension the result should be returned in, e.g. "in"public static double absolutizeLength(java.lang.String length, java.lang.String fontsize, java.lang.String referenceValue, java.lang.String resultDimension)
length
- the specified length, e.g. "2mm" or "100%" or "1.2em". When no dimension is specified, "px" is assumed.fontsize
- the current fontsizereferenceValue
- the reference value for percent specifications. When no dimension is specified, "px" is assumed.resultDimension
- the dimension the result should be returned in, e.g. "in". When the specified dimension is unknown or unsupprted, "mm" is used instead.public static double subtractLengths(java.lang.String length1, java.lang.String length2, java.lang.String resultDimension)
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"public static double multiplyLength(java.lang.String length, double factor, java.lang.String resultDimension)
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"public static java.lang.String makeValidXMLName(java.lang.String src)
:
'
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.
src
- the source stringName
production.public static java.lang.String makeValidXML_ID(java.lang.String src)
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.
src
- the source stringName
production.public static java.lang.String filterCSSExcluding(java.lang.String src, java.util.List filter)
Filter the passed string of CSS property definitions so that it contains all properties not matching one of the elements in the Sequence of filter string. For the string sequence of filter, you either pass the (unquoted) full property name, or you can use the '*' qualifier to be used after some prefix. E.g. to filter out all Mozilla-proprietary CSS properties, use a filter sequence of ('-moz-*').
Example:
ilext:filterCSSExcluding( @style, ( 'width', '-moz-*') )
removes the width
property and all
properties starting with -moz-
from the property list string in the style
attribute of the context element.
src
- the source string, consisting of property definitionsfilter
- a sequence of filter stringspublic static java.lang.String filterCSSIncluding(java.lang.String src, java.util.List filter)
Filter the passed string of CSS property definitions so that it contains all properties matching one of the elements in the Sequence of filter string. For the string sequence of filter, you either pass the (unquoted) full property name, or you can use the '*' qualifier to be used after some prefix. E.g. to only retain all Mozilla-proprietary CSS properties, use a filter sequence of ('-moz-*').
Example:
ilext:filterCSSIncluding( @style, ( 'width', '-moz-*') )
removes all properties except for the
width
property and all properties starting with -moz-
from the property list string
in the style
attribute of the context element.
src
- the source string, consisting of property definitionsfilter
- a sequence of filter stringspublic static java.lang.String toHexString(long value)
value
- the value to encodepublic static java.lang.String toHexString(long value, int length)
value
- the value to encodelength
- the expected length in characters of the resulting stringpublic static int parseInteger(java.lang.String numberstring, int radix)
numberstring
- the integer number to convert as string. Leading zeroes are allowed.radix
- the numbering system base to use. This is 10 for decimal system, 16 for hex numbers, 8 for octal numbers, and 2 for binary numbers (consisting of 0 and 1 digits).java.lang.NumberFormatException
- when the string does not represent a parseable number in the specified numbering systempublic static org.w3c.dom.DocumentFragment parsePersonname(java.lang.String s)
public static java.lang.String parseDate(java.lang.String date, java.lang.String format)
parseDate( 'December 25, 2015' ) into "2015-12-25T". The parsing is done using the system's current Locale setting.
When the datestring cannot be converted to a date, the empty string is returned.
date
- a string with date information according to the passed patternformat
- the format string the date is in, per Java's java.text.SimpleDateFormat classpublic static java.lang.String parseDate(java.lang.String date, java.lang.String format, java.lang.String languageTag)
parseDate( 'December 25, 2015' ) into "2015-12-25T".
When the datestring cannot be converted to a date, the empty string is returned.
date
- a string with date information according to the passed patternformat
- the format string the date is in, per Java's java.text.SimpleDateFormat classlanguageTag
- the IETF BCP 47 language tag to use for date parsingpublic static java.lang.String getVersion()
public static boolean writeStringToFile(java.lang.String url, java.lang.String data, boolean append)
url
- the file to write todata
- the data to writeappend
- when true, data is appended to existing filepublic static boolean writeStringToFile(java.lang.String url, java.lang.String data, boolean append, java.lang.String encoding)
url
- the file to write todata
- the data to writeappend
- when true, data is appended to existing fileencoding
- the Java encoding name to use for writing; UTF-8 when the empty stringpublic static boolean writeLineToFile(java.lang.String url, java.lang.String line, boolean append)
url
- the file to write toline
- the line of text to writeappend
- when true, data is appended to existing filepublic static boolean writeLineToFile(java.lang.String url, java.lang.String line, boolean append, java.lang.String encoding)
url
- the file to write toline
- the line of text to writeappend
- when true, data is appended to existing fileencoding
- the Java encoding name to use for writing; UTF-8 when the empty stringpublic static java.lang.String parseCSSDeclaration(java.lang.String css)
css
- the CSS code as stringpublic static org.w3c.dom.DocumentFragment parseCSSDeclarationToDocFrag(java.lang.String css) throws java.lang.Exception
css
- the CSS code as stringjava.lang.Exception
public static java.lang.String parseCSSStylesheet(java.lang.String css)
css
- the CSS code as stringpublic static java.lang.String url2xml(java.lang.String url)
This method parses a passed URL into its constituents and returns a string of XML source code that encapsulates the various parts into appropriate elements. You can use e.g. saxon:parse() to make an XML document from the returned string and extract parts of the tree using standard XSLT code.
Example:
http://login:passwd@infinity-loop.de:1234/dir1/dir2/file.html?par1&par2=v2&par3=v3#fragment-id
will be translated into a string like
<url source="http://login:passwd@infinity-loop.de:1234/dir1/dir2/file.html?par1&par2=v2&par3=v3#fragment-id"> <protocol>http</protocol> <default-port>80</default-port> <authority> <literal>login:passwd@infinity-loop.de:1234</literal> <user-info> <literal>login:passwd</literal> <login>login</login> <password>passwd</password> </user-info> <host>infinity-loop.de</host> <port>1234</port> </authority> <path> <literal>/dir1/dir2/file.html</literal> <elem index="1">dir1</elem> <elem index="2">dir2</elem> <elem index="3">file.html</elem> </path> <query> <literal>par1&par2=v2&par3=v3</literal> <param index="1" name="par1" value="" /> <param index="2" name="par2" value="v2" /> <param index="3" name="par3" value="v3" /> </query> <fragment>fragment-id</fragment> </url>
url
- the URL to parsepublic static java.lang.String calcCALSTablegrid(java.lang.String xml)
public static java.lang.String calcTableColgroup(java.lang.String tableXMLCode)
tableXMLCode
- the XML code for the table to calculate the column widths for. We recognize table, tr, th and td.public static java.lang.String getRealmValue(java.lang.String pipelineInstanceId, java.lang.String realm, java.lang.String name, java.lang.String dflt)
${pipeline:PipelineInstanceId}
.pipelineInstanceId
- the pipeline's instance idrealm
- the variable realm to retrieve a value from, e.g. application, pipeline
or javaproperty
name
- the name of the variabledflt
- the default value to be returned when either the pipeline object can not be retrieved or the specified variable does not existpublic static java.lang.String getUpcastNodeId(org.w3c.dom.Node contextNode)
qname
- the qualified name of the attribute to retrieve (namespace resolution context as for prefixes is the XSLT stylesheet)dflt
- the default value to be returned when the attribute does not exist or a value cannot be determinedpublic static void setRealmValue(java.lang.String pipelineInstanceId, java.lang.String realm, java.lang.String name, java.lang.String value)
${pipeline:PipelineInstanceId}
.pipelineInstanceId
- the pipeline's instance idrealm
- the variable realm to set the value in. Currently, this can only be the single realm value pipeline
name
- the name of the variablevalue
- the value to be setjava.lang.Exception
- when the value could not be setpublic static int getUnicodeFromFontchar(java.lang.String pipelineInstanceId, java.lang.String fontFamily, int index)
index
in font fontFamily
. You must supply the pipeline's instance id to this
function, which you should pass as a parameter to your stylesheet. The pipeline's instance id can be retrieved using ${pipeline:PipelineInstanceId}
. The method consults
the pipeline's font configuration setting and uses the resolution fallbacks described in the upCast manual.
When there is no Unicode codepoint equivalent for the font+character index passed in, the codepoint for the Unicode replacement character U+FFFD (65533) is returned.pipelineInstanceId
- the pipeline's instance idfontFamily
- the font family name the character index
is inindex
- the character index in the specified font in the range from 0..255 for single-byte characters. Values greater than 255 are interpreted as meaning a double-byte
character sequence where bits 15..8 are the first byte and bits 7..0 are the second byte of the DBCS.public static int getUnicodeFromFontchar(java.lang.String pipelineUID, java.lang.String fontFamily, java.lang.String string)
index
in font fontFamily
. You must supply the pipeline's instance id to this
function, which you should pass as a parameter to your stylesheet. The pipeline's instance id can be retrieved using ${pipeline:PipelineInstanceId}
. The method consults
the pipeline's font configuration setting and uses the resolution fallbacks described in the upCast manual.
When there is no Unicode codepoint equivalent for the font+character index passed in, the codepoint for the Unicode replacement character U+FFFD (65533) is returned.pipelineInstanceId
- the pipeline's instance idfontFamily
- the font family name the character index
is instring
- the character index as a hex string, e.g. "A0" or "B5E4" (for a DBCS)public static java.lang.String generateUUID()
UUID
class by evaluating UUID.randomUUID().toString().intern()
public static java.lang.Double random()
public static java.lang.String generateUniqueResultURL(java.lang.String actualURL)
public static org.w3c.dom.DocumentFragment parseTagSoup(java.lang.String src) throws java.lang.Exception
src
- the source HTML code to tidyjava.lang.Exception
public static org.w3c.dom.DocumentFragment parseTagSoup(java.lang.String pid, java.lang.String src) throws java.lang.Exception
pid
- Pipeline Instance ID to have logging output (and maybe other context info) fetched from the respective pipelinesrc
- the source HTML code to tidyjava.lang.Exception
public static void main(java.lang.String[] args)
args
-