Changeset 1633 for trunk/src/java
- Timestamp:
- 05/26/10 18:16:57 (21 months ago)
- Location:
- trunk/src/java/org/jcoderz/commons
- Files:
-
- 2 modified
-
taskdefs/XsltBasedTask.java (modified) (8 diffs)
-
util/XmlUtil.java (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/java/org/jcoderz/commons/taskdefs/XsltBasedTask.java
r1592 r1633 39 39 import java.io.IOException; 40 40 import java.io.InputStream; 41 import java.net.URISyntaxException; 42 import java.net.URL; 41 43 import java.util.Properties; 42 44 … … 57 59 import org.jcoderz.commons.util.IoUtil; 58 60 import org.jcoderz.commons.util.StringUtil; 61 import org.jcoderz.commons.util.XmlUtil; 59 62 import org.xml.sax.EntityResolver; 60 63 import org.xml.sax.InputSource; … … 391 394 throws BuildException 392 395 { 396 StreamResult out = null; 393 397 try 394 398 { 395 final String xmlParserConfig = System396 .getProperty(XML_PARSER_CONFIGURATION_PROPERTY);399 final String xmlParserConfig 400 = System.getProperty(XML_PARSER_CONFIGURATION_PROPERTY); 397 401 if (!XML_PARSER_CONFIG_WITH_XINCLUDE.equals(xmlParserConfig)) 398 402 { 399 System.setProperty(XML_PARSER_CONFIGURATION_PROPERTY, 403 System.setProperty( 404 XML_PARSER_CONFIGURATION_PROPERTY, 400 405 XML_PARSER_CONFIG_WITH_XINCLUDE); 401 406 log("Using XML Parser configuration " … … 404 409 // Xalan2 transformer is required, 405 410 // 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()); 409 416 410 417 factory.setURIResolver(new JarArchiveUriResolver(this)); 411 final InputStream xslStream = getXslFileAsStream();412 final Transformer transformer = factory413 .newTransformer(new StreamSource(xslStream));418 final StreamSource source = getXslFileAsSource(); 419 final Transformer transformer 420 = factory.newTransformer(source); 414 421 setAdditionalTransformerParameters(transformer); 415 422 transformer.setParameter("outdir", mDestDir != null ? mDestDir 416 423 .getAbsolutePath() : ""); 417 424 final Source xml = getInAsStreamSource(); 418 final StreamResult out = newStreamResult(mOutFile);425 out = XmlUtil.createStreamResult(mOutFile); 419 426 transformer.setErrorListener(new MyErrorListener()); 420 427 transformer.transform(xml, out); … … 426 433 finally 427 434 { 435 if (out != null) 436 { 437 IoUtil.close(out.getOutputStream()); 438 } 428 439 if (mClassLoader != null) 429 440 { … … 457 468 } 458 469 459 private InputStream getXslFileAsStream()460 { 461 final InputStreamresult;470 private StreamSource getXslFileAsSource () 471 { 472 final StreamSource result; 462 473 final InputStream xslStream 463 474 = XsltBasedTask.class.getResourceAsStream(mXslFile); … … 466 477 try 467 478 { 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()); 470 483 } 471 484 catch (FileNotFoundException e) … … 477 490 else 478 491 { 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 } 480 506 } 481 507 return result; -
trunk/src/java/org/jcoderz/commons/util/XmlUtil.java
r1523 r1633 33 33 package org.jcoderz.commons.util; 34 34 35 import java.io.File; 36 import java.io.FileOutputStream; 37 import java.io.IOException; 38 import javax.xml.transform.stream.StreamResult; 35 39 import org.xml.sax.Attributes; 36 40 … … 329 333 } 330 334 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 331 364 private static void indent (final int i, StringBuffer b) 332 365 {
