Re: [xsl] Copy a binary file using Java extension

Subject: Re: [xsl] Copy a binary file using Java extension
From: Stefan Krause <stf@xxxxxxxx>
Date: Sun, 7 Nov 2010 13:58:38 +0100
Starting with the given java code below, I wrote a quick solution which works
fine with Saxon 9.2.1.2 (Oxygen build).

public void copy(File source, File target) throws IOException {
	FileChannel fcin = (new FileInputStream(source)).getChannel();
	FileChannel fcout = (new FileOutputStream(target)).getChannel();
	fcin.transferTo(0, source.length(), fcout);
	fcin.close();
	fcout.close();
}


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
	xmlns:xs="http://www.w3.org/2001/XMLSchema";
	xmlns:test="something"

	xmlns:java-uri="java:java.net.URI"
	xmlns:java-file="java:java.io.File"
	xmlns:java-fis="java:java.io.FileInputStream"
	xmlns:java-fos="java:java.io.FileOutputStream"
	xmlns:java-fc="java.nio.channels.FileChannel"

	version="2.0">

	<xsl:function name="test:copy-file">
		<xsl:param name="sourcefileURI" as="xs:string"/>
		<xsl:param name="targetFileURI" as="xs:string"/>
		<xsl:variable name="FileChannelIn"
select="java-fis:getChannel(java-fis:new(java-file:new(java-uri:new($sourcefi
leURI) ) ) )"/>
		<xsl:variable name="FileChannelOut"
select="java-fos:getChannel(java-fos:new(java-file:new(java-uri:new($targetFi
leURI) ) ) )"/>
		<xsl:variable name="size"
select="java-file:length(java-file:new(java-uri:new($sourcefileURI) ) )"/>
		<xsl:value-of select="java-fc:transferTo($FileChannelIn, 0, $size,
$FileChannelOut), java-fc:close($FileChannelIn),
java-fc:close($FileChannelOut)"/>
	</xsl:function>
</xsl:stylesheet>

I use java.net.URI only to check the arguments. You can build java.io.File
similarly from a string.

Because Saxon didn't recognize the type "java.io.File" of a variable during
java-fis:new(), the size is calculated with a second instance of File.

This stylesheet is not deeply tested, but maybe you got a first impression.

Stefan


Am 06.11.2010 um 17:12 schrieb Geurt Lagemaat:

> Hi,
>
> I'm trying to copy a binary file using XSLT (2) (Saxon8) with java
> extensions.
>
> I know I can use xmlns:fos="java.io.FileOutputStream",
> xmlns:file="java.io.File" to write functions like
>
> <xsl:variable name="testfile" select="file:new(resolve-uri($filename))"/>
> <xsl:choose>
> <xsl:when test="not(file:exists($testfile))">
> <xsl:value-of select="false()"/>
> </xsl:when>
> <xsl:otherwise>
> <xsl:value-of select="true()"/>
> </xsl:otherwise>
> </xsl:choose>
>
> and
>
> <xsl:variable name="fos" select="fos:new($ImgFilePath)"/>
> <!-- de base64 content as string -->
> <xsl:variable name="b64" select="b64:new(string(image/binair))"/>
> <!-- write to filestream while converting base64 to binary -->
> <xsl:value-of select="fos:write($fos, b64:getBinaryValue($b64))"/>
> <xsl:value-of select="fos:close($fos)"/>
>
> But I cant rework these fragments to a functional copyFile. Problem is
> there is no simple binary fileCopy available without opening streams
> etc. So the most obvious way is to work with the fos, but how to read a
> binary file in a way that fos can write it?
>
> Another idea was to use the java.io.File  renameTo , but I cant
> translate the Java code is a XSLT way to do it.
>
> Anyone has a idea? I don't want to use a Java class because the target
> environment is a sort of restricted area with no access to classpaths
> etc (I'm suspecting trouble with the classpath if I want to use a Java
> class to get this functionality).
>
> Regards,
>
> Geurt Lagemaat

Current Thread