1 20 21 package net.innig.macker.ant; 22 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.net.URL ; 27 import java.net.MalformedURLException ; 28 import java.util.*; 29 30 import org.mpr.util.StreamSplitter; 31 32 import org.apache.tools.ant.Task; 33 import org.apache.tools.ant.BuildException; 34 35 import javax.xml.transform.Transformer ; 36 import javax.xml.transform.TransformerFactory ; 37 import javax.xml.transform.TransformerException ; 38 import javax.xml.transform.stream.StreamSource ; 39 import javax.xml.transform.stream.StreamResult ; 40 41 47 public class MackerReportAntTask extends Task 48 { 49 public MackerReportAntTask() 50 { tFactory = TransformerFactory.newInstance(); } 51 52 public void setFormat(String formatName) 53 throws BuildException 54 { formatUrl = resolveInternalResource(formatName, "format", "xsl"); } 55 56 public void setFormatFile(File formatFile) 57 throws BuildException 58 { formatUrl = resolveFile(formatFile, "format"); } 59 60 public void setFormatUrl(String formatUrlS) 61 throws BuildException 62 { formatUrl = resolveUrl(formatUrlS, "format"); } 63 64 public void setSkin(String skinName) 65 throws BuildException 66 { skinUrl = resolveInternalResource(skinName, "skin", "css"); } 67 68 public void setSkinFile(File skinFile) 69 throws BuildException 70 { skinUrl = resolveFile(skinFile, "skin"); } 71 72 public void setSkinUrl(String skinUrlS) 73 throws BuildException 74 { skinUrl = resolveUrl(skinUrlS, "skin"); } 75 76 public void setXmlReportFile(File xmlReportFile) 77 throws BuildException 78 { reportUrl = resolveFile(xmlReportFile, "report"); } 79 80 public void setXmlReportUrl(String xmlReportUrlS) 81 throws BuildException 82 { reportUrl = resolveUrl(xmlReportUrlS, "report"); } 83 84 public void setOutputFile(File outputFile) 85 { this.outputFile = outputFile; } 86 87 private URL resolveFile(File file, String kind) 88 throws BuildException 89 { 90 if(!file.exists()) 91 throw new BuildException(kind + " file " + file + " does not exist"); 92 if(!file.isFile()) 93 throw new BuildException(kind + " file " + file + " is not a file"); 94 95 try { return file.toURL(); } 96 catch(MalformedURLException murle) 97 { throw new BuildException("Invalid " + kind + " file " + file, murle); } 98 } 99 100 private URL resolveUrl(String urlS, String kind) 101 throws BuildException 102 { 103 try { return new URL (urlS); } 104 catch(MalformedURLException murle) 105 { throw new BuildException("Invalid " + kind + " URL " + urlS, murle); } 106 } 107 108 private URL resolveInternalResource(String name, String kind, String extension) 109 throws BuildException 110 { 111 String resourceName = "net/innig/macker/report/" + kind + '/' + name + '.' + extension; 112 URL resource = getClass().getClassLoader().getResource(resourceName); 113 if(resource == null) 114 throw new BuildException( 115 "No internal Macker report " + kind + " named \"" + name 116 + "\" (can't find \"" + resourceName + "\")"); 117 return resource; 118 } 119 120 public void execute() 121 throws BuildException 122 { 123 if(reportUrl == null) 124 throw new BuildException("xmlReportFile or xmlReportUrl required"); 125 if(outputFile == null) 126 throw new BuildException("outputFile required"); 127 128 if(formatUrl == null) 129 setFormat("html-basic"); 130 if(skinUrl == null) 131 setSkin("vanilla"); 132 133 File outputDir = outputFile.getParentFile(); 134 135 try { 136 Transformer transformer = tFactory.newTransformer(new StreamSource (formatUrl.openStream())); 137 transformer.transform( 138 new StreamSource (reportUrl.openStream()), 139 new StreamResult (new FileOutputStream (outputFile))); 140 } 141 catch(IOException ioe) 142 { throw new BuildException("Unable to process report: " + ioe, ioe); } 143 catch(TransformerException te) 144 { throw new BuildException("Unable to apply report formatting: " + te.getMessage(), te); } 145 146 File skinOutputFile = new File (outputDir, "macker-report.css"); 147 try { 148 new StreamSplitter( 149 skinUrl.openStream(), 150 new FileOutputStream (skinOutputFile)) 151 .run(); 152 } 153 catch(IOException ioe) 154 { throw new BuildException("Unable to copy skin to " + skinOutputFile, ioe); } 155 } 156 157 private URL formatUrl, skinUrl; 158 private URL reportUrl; 159 private File outputFile; 160 private TransformerFactory tFactory; 161 } 162 163 | Popular Tags |