| 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.commons.taskdefs; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.FileInputStream; |
| 37 | | | import java.io.IOException; |
| 38 | | | import java.util.ArrayList; |
| 39 | | | import java.util.Collections; |
| 40 | | | import java.util.Iterator; |
| 41 | | | import java.util.List; |
| 42 | | | |
| 43 | | | import javax.xml.parsers.SAXParser; |
| 44 | | | import javax.xml.parsers.SAXParserFactory; |
| 45 | | | |
| 46 | | | import org.apache.tools.ant.BuildException; |
| 47 | | | import org.apache.tools.ant.Project; |
| 48 | | | import org.apache.tools.ant.Task; |
| 49 | | | import org.apache.tools.ant.taskdefs.Javadoc; |
| 50 | | | import org.apache.tools.ant.taskdefs.Javadoc.DocletInfo; |
| 51 | | | import org.apache.tools.ant.taskdefs.Javadoc.DocletParam; |
| 52 | | | import org.apache.tools.ant.types.FileSet; |
| 53 | | | import org.apache.tools.ant.types.Path; |
| 54 | | | import org.apache.tools.ant.types.PatternSet.NameEntry; |
| 55 | | | import org.jcoderz.phoenix.sqlparser.SqlToXml; |
| 56 | | | import org.xml.sax.Attributes; |
| 57 | | | import org.xml.sax.InputSource; |
| 58 | | | import org.xml.sax.helpers.DefaultHandler; |
| 59 | | | |
| 60 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | | @author |
| 64 | | | |
| 65 | 0 | | public class DiagramTask |
| 66 | | | extends Task |
| 67 | | | { |
| 68 | | | |
| 69 | | | public static final String NAME = "diagram"; |
| 70 | | | |
| 71 | | | private static final String DOTTY_EXTENSION = ".dot"; |
| 72 | | | |
| 73 | | | private static final String JAVA_EXTENSION = ".java"; |
| 74 | | | |
| 75 | | | private static final String DEFAULT_FONTNAME = "verdana"; |
| 76 | | | |
| 77 | | | private static final String DEFAULT_FONTSIZE = "7"; |
| 78 | | | |
| 79 | | | |
| 80 | | | private File mOutDir; |
| 81 | | | |
| 82 | | | private File mInFile; |
| 83 | | | |
| 84 | | | private boolean mFailOnError; |
| 85 | | | |
| 86 | | | private Path mDocletPath; |
| 87 | | | |
| 88 | 0 | | private final List mSources = new ArrayList(); |
| 89 | | | |
| 90 | | | |
| 91 | | | |
| 92 | | | @param |
| 93 | | | |
| 94 | | | public void setIn (File f) |
| 95 | | | { |
| 96 | 0 | | mInFile = f; |
| 97 | 0 | | } |
| 98 | | | |
| 99 | | | |
| 100 | | | |
| 101 | | | |
| 102 | | | @param |
| 103 | | | |
| 104 | | | public void setOut (File dir) |
| 105 | | | { |
| 106 | 0 | | mOutDir = dir; |
| 107 | 0 | | } |
| 108 | | | |
| 109 | | | |
| 110 | | | |
| 111 | | | @param |
| 112 | | | |
| 113 | | | public void setType (String type) |
| 114 | | | { |
| 115 | | (1) | |
| 116 | 0 | | } |
| 117 | | | |
| 118 | | | |
| 119 | | | |
| 120 | | | @param |
| 121 | | | |
| 122 | | | public void setFailonerror (boolean b) |
| 123 | | | { |
| 124 | 0 | | mFailOnError = b; |
| 125 | 0 | | } |
| 126 | | | |
| 127 | | | |
| 128 | | | |
| 129 | | | @param |
| 130 | | | |
| 131 | | | |
| 132 | | | public void addSrc (SourceDirectory src) |
| 133 | | | { |
| 134 | 0 | | mSources.add(src); |
| 135 | 0 | | } |
| 136 | | | |
| 137 | | | |
| 138 | | | |
| 139 | | | @param |
| 140 | | | |
| 141 | | | |
| 142 | | | public void setDocletPath (Path path) |
| 143 | | | { |
| 144 | 0 | | if (mDocletPath == null) |
| 145 | | | { |
| 146 | 0 | | mDocletPath = path; |
| 147 | | | } |
| 148 | | | else |
| 149 | | | { |
| 150 | 0 | | mDocletPath.add(path); |
| 151 | | | } |
| 152 | 0 | | } |
| 153 | | | |
| 154 | | | |
| 155 | | | |
| 156 | | | |
| 157 | | | @throws |
| 158 | | | |
| 159 | | | public void execute () |
| 160 | | | throws BuildException |
| 161 | | | { |
| 162 | | | try |
| 163 | | | { |
| 164 | 0 | | checkAttributes(); |
| 165 | | | |
| 166 | 0 | | final DiagramSaxHandler handler = parse(); |
| 167 | 0 | | log("Diagrams: " + handler.diagrams().toString(), Project.MSG_DEBUG); |
| 168 | | | |
| 169 | 0 | | final Iterator iterator = handler.diagrams().iterator(); |
| 170 | 0 | | while (iterator.hasNext()) |
| 171 | | | { |
| 172 | 0 | | final Diagram diagram = (Diagram) iterator.next(); |
| 173 | 0 | | if ("class".equals(diagram.getType())) |
| 174 | | | { |
| 175 | 0 | | generateUmlDiagram(diagram); |
| 176 | | | } |
| 177 | 0 | | else if ("ER".equals(diagram.getType())) |
| 178 | | | { |
| 179 | 0 | | generateEntityRelationshipDiagram(diagram); |
| 180 | | | } |
| 181 | 0 | | } |
| 182 | 0 | | generateStateDiagram(); |
| 183 | 0 | | AntTaskUtil.renderDotFiles(this, mOutDir, mFailOnError); |
| 184 | | | } |
| 185 | 0 | | catch (BuildException e) |
| 186 | | | { |
| 187 | 0 | | if (mFailOnError) |
| 188 | | | { |
| 189 | 0 | | throw e; |
| 190 | | | } |
| 191 | 0 | | log(e.getMessage(), Project.MSG_ERR); |
| 192 | 0 | | } |
| 193 | 0 | | } |
| 194 | | | |
| 195 | | | |
| 196 | | | private void generateEntityRelationshipDiagram (Diagram diagram) |
| 197 | | | { |
| 198 | 0 | | final File inFile = new File( |
| 199 | | | getProject().getBaseDir(), diagram.getFile()); |
| 200 | | | final File tmpFile; |
| 201 | | | try |
| 202 | | | { |
| 203 | 0 | | tmpFile = File.createTempFile("xdoc", ".tmp"); |
| 204 | | | } |
| 205 | 0 | | catch (IOException e) |
| 206 | | | { |
| 207 | 0 | | throw new BuildException("Failed to create temp file: " + e, e); |
| 208 | 0 | | } |
| 209 | 0 | | final SqlToXml sqlToXml |
| 210 | | | = new SqlToXml(inFile.getAbsolutePath(), tmpFile.getAbsolutePath()); |
| 211 | | | try |
| 212 | | | { |
| 213 | 0 | | sqlToXml.transformSqlToXml(); |
| 214 | | | } |
| 215 | 0 | | catch (Exception e) |
| 216 | | | { |
| 217 | 0 | | throw new BuildException("Failed to transform SQL '" + inFile |
| 218 | | | + "' file to XML: " + e, e); |
| 219 | 0 | | } |
| 220 | | | |
| 221 | 0 | | final File outFile = new File(mOutDir, diagram.getName() + ".dot"); |
| 222 | 0 | | final XsltBasedTask t = new XsltBasedTask() |
| 223 | 0 | (2) | { |
| 224 | | | String getDefaultStyleSheet () |
| 225 | | | { |
| 226 | 0 | | return "generate-er-diagram.xsl"; |
| 227 | | | } |
| 228 | | | }; |
| 229 | 0 | | t.setProject(getProject()); |
| 230 | 0 | | t.setTaskName("xml2dot"); |
| 231 | 0 | | t.setFailonerror(mFailOnError); |
| 232 | 0 | | t.setIn(tmpFile); |
| 233 | 0 | | t.setForce(true); |
| 234 | 0 | | t.setDestdir(mOutDir); |
| 235 | 0 | | t.setOut(outFile); |
| 236 | 0 | | log("Generating ER diagram " + outFile, Project.MSG_VERBOSE); |
| 237 | 0 | | t.execute(); |
| 238 | 0 | | } |
| 239 | | | |
| 240 | | | private void generateStateDiagram () |
| 241 | | | { |
| 242 | 0 | | final XsltBasedTask t = new XsltBasedTask() |
| 243 | 0 | (3) | { |
| 244 | | | String getDefaultStyleSheet () |
| 245 | | | { |
| 246 | 0 | | return "generate-state-diagram.xsl"; |
| 247 | | | } |
| 248 | | | }; |
| 249 | 0 | | t.setProject(getProject()); |
| 250 | 0 | | t.setTaskName("xml2dot"); |
| 251 | 0 | | t.setFailonerror(mFailOnError); |
| 252 | 0 | | t.setIn(mInFile); |
| 253 | 0 | | t.setForce(true); |
| 254 | 0 | | t.setDestdir(mOutDir); |
| 255 | | | try |
| 256 | | | { |
| 257 | 0 | | t.setOut(File.createTempFile("xdoc", ".tmp")); |
| 258 | | | } |
| 259 | 0 | | catch (IOException e) |
| 260 | | | { |
| 261 | 0 | | throw new BuildException("Cannot create temp file: " + e, e); |
| 262 | 0 | | } |
| 263 | 0 | | log("Generating state diagrams from file " |
| 264 | | | + mInFile, Project.MSG_VERBOSE); |
| 265 | 0 | | t.execute(); |
| 266 | 0 | | } |
| 267 | | | |
| 268 | | | |
| 269 | | | |
| 270 | | | |
| 271 | | | |
| 272 | | | |
| 273 | | | |
| 274 | | | |
| 275 | | | |
| 276 | | | |
| 277 | | | |
| 278 | | | |
| 279 | | | |
| 280 | | | |
| 281 | | | |
| 282 | | | |
| 283 | | | |
| 284 | | | |
| 285 | | | |
| 286 | | | |
| 287 | | | |
| 288 | | | |
| 289 | | | |
| 290 | | | |
| 291 | | | |
| 292 | | | |
| 293 | | | |
| 294 | | | |
| 295 | | | |
| 296 | | | |
| 297 | | | |
| 298 | | | |
| 299 | | | |
| 300 | | | |
| 301 | | | |
| 302 | | | |
| 303 | | | |
| 304 | | | |
| 305 | | | |
| 306 | | | private void generateUmlDiagram (final Diagram diagram) |
| 307 | | | { |
| 308 | 0 | | final Javadoc javadocTask = new Javadoc(); |
| 309 | 0 | | javadocTask.setProject(getProject()); |
| 310 | 0 | | javadocTask.setFailonerror(mFailOnError); |
| 311 | 0 | | javadocTask.setTaskName("umlgraph"); |
| 312 | 0 | | javadocTask.setPackage(true); |
| 313 | 0 | | javadocTask.setClasspath(mDocletPath); |
| 314 | 0 | | javadocTask.setClasspath(Path.systemClasspath); |
| 315 | 0 | | for (final Iterator i = mSources.iterator(); i.hasNext();) |
| 316 | | | { |
| 317 | 0 | | final SourceDirectory fs = (SourceDirectory) i.next(); |
| 318 | 0 | | javadocTask.addFileset(addClasses(diagram, fs.getDir())); |
| 319 | 0 | | } |
| 320 | 0 | | final DocletInfo info = javadocTask.createDoclet(); |
| 321 | 0 | | info.setProject(getProject()); |
| 322 | 0 | | info.setName("UmlGraph"); |
| 323 | 0 | | info.setPath(mDocletPath); |
| 324 | 0 | | addDocletParam(info, "-operations"); |
| 325 | 0 | | addDocletParam(info, "-visibility"); |
| 326 | 0 | | addDocletParam(info, "-types"); |
| 327 | 0 | | addDocletParam(info, "-noguillemot"); |
| 328 | 0 | | addDocletParam(info, "-nodefontname", DEFAULT_FONTNAME); |
| 329 | 0 | | addDocletParam(info, "-nodefontsize", DEFAULT_FONTSIZE); |
| 330 | 0 | | addDocletParam(info, "-nodefontabstractname", DEFAULT_FONTNAME); |
| 331 | 0 | | addDocletParam(info, "-edgefontname", DEFAULT_FONTNAME); |
| 332 | 0 | | addDocletParam(info, "-edgefontsize", DEFAULT_FONTSIZE); |
| 333 | 0 | | final File dotFile |
| 334 | | | = new File(mOutDir, diagram.getName() + DOTTY_EXTENSION); |
| 335 | 0 | (4) | dotFile.getParentFile().mkdirs(); |
| 336 | 0 | | addDocletParam(info, "-output", dotFile.getAbsolutePath()); |
| 337 | 0 | | javadocTask.execute(); |
| 338 | 0 | | } |
| 339 | | | |
| 340 | | | |
| 341 | | | private FileSet addClasses (final Diagram diagram, File path) |
| 342 | | | { |
| 343 | 0 | | final FileSet filez = new FileSet(); |
| 344 | 0 | | filez.setProject(getProject()); |
| 345 | 0 | | filez.setDir(path); |
| 346 | 0 | | final Iterator i = diagram.classList().iterator(); |
| 347 | 0 | | while (i.hasNext()) |
| 348 | | | { |
| 349 | 0 | | final String name = (String) i.next(); |
| 350 | 0 | | final NameEntry entry = filez.createInclude(); |
| 351 | 0 | | final String pathName = name.replaceAll("\\.", "/") + JAVA_EXTENSION; |
| 352 | 0 | | log("Adding Source file " + pathName, Project.MSG_VERBOSE); |
| 353 | 0 | | entry.setName(pathName); |
| 354 | 0 | | } |
| 355 | 0 | | return filez; |
| 356 | | | } |
| 357 | | | |
| 358 | | | private void addDocletParam (DocletInfo info, String key) |
| 359 | | | { |
| 360 | 0 | | final DocletParam param = info.createParam(); |
| 361 | 0 | | param.setName(key); |
| 362 | 0 | | } |
| 363 | | | |
| 364 | | | private void addDocletParam (DocletInfo info, String key, String value) |
| 365 | | | { |
| 366 | 0 | | final DocletParam param = info.createParam(); |
| 367 | 0 | | param.setName(key); |
| 368 | 0 | | param.setValue(value); |
| 369 | 0 | | } |
| 370 | | | |
| 371 | | | private DiagramSaxHandler parse () |
| 372 | | | { |
| 373 | 0 | (5) | final DiagramSaxHandler handler = new DiagramSaxHandler(); |
| 374 | | | try |
| 375 | | | { |
| 376 | | | |
| 377 | 0 | | final SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 378 | 0 | | factory.setNamespaceAware(true); |
| 379 | 0 | | factory.setValidating(true); |
| 380 | 0 | | final SAXParser parser = factory.newSAXParser(); |
| 381 | | | |
| 382 | | | |
| 383 | | | |
| 384 | | | |
| 385 | | | |
| 386 | 0 | | parser.parse(new InputSource(new FileInputStream(mInFile)), handler); |
| 387 | 0 | | log(mInFile + " parsed successfully.", Project.MSG_INFO); |
| 388 | | | } |
| 389 | 0 | | catch (Exception e) |
| 390 | | | { |
| 391 | 0 | | throw new BuildException("Failed to parse " + mInFile + ": " + e, e); |
| 392 | 0 | | } |
| 393 | 0 | | return handler; |
| 394 | | | } |
| 395 | | | |
| 396 | | | |
| 397 | | | |
| 398 | | | @throws |
| 399 | | | |
| 400 | | | private void checkAttributes () |
| 401 | | | throws BuildException |
| 402 | | | { |
| 403 | 0 | | checkAttributeInFile(); |
| 404 | 0 | | } |
| 405 | | | |
| 406 | | | private void checkAttributeInFile () |
| 407 | | | { |
| 408 | 0 | | if (mInFile == null) |
| 409 | | | { |
| 410 | 0 | | throw new BuildException( |
| 411 | | | "Missing mandatory attribute 'in'.", getLocation()); |
| 412 | | | } |
| 413 | 0 | | if (!mInFile.exists()) |
| 414 | | | { |
| 415 | 0 | | throw new BuildException( |
| 416 | | | "Input file '" + mInFile + "' not found.", getLocation()); |
| 417 | | | } |
| 418 | 0 | | } |
| 419 | | | |
| 420 | | | |
| 421 | 0 | | private static class DiagramSaxHandler |
| 422 | | | extends DefaultHandler |
| 423 | | | { |
| 424 | 0 | | private final StringBuffer mBuffer = new StringBuffer(); |
| 425 | 0 | | private boolean mCaptureCharacters = false; |
| 426 | | | |
| 427 | 0 | | private final List mDiagrams = new ArrayList(); |
| 428 | 0 | | private Diagram mCurrentDiagram = null; |
| 429 | | | |
| 430 | | | {@inheritDoc} |
| 431 | | | public void startElement (String uri, String localName, String qName, |
| 432 | | | Attributes attributes) |
| 433 | | | { |
| 434 | 0 | | if ("diagram".equals(localName)) |
| 435 | | | { |
| 436 | 0 | | mCurrentDiagram = new Diagram( |
| 437 | | | attributes.getValue("name"), attributes.getValue("type")); |
| 438 | 0 | | if (attributes.getValue("file") != null) |
| 439 | | | { |
| 440 | 0 | | mCurrentDiagram.setFile(attributes.getValue("file")); |
| 441 | | | } |
| 442 | 0 | | mDiagrams.add(mCurrentDiagram); |
| 443 | | | } |
| 444 | 0 | | else if ("class".equals(localName) && mCurrentDiagram != null) |
| 445 | | | { |
| 446 | 0 | | mCurrentDiagram.add(attributes.getValue("name")); |
| 447 | | | } |
| 448 | 0 | | else if ("description".equals(localName) && mCurrentDiagram != null) |
| 449 | | | { |
| 450 | 0 | | captureCharacters(); |
| 451 | | | } |
| 452 | 0 | | } |
| 453 | | | |
| 454 | | | {@inheritDoc} |
| 455 | | | public void endElement (String uri, String localName, String qName) |
| 456 | | | { |
| 457 | 0 | | if ("diagram".equals(localName)) |
| 458 | | | { |
| 459 | 0 | | mCurrentDiagram = null; |
| 460 | | | } |
| 461 | 0 | | else if ("description".equals(localName) && mCurrentDiagram != null) |
| 462 | | | { |
| 463 | 0 | | mCurrentDiagram.setDescription(characters().trim()); |
| 464 | | | } |
| 465 | | | |
| 466 | 0 | | } |
| 467 | | | |
| 468 | | | {@inheritDoc} |
| 469 | | | public void characters (char[] ch, int start, int length) |
| 470 | | | { |
| 471 | 0 | | if (mCaptureCharacters) |
| 472 | | | { |
| 473 | 0 | | mBuffer.append(ch, start, length); |
| 474 | | | } |
| 475 | 0 | | } |
| 476 | | | |
| 477 | | | |
| 478 | | | void captureCharacters () |
| 479 | | | { |
| 480 | 0 | | mCaptureCharacters = true; |
| 481 | 0 | | } |
| 482 | | | |
| 483 | | | |
| 484 | | | <b></b> |
| 485 | | | |
| 486 | | | @return |
| 487 | | | |
| 488 | | | String characters () |
| 489 | | | { |
| 490 | 0 | | final String result = mBuffer.toString(); |
| 491 | 0 | | mBuffer.setLength(0); |
| 492 | 0 | | mCaptureCharacters = false; |
| 493 | 0 | | return result; |
| 494 | | | } |
| 495 | | | |
| 496 | | | |
| 497 | | | |
| 498 | | | {@link } |
| 499 | | | @return{@link } |
| 500 | | | |
| 501 | | | public List diagrams () |
| 502 | | | { |
| 503 | 0 | | return Collections.unmodifiableList(mDiagrams); |
| 504 | | | } |
| 505 | | | |
| 506 | | | } |
| 507 | | | |
| 508 | 0 | | private static class Diagram |
| 509 | | | { |
| 510 | | | private final String mName; |
| 511 | | | private final String mType; |
| 512 | 0 | | private final List mClasses = new ArrayList(); |
| 513 | 0 | | private String mDescription = ""; |
| 514 | | | private String mFile; |
| 515 | | | |
| 516 | | | Diagram (String name, String type) |
| 517 | 0 | | { |
| 518 | 0 | | mName = name; |
| 519 | 0 | | mType = type; |
| 520 | 0 | | } |
| 521 | | | |
| 522 | | | void add (String clazz) |
| 523 | | | { |
| 524 | 0 | | mClasses.add(clazz); |
| 525 | 0 | | } |
| 526 | | | |
| 527 | | | List classList () |
| 528 | | | { |
| 529 | 0 | | return Collections.unmodifiableList(mClasses); |
| 530 | | | } |
| 531 | | | |
| 532 | | | String getName () |
| 533 | | | { |
| 534 | 0 | | return mName; |
| 535 | | | } |
| 536 | | | |
| 537 | | | String getType () |
| 538 | | | { |
| 539 | 0 | | return mType; |
| 540 | | | } |
| 541 | | | |
| 542 | | | String getDescription () |
| 543 | | | { |
| 544 | 0 | | return mDescription; |
| 545 | | | } |
| 546 | | | |
| 547 | | | void setDescription (String description) |
| 548 | | | { |
| 549 | 0 | | mDescription = description; |
| 550 | 0 | | } |
| 551 | | | |
| 552 | | | String getFile () |
| 553 | | | { |
| 554 | 0 | | return mFile; |
| 555 | | | } |
| 556 | | | |
| 557 | | | void setFile (String file) |
| 558 | | | { |
| 559 | 0 | | mFile = file; |
| 560 | 0 | | } |
| 561 | | | |
| 562 | | | {@inheritDoc} |
| 563 | | | public String toString () |
| 564 | | | { |
| 565 | 0 | (6) | final StringBuffer sb = new StringBuffer(); |
| 566 | 0 | | sb.append("diagram "); |
| 567 | 0 | | sb.append(mName); |
| 568 | 0 | | sb.append(" ("); |
| 569 | 0 | | sb.append(mType); |
| 570 | 0 | | sb.append(") = "); |
| 571 | 0 | | sb.append(mClasses); |
| 572 | 0 | | sb.append(" description: '"); |
| 573 | 0 | | sb.append(mDescription); |
| 574 | 0 | | sb.append('\''); |
| 575 | 0 | | return sb.toString(); |
| 576 | | | } |
| 577 | | | } |
| 578 | | | } |