This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: [java] transforming a file to itself - buffered?
- From: "Michael Kay" <michael dot h dot kay at ntlworld dot com>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Tue, 4 Jun 2002 23:35:36 +0100
- Subject: RE: [xsl] [java] transforming a file to itself - buffered?
- Reply-to: xsl-list at lists dot mulberrytech dot com
> What is the best way to handle transforming a file to itself?
>
> Currently I do:
> File source = new File(servlet_context.getRealPath(_xml));
> transformer.transform(new StreamSource(source), new
> StreamResult(source));
I wouldn't recommend using the same file as input and output. Transform
to a byte array as the output, then write the byte array to the file if
the transformation succeeds.
>
> Is there any way to do buffered reads and writes to this
> file? Is there a benefit to doing this?
Saxon will of course try to do the reads and writes efficiently, but if
you think you can do better (e.g. by taking advantage of the JDK 1.4 nio
package) you could try implementing your own InputStream or OutputStream
classes.
>
> Is there a way to use the new 1.4 non blocking I/O
> (java.nio)? <random_thought> The code below should(?) work on
> the entire file, but
> could you possibly identify a section (nodeset) of an XML file and
> perform a transformation on it? Is this just silly?</random_thought>
If you only want to transform one subtree of an XML file, the best
approach is to write a SAX pipeline. Pass the events relating to the
non-transformed part of the file straight from your input handler to
your output handler; for the transformed part, treat the subtree as a
document thaht is passed to the transformer (treating it as a SAX filter
using the TransformerHandler interface) and passing its output events to
your output handler.
>
> I tried to do the following but I get an error in saxon 6.52 saying:
> (The requested operation cannot be performed on a file with a
> user-mapped section open)
Then you are misusing parts of the Java IO library in a way that I don't
pretend to understand.
Michael Kay
Software AG
home: Michael.H.Kay@ntlworld.com
work: Michael.Kay@softwareag.com
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list