Changeset 1633 for trunk/src/java

Show
Ignore:
Timestamp:
05/26/10 18:16:57 (21 months ago)
Author:
amandel
Message:

#81 open the stream directly in StreamSource? to avoid whitespace in path issue.

Location:
trunk/src/java/org/jcoderz/commons
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/java/org/jcoderz/commons/taskdefs/XsltBasedTask.java

    r1592 r1633  
    3939import java.io.IOException; 
    4040import java.io.InputStream; 
     41import java.net.URISyntaxException; 
     42import java.net.URL; 
    4143import java.util.Properties; 
    4244 
     
    5759import org.jcoderz.commons.util.IoUtil; 
    5860import org.jcoderz.commons.util.StringUtil; 
     61import org.jcoderz.commons.util.XmlUtil; 
    5962import org.xml.sax.EntityResolver; 
    6063import org.xml.sax.InputSource; 
     
    391394        throws BuildException 
    392395    { 
     396        StreamResult out = null; 
    393397        try 
    394398        { 
    395             final String xmlParserConfig = System 
    396                 .getProperty(XML_PARSER_CONFIGURATION_PROPERTY); 
     399            final String xmlParserConfig 
     400                = System.getProperty(XML_PARSER_CONFIGURATION_PROPERTY); 
    397401            if (!XML_PARSER_CONFIG_WITH_XINCLUDE.equals(xmlParserConfig)) 
    398402            { 
    399                 System.setProperty(XML_PARSER_CONFIGURATION_PROPERTY, 
     403                System.setProperty( 
     404                    XML_PARSER_CONFIGURATION_PROPERTY, 
    400405                    XML_PARSER_CONFIG_WITH_XINCLUDE); 
    401406                log("Using XML Parser configuration " 
     
    404409            // Xalan2 transformer is required, 
    405410            // that why we explicit use this factory 
    406             final TransformerFactory factory = (TransformerFactory) 
    407             (loadClass("org.apache.xalan.processor.TransformerFactoryImpl") 
    408                 .newInstance()); 
     411            final TransformerFactory factory 
     412                = (TransformerFactory) 
     413                    (loadClass( 
     414                        "org.apache.xalan.processor.TransformerFactoryImpl") 
     415                            .newInstance()); 
    409416 
    410417            factory.setURIResolver(new JarArchiveUriResolver(this)); 
    411             final InputStream xslStream = getXslFileAsStream(); 
    412             final Transformer transformer = factory 
    413                 .newTransformer(new StreamSource(xslStream)); 
     418            final StreamSource source = getXslFileAsSource(); 
     419            final Transformer transformer 
     420                = factory.newTransformer(source); 
    414421            setAdditionalTransformerParameters(transformer); 
    415422            transformer.setParameter("outdir", mDestDir != null ? mDestDir 
    416423                .getAbsolutePath() : ""); 
    417424            final Source xml = getInAsStreamSource(); 
    418             final StreamResult out = new StreamResult(mOutFile); 
     425            out = XmlUtil.createStreamResult(mOutFile); 
    419426            transformer.setErrorListener(new MyErrorListener()); 
    420427            transformer.transform(xml, out); 
     
    426433        finally 
    427434        { 
     435            if (out != null) 
     436            { 
     437                IoUtil.close(out.getOutputStream()); 
     438            } 
    428439            if (mClassLoader != null) 
    429440            { 
     
    457468    } 
    458469 
    459     private InputStream getXslFileAsStream () 
    460     { 
    461         final InputStream result; 
     470    private StreamSource getXslFileAsSource () 
     471    { 
     472        final StreamSource result; 
    462473        final InputStream xslStream 
    463474            = XsltBasedTask.class.getResourceAsStream(mXslFile); 
     
    466477            try 
    467478            { 
    468                 final InputStream xslFile = new FileInputStream(mXslFile); 
    469                 result = xslFile; 
     479                final File file = new File(mXslFile); 
     480                final InputStream xslFile = new FileInputStream(file); 
     481                result = new StreamSource(xslFile); 
     482                result.setSystemId(file.toURI().toASCIIString()); 
    470483            } 
    471484            catch (FileNotFoundException e) 
     
    477490        else 
    478491        { 
    479             result = xslStream; 
     492            result = new StreamSource(xslStream); 
     493            final URL url = XsltBasedTask.class.getResource(mXslFile); 
     494            if (url != null) 
     495            { 
     496                try 
     497                { 
     498                    result.setSystemId(url.toURI().toASCIIString()); 
     499                } 
     500                catch (URISyntaxException ex) 
     501                { 
     502                    log("Failed to set systemId. Got " + ex, 
     503                            Project.MSG_VERBOSE); 
     504                } 
     505            } 
    480506        } 
    481507        return result; 
  • trunk/src/java/org/jcoderz/commons/util/XmlUtil.java

    r1523 r1633  
    3333package org.jcoderz.commons.util; 
    3434 
     35import java.io.File; 
     36import java.io.FileOutputStream; 
     37import java.io.IOException; 
     38import javax.xml.transform.stream.StreamResult; 
    3539import org.xml.sax.Attributes; 
    3640 
     
    329333   } 
    330334 
     335    /** 
     336     * Creae a stream result based on a File. 
     337     * Other than the default implementation not only the File is used to 
     338     * initialize the result, but also a Stream is opened and initialized. 
     339     * This works around an issue with whitespaces in the pathname which 
     340     * otherwise would lead to a file not found exception 
     341     * <pre> 
     342     * Error during transformation: javax.xml.transform.TransformerException: java.io.FileNotFoundException: ...%20... 
     343     *   at org.apache.xalan.transformer.TransformerImpl.createSerializationHandler(TransformerImpl.java:1218) 
     344     *   at org.apache.xalan.transformer.TransformerImpl.createSerializationHandler(TransformerImpl.java:1060) 
     345     *   at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1268) 
     346     *   at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1251) 
     347     *   .... 
     348     * </pre>. 
     349     * The caller must ensure that the created outputstream is closed. 
     350     * @param outFile the file to be used in the stream result. 
     351     * @return a new StreamResult piontint to the given File. 
     352     * @throws IOException in case of an issue while creating the stream. 
     353     */ 
     354    public static StreamResult createStreamResult(File outFile) 
     355        throws IOException 
     356    { 
     357        final StreamResult result = new StreamResult(outFile); 
     358        // set the stream directly to avoid issues with blanks in the 
     359        // filename. 
     360        result.setOutputStream(new FileOutputStream(outFile)); 
     361        return result; 
     362    } 
     363 
    331364   private static void indent (final int i, StringBuffer b) 
    332365   {