]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Worked around a bug in Xalan 2.4.0 (bundled with Sun Java 1.4.2_xx) where file URLs...
authorJeremias Maerki <jeremias@apache.org>
Wed, 16 Jul 2008 20:16:51 +0000 (20:16 +0000)
committerJeremias Maerki <jeremias@apache.org>
Wed, 16 Jul 2008 20:16:51 +0000 (20:16 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@677404 13f79535-47bb-0310-9956-ffa450edef68

src/codegen/java/org/apache/fop/tools/EventProducerCollectorTask.java
src/java/org/apache/fop/events/model/EventModel.java

index ed92a55e03acf345d3f4e5a14d3ad121de7d23d8..e00b05b557d451a6c30214ddba873a8ee3ce6862 100644 (file)
@@ -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());
index a10237ad552f181b49a4d40e5c3c2c20f96fd907..454e4a8ef46995e745353483b35680b821cc9168 100644 (file)
@@ -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);
         }
     }