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

📄 templateact.java

📁 JEECMS是JavaEE版网站管理系统(Java Enterprise Edition Content Manage System)的简称。 基于java技术开发
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.jeecms.core.action;

import static com.jeecms.core.Constants.ENCODING;
import static com.jeecms.core.Constants.FILE_SPT;
import static com.jeecms.core.Constants.SPT;

import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jeecms.core.JeeCoreAction;
import com.jeecms.core.util.FileWrap;
import com.jeecms.core.util.UploadRule;
import com.ponyjava.common.util.ComUtils;
import com.ponyjava.common.util.Zipper;
import com.ponyjava.common.util.Zipper.FileEntry;

@SuppressWarnings("serial")
@Scope("prototype")
@Controller("core.templateAct")
public class TemplateAct extends JeeCoreAction {
	private static final Logger log = LoggerFactory
			.getLogger(TemplateAct.class);

	public String resMain() {
		return MAIN;
	}

	public String left() {
		String path = getWeb().getTplRootReal(contextPvd.getAppRoot())
				.toString().replace(SPT, FILE_SPT);
		File tplFile = new File(path);
		treeRoot = new FileWrap(tplFile, path, new FileFilter() {
			public boolean accept(File f) {
				if (f.getName().startsWith(".") || f.getName().startsWith("$")) {
					return false;
				} else {
					return true;
				}
			}
		});
		return LEFT;
	}

	public String resLeft() {
		String resPath = contextPvd.getAppRealPath(getWeb().getResRootBuf()
				.toString());
		treeRoot = new FileWrap(new File(resPath), resPath, new FileFilter() {
			public boolean accept(File f) {
				if (f.isDirectory() || f.getName().endsWith(".js")
						|| f.getName().endsWith(".css")
						|| f.getName().endsWith(".html")
						|| f.getName().endsWith(".txt")) {
					return true;
				} else {
					return false;
				}
			}
		});
		return LEFT;
	}

	public String right() {
		return RIGHT;
	}

	public String list() {
		String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
				relPath).toString());
		File dir = new File(path);
		subDir = new FileWrap(dir).getChild();
		return LIST;
	}

	public String resList() {
		String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
				.append(relPath).toString());
		File dir = new File(path);
		subDir = new FileWrap(dir).getChild();
		return LIST;
	}

	public String add() {
		// 设置上传规则
		addUploadRule();
		return ADD;
	}

	public String resAdd() {
		return ADD;
	}

	private boolean saveFile(String path) {
		File tpl = new File(path + relPath + FILE_SPT + tplName);
		try {
			FileUtils.writeStringToFile(tpl, tplContent, ENCODING);
			return true;
		} catch (IOException e) {
			log.error("写入模板失败!", e);
			addActionError("写入模板失败!");
			return false;
		}
	}

	public String save() {
		String path = contextPvd.getAppRealPath(getWeb().getTplRoot()
				.toString());
		if (saveFile(path)) {
			removeUploadRule();
		}
		return list();
	}

	public String resSave() {
		String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
				.toString());
		saveFile(path);
		return resList();
	}

	private void editFile(String path) {
		File tpl = new File(path);
		tplName = tpl.getName();
		parentPath = relPath.substring(0, relPath.lastIndexOf(FILE_SPT));
		try {
			tplContent = FileUtils.readFileToString(tpl, ENCODING);
		} catch (IOException e) {
			log.error("读取模板文件失败", e);
			addActionError("读取模板文件失败!");
		}
	}

	public String edit() {
		String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
				relPath).toString());
		editFile(path);
		// 设置上传规则
		addUploadRule();
		return EDIT;
	}

	public String resEdit() {
		String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
				.append(relPath).toString());
		editFile(path);
		return EDIT;
	}

	private boolean updateFile(String path) {
		File tpl = new File(path);
		File newFile = tpl;
		if (!tpl.getName().equals(tplName)) {
			newFile = new File(tpl.getParent() + FILE_SPT + tplName);
			tpl.renameTo(newFile);
		}
		try {
			FileUtils.writeStringToFile(newFile, tplContent, ENCODING);
			jsonRoot.put("success", true);
			jsonRoot.put("msg", "保存成功");
			return true;
		} catch (IOException e) {
			log.error("写文件失败", e);
			jsonRoot.put("success", false);
			jsonRoot.put("msg", "写文件失败!");
			return false;
		}
	}

	public String update() {
		String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
				relPath).toString());
		if (updateFile(path)) {
			removeUploadRule();
		}
		return SUCCESS;
	}

	public String resUpdate() {
		String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
				.append(relPath).toString());
		updateFile(path);
		return SUCCESS;
	}

	private void renameFile(String path) {
		File tpl = new File(path);
		if (!origName.equals(tplName)) {
			boolean b = tpl.renameTo(new File(tpl.getParent() + FILE_SPT
					+ tplName));
			if (!b) {
				jsonRoot.put("success", false);
			}
		}
		jsonRoot.put("success", true);
	}

	public String rename() {
		String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
				relPath).append(SPT).append(origName).toString());
		renameFile(path);
		return SUCCESS;
	}

	public String resRename() {
		String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
				.append(relPath).append(SPT).append(origName).toString());
		renameFile(path);
		return SUCCESS;
	}

	private void deleteFile(String path) {
		File tpl = new File(path);
		if (FileUtils.deleteQuietly(tpl)) {
			addActionError("删除成功!");
		} else {
			addActionError("删除失败!");
		}
	}

	public String delete() {
		String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
				relPath).append(SPT).append(tplName).toString());
		deleteFile(path);
		return list();
	}

	public String resDelete() {
		String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
				.append(relPath).append(SPT).append(tplName).toString());
		deleteFile(path);
		return resList();
	}

	public String createDir() {
		String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
				relPath).append(SPT).append(dirName).toString());
		File dir = new File(path);
		dir.mkdir();
		return list();
	}

	public String resCreateDir() {
		String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
				.append(relPath).append(SPT).append(dirName).toString());
		File dir = new File(path);
		dir.mkdir();
		return resList();
	}

	public String resUpload() {
		return "upload";
	}

	public String resUploadSubmit() {
		String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
				.append(relPath).append(SPT).toString());
		if (resFile != null) {
			try {
				for (int i = 0; i < resFile.length; i++) {
					FileUtils.copyFile(resFile[i], new File(path + FILE_SPT
							+ resFileFileName[i]));
				}
				addActionMessage("上传成功!");
			} catch (IOException e) {
				addActionError("上传失败!" + e.getMessage());
			}
		}
		return resList();
	}

	public String solutionEdit() {
		// 数据库和目录中都有的模板套件才显示
		// 数据库中模板方案
		solMap = getWeb().getSolutions();
		// 模板目录中模板方案
		dirMap = new LinkedHashMap<String, String[]>();
		String path = contextPvd.getAppRealPath(getWeb().getTplRoot()
				.toString());
		File[] tplFiles = new File(path).listFiles(ComUtils.DIR_FILE_FILTER);
		for (File f : tplFiles) {
			if (solMap.containsKey(f.getName())) {
				dirMap.put(f.getName(), f.list(ComUtils.DIR_FILE_FILTER));
			}
		}
		return "solution";
	}

	public String solutionUpdate() {
		Map<String, String> smap = getWeb().getSolutions();
		for (String key : smap.keySet()) {
			String s = solMap.get(key);
			if (s != null) {
				smap.put(key, s);
			}
		}
		addActionMessage("操作成功");
		return solutionEdit();
	}

	public String exportTpl() {
		solSet = new LinkedHashSet<String>();
		solMap = getWeb().getSolutions();
		String path = contextPvd.getAppRealPath(getWeb().getTplRoot()
				.toString());
		File[] sysDirs = new File(path).listFiles(ComUtils.DIR_FILE_FILTER);
		for (File sysDir : sysDirs) {
			if (solMap.containsKey(sysDir.getName())) {
				for (File sol : sysDir.listFiles(ComUtils.DIR_FILE_FILTER)) {
					solSet.add(sol.getName());
				}
			}
		}
		return "exportTpl";
	}

⌨️ 快捷键说明

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