mercredi, janvier 03, 2007

Shell script to launch Saxon

Here is the little script I use to start Saxon from the command line. With the help of two or three environment variables, it can be used as your day-to-day XSLT transformer. But having options to deal with the class path, it can also be used in Makefiles (or whatever) in projects that use extensions or even modified version of Saxon.

I think the doc a the beginning of the script is clear enough. Unfortunately, I can't translate it to a Windows BATCH script as I don't know it. The script is below and can be downloaded here.


#! /bin/sh

## saxon [--b|--sa]? [--catalogs=...]* [--catalog-verbose[=...]]* \
## [--add-cp=...]* [--cp=...]* <original Saxon args>
##
## Order of arguments is not significant, but the arguments to be
## forwarded to Saxon must be at the end. See below for an
## explanation of the arguments.
##
## Depends on the following environment variables:
##
## - APACHE_XML_RESOLVER_JAR (if catalogs are used)
## - SAXON_SCRIPT_DIR (must contain saxon8.jar or saxon8sa.jar, and
## the licence file and saxon8-sql.jar if used)
## - SAXON_SCRIPT_HOME (if different from $HOME, for tilde "~"
## substitution)

JAVA=java

# Use saxon8.jar if the default has to be the B version.
SAXON_JAR="${SAXON_SCRIPT_DIR}/saxon8sa.jar"
SAXON_SQL="${SAXON_SCRIPT_DIR}/saxon8-sql.jar"

# Use net.sf.saxon.Transform if the default has to be the B version.
SAXON_CLASS=com.saxonica.Transform
CATALOG_VERB=1
USE_SQL=false
if [[ -z "$SAXON_SCRIPT_HOME" ]]; then
MY_HOME=$HOME
else
MY_HOME=$SAXON_SCRIPT_HOME
fi
CP_DELIM=";"

while echo "$1" | grep -- ^-- >/dev/null 2>&1; do
case "$1" in
# XSLT Basic version.
--b)
SAXON_CLASS=net.sf.saxon.Transform;;
# XSLT Schema-Aware version.
--sa)
SAXON_CLASS=com.saxonica.Transform;;
# Add XML Catalogs URI resolution, by adding a catalog to the
# catalog list. Resolve "~" only on the head of the option.
# May be repeated.
--catalogs=*)
# Add separator.
if [[ -n $CATALOGS ]]; then
CATALOGS="$CATALOGS$CP_DELIM"
fi
# Resolve "~".
TMP_CAT=`echo $1 | sed s/^--catalogs=//`
if echo "$TMP_CAT" | grep -- '^~' >/dev/null 2>&1; then
TMP_CAT="$MY_HOME"`echo $TMP_CAT | sed s/^~//`;
fi
CATALOGS="$CATALOGS$TMP_CAT";;
# Set the XML Catalogs resolver verbosity.
--catalog-verbose=*)
CATALOG_VERB=`echo $1 | sed s/^--catalog-verbose=//`;;
# Set the XML Catalogs resolver verbosity to 3.
--catalog-verbose)
CATALOG_VERB=3;;
# Add some path to the class path. Resolve "~" only on the
# head of the option. May be repeated.
--add-cp=*)
# Resolve "~".
TMP_CP=`echo $1 | sed s/^--cp=//`
if echo "$TMP_CP" | grep -- '^~' >/dev/null 2>&1; then
TMP_CP="$MY_HOME"`echo $TMP_CP | sed s/^~//`;
fi
ADD_CP="$ADD_CP$CP_DELIM$TMP_CP";;
# Set the class path. Resolve "~" only on the head of the
# option. May be repeated.
--cp=*)
# Resolve "~".
TMP_CP=`echo $1 | sed s/^--cp=//`
if echo "$TMP_CP" | grep -- '^~' >/dev/null 2>&1; then
TMP_CP="$MY_HOME"`echo $TMP_CP | sed s/^~//`;
fi
CP="$CP$CP_DELIM$TMP_CP";;
# Add the Saxon SQL jar to the class path.
--sql)
USE_SQL=true
esac
shift;
done

if [[ -z "$CP" ]]; then
CP="$SAXON_JAR"
fi

if [[ "$SAXON_CLASS" = com.saxonica.Transform ]]; then
CP="$CP$CP_DELIM$SAXON_SCRIPT_DIR"
fi

if [[ "$USE_SQL" ]]; then
CP="$CP$CP_DELIM$SAXON_SQL"
fi

if [[ -z "$CATALOGS" ]]; then
"$JAVA" \
-cp "$CP$ADD_CP" \
$SAXON_CLASS \
"$@"
else
"$JAVA" \
-cp "$CP$CP_DELIM$APACHE_XML_RESOLVER_JAR$ADD_CP" \
-Dxml.catalog.files="$CATALOGS" \
-Dxml.catalog.verbosity=$CATALOG_VERB \
$SAXON_CLASS \
-r org.apache.xml.resolver.tools.CatalogResolver \
-x org.apache.xml.resolver.tools.ResolvingXMLReader \
-y org.apache.xml.resolver.tools.ResolvingXMLReader \
"$@"
fi