⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flowimagegenerator.java

📁 一个java写的business process management系统
💻 JAVA
字号:
/*
 * Copyright (c) 2003-2004, Alexander Greif
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Flow4J-Eclipse project nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.orthanc.flow4j.tools.ant;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Vector;

import net.orthanc.flow4j.base.IOUtils;
import net.orthanc.flow4j.model.bind.BindingHandler;
import net.orthanc.flow4j.model.bind.FlowModelBind;
import net.orthanc.flow4j.model.codegen.svg.SVGModelDigester;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;

/**
 * @author greifa
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class FlowImageGenerator extends MatchingTask {

	protected Vector filesets = new Vector();

	private File destDir;

	private Path compileClasspath;
//castor	private File mappingFile;

	private boolean jpg;
	private boolean pdf;

	/**
	 * Set the destination directory into which the flow jpg or pdf
	 * files should be generated.
	 */
	public void setDestdir(File destDir) {
		this.destDir = destDir;
	}

	/**
	 * Set the classpath to be used for jpg or pdf generation.
	 * 
	 * @param classpath an Ant Path object containing the generation classpath.
	 */
	public void setClasspath(Path classpath) {
		if (compileClasspath == null) {
			compileClasspath = classpath;
		} else {
			compileClasspath.append(classpath);
		}
	}

	/** Gets the classpath to be used for this generation. */
	public Path getClasspath() {
		return compileClasspath;
	}

	/**
	 * Adds a path to the classpath.
	 */
	public Path createClasspath() {
		if (compileClasspath == null) {
			compileClasspath = new Path(getProject());
		}
		return compileClasspath.createPath();
	}

	/**
	 * Adds a reference to a classpath defined elsewhere.
	 */
	public void setClasspathRef(Reference r) {
		createClasspath().setRefid(r);
	}

	/**
	 * The location of the castor mapping file
	 * @param mappingFile
	 */
//	public void setMappingFile(File mappingFile) {
//		this.mappingFile = mappingFile;
//	}

	/**
	 * Sets the flag whether JPEG should be created.
	 * @param b
	 */
	public void setJpg(boolean b) {
		jpg = b;
	}

	/**
	 * Sets the flag whether PDF should be created.
	 * @param b
	 */
	public void setPdf(boolean b) {
		pdf = b;
	}

	/**
	 * Adds a set of files to be deleted.
	 * @param set the set of files to be deleted
	 */
	public void addFileset(FileSet set) {
		filesets.addElement(set);
	}

	/**
	 * Executes the task.
	 */
	public void execute() throws BuildException {
		checkParameters();
//		loadCastorMapping();
		if (!destDir.exists())
			destDir.mkdirs();

		for (int i = 0; i < filesets.size(); i++) {
			FileSet fs = (FileSet) filesets.elementAt(i);
			try {
				DirectoryScanner ds = fs.getDirectoryScanner(getProject());
				String[] files = ds.getIncludedFiles();
				for (int j = 0; j < files.length; j++) {
					String relFilePath = files[j];
					File flowFile =
						new File(fs.getDir(getProject()), relFilePath);
					File relFile = new File(relFilePath);
					File relFileParent = relFile.getParentFile();
					File destFolder =
						new File(destDir, relFileParent.toString());
					try {
						if (notifyFlowStart())
							createFlowdoc(flowFile, destFolder);
					} catch (Exception e) {
						e.printStackTrace();
						log(e.getMessage(), Project.MSG_ERR);
					} finally {
						notifyFlowFinished();
					}
				}
			} catch (BuildException be) {
				// directory doesn't exist or is not readable
				log(be.getMessage(), Project.MSG_WARN);
			}
		}
	}

	/**
	 * Loads the castor maping file.
	 * @throws BuildException
	 */
//	private void loadCastorMapping() throws BuildException {
//		try {
//			//log("destination dir: " + destDir);
//			//log("Castor mapping file from: " + mappingFile);
//			FlowModelBind.loadMapping(mappingFile);
//		} catch (Exception e) {
//			e.printStackTrace();
//			throw new BuildException(
//				"Could not create FlowImageGenerator",
//				getLocation());
//		}
//	}

	/**
	 * Check that all required attributes have been set and nothing
	 * silly has been entered.
	 *
	 * @since Ant 1.5
	 */
	protected void checkParameters() throws BuildException {
/*castor		if (mappingFile == null) {
			throw new BuildException(
				"mappingFile attribute must be set!",
				getLocation());
		}
		if (!mappingFile.exists()) {
			throw new BuildException(
				"mappingFile does not exist!",
				getLocation());
		}
*/
		if (destDir != null && !destDir.isDirectory()) {
			throw new BuildException(
				"destination directory \""
					+ destDir
					+ "\" does not exist "
					+ "or is not a directory",
				getLocation());
		}
	}

	/**
	 * Creates the flow documentation of the files collected
	 * in the <code>flowFileList</code> field.
	 * 
	 */
	protected void createFlowdoc(File flowFile, File destFolder)
		throws Exception {
		log("Flow " + flowFile.getAbsolutePath());
		if (!destFolder.exists())
			destFolder.mkdirs();
//		FlowModelBind flowDiagramBind = FlowModelBind.loadFlowDiagram(flowFile);

		FlowModelBind flowModelBind = BindingHandler.getInstance().loadFlowModel(flowFile);

		//	SVG
		SVGModelDigester svgDigester =
			new SVGModelDigester(getClass().getClassLoader());
		svgDigester.digest(flowModelBind);

		boolean useCSS = true;
		byte[] SVGXMLBytes = svgDigester.getSVGXMLBytes(useCSS);
		String flowFileName =
			flowFile.getName().substring(
				0,
				flowFile.getName().lastIndexOf("."));

		if (jpg) {
			generateJPG(
				svgDigester,
				SVGXMLBytes,
				flowFileName,
				destFolder,
				useCSS);
		}
		if (pdf) {
			generatePDF(
				svgDigester,
				SVGXMLBytes,
				flowFileName,
				destFolder,
				useCSS);
		}
	}

	/**
	 * Genertes the JPEG picture and writes
	 * it to the given folder.
	 * @param digester
	 * @param SVGXMLBytes
	 * @param flowFileName
	 * @param outputFolder
	 * @param useCSS
	 * @throws Exception
	 */
	private void generateJPG(
		SVGModelDigester digester,
		byte[] SVGXMLBytes,
		String flowFileName,
		File outputFolder,
		boolean useCSS)
		throws Exception {
		FileOutputStream fout;
		InputStream in;

		fout =
			new FileOutputStream(new File(outputFolder, flowFileName + ".jpg"));
		in =
			digester.getSVGJPEGStream(
				new ByteArrayInputStream(SVGXMLBytes),
				useCSS);
		copyCloseStream(in, fout);
	}

	/**
	 * Genertes the PDF file and writes
	 * it to the given folder.
	 * @param digester
	 * @param SVGXMLBytes
	 * @param flowFileName
	 * @param outputFolder
	 * @param useCSS
	 * @throws Exception
	 */
	private void generatePDF(
		SVGModelDigester digester,
		byte[] SVGXMLBytes,
		String flowFileName,
		File outputFolder,
		boolean useCSS)
		throws Exception {
		FileOutputStream fout;
		InputStream in;

		fout =
			new FileOutputStream(new File(outputFolder, flowFileName + ".pdf"));
		in =
			digester.getSVGPDFStream(
				new ByteArrayInputStream(SVGXMLBytes),
				useCSS);
		copyCloseStream(in, fout);
	}

	/**
	 * Copies one stream to the other and closes both streams afterwards.
	 * @param in
	 * @param fout
	 * @throws IOException
	 */
	private void copyCloseStream(InputStream in, FileOutputStream fout)
		throws IOException {
			IOUtils.copyBufferedStream(in, fout);
		in.close();
		fout.close();
	}

	/**
	 * Copies one stream to the other and closes both streams afterwards.
	 * @param in
	 * @param fout
	 * @throws IOException
	 */
	private void copyCloseStream(Reader in, Writer fout) throws IOException {
		IOUtils.copyChars(in, fout);
		in.close();
		fout.close();
	}

	protected boolean notifyFlowStart() {
		return true;
	}

	protected void notifyFlowFinished() {
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -