1 18 package org.apache.batik.test.xml; 19 20 import java.io.File ; 21 import java.io.FileOutputStream ; 22 23 import javax.xml.transform.TransformerFactory ; 24 import javax.xml.transform.Transformer ; 25 import javax.xml.transform.stream.StreamSource ; 26 import javax.xml.transform.stream.StreamResult ; 27 28 32 33 import org.apache.batik.test.TestException; 34 35 43 public class XSLXMLReportConsumer 44 implements XMLTestReportProcessor.XMLReportConsumer { 45 48 public static final String ERROR_OUTPUT_DIRECTORY_UNUSABLE 49 = "xml.XSLXMLReportConsumer.error.output.directory.unusable"; 50 51 54 private String stylesheet; 55 56 60 private String outputDirectory; 61 62 65 private String outputFileName; 66 67 74 public XSLXMLReportConsumer(String stylesheet, 75 String outputDirectory, 76 String outputFileName){ 77 this.stylesheet = stylesheet; 78 this.outputDirectory = outputDirectory; 79 this.outputFileName = outputFileName; 80 } 81 82 86 public void onNewReport(File xmlReport, 87 File reportDirectory) 88 throws Exception { 89 90 TransformerFactory tFactory = TransformerFactory.newInstance(); 91 Transformer transformer = tFactory.newTransformer(new StreamSource (stylesheet)); 92 93 transformer.transform(new StreamSource (xmlReport.toURL().toString()), 94 new StreamResult (new FileOutputStream (createNewReportOutput(reportDirectory).getAbsolutePath()))); 95 } 96 97 101 public File createNewReportOutput(File reportDirectory) throws Exception { 102 File dir = new File (reportDirectory, outputDirectory); 103 checkDirectory(dir); 104 return new File (dir, outputFileName); 105 } 106 107 112 public void checkDirectory(File dir) 113 throws TestException { 114 boolean dirOK = false; 115 try{ 116 if(!dir.exists()){ 117 dirOK = dir.mkdir(); 118 } 119 else if(dir.isDirectory()){ 120 dirOK = true; 121 } 122 }finally{ 123 if(!dirOK){ 124 throw new TestException(ERROR_OUTPUT_DIRECTORY_UNUSABLE, 125 new Object [] {dir.getAbsolutePath()}, 126 null); 127 128 } 129 } 130 } 131 132 } 133 | Popular Tags |