1 17 package org.apache.tools.ant.taskdefs.optional.metamata; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileOutputStream ; 22 import java.io.IOException ; 23 import java.util.Vector ; 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.Project; 26 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler; 27 import org.apache.tools.ant.taskdefs.LogStreamHandler; 28 import org.apache.tools.ant.types.EnumeratedAttribute; 29 import org.apache.tools.ant.types.Path; 30 31 41 public class MMetrics extends AbstractMetamataTask { 42 74 75 76 private String granularity = null; 77 78 79 private File outFile = null; 80 81 82 private File tmpFile; 83 84 private Path path = null; 85 86 88 89 public MMetrics() { 90 super("com.metamata.sc.MMetrics"); 91 } 92 93 96 public static class GranularityAttribute extends EnumeratedAttribute { 97 public String [] getValues() { 98 return new String []{"compilation-units", "files", "methods", "types", "packages"}; 99 } 100 } 101 102 107 public void setGranularity(GranularityAttribute granularity) { 108 this.granularity = granularity.getValue(); 109 } 110 111 115 public void setTofile(File file) { 116 this.outFile = file; 117 } 118 119 123 public Path createPath() { 124 if (path == null) { 125 path = new Path(getProject()); 126 } 127 return path; 128 129 } 130 131 133 134 protected void checkOptions() throws BuildException { 136 super.checkOptions(); 137 138 if (outFile == null) { 139 throw new BuildException("Output XML file must be set via 'tofile' attribute."); 140 } 141 if (path == null && fileSets.size() == 0) { 142 throw new BuildException("Must set either paths (path element) " 143 + "or files (fileset element)"); 144 } 145 if (path != null && fileSets.size() > 0) { 148 throw new BuildException("Cannot set paths (path element) and " 149 + "files (fileset element) at the same time"); 150 } 151 tmpFile = createTmpFile(); 152 } 153 154 protected void execute0(ExecuteStreamHandler handler) throws BuildException { 155 super.execute0(handler); 156 transformFile(); 157 } 158 159 165 protected void transformFile() throws BuildException { 166 FileInputStream tmpStream = null; 167 try { 168 tmpStream = new FileInputStream (tmpFile); 169 } catch (IOException e) { 170 throw new BuildException("Error reading temporary file: " + tmpFile, e); 171 } 172 FileOutputStream xmlStream = null; 173 try { 174 xmlStream = new FileOutputStream (outFile); 175 ExecuteStreamHandler xmlHandler = new MMetricsStreamHandler(this, xmlStream); 176 xmlHandler.setProcessOutputStream(tmpStream); 177 xmlHandler.start(); 178 xmlHandler.stop(); 179 } catch (IOException e) { 180 throw new BuildException("Error creating output file: " + outFile, e); 181 } finally { 182 if (xmlStream != null) { 183 try { 184 xmlStream.close(); 185 } catch (IOException ignored) { 186 } 187 } 188 if (tmpStream != null) { 189 try { 190 tmpStream.close(); 191 } catch (IOException ignored) { 192 } 193 } 194 } 195 } 196 197 198 199 protected void cleanUp() throws BuildException { 200 try { 201 super.cleanUp(); 202 } finally { 203 if (tmpFile != null) { 204 tmpFile.delete(); 205 tmpFile = null; 206 } 207 } 208 } 209 210 215 protected ExecuteStreamHandler createStreamHandler() { 216 return new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_INFO); 219 } 220 221 222 protected Vector getOptions() { 223 Vector options = new Vector (512); 224 if (sourcePath != null) { 228 sourcePath.append(classPath); classPath = sourcePath; 230 sourcePath = null; } 232 233 if (classPath != null) { 235 options.addElement("-classpath"); 236 options.addElement(classPath.toString()); 237 } 238 options.addElement("-output"); 239 options.addElement(tmpFile.toString()); 240 241 options.addElement("-" + granularity); 242 243 options.addElement("-format"); 246 247 options.addElement("tab"); 250 251 options.addElement("-i"); 253 options.addElement("/"); 254 255 String [] dirs = path.list(); 257 for (int i = 0; i < dirs.length; i++) { 258 options.addElement(dirs[i]); 259 } 260 addAllVector(options, includedFiles.keys()); 262 return options; 263 } 264 265 } 266 | Popular Tags |