1 6 package fr.jayasoft.ivy.ant; 7 8 import java.io.File ; 9 import java.io.IOException ; 10 import java.util.ArrayList ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import org.apache.tools.ant.BuildException; 15 import org.apache.tools.ant.taskdefs.XSLTProcess; 16 import org.apache.tools.ant.types.Mapper; 17 import org.apache.tools.ant.util.FileNameMapper; 18 import org.apache.tools.ant.util.GlobPatternMapper; 19 20 import fr.jayasoft.ivy.Ivy; 21 import fr.jayasoft.ivy.ModuleId; 22 import fr.jayasoft.ivy.report.XmlReportOutputter; 23 import fr.jayasoft.ivy.util.FileUtil; 24 import fr.jayasoft.ivy.util.IvyPatternHelper; 25 import fr.jayasoft.ivy.util.Message; 26 27 32 public class IvyReport extends IvyTask { 33 private File _todir; 34 private String _organisation; 35 private String _module; 36 private String _conf; 37 private File _cache; 38 private boolean _graph = true; 39 private boolean _dot = false; 40 private boolean _xml = false; 41 private boolean _xsl = true; 42 private String _xslFile; 43 private String _outputpattern; 44 private String _xslext = "html"; 45 private List _params = new ArrayList (); 46 47 public File getTodir() { 48 return _todir; 49 } 50 public void setTodir(File todir) { 51 _todir = todir; 52 } 53 public File getCache() { 54 return _cache; 55 } 56 public void setCache(File cache) { 57 _cache = cache; 58 } 59 public String getConf() { 60 return _conf; 61 } 62 public void setConf(String conf) { 63 _conf = conf; 64 } 65 public String getModule() { 66 return _module; 67 } 68 public void setModule(String module) { 69 _module = module; 70 } 71 public String getOrganisation() { 72 return _organisation; 73 } 74 public void setOrganisation(String organisation) { 75 _organisation = organisation; 76 } 77 78 public boolean isGraph() { 79 return _graph; 80 } 81 82 public void setGraph(boolean graph) { 83 _graph = graph; 84 } 85 public String getXslfile() { 86 return _xslFile; 87 } 88 89 public void setXslfile(String xslFile) { 90 _xslFile = xslFile; 91 } 92 public String getOutputpattern() { 93 return _outputpattern; 94 } 95 96 public void setOutputpattern(String outputpattern) { 97 _outputpattern = outputpattern; 98 } 99 100 101 public void execute() throws BuildException { 102 Ivy ivy = getIvyInstance(); 103 104 _organisation = getProperty(_organisation, ivy, "ivy.organisation"); 105 _module = getProperty(_module, ivy, "ivy.module"); 106 if (_cache == null) { 107 _cache = ivy.getDefaultCache(); 108 } 109 _conf = getProperty(_conf, ivy, "ivy.resolved.configurations"); 110 if (_conf.equals("*")) { 111 _conf = getProperty(ivy, "ivy.resolved.configurations"); 112 } 113 if (_conf == null) { 114 throw new BuildException("no conf provided for ivy report task: It can either be set explicitely via the attribute 'conf' or via 'ivy.resolved.configurations' property or a prior call to <resolve/>"); 115 } 116 if (_todir == null) { 117 String t = getProperty(ivy, "ivy.report.todir"); 118 if (t != null) { 119 _todir = new File (t); 120 } 121 } 122 _outputpattern = getProperty(_outputpattern, ivy, "ivy.report.output.pattern"); 123 if (_todir != null && _todir.exists()) { 124 _todir.mkdirs(); 125 } 126 if (_outputpattern == null) { 127 _outputpattern = "[organisation]-[module]-[conf].[ext]"; 128 } 129 130 if (_todir != null && _todir.exists() && !_todir.isDirectory()) { 131 throw new BuildException("destination directory should be a directory !"); 132 } 133 if (_organisation == null) { 134 throw new BuildException("no organisation provided for ivy report task: It can either be set explicitely via the attribute 'organisation' or via 'ivy.organisation' property or a prior call to <resolve/>"); 135 } 136 if (_module == null) { 137 throw new BuildException("no module name provided for ivy report task: It can either be set explicitely via the attribute 'module' or via 'ivy.module' property or a prior call to <resolve/>"); 138 } 139 try { 140 String [] confs = splitConfs(_conf); 141 if (_xsl) { 142 genreport(_cache, _organisation, _module, confs); 143 } 144 if (_xml) { 145 genxml(_cache, _organisation, _module, confs); 146 } 147 if (_graph) { 148 genStyled(_cache, _organisation, _module, confs, getStylePath(_cache, "ivy-report-graph.xsl"), "graphml"); 149 } 150 if (_dot) { 151 genStyled(_cache, _organisation, _module, confs, getStylePath(_cache, "ivy-report-dot.xsl"), "dot"); 152 } 153 } catch (IOException e) { 154 throw new BuildException("impossible to generate report: "+e, e); 155 } 156 } 157 158 private void genxml(File cache, String organisation, String module, String [] confs) throws IOException { 159 for (int i = 0; i < confs.length; i++) { 160 File xml = new File (cache, XmlReportOutputter.getReportFileName(new ModuleId(organisation, module), confs[i])); 161 162 File out; 163 if (_todir != null) { 164 out = new File (_todir, IvyPatternHelper.substitute(_outputpattern, organisation, module, "", "", "", "xml", confs[i])); 165 } else { 166 out = new File (IvyPatternHelper.substitute(_outputpattern, organisation, module, "", "", "", "xml", confs[i])); 167 } 168 169 FileUtil.copy(xml, out, null); 170 } 171 } 172 private void genreport(File cache, String organisation, String module, String [] confs) throws IOException { 173 genStyled(cache, organisation, module, confs, getReportStylePath(cache), _xslext); 174 175 if (_todir != null && _xslFile == null) { 177 File css = new File (_todir, "ivy-report.css"); 178 if (!css.exists()) { 179 Message.debug("copying report css to "+_todir); 180 FileUtil.copy(XmlReportOutputter.class.getResourceAsStream("ivy-report.css"), css, null); 181 } 182 FileUtil.copy(XmlReportOutputter.class.getResourceAsStream("ivy-report.css"), new File (cache, "ivy-report.css"), null); 183 } 184 } 185 186 private String getReportStylePath(File cache) throws IOException { 187 if (_xslFile != null) { 188 return _xslFile; 189 } 190 File style = new File (cache, "ivy-report.xsl"); 193 FileUtil.copy(XmlReportOutputter.class.getResourceAsStream("ivy-report.xsl"), style, null); 194 return style.getAbsolutePath(); 195 } 196 197 private void genStyled(File cache, String organisation, String module, String [] confs, String style, String ext) throws IOException { 198 File out; 200 if (_todir != null) { 201 out = _todir; 202 } else { 203 out = new File ("."); 204 } 205 206 XSLTProcess xslt = new XSLTProcess(); 207 xslt.setTaskName(getTaskName()); 208 xslt.setProject(getProject()); 209 xslt.init(); 210 211 xslt.setDestdir(out); 212 xslt.setBasedir(cache); 213 214 Mapper mapper = new Mapper(getProject()); 215 xslt.addMapper(mapper); 216 217 for (int i = 0; i < confs.length; i++) { 218 String reportFileName = XmlReportOutputter.getReportFileName(new ModuleId(organisation, module), confs[i]); 219 xslt.setIncludes(reportFileName); 220 221 FileNameMapper reportMapper = new GlobPatternMapper(); 222 reportMapper.setFrom(reportFileName); 223 reportMapper.setTo(IvyPatternHelper.substitute(_outputpattern, organisation, module, "", "", "", ext, confs[i])); 224 mapper.add(reportMapper); 225 } 226 xslt.setStyle(style); 227 228 XSLTProcess.Param param = xslt.createParam(); 229 param.setName("confs"); 230 param.setExpression(_conf); 231 param = xslt.createParam(); 232 param.setName("extension"); 233 param.setExpression(_xslext); 234 235 for (Iterator it = _params.iterator(); it.hasNext(); ) { 237 param = (XSLTProcess.Param) it.next(); 238 XSLTProcess.Param realParam = xslt.createParam(); 239 realParam.setName(param.getName()); 240 realParam.setExpression(param.getExpression()); 241 } 242 243 xslt.execute(); 244 } 245 246 private String getStylePath(File cache, String styleResourceName) throws IOException { 247 File style = new File (cache, styleResourceName); 250 FileUtil.copy(XmlReportOutputter.class.getResourceAsStream(styleResourceName), style, null); 251 return style.getAbsolutePath(); 252 } 253 254 public boolean isXml() { 255 return _xml; 256 } 257 public void setXml(boolean xml) { 258 _xml = xml; 259 } 260 public boolean isXsl() { 261 return _xsl; 262 } 263 public void setXsl(boolean xsl) { 264 _xsl = xsl; 265 } 266 public String getXslext() { 267 return _xslext; 268 } 269 public void setXslext(String xslext) { 270 _xslext = xslext; 271 } 272 273 public XSLTProcess.Param createParam() { 274 XSLTProcess.Param result = new XSLTProcess.Param(); 275 _params.add(result); 276 return result; 277 } 278 public boolean isDot() { 279 return _dot; 280 } 281 public void setDot(boolean dot) { 282 _dot = dot; 283 } 284 285 } 286 | Popular Tags |