| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.phoenix.report; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.IOException; |
| 37 | | | import java.util.ArrayList; |
| 38 | | | import java.util.Iterator; |
| 39 | | | import java.util.List; |
| 40 | | | |
| 41 | | | import org.apache.tools.ant.BuildException; |
| 42 | | | import org.apache.tools.ant.Project; |
| 43 | | | import org.apache.tools.ant.taskdefs.Execute; |
| 44 | | | import org.apache.tools.ant.taskdefs.LogStreamHandler; |
| 45 | | | import org.apache.tools.ant.taskdefs.MatchingTask; |
| 46 | | | import org.apache.tools.ant.types.Commandline; |
| 47 | | | import org.apache.tools.ant.types.CommandlineJava; |
| 48 | | | import org.apache.tools.ant.types.Environment; |
| 49 | | | import org.apache.tools.ant.types.Path; |
| 50 | | | |
| 51 | | | |
| 52 | | | |
| 53 | | | |
| 54 | | | |
| 55 | | | |
| 56 | | | <code></code> |
| 57 | | | |
| 58 | | | @author |
| 59 | | | |
| 60 | 0 | | public final class JcoderzReportAntTask |
| 61 | | | extends MatchingTask |
| 62 | | | { |
| 63 | | | |
| 64 | 0 | | private File mWorkingDir = null; |
| 65 | | | |
| 66 | | | |
| 67 | 0 | | private File mOut = new File("."); |
| 68 | | | |
| 69 | 0 | | private File mFilter = null; |
| 70 | | | |
| 71 | 0 | | private String mName = "Not defined"; |
| 72 | | | |
| 73 | 0 | | private OutputFormat mOutputFormat = OutputFormat.XML; |
| 74 | | | |
| 75 | 0 | | private boolean mFailOnError = false; |
| 76 | | | |
| 77 | 0 | | private final List mReportFiles = new ArrayList(); |
| 78 | | | |
| 79 | 0 | | private final CommandlineJava mCommandline = new CommandlineJava(); |
| 80 | | | |
| 81 | | | |
| 82 | | | |
| 83 | 0 | | private final List mSourceDirectories = new ArrayList(); |
| 84 | | | |
| 85 | | | |
| 86 | 0 | | private boolean mDebug = false; |
| 87 | | | |
| 88 | | | |
| 89 | | | |
| 90 | | | |
| 91 | | | |
| 92 | | | |
| 93 | | | @param |
| 94 | | | |
| 95 | | (1) | public final void setOut (File dir) |
| 96 | | | { |
| 97 | 0 | | mOut = dir; |
| 98 | 0 | | } |
| 99 | | | |
| 100 | | | |
| 101 | | | |
| 102 | | | |
| 103 | | | @param |
| 104 | | | |
| 105 | | (2) | public final void setFilter (File f) |
| 106 | | | { |
| 107 | 0 | | mFilter = f; |
| 108 | 0 | | } |
| 109 | | | |
| 110 | | | |
| 111 | | | <code></code> |
| 112 | | | |
| 113 | | | @param |
| 114 | | | |
| 115 | | (3) | public final void setName (String projectName) |
| 116 | | | { |
| 117 | 0 | | mName = projectName; |
| 118 | 0 | | } |
| 119 | | | |
| 120 | | | |
| 121 | | | |
| 122 | | | |
| 123 | | | @param |
| 124 | | | |
| 125 | | | public void setDir (File dir) |
| 126 | | | { |
| 127 | 0 | | mWorkingDir = dir; |
| 128 | 0 | | } |
| 129 | | | |
| 130 | | | |
| 131 | | | |
| 132 | | | |
| 133 | | | @param |
| 134 | | | |
| 135 | | | public void setFailonerror (boolean b) |
| 136 | | | { |
| 137 | 0 | | mFailOnError = b; |
| 138 | 0 | | } |
| 139 | | | |
| 140 | | | |
| 141 | | | |
| 142 | | | |
| 143 | | | @param |
| 144 | | | @throws |
| 145 | | | |
| 146 | | | |
| 147 | | | public void setFormat (String s) |
| 148 | | | throws BuildException |
| 149 | | | { |
| 150 | | | try |
| 151 | | | { |
| 152 | 0 | | mOutputFormat = OutputFormat.fromString(s); |
| 153 | | | } |
| 154 | 0 | | catch (IllegalArgumentException e) |
| 155 | | | { |
| 156 | 0 | | throw new BuildException("Unsupported output format '" |
| 157 | | | + s + "'", e, getLocation()); |
| 158 | 0 | | } |
| 159 | 0 | (4) | } |
| 160 | | | |
| 161 | | | |
| 162 | | | |
| 163 | | | |
| 164 | | | @param |
| 165 | | | |
| 166 | | | public void setDebug (boolean b) |
| 167 | | | { |
| 168 | 0 | | mDebug = b; |
| 169 | 0 | | } |
| 170 | | | |
| 171 | | | |
| 172 | | | |
| 173 | | | |
| 174 | | | @return |
| 175 | | | |
| 176 | | | public Path createClasspath () |
| 177 | | | { |
| 178 | 0 | | return mCommandline.createClasspath(getProject()).createPath(); |
| 179 | | | } |
| 180 | | | |
| 181 | | | |
| 182 | | | |
| 183 | | | |
| 184 | | | @return |
| 185 | | | |
| 186 | | | public Path createBootclasspath () |
| 187 | | | { |
| 188 | 0 | | return mCommandline.createBootclasspath(getProject()).createPath(); |
| 189 | | | } |
| 190 | | | |
| 191 | | | |
| 192 | | | |
| 193 | | | |
| 194 | | | @param |
| 195 | | | |
| 196 | | | public void addSysproperty (Environment.Variable sysp) |
| 197 | | | { |
| 198 | 0 | | mCommandline.addSysproperty(sysp); |
| 199 | 0 | | } |
| 200 | | | |
| 201 | | | |
| 202 | | | |
| 203 | | | |
| 204 | | | @return |
| 205 | | | |
| 206 | | | public Commandline.Argument createJvmarg () |
| 207 | | | { |
| 208 | 0 | | return mCommandline.createVmArgument(); |
| 209 | | | } |
| 210 | | | |
| 211 | | | |
| 212 | | | |
| 213 | | | |
| 214 | | | @return |
| 215 | | | |
| 216 | | | public Report createReport () |
| 217 | | | { |
| 218 | 0 | | final Report ret = new Report(); |
| 219 | 0 | (5) | mReportFiles.add(ret); |
| 220 | 0 | | return ret; |
| 221 | | | } |
| 222 | | | |
| 223 | | | |
| 224 | | | |
| 225 | | | |
| 226 | | | @return |
| 227 | | | |
| 228 | | | public SourceDirectory createSrcDir () |
| 229 | | | { |
| 230 | 0 | | final SourceDirectory ret = new SourceDirectory(); |
| 231 | 0 | (6) | mSourceDirectories.add(ret); |
| 232 | 0 | | return ret; |
| 233 | | | } |
| 234 | | | |
| 235 | | | |
| 236 | | | |
| 237 | | | |
| 238 | | | @throws |
| 239 | | | |
| 240 | | | public void execute () |
| 241 | | | throws BuildException |
| 242 | | | { |
| 243 | 0 | | checkParameters(); |
| 244 | | | |
| 245 | | | try |
| 246 | | | { |
| 247 | | | int exitValue; |
| 248 | | | |
| 249 | 0 | | exitValue = executeAsForked(); |
| 250 | | | |
| 251 | 0 | | if (exitValue != 0) |
| 252 | | | { |
| 253 | 0 | | final String msg = "ReportNormalizer returned with exit code '" |
| 254 | | | + exitValue + "'"; |
| 255 | 0 | | log(msg, Project.MSG_WARN); |
| 256 | 0 | | throw new BuildException(msg, getLocation()); |
| 257 | | | } |
| 258 | | | } |
| 259 | 0 | | catch (BuildException e) |
| 260 | | | { |
| 261 | 0 | | if (mFailOnError) |
| 262 | | | { |
| 263 | 0 | | throw e; |
| 264 | | | } |
| 265 | 0 | | log(e.getMessage(), e, Project.MSG_ERR); |
| 266 | 0 | | } |
| 267 | 0 | | } |
| 268 | | | |
| 269 | | | |
| 270 | | | |
| 271 | | | |
| 272 | | | @return |
| 273 | | | |
| 274 | | | private CommandlineJava createCommandline () |
| 275 | | | { |
| 276 | | | final CommandlineJava cmd; |
| 277 | | | try |
| 278 | | | { |
| 279 | 0 | | cmd = (CommandlineJava) mCommandline.clone(); |
| 280 | | | } |
| 281 | 0 | | catch (CloneNotSupportedException unexpected) |
| 282 | | | { |
| 283 | 0 | | throw new RuntimeException( |
| 284 | | | "Ups, CommandLineJava doesn't support the method clone()", |
| 285 | | | unexpected); |
| 286 | 0 | | } |
| 287 | | | |
| 288 | 0 | | cmd.setClassname(ReportNormalizer.class.getName()); |
| 289 | | | |
| 290 | 0 | | cmd.createArgument().setValue("-out"); |
| 291 | 0 | | cmd.createArgument().setFile(mOut); |
| 292 | | | |
| 293 | 0 | | cmd.createArgument().setValue("-projectName"); |
| 294 | 0 | | cmd.createArgument().setValue(mName); |
| 295 | | | |
| 296 | 0 | | cmd.createArgument().setValue("-format"); |
| 297 | 0 | (7) | cmd.createArgument().setValue(mOutputFormat.toString()); |
| 298 | | | |
| 299 | 0 | | cmd.createArgument().setValue("-loglevel"); |
| 300 | 0 | | if (mDebug) |
| 301 | | | { |
| 302 | 0 | | cmd.createArgument().setValue("ALL"); |
| 303 | | | } |
| 304 | | | else |
| 305 | | | { |
| 306 | 0 | | cmd.createArgument().setValue("INFO"); |
| 307 | | | } |
| 308 | | | |
| 309 | 0 | | if (mFilter != null) |
| 310 | | | { |
| 311 | 0 | | cmd.createArgument().setValue("-filter"); |
| 312 | 0 | | cmd.createArgument().setFile(mFilter); |
| 313 | | | } |
| 314 | | | |
| 315 | 0 | | for (final Iterator iterator = mSourceDirectories.iterator(); |
| 316 | 0 | | iterator.hasNext();) |
| 317 | | | { |
| 318 | 0 | | final SourceDirectory sourceDir = (SourceDirectory) iterator.next(); |
| 319 | 0 | | cmd.createArgument().setValue("-srcDir"); |
| 320 | 0 | | cmd.createArgument().setPath( |
| 321 | | | new Path(getProject(), sourceDir.getDir())); |
| 322 | 0 | | } |
| 323 | | | |
| 324 | 0 | | for (final Iterator iterator = mReportFiles.iterator(); |
| 325 | 0 | | iterator.hasNext();) |
| 326 | | | { |
| 327 | 0 | | final Report reportFile = (Report) iterator.next(); |
| 328 | 0 | | if (reportFile.testIfCondition()) |
| 329 | | | { |
| 330 | 0 | | cmd.createArgument().setValue("-" + reportFile.getFormat()); |
| 331 | 0 | | cmd.createArgument().setFile(reportFile.getFile()); |
| 332 | | | } |
| 333 | 0 | | } |
| 334 | | | |
| 335 | 0 | | return cmd; |
| 336 | | | } |
| 337 | | | |
| 338 | | | private int executeAsForked () |
| 339 | | | throws BuildException |
| 340 | | | { |
| 341 | 0 | | final Execute execute |
| 342 | | | = new Execute(new LogStreamHandler( |
| 343 | | | this, Project.MSG_INFO, Project.MSG_WARN)); |
| 344 | | | |
| 345 | 0 | | final CommandlineJava cmd = createCommandline(); |
| 346 | 0 | | execute.setCommandline(cmd.getCommandline()); |
| 347 | | | |
| 348 | 0 | | if (mWorkingDir != null) |
| 349 | | | { |
| 350 | 0 | | log("Setting working directory to : " |
| 351 | | | + mWorkingDir, Project.MSG_VERBOSE); |
| 352 | 0 | | execute.setWorkingDirectory(mWorkingDir); |
| 353 | 0 | | execute.setAntRun(getProject()); |
| 354 | | | } |
| 355 | | | |
| 356 | 0 | | final Path classpath = mCommandline.getClasspath(); |
| 357 | 0 | | if (classpath != null) |
| 358 | | | { |
| 359 | 0 | | log("Using CLASSPATH " + classpath, Project.MSG_VERBOSE); |
| 360 | | | } |
| 361 | | | |
| 362 | 0 | | log("Executing: [fork] " + cmd.toString(), Project.MSG_VERBOSE); |
| 363 | | | |
| 364 | | | try |
| 365 | | | { |
| 366 | 0 | | return execute.execute(); |
| 367 | | | } |
| 368 | 0 | | catch (IOException e) |
| 369 | | | { |
| 370 | 0 | | throw new BuildException("Process fork failed.", e, getLocation()); |
| 371 | | | } |
| 372 | | | } |
| 373 | | | |
| 374 | | | private void checkParameters () |
| 375 | | | { |
| 376 | 0 | (8) | if (mSourceDirectories.size() == 0) |
| 377 | | | { |
| 378 | 0 | | throw new BuildException( |
| 379 | | | "at least one srcdir element must be specified!", getLocation()); |
| 380 | | | } |
| 381 | 0 | | } |
| 382 | | | |
| 383 | | | |
| 384 | 0 | | public final class Report |
| 385 | | | { |
| 386 | | | private ReportFormat mReportFormat; |
| 387 | | | private File mReportFilename; |
| 388 | 0 | | private String mIfCondition = ""; |
| 389 | | | |
| 390 | | | |
| 391 | | | |
| 392 | | | |
| 393 | | | @return |
| 394 | | | |
| 395 | | | public File getFile () |
| 396 | | | { |
| 397 | 0 | | return mReportFilename; |
| 398 | | | } |
| 399 | | | |
| 400 | | | |
| 401 | | | |
| 402 | | | |
| 403 | | | @param |
| 404 | | | |
| 405 | | | public void setFile (File reportFilename) |
| 406 | | | { |
| 407 | 0 | | mReportFilename = reportFilename; |
| 408 | 0 | | } |
| 409 | | | |
| 410 | | | |
| 411 | | | |
| 412 | | | |
| 413 | | | @return |
| 414 | | | |
| 415 | | | public ReportFormat getFormat () |
| 416 | | | { |
| 417 | 0 | | return mReportFormat; |
| 418 | | | } |
| 419 | | | |
| 420 | | | |
| 421 | | | |
| 422 | | | |
| 423 | | | @param |
| 424 | | | |
| 425 | | | public void setFormat (String reportFormat) |
| 426 | | | { |
| 427 | 0 | | mReportFormat = ReportFormat.fromString(reportFormat); |
| 428 | 0 | | } |
| 429 | | | |
| 430 | | | |
| 431 | | | <code></code> |
| 432 | | | |
| 433 | | | @param |
| 434 | | | |
| 435 | | | public void setif (String property) |
| 436 | | | { |
| 437 | 0 | | mIfCondition = (property == null) ? "" : property; |
| 438 | 0 | | } |
| 439 | | | |
| 440 | | | |
| 441 | | | |
| 442 | | | |
| 443 | | | @return |
| 444 | | | |
| 445 | | | <code></code> |
| 446 | | | |
| 447 | | | private boolean testIfCondition () |
| 448 | | | { |
| 449 | | | final boolean result; |
| 450 | 0 | | if ("".equals(mIfCondition)) |
| 451 | | | { |
| 452 | 0 | | result = true; |
| 453 | | | } |
| 454 | | | else |
| 455 | | | { |
| 456 | 0 | | final String test = getProject().replaceProperties(mIfCondition); |
| 457 | 0 | | result = getProject().getProperty(test) != null; |
| 458 | | | } |
| 459 | 0 | | return result; |
| 460 | | | } |
| 461 | | | } |
| 462 | | | |
| 463 | 0 | | public static final class SourceDirectory |
| 464 | | | { |
| 465 | | | private String mSourceDir; |
| 466 | | | |
| 467 | | | |
| 468 | | | |
| 469 | | | |
| 470 | | | @return |
| 471 | | | |
| 472 | | (9) | public final String getDir () |
| 473 | | | { |
| 474 | 0 | | return mSourceDir; |
| 475 | | | } |
| 476 | | | |
| 477 | | | |
| 478 | | | |
| 479 | | | |
| 480 | | | @param |
| 481 | | | |
| 482 | | (10) | public final void setDir (String sourceDir) |
| 483 | | | { |
| 484 | 0 | | mSourceDir = sourceDir; |
| 485 | 0 | | } |
| 486 | | | } |
| 487 | | | } |