templatefactory.java
来自「电信的网厅的整站代码」· Java 代码 · 共 317 行
JAVA
317 行
package com.doone.webtemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import org.jdom.Document;
//import org.jdom.Element;
//import org.jdom.output.XMLOutputter;
//import org.jdom.output.Format;
//import org.jdom.input.SAXBuilder;
import com.doone.util.FileLogger;
import com.doone.util.XmlParse;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.FileInputStream;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.DataOutputStream;
import java.io.File;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.OutputFormat;
/**
*
* <p>Title:在应用服务里用于修改首页的配置XML文件 </p>
*
* <p>Description:与WEB的显示的不同。这里是直接保存到upload里 </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: New Doone</p>
*
* @author wull
* @version 1.0
*/
public class TemplateFactory {
private static String path = null;
private static Map codeFactory = new HashMap();
private static List rootModule = null; // ModuleConfig类型数据。
private static ModuleConfig defaultModule = null;
private static String configfile = "/templateconfig.xml";
private static String newpath = null;
public TemplateFactory(String newpath) {
this.newpath = newpath;
loadConfig();
}
// static {
// loadConfig();
// }
/**
* 从文件中加载配置。
*/
public synchronized static void loadConfig() {
try {
InputStream is= new FileInputStream(newpath);
SAXReader reader = new SAXReader();
Document doc = reader.read(is);
Element rootElt = doc.getRootElement();
if (!rootElt.getName().equals("config")) {
throw new RuntimeException("配置文件格式不正确,根节点必须是config。");
}
// 设置应用程序根目录。
setRootPath(rootElt.attributeValue("path"));
// 获取根级模块节点。
List module = rootElt.elements("module");
rootModule = new ArrayList();
codeFactory.clear(); // 清空所有历史对象。
Map modules = new HashMap();
for (int i = 0; i < module.size(); i++) {
Element moduleElt = (Element) module.get(i);
ModuleConfig mc = ModuleConfig.getInstance(null, moduleElt);
if (mc != null) {
// 检查名称和Code是否重复。
if (getCodeFactory(mc.getCode()) != null) {
FileLogger.getLogger().warn(
"根级模块编号“" + mc.getCode() + "”重复,系统将不再加载新模块信息.");
continue;
}
if (modules.get(mc.getName()) != null) {
FileLogger.getLogger().warn(
"根级模块名称“" + mc.getName() + "”重复,系统将不再加载新模块信息。");
continue;
}
modules.put(mc.getName(), new Integer(i));
rootModule.add(mc);
putCodeFactory(mc);
}
}
// 获取根级默认模块。
Element dme = rootElt.element("defaultmodule");
defaultModule = ModuleConfig.getInstance(null, dme);
} catch (Exception ex) {
FileLogger.getLogger().warn("加载模板配置文件出错:", ex);
}
}
static void putCodeFactory(ModuleConfig module) {
if (module != null) {
codeFactory.put(module.getCode(), module);
}
}
static ModuleConfig getCodeFactory(String code) {
return (ModuleConfig) codeFactory.get(code);
}
/**
* 将当前配置写入文件。
*/
public static void saveConfig() {
try {
Document doc = DocumentHelper.createDocument();
Element rootElt = doc.addElement("config");
rootElt.addAttribute("path", path);
if (rootModule == null)
rootModule = new ArrayList();
for (int i = 0; i < rootModule.size(); i++) {
ModuleConfig mc = (ModuleConfig) rootModule.get(i);
Element mce = mc.getXmlElement();
if (mce != null)
rootElt.add(mce);
}
// 设置默认节点。
if (defaultModule != null) {
Element mce = defaultModule.getXmlElement();
/*增加*/
mce.setName("defaultmodule");
if (mce != null)
rootElt.add(mce);
}
/*直接保存文件。*/
if (newpath == null) throw new RuntimeException("FileName is null!");
File file = new File(newpath);
DataOutputStream out = new DataOutputStream(new
FileOutputStream(file));
OutputFormat xmlFormat = OutputFormat.createCompactFormat();
xmlFormat.setEncoding("GBK");
xmlFormat.setIndent(true);
xmlFormat.setTrimText(false);
xmlFormat.setNewlines(true);
XMLWriter xmlOut = new XMLWriter(out, xmlFormat);
xmlOut.write(doc);
xmlOut.close();
// /*暂时测试用保存到指定目录*/
// Format xmlFormat = Format.getPrettyFormat();
// xmlFormat.setEncoding("GBK");
// XMLOutputter XMLOut = new XMLOutputter(xmlFormat);
// //生成XML文件
// XMLOut.output(doc, new FileOutputStream(newpath));
} catch (Exception ex) {
FileLogger.getLogger().warn("加载模板配置文件出错:", ex);
}
}
/**
* 根据模块编号获取相应的模块配置。
*
* @param code
* 模块编号。
* @return 返回模块编号对应的配置;
*/
public static ModuleConfig getModuleConfig(String code) {
return getCodeFactory(code);
}
/**
* 通过路径与名称获取相应的模块信息。
*
* @param sPath
* 模块路径,如果为null或“""”则代表获取根目录下的模块。
* @param sName
* 模块名称,如果为null或“""”则代表获取当前路径指定模块。否则获取当前路径下指定名称的子模块。
* @return
*/
public static ModuleConfig getModuleConfig(String sPath, String sName) {
ModuleConfig ret = null;
if (sName == null)
sName = "";
if (sPath == null)
sPath = "";
sPath = sPath.trim().toLowerCase();
if (sPath.length() > 0) {
FileLogger.getLogger().debug("模块路径sPath:" + sPath);
if (!sPath.startsWith(getRootPath()))
throw new RuntimeException("sPath参数必须是绝对路径。");
else
sPath = sPath.substring(getRootPath().length());
}
// URL是不区分大小写的。
if ( sPath.equals("")) {
// 找到根模块。
for (int i = 0; i < rootModule.size(); i++) {
ModuleConfig mc = (ModuleConfig) rootModule.get(i);
if (mc.getName().equals(sName)) {
ret = mc;
break;
}
}
} else {
String[] paths = sPath.split("/");
// 找到根模块。
for (int i = 0; i < rootModule.size(); i++) {
ModuleConfig mc = (ModuleConfig) rootModule.get(i);
if (mc.getPath().equals(paths[0])) {
ret = mc;
break;
}
}
if (ret != null) {
for (int i = 1; i < paths.length; i++) {
if (paths[i] != null && !paths[i].equals(""))
ret = ret.getChildNode(paths[i]);
}
if (!sName.equals("")) {
ret = ret.getChildren(sName);
}
}
}
if (ret == null)
ret = getDefaultModule();
return ret;
}
public static ModuleConfig getDefaultModule() {
return defaultModule;
}
/**
* 添加一个新的模块配置对象。
*
* @return 返回一个新的模块配置对象。
*/
public static ModuleConfig newModuleConfig() {
return ModuleConfig.getInstance();
}
public static void addModuleConfig(String sPath, ModuleConfig mc) {
// TODO 完成实现。
}
public static void delModuleConfig(String code) {
// TODO 根据模块编号删除相应的模块。
}
/**
* 获取应用程序的根目录。
*
* @return
*/
public static String getRootPath() {
return path;
}
/**
* 设置应用程序的根目录。
*
* @param sPath
*/
public static void setRootPath(String sPath) {
if (sPath == null)
sPath = "/";
if (sPath.charAt(0) != '/') {
FileLogger.getLogger().warn("模块根目录不能是相对路径。");
sPath = "/" + sPath;
}
if (sPath.charAt(sPath.length() - 1) != '/')
sPath += "/";
path = sPath.toLowerCase();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?