From 1ec5f0c34786a0589aa34cf06a523c05bb42ec61 Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Wed, 16 Jul 2008 20:16:51 +0000 Subject: [PATCH] Worked around a bug in Xalan 2.4.0 (bundled with Sun Java 1.4.2_xx) where file URLs like file:/C:/.... got converted to file:\C:\.... on Windows causing a FileNotFoundException. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@677404 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/tools/EventProducerCollectorTask.java | 53 +++++++++++-------- .../apache/fop/events/model/EventModel.java | 15 +++++- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/src/codegen/java/org/apache/fop/tools/EventProducerCollectorTask.java b/src/codegen/java/org/apache/fop/tools/EventProducerCollectorTask.java index ed92a55e0..e00b05b55 100644 --- a/src/codegen/java/org/apache/fop/tools/EventProducerCollectorTask.java +++ b/src/codegen/java/org/apache/fop/tools/EventProducerCollectorTask.java @@ -22,6 +22,7 @@ package org.apache.fop.tools; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.OutputStream; import java.util.Iterator; import java.util.List; @@ -38,6 +39,7 @@ import javax.xml.transform.stream.StreamSource; import org.w3c.dom.Node; +import org.apache.commons.io.IOUtils; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; @@ -121,29 +123,38 @@ public class EventProducerCollectorTask extends Task { //Generate translation file (with potentially new translations) src = new DOMSource(sourceDocument); - Result res = new StreamResult(getTranslationFile().toURI().toURL().toExternalForm()); - StreamSource xslt2 = new StreamSource( - getClass().getResourceAsStream(MERGETRANSLATION)); - if (xslt2.getInputStream() == null) { - throw new FileNotFoundException(MERGETRANSLATION + " not found"); - } - transformer = tFactory.newTransformer(xslt2); - transformer.setURIResolver(new URIResolver() { - public Source resolve(String href, String base) throws TransformerException { - if ("my:dom".equals(href)) { - return new DOMSource(generated); + + //The following triggers a bug in older Xalan versions + //Result res = new StreamResult(getTranslationFile()); + OutputStream out = new java.io.FileOutputStream(getTranslationFile()); + out = new java.io.BufferedOutputStream(out); + Result res = new StreamResult(out); + try { + StreamSource xslt2 = new StreamSource( + getClass().getResourceAsStream(MERGETRANSLATION)); + if (xslt2.getInputStream() == null) { + throw new FileNotFoundException(MERGETRANSLATION + " not found"); + } + transformer = tFactory.newTransformer(xslt2); + transformer.setURIResolver(new URIResolver() { + public Source resolve(String href, String base) throws TransformerException { + if ("my:dom".equals(href)) { + return new DOMSource(generated); + } + return null; } - return null; + }); + if (resultExists) { + transformer.setParameter("generated-url", "my:dom"); } - }); - if (resultExists) { - transformer.setParameter("generated-url", "my:dom"); - } - transformer.transform(src, res); - if (resultExists) { - log("Translation file updated: " + getTranslationFile()); - } else { - log("Translation file generated: " + getTranslationFile()); + transformer.transform(src, res); + if (resultExists) { + log("Translation file updated: " + getTranslationFile()); + } else { + log("Translation file generated: " + getTranslationFile()); + } + } finally { + IOUtils.closeQuietly(out); } } catch (TransformerException te) { throw new IOException(te.getMessage()); diff --git a/src/java/org/apache/fop/events/model/EventModel.java b/src/java/org/apache/fop/events/model/EventModel.java index a10237ad5..454e4a8ef 100644 --- a/src/java/org/apache/fop/events/model/EventModel.java +++ b/src/java/org/apache/fop/events/model/EventModel.java @@ -21,6 +21,7 @@ package org.apache.fop.events.model; import java.io.File; import java.io.IOException; +import java.io.OutputStream; import java.io.Serializable; import java.util.Iterator; import java.util.Map; @@ -38,6 +39,8 @@ import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; +import org.apache.commons.io.IOUtils; + import org.apache.xmlgraphics.util.XMLizable; /** @@ -102,12 +105,18 @@ public class EventModel implements Serializable, XMLizable { } private void writeXMLizable(XMLizable object, File outputFile) throws IOException { - //Result res = new StreamResult(outputFile); //Does not seem to work in all environments - Result res = new StreamResult(outputFile.toURI().toURL().toExternalForm()); + //These two approaches do not seem to work in all environments: + //Result res = new StreamResult(outputFile); + //Result res = new StreamResult(outputFile.toURI().toURL().toExternalForm()); + //With an old Xalan version: file:/C:/.... --> file:\C:\..... + OutputStream out = new java.io.FileOutputStream(outputFile); + out = new java.io.BufferedOutputStream(out); + Result res = new StreamResult(out); try { SAXTransformerFactory tFactory = (SAXTransformerFactory)SAXTransformerFactory.newInstance(); + System.out.println(tFactory.getClass().getName()); TransformerHandler handler = tFactory.newTransformerHandler(); Transformer transformer = handler.getTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); @@ -121,6 +130,8 @@ public class EventModel implements Serializable, XMLizable { throw new IOException(e.getMessage()); } catch (SAXException e) { throw new IOException(e.getMessage()); + } finally { + IOUtils.closeQuietly(out); } } -- 2.39.5