Newer
Older
Handbook / make-includes / xslt_functions.make
################################################################################
#
# Make functions for handling XSLT tasks.
#
################################################################################


################################################################################
#
# XSLT must be defined as an environment variable. It's an error not to do so.
#
XSLT?=$(error The required environment variable XSLT has not been defined. Please set it to either "saxon", "xalan-c" or "xalan-j", to indicate to your preferred XSLT processor)


################################################################################
#
# Resolve a URI for some XML entity such as a stylesheet into a path to
# the actual item. This works by calling the example resolver application
# that comes with the entity resolver, then extracting the full path from
# the result. If the URI cannot be resolved, the function returns the
# original filename so that the XSLT processor can fail with a meaningful
# error message.
# 
# This is a workaround for the mysterious breakage that occured on Nigel's
# machine in early 2006 where all of the XSLT processors suddenly stopped
# accepting resolver class names as command line arguments.
# 
# This approach, while apparently kludgey, does have a particular
# advantage, however, in that it can be used with non-Java based XSLT
# processors such as Xalan C++, whereas the previous approach only worked
# with Saxon and Xalan-J. In other words, this will work with ANY XSLT
# processor.
#
# Note that the function adds the "file:///" protocol to the front of the
# argument, so callers should NOT add this themselves!
#
# For reference, the typical output from the resolver application looks
# something like this for a successful resolution:
#
# % java org.apache.xml.resolver.apps.resolver -u file:///foo.xsl uri
# Resolve URI (uri):
#         uri: file:///foo.xsl
# Result: file:/path/to/item/foo.xsl
#
# and like this for a failure:
#
# % java org.apache.xml.resolver.apps.resolver -u file:///missing.xsl uri
# Resolve URI (uri):
#         uri: file:///missing.xsl
# Result: null
#
# Update (2007-07-11): Added fix for mysterious breakage on Chris's machine
# that occurred for no apparent reason. An extra / was being put on the 
# front of the returned Windows path, e.g., /D:/blah/blah. Added a sed
# to remove it when necessary.
#
# Arguments:
#	$(1)	file name of item to be resolved, WITHOUT the "file:///"
#
resolve_uri = `$(RESOLVER) -u file:///$(1) uri | ( $(GREP) 'Result: file:' || $(ECHO) '::$(1)' ) | $(CUT) -d':' -f3- | $(SED) -e 's|/\([A-Z]:\)|\1|'`


################################################################################
#
# Pass stylesheet parameters as appropriate to the XSLT processor.
# $(XSLT) should be either defined as an environment variable, or
# passed in as a parameter to make.
#
# Note: we expect the value of the stylesheet parameter to be quoted,
# because it may potentially contain spaces. If it's not quoted before
# being passed to this function, Weird Things May Occur.
#
# Arguments:
#	$(1)	name of stylesheet parameter
#	$(2)	value for stylesheet parameter (quoted)
#
xslt_parameter = $(if $(findstring $(XSLT),'xalan-c'),-p $(1) $(2),$(if $(findstring $(XSLT),'xalan-j'),-param $(1) $(2),$(if $(findstring $(XSLT),'saxon'),$(1)=$(2),$(if $(findstring $(XSLT),'saxon-b'),$(1)=$(2)))))


################################################################################
#
# XSLT processor specific calling templates/functions, with parameters for
# subject-code, paper-number, standalone, showanswers and base-path.
# Unfortunately each of the major XSLT processors differs in the way it
# handles command-line arguments :(  Even worse, Xalan-C differs from
# Xalan-J!!
#
# Note that the path to the XSLT processor is stored in the make variable
# XSLTPROC, which is set by the local configuration script.
# 
# Examples:
# 
# 	Xalan -p department "'INFO'" -p paper "'111'" -p standalone "'no'" 
# 	      -p showanswers "'no'" -p base-path "'../Tutorials'" 
# 	      -p image-format "'eps'" $< xml2latex.xsl > $@
# 
# 	saxon $< xml2latex.xsl department='INFO' paper='111' standalone='no'
# 	      showanswers='no' base-path='../Tutorials' image-format='eps' > $@
#
# Arguments:
#	$(1)	source XML file
#	$(2)	XSL stylesheet
#	$(3)->	Parameter strings for various stylesheet parameters, as many
#			as required. Currently supported are "department", "paper",
#			"standalone", "showanswers", "base-path", "image-format").
#			The order of parameters doesn't matter.
#
# Parameter strings must be pre-formatted using the xslt_parameter function.
#

#
# Xalan-C
#
# xalanc = Xalan $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10) $(1) $(2)
#
xalanc = $(XSLTPROC) $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10) $(1) $(call resolve_uri,$(2))

#
# Xalan-J
#
# xalanj = xalan -in $(1) -xsl $(2) \
# 			-uriresolver org.apache.xml.resolver.tools.CatalogResolver \
# 			$(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10)
#
xalanj = $(XSLTPROC) -in $(1) -xsl $(call resolve_uri,$(2)) $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10)

#
# SAXON
#
# saxon = saxon -x org.apache.xml.resolver.tools.ResolvingXMLReader \
# 			-y org.apache.xml.resolver.tools.ResolvingXMLReader \
# 			-r org.apache.xml.resolver.tools.CatalogResolver \
# 			$(1) file:///$(2) $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10)
#
saxon = $(XSLTPROC) $(1) $(call resolve_uri,$(2)) $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10)

#
# SAXON-B
#
# saxon = saxon-b -s:$(1) -xsl:file:///$(2) $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10)
#
saxonb = $(XSLTPROC) -s:$(1) -xsl:$(call resolve_uri,$(2)) $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10)


################################################################################
#
# Generic function for runnning the local preferred XSLT processor.
# Redirecting the output to an appropriate output file is handled by the
# caller.
#
# Arguments:
#	$(1)	source XML file
#	$(2)	XSL stylesheet
#	$(3)->	Parameter strings for various stylesheet arguments, as many
#			as required. Currently supported are "subject-code",
#			"paper-number", "standalone", "showanswers", "base-path",
#			"image-format"). The order of parameters doesn't matter.
#
# Parameter strings must be pre-formatted using the xslt_parameter function.
#
xslt = $(if $(findstring $(XSLT),'xalan-c'),$(call xalanc,$(1),$(2),$(3),$(4),$(5),$(6),$(7),$(8),$(9),$(10)),$(if $(findstring $(XSLT),'xalan-j'),$(call xalanj,$(1),$(2),$(3),$(4),$(5),$(6),$(7),$(8),$(9),$(10)),$(if $(findstring $(XSLT),'saxon'),$(call saxon,$(1),$(2),$(3),$(4),$(5),$(6),$(7),$(8),$(9),$(10)),$(if $(findstring $(XSLT),'saxon-b'),$(call saxonb,$(1),$(2),$(3),$(4),$(5),$(6),$(7),$(8),$(9),$(10))))))


# xslt_debug:
# 	@announce "XSLT invocation (change value of XSLT to test others)"
# 	@echo "$(call xslt,input.xml,stylesheet.xsl,$(call xslt_parameter,department,'$(SUBJECT_CODE)'),$(call xslt_parameter,paper,'$(PAPER_NUMBER)'),$(call xslt_parameter,standalone,'no'),$(call xslt_parameter,showanswers,'no'),$(call xslt_parameter,base-path,'$(SECTION)'),$(call xslt_parameter,image-format,'pdf')) > output_file"