2010-11-15 20:35:36 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
2013-12-10 19:36:50 +00:00
|
|
|
* (C) Copyright 2009-2013, Arnaud Roques
|
2010-11-15 20:35:36 +00:00
|
|
|
*
|
|
|
|
* Project Info: http://plantuml.sourceforge.net
|
|
|
|
*
|
|
|
|
* This file is part of PlantUML.
|
|
|
|
*
|
|
|
|
* PlantUML is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PlantUML distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
2013-12-10 19:36:50 +00:00
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
2010-11-15 20:35:36 +00:00
|
|
|
* License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
|
|
|
|
* in the United States and other countries.]
|
|
|
|
*
|
|
|
|
* Original Author: Arnaud Roques
|
|
|
|
*
|
2013-12-10 19:36:50 +00:00
|
|
|
* Revision $Revision: 11786 $
|
2010-11-15 20:35:36 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
package net.sourceforge.plantuml.ant;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
2011-08-08 17:48:29 +00:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2010-11-15 20:35:36 +00:00
|
|
|
|
|
|
|
import net.sourceforge.plantuml.FileFormat;
|
|
|
|
import net.sourceforge.plantuml.GeneratedImage;
|
|
|
|
import net.sourceforge.plantuml.Option;
|
|
|
|
import net.sourceforge.plantuml.OptionFlags;
|
|
|
|
import net.sourceforge.plantuml.SourceFileReader;
|
|
|
|
import net.sourceforge.plantuml.preproc.Defines;
|
|
|
|
|
|
|
|
import org.apache.tools.ant.BuildException;
|
|
|
|
import org.apache.tools.ant.DirectoryScanner;
|
|
|
|
import org.apache.tools.ant.Task;
|
|
|
|
import org.apache.tools.ant.types.FileList;
|
|
|
|
import org.apache.tools.ant.types.FileSet;
|
|
|
|
|
|
|
|
// <?xml version="1.0"?>
|
|
|
|
//
|
|
|
|
// <project name="OwnTaskExample" default="main" basedir=".">
|
|
|
|
// <taskdef name="plot" classname="plot.PlotTask" classpath="build"/>
|
|
|
|
//
|
|
|
|
// <target name="main">
|
|
|
|
// <mytask message="Hello World! MyVeryOwnTask works!"/>
|
|
|
|
// </target>
|
|
|
|
// </project>
|
|
|
|
|
|
|
|
// Carriage Return in UTF-8 XML:
|
|
|
|
// Line Feed in UTF-8 XML:
|
|
|
|
public class PlantUmlTask extends Task {
|
|
|
|
|
|
|
|
private String dir = null;
|
|
|
|
private final Option option = new Option();
|
|
|
|
private List<FileSet> filesets = new ArrayList<FileSet>();
|
|
|
|
private List<FileList> filelists = new ArrayList<FileList>();
|
2011-08-08 17:48:29 +00:00
|
|
|
private AtomicInteger nbFiles = new AtomicInteger(0);
|
|
|
|
private ExecutorService executorService;
|
2010-11-15 20:35:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a set of files to touch
|
|
|
|
*/
|
|
|
|
public void addFileset(FileSet set) {
|
|
|
|
filesets.add(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a filelist to touch
|
|
|
|
*/
|
|
|
|
public void addFilelist(FileList list) {
|
|
|
|
filelists.add(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The method executing the task
|
|
|
|
@Override
|
|
|
|
public void execute() throws BuildException {
|
|
|
|
|
|
|
|
this.log("Starting PlantUML");
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (dir != null) {
|
2011-08-08 17:48:29 +00:00
|
|
|
final File error = processingSingleDirectory(new File(dir));
|
|
|
|
checkError(error);
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|
|
|
|
for (FileSet fileSet : filesets) {
|
2011-08-08 17:48:29 +00:00
|
|
|
final File error = manageFileSet(fileSet);
|
|
|
|
checkError(error);
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|
|
|
|
for (FileList fileList : filelists) {
|
2011-08-08 17:48:29 +00:00
|
|
|
final File error = manageFileList(fileList);
|
|
|
|
checkError(error);
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|
2011-08-08 17:48:29 +00:00
|
|
|
if (executorService != null) {
|
|
|
|
executorService.shutdown();
|
|
|
|
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
|
|
|
|
}
|
|
|
|
this.log("Nb images generated: " + nbFiles.get());
|
2010-11-15 20:35:36 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new BuildException(e.toString());
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new BuildException(e.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-08-08 17:48:29 +00:00
|
|
|
private void checkError(final File error) throws IOException {
|
|
|
|
if (error != null && OptionFlags.getInstance().isFailOnError()) {
|
|
|
|
this.log("Error in file " + error.getCanonicalPath());
|
|
|
|
throw new BuildException("Error in file " + error.getCanonicalPath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private File manageFileList(FileList fl) throws IOException, InterruptedException {
|
2010-11-15 20:35:36 +00:00
|
|
|
final File fromDir = fl.getDir(getProject());
|
|
|
|
|
|
|
|
final String[] srcFiles = fl.getFiles(getProject());
|
|
|
|
|
|
|
|
for (String src : srcFiles) {
|
|
|
|
final File f = new File(fromDir, src);
|
2011-08-08 17:48:29 +00:00
|
|
|
final boolean error = processingSingleFile(f);
|
|
|
|
if (error) {
|
|
|
|
return f;
|
|
|
|
}
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|
2011-08-08 17:48:29 +00:00
|
|
|
return null;
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|
|
|
|
|
2011-08-08 17:48:29 +00:00
|
|
|
private File manageFileSet(FileSet fs) throws IOException, InterruptedException {
|
2010-11-15 20:35:36 +00:00
|
|
|
final DirectoryScanner ds = fs.getDirectoryScanner(getProject());
|
|
|
|
final File fromDir = fs.getDir(getProject());
|
|
|
|
|
|
|
|
final String[] srcFiles = ds.getIncludedFiles();
|
|
|
|
final String[] srcDirs = ds.getIncludedDirectories();
|
|
|
|
|
|
|
|
for (String src : srcFiles) {
|
|
|
|
final File f = new File(fromDir, src);
|
2011-08-08 17:48:29 +00:00
|
|
|
final boolean error = processingSingleFile(f);
|
|
|
|
if (error) {
|
|
|
|
return f;
|
|
|
|
}
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (String src : srcDirs) {
|
|
|
|
final File dir = new File(fromDir, src);
|
2011-08-08 17:48:29 +00:00
|
|
|
final File errorFile = processingSingleDirectory(dir);
|
|
|
|
if (errorFile != null) {
|
|
|
|
return errorFile;
|
|
|
|
}
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|
2011-08-08 17:48:29 +00:00
|
|
|
return null;
|
2010-11-15 20:35:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-08-08 17:48:29 +00:00
|
|
|
private boolean processingSingleFile(final File f) throws IOException, InterruptedException {
|
|
|
|
if (OptionFlags.getInstance().isVerbose()) {
|
|
|
|
this.log("Processing " + f.getAbsolutePath());
|
|
|
|
}
|
2010-11-15 20:35:36 +00:00
|
|
|
final SourceFileReader sourceFileReader = new SourceFileReader(new Defines(), f, option.getOutputDir(), option
|
2011-01-05 18:23:06 +00:00
|
|
|
.getConfig(), option.getCharset(), option.getFileFormatOption());
|
2011-08-08 17:48:29 +00:00
|
|
|
|
|
|
|
if (option.isCheckOnly()) {
|
|
|
|
return sourceFileReader.hasError();
|
|
|
|
}
|
|
|
|
if (executorService == null) {
|
|
|
|
return doFile(f, sourceFileReader);
|
|
|
|
}
|
|
|
|
|
|
|
|
executorService.submit(new Callable<Boolean>() {
|
|
|
|
public Boolean call() throws Exception {
|
|
|
|
return doFile(f, sourceFileReader);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean doFile(final File f, final SourceFileReader sourceFileReader) throws IOException,
|
|
|
|
InterruptedException {
|
2010-11-15 20:35:36 +00:00
|
|
|
final Collection<GeneratedImage> result = sourceFileReader.getGeneratedImages();
|
2011-08-08 17:48:29 +00:00
|
|
|
boolean error = false;
|
2010-11-15 20:35:36 +00:00
|
|
|
for (GeneratedImage g : result) {
|
2011-08-08 17:48:29 +00:00
|
|
|
if (OptionFlags.getInstance().isVerbose()) {
|
|
|
|
myLog(g + " " + g.getDescription());
|
|
|
|
}
|
|
|
|
nbFiles.addAndGet(1);
|
|
|
|
if (g.isError()) {
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
myLog("Error: " + f.getCanonicalPath());
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|
2011-08-08 17:48:29 +00:00
|
|
|
if (error && OptionFlags.getInstance().isFailOnError()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized void myLog(String s) {
|
|
|
|
this.log(s);
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|
|
|
|
|
2011-08-08 17:48:29 +00:00
|
|
|
private File processingSingleDirectory(File dir) throws IOException, InterruptedException {
|
|
|
|
if (dir.exists() == false) {
|
|
|
|
final String s = "The file " + dir.getAbsolutePath() + " does not exists.";
|
2010-11-15 20:35:36 +00:00
|
|
|
this.log(s);
|
|
|
|
throw new BuildException(s);
|
|
|
|
}
|
2011-08-08 17:48:29 +00:00
|
|
|
for (File f : dir.listFiles()) {
|
|
|
|
if (f.isFile() == false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (fileToProcess(f.getName()) == false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final boolean error = processingSingleFile(f);
|
|
|
|
if (error) {
|
|
|
|
return f;
|
|
|
|
}
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|
2011-08-08 17:48:29 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean fileToProcess(String name) {
|
|
|
|
return name.matches(Option.getPattern());
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setDir(String s) {
|
|
|
|
this.dir = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOutput(String s) {
|
|
|
|
option.setOutputDir(new File(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setCharset(String s) {
|
|
|
|
option.setCharset(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setConfig(String s) {
|
|
|
|
try {
|
|
|
|
option.initConfig(s);
|
|
|
|
} catch (IOException e) {
|
|
|
|
log("Error reading " + s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setKeepTmpFiles(String s) {
|
|
|
|
if ("true".equalsIgnoreCase(s)) {
|
|
|
|
OptionFlags.getInstance().setKeepTmpFiles(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setVerbose(String s) {
|
|
|
|
if ("true".equalsIgnoreCase(s)) {
|
|
|
|
OptionFlags.getInstance().setVerbose(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFormat(String s) {
|
2011-01-05 18:23:06 +00:00
|
|
|
if ("xmi".equalsIgnoreCase(s)) {
|
|
|
|
option.setFileFormat(FileFormat.XMI_STANDARD);
|
|
|
|
}
|
|
|
|
if ("xmi:argo".equalsIgnoreCase(s)) {
|
|
|
|
option.setFileFormat(FileFormat.XMI_ARGO);
|
|
|
|
}
|
|
|
|
if ("xmi:start".equalsIgnoreCase(s)) {
|
|
|
|
option.setFileFormat(FileFormat.XMI_STAR);
|
|
|
|
}
|
2010-11-15 20:35:36 +00:00
|
|
|
if ("eps".equalsIgnoreCase(s)) {
|
|
|
|
option.setFileFormat(FileFormat.EPS);
|
|
|
|
}
|
2011-08-08 17:48:29 +00:00
|
|
|
if ("pdf".equalsIgnoreCase(s)) {
|
|
|
|
option.setFileFormat(FileFormat.PDF);
|
|
|
|
}
|
|
|
|
if ("eps:text".equalsIgnoreCase(s)) {
|
|
|
|
option.setFileFormat(FileFormat.EPS_TEXT);
|
|
|
|
}
|
2010-11-15 20:35:36 +00:00
|
|
|
if ("svg".equalsIgnoreCase(s)) {
|
|
|
|
option.setFileFormat(FileFormat.SVG);
|
|
|
|
}
|
|
|
|
if ("txt".equalsIgnoreCase(s)) {
|
|
|
|
option.setFileFormat(FileFormat.ATXT);
|
|
|
|
}
|
|
|
|
if ("utxt".equalsIgnoreCase(s)) {
|
|
|
|
option.setFileFormat(FileFormat.UTXT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setGraphvizDot(String s) {
|
|
|
|
OptionFlags.getInstance().setDotExecutable(s);
|
|
|
|
}
|
|
|
|
|
2011-08-08 17:48:29 +00:00
|
|
|
public void setNbThread(String s) {
|
|
|
|
if (s != null && s.matches("\\d+")) {
|
|
|
|
option.setNbThreads(Integer.parseInt(s));
|
|
|
|
final int nbThreads = option.getNbThreads();
|
|
|
|
this.executorService = Executors.newFixedThreadPool(nbThreads);
|
|
|
|
}
|
|
|
|
if ("auto".equalsIgnoreCase(s)) {
|
|
|
|
option.setNbThreads(Option.defaultNbThreads());
|
|
|
|
final int nbThreads = option.getNbThreads();
|
|
|
|
this.executorService = Executors.newFixedThreadPool(nbThreads);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setNbThreads(String s) {
|
|
|
|
setNbThread(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSuggestEngine(String s) {
|
|
|
|
OptionFlags.getInstance().setUseSuggestEngine("true".equalsIgnoreCase(s) || "yes".equalsIgnoreCase(s) || "on".equalsIgnoreCase(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFailOnError(String s) {
|
|
|
|
OptionFlags.getInstance().setFailOnError("true".equalsIgnoreCase(s) || "yes".equalsIgnoreCase(s) || "on".equalsIgnoreCase(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setCheckOnly(String s) {
|
|
|
|
final boolean flag = "true".equalsIgnoreCase(s) || "yes".equalsIgnoreCase(s) || "on".equalsIgnoreCase(s);
|
|
|
|
option.setCheckOnly(true);
|
|
|
|
OptionFlags.getInstance().setFailOnError(flag);
|
|
|
|
}
|
|
|
|
|
2013-12-10 19:36:50 +00:00
|
|
|
public void setOverwrite(String s) {
|
|
|
|
final boolean flag = "true".equalsIgnoreCase(s) || "yes".equalsIgnoreCase(s) || "on".equalsIgnoreCase(s);
|
|
|
|
OptionFlags.getInstance().setOverwrite(flag);
|
|
|
|
}
|
|
|
|
|
2010-11-15 20:35:36 +00:00
|
|
|
}
|