Compile stylesheet to file and read?
Added by Anonymous over 15 years ago
Legacy ID: #7483253 Legacy Poster: Lea Hayes (numberkruncher)
I am using the following code (which I ported from a .NET application that I wrote) with the Java Saxon library. Please note, the following is an abridged version, so it may not actually compile: // Create a Processor instance. Processor processor = new Processor(false); // Create a transformer for the stylesheet. Source xslSource = new StreamSource(new FileReader(new File(xslURI))); // Set base-uri of stylesheet resource. xslSource.setSystemId(xslBaseURI.toString()); XsltExecutable saxonExecutable; if (checkForCompiledStylesheet() == true) { // TODO: Some how load the previously compiled stylesheet. saxonExecutable = ???; } else { saxonExecutable = processor.newXsltCompiler().compile(xslSource); // TODO: Some how save compiled form of stylesheet... } // Create XSLT transformer. XsltTransformer transformer = saxonExecutable.load(); // Initialise parameters. transformer.setParameter(new QName("server-base-uri"), new XdmAtomicValue(xslBaseURI.resolve("../doc/doc/"))); transformer.setParameter(new QName("client-base-uri"), new XdmAtomicValue(getServer().getURI())); // Load the source document. Source xmlSource = new StreamSource(new FileReader(new File(xmlURI))); // Set base-uri of XML resource. xmlSource.setSystemId(xmlURI.toString()); // Construct input node. XdmNode input = processor.newDocumentBuilder().build(xmlSource); // Set the root node of the source document to be the initial context node. transformer.setInitialContextNode(input); // Create a serializer. Serializer serializer = new Serializer(); serializer.setOutputStream(outputStream()); // Transform the source XML to System.out. transformer.setDestination(serializer); transformer.transform(); Compiling the stylesheets has greatly improved performance in my application, but it has also increased start-up time. Is it possible to store the compiled stylesheet as a file which can then be read again at a later stage. I came across a message on the Internet which suggests that the compilation can be achieved from the command line. Can it be done from within the Java application to prevent instantly reloading the compiled form immediately after compilation? I had a look through the various examples, but they appear to transform the documents using different interfaces. Please excuse my ignorance, I am fairly new to Java. Many thanks, Lea Hayes
Replies (6)
Please register to reply
RE: Compile stylesheet to file and read? - Added by Anonymous over 15 years ago
Legacy ID: #7483312 Legacy Poster: Michael Kay (mhkay)
There are interfaces to save a compiled stylesheet to disk, but (a) they aren't available via s9api, and (b) they don't actually help performance significantly - loading the compiled stylesheet from disk turns out to take about as long as recompiling it from source in many cases. But by all means try it. The command-line Compile interface is a thin wrapper around internal calls to serialize the PreparedStylesheet object (in the sense of Java serialization) and if you want to do this from your own code, just copy-and-paste from net.sf.saxon.Compile.java. What I would recommend, though, is to reduce your problem of start-up time by compiling lazily, that is, compiling a stylesheet the first time it is used.
RE: Compile stylesheet to file and read? - Added by Anonymous over 15 years ago
Legacy ID: #7486060 Legacy Poster: Lea Hayes (numberkruncher)
> (a) they aren't available via s9api I only used the s9api because is very closely resembled the code that I wrote using the .NET framework. Is the alternate approach generally preferred? If I understand correctly, there are two other approaches? > (b) they don't actually help performance significantly - loading the compiled stylesheet from disk turns out to take about as long as recompiling it from source in many cases. That's an interesting point. > if you want to do this from your own code, just copy-and-paste from net.sf.saxon.Compile.java I will take a look into that. > What I would recommend, though, is to reduce your problem of start-up time by compiling lazily, that is, compiling a stylesheet the first time it is used. I had actually already written that into the logic, and that has shown huge improvements in performance. I am keeping the processor and the compiled executable in a member variable. This did cause one problem with regards to "generate-id" and document numbering. After a while, generated identifiers were becoming extremely large where the number generator was not being reset. For now I have added the following line which appears to resolve this particular problem, but I fear that this could cause some internal damage: processor.getUnderlyingConfiguration().setDocumentNumberAllocator(new DocumentNumberAllocator()); I have tried using the reset() and clearDocumentPool() functions, but neither appeared to make any difference. I have, however, kept the call to clearDocumentPool() to ensure that XML documents are not cached because they can change in between transforms. Many thanks,
RE: Compile stylesheet to file and read? - Added by Anonymous over 15 years ago
Legacy ID: #7487172 Legacy Poster: Michael Kay (mhkay)
>I only used the s9api because is very closely resembled the code that I wrote using the .NET framework. Is the alternate approach generally preferred? If I understand correctly, there are two other approaches? s9api tries to provide access to "mainstream" functionality, and to provide trapdoors to lower-level interfaces where appropriate. But occasionally there are things you can't do via s9api. I don't consider "compiling" (=serializing) to be mainstream enough, because user experience suggests that the benefits are often disappointing. >This did cause one problem with regards to "generate-id" and document numbering. After a while, generated identifiers were becoming extremely large where the number generator was not being reset. I can't quite see why large document numbers should be a problem. If you're running multiple threads sharing a Configuration, then it would only be safe to reset the DocumentNumberAllocator at a quiet point when nothing is happening, which in general is rather hard to determine. Rather than using reset() and clearDocumentPool() between transformations, I would recommend creating a new Transformer for each transformation. Cache the Templates object, which represents the compiled stylesheet, but create a new Transformer each time you use it.
RE: Compile stylesheet to file and read? - Added by Anonymous over 15 years ago
Legacy ID: #7487678 Legacy Poster: Lea Hayes (numberkruncher)
> I can't quite see why large document numbers should be a problem. My concern was with when the maximum document number is met. My ASP.NET implementation is using multithreading, but the Java implementation, at the moment at least, is only using the one thread. > Rather than using reset() and clearDocumentPool() between transformations, I would recommend creating a new Transformer for each transformation. Thanks for that, I have now replaced those two calls with a new Transformer instance.
RE: Compile stylesheet to file and read? - Added by Anonymous over 15 years ago
Legacy ID: #7487774 Legacy Poster: Michael Kay (mhkay)
>My concern was with when the maximum document number is met. At 100 transformations per second you'll overflow after 223 days - I suppose it's just about possible. I'll change it to a long.
RE: Compile stylesheet to file and read? - Added by Anonymous over 15 years ago
Legacy ID: #7488163 Legacy Poster: Lea Hayes (numberkruncher)
Excellent, thanks again for your help!
Please register to reply