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

📄 genfilescommand.java

📁 Eclipse代码生成器,支持Eclipse3.3 WTP版本,该项目实现了Spring + hibernate + webService + struts 框架文件的自动生成,有这方面需求的个人公司
💻 JAVA
字号:
package com.newegg.eclipse.soaw.facet.codegen.ui.command.common;

import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jst.ws.internal.common.ResourceUtils;
import org.eclipse.wst.command.internal.env.core.common.ProgressUtils;
import org.eclipse.wst.command.internal.env.core.context.ResourceContext;
import org.eclipse.wst.command.internal.env.core.context.TransientResourceContext;
import org.eclipse.wst.common.environment.IStatusHandler;

import com.newegg.eclipse.soaw.facet.codegen.ui.beans.ClassDataModel;
import com.newegg.eclipse.soaw.facet.codegen.ui.plugin.CodeGenPlugin;
import com.newegg.eclipse.soaw.facet.codegen.ui.util.Constants;
import com.newegg.eclipse.soaw.facet.codegen.ui.util.FreeMarkerContext;
import com.newegg.eclipse.soaw.facet.codegen.ui.util.Utils;
import com.sun.org.apache.regexp.internal.CharacterArrayCharacterIterator;

import freemarker.template.Configuration;
import freemarker.template.SimpleHash;
import freemarker.template.Template;

public class GenFilesCommand
{
	private ClassDataModel classDataModel = null;
	private IProject pj = null;
	private IProgressMonitor monitor = null;
	private IStatusHandler handler = null;
	private String currentTemplateName = null;

	public GenFilesCommand ( IProject pj , ClassDataModel classDataModel , String currentTemplateName ,
			final IProgressMonitor monitor , IStatusHandler handler )
	{
		this.pj = pj;
		this.classDataModel = classDataModel;
		this.currentTemplateName = currentTemplateName;
		this.monitor = monitor;
		this.handler = handler;
	}

	public GenFilesCommand ( IProject pj , IProgressMonitor monitor , IStatusHandler handler )
	{
		this.pj = pj;
		this.monitor = monitor;
		this.handler = handler;
	}

	/**
	 * 
	 * @throws CoreException
	 */
	public void execute() throws CoreException
	{
		try
		{
			Configuration cfg = FreeMarkerContext.getInstance().getConfiguration();
			SimpleHash root = new SimpleHash();
			root.put(Constants.DATA_MODEL_NAME, classDataModel);
			if (classDataModel.getDependClasses() != null)
			{
				root.put(Constants.DATA_MODEL_CHIL_NAME, classDataModel.getDependClasses());
			}
			Template temp = cfg.getTemplate(currentTemplateName);
			IPath[] paths = ResourceUtils.getAllJavaSourceLocations(pj);
			if (paths != null && paths.length > 0)
			{
				File outFile = Utils.getEmptyFileFormIFile(paths[0], pj, classDataModel, handler);
				if (outFile == null || !outFile.exists())
				{
					outFile = Utils.getEmptyFileFormResource(paths[0], pj, classDataModel);
				}
				Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
				// IPath newFilePath = new Path(outFile.getAbsolutePath());
				// IFile file = new org.eclipse.core.internal.resources.File(newFilePath,);
				// file.setContents(source, updateFlags, monitor)
				temp.process(root, out);
				out.flush();

				//IPath newFilePath = new Path(outFile.getAbsolutePath());
				IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
				IResource resource = workspaceRoot.findMember(paths[0]);
				resource.refreshLocal(IResource.DEPTH_ZERO, monitor);
			} else
			{
				throw new CoreException(CodeGenPlugin.createErrorStatus(pj.getName(), new Exception(
						": No source folder!")));
			}
		} catch (Exception e)
		{
			throw new CoreException(CodeGenPlugin.createErrorStatus(e.getMessage(), e));
		}
	}

	/**
	 * @param classDataModel
	 *            the classDataModel to set
	 */
	public void setClassDataModel(ClassDataModel classDataModel)
	{
		this.classDataModel = classDataModel;
	}

	/**
	 * @param currentTemplateName
	 *            the currentTemplateName to set
	 */
	public void setCurrentTemplateName(String currentTemplateName)
	{
		this.currentTemplateName = currentTemplateName;
	}
}

⌨️ 快捷键说明

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