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

📄 flowbuilder.java

📁 一个java写的business process management系统
💻 JAVA
字号:
/*
 * Copyright (c) 2003, 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.designer.builder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.orthanc.flow4j.designer.core.Flow4JPlugin;
import net.orthanc.flow4j.designer.core.JavaTaskFlowletClassLoader;
import net.orthanc.flow4j.model.codegen.javasrc.Consts;
import net.orthanc.flow4j.model.codegen.javasrc.JavaSrcModelDigester;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaModelException;

/**
 * @author agreif
 *
 * TODO
 * 
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class FlowBuilder extends IncrementalProjectBuilder {

	/**
	 * Creates a new FlowBuilder instance.
	 * 
	 */
	public FlowBuilder() {
		super();
	}


	/**
	 * Returns the JavaProject instance of the Flow4J project.
	 * @return the JavaProject instance of the Flow4J project.
	 */
	IJavaProject getJavaProject() {
		return Flow4JPlugin.getJavaProject(getProject());
	}


	/**
	 * TODO
	 * @return
	 * @throws JavaModelException
	 */
	private IContainer[] getClasspathSrcContainers() throws JavaModelException {
		IClasspathEntry[] classpathEntries = getJavaProject().getRawClasspath();
		List entries = new ArrayList();
		IProject project = getProject();
		for (int i = 0; i < classpathEntries.length; i++) {
			IClasspathEntry entry = classpathEntries[i];
			if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
				if (entry.getPath().segmentCount() < 2)
					entries.add(project);
				else
					entries.add(project.getFolder(entry.getPath().removeFirstSegments(1)));
			}
		}
		return (IContainer[])entries.toArray(new IContainer[entries.size()]);
	}




	/**
	 * Performs a full build or an incremental build and returns a list of
	 * projects which have to be built too or null if no other projects
	 * need a rebuild.
	 * @see org.eclipse.core.internal.events.InternalBuilder#build(int, java.util.Map, org.eclipse.core.runtime.IProgressMonitor)
	 */
	protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException {
		IResourceDelta delta = null;
		if (kind != FULL_BUILD)
			delta = getDelta(getProject());

		if (delta == null || kind == FULL_BUILD) {
			// Full build
			IProject project = getProject();
			if (!isBuildableProject(project))
				return null;
			//	build project source folders
			IContainer[] classpathContainers = getClasspathSrcContainers();
			for (int i = 0; i < classpathContainers.length; i++) {
				IContainer container = classpathContainers[i];
				buildFlows(container, container, monitor);
			}

/*
			IPath flowsFolderPath = project.getFullPath().append(Flow4JPlugin.FOLDER_NAME_FLOWS);
			IWorkspace workspace = project.getWorkspace();
			if (workspace.getRoot().exists(flowsFolderPath)) {
				IResource flowsFolderResource = workspace.getRoot().findMember(flowsFolderPath);
				if (flowsFolderResource == null)
					return null;
				else if (flowsFolderResource instanceof IFolder)
					buildFlows((IFolder)flowsFolderResource, monitor);
				else if (flowsFolderResource instanceof IFile)
					buildFlow((IFile)flowsFolderResource, monitor);
			}
*/
		} else {
			//	incremental build
			delta.accept(new BuildDeltaVisitor(monitor, this));
		}

		return null;
	}


	/**
	 * TODO
	 * @param resource
	 * @return
	 * @throws CoreException
	 */
	IContainer getClasspathSrcFolder(IResource resource) throws CoreException {
/*		IProject project = getProject();
		IClasspathEntry[] classpathEntries = getJavaProject().getRawClasspath();
		for (int i = 0; i < classpathEntries.length; i++) {
			IClasspathEntry entry = classpathEntries[i];
			if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
				IFolder srcFolder = project.getFolder(entry.getPath().removeFirstSegments(1));
				if (resource.getFullPath().matchingFirstSegments(srcFolder.getFullPath()) == srcFolder.getFullPath().segments().length) {
					return srcFolder;
				}
			}
		}
		return project;
		*/
		IContainer[] classpathContainers = getClasspathSrcContainers();
		for (int i = 0; i < classpathContainers.length; i++) {
			IContainer container = classpathContainers[i];
			if (resource.getFullPath().matchingFirstSegments(container.getFullPath()) == container.getFullPath().segmentCount())
				return container;
		}
		return getProject();
	}



	/**
	 * Builds the flow. Creates the java source in the same folder where the flow file resides.
	 * Creates all intermediate folder resources if necessary.
	 * @param srcFolder the parent src folder that is sepcified in the classpath
	 * @param file the flow file to build
	 * @param monitor the progress monitor
	 */
	protected void buildFlow(IContainer srcFolder, IFile file, IProgressMonitor monitor) {
		InputStream in = null;
		try {
			String className = file.getFullPath().removeFileExtension().lastSegment();
			IPath packagePath = file.getFullPath().removeFirstSegments(file.getFullPath().matchingFirstSegments(srcFolder.getFullPath())).removeLastSegments(1);
			String packageName = packagePath.toString().replace(IPath.SEPARATOR, '.');
			
			//	generate the java source code
			JavaTaskFlowletClassLoader taskFlowletClassLoader = Flow4JPlugin.getTaskFlowletClassLoader(file);
			//in = JavaSrcModelDigester.generateCode(file.getLocation().toFile(), packageName, Flow4JPlugin.getWorkspaceRoot().getLocation().toFile(), taskFlowletClassLoader);
			in = JavaSrcModelDigester.generateCode(file.getLocation().toFile(), packageName, Flow4JPlugin.getWorkspaceRoot().getLocation().toFile(), taskFlowletClassLoader);
			
			
			IFile outputFile = getProject().getFile(file.getFullPath().removeFirstSegments(1).removeFileExtension().addFileExtension(Consts.FILE_EXTENSION_JAVA));
			monitor.subTask("Build java source: " + outputFile);
			if (! outputFile.exists())
				outputFile.create(in, true, monitor);
			else
				outputFile.setContents(in, true, true, monitor);

		} catch (Exception e) {
			Flow4JPlugin.log(e);
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e1) {
				Flow4JPlugin.log(e1);
			}
			monitor.done();
		}
	}

	/**
	 * Builds all flows in the given folder.
	 * Also builds recursively subfolders
	 * @param srcFolder the parent src folder that is sepcified in the classpath
	 * @param buildFolder the folder which is checked for flow resources
	 * @param monitor
	 * @throws CoreException
	 */
	private void buildFlows(IContainer srcFolder, IContainer buildFolder, IProgressMonitor monitor) throws CoreException {
		monitor.subTask(Flow4JPlugin.getResourceString("Monitor_Builder.buildingFlows"));
		IResource[] members = buildFolder.members();

		for (int i = 0; i < members.length; i++) {
			IResource member = members[i];
			if (member instanceof IFolder)
				buildFlows(srcFolder, (IFolder)member, monitor);
			else if (member instanceof IFile && Flow4JPlugin.isFlowFile((IFile)member)) {
				buildFlow(srcFolder, (IFile)member, monitor);
			}
		}
		monitor.done();
	}

	/**
	 * Returns whether the project is buildable by the FlowBuilder.
	 * @param project
	 * @return true if the FlowBuilder can build the given project
	 */
	static boolean isBuildableProject(IProject project) {
		return Flow4JPlugin.hasFlow4JNature(project);
	}

}

⌨️ 快捷键说明

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