📄 thememgr.java~16~
字号:
package com.redmoon.forum.ui;
import java.io.*;
import java.util.*;
import java.net.URL;
import org.jdom.Document;
import org.jdom.output.*;
import org.jdom.input.*;
import org.jdom.Element;
import org.apache.log4j.Logger;
import java.net.URLDecoder;
import com.redmoon.kit.util.FileUpload;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletContext;
import com.redmoon.kit.util.FileUpload;
import org.apache.log4j.Logger;
import org.jdom.*;
import com.redmoon.kit.util.FileInfo;
import cn.js.fan.util.*;
import cn.js.fan.web.*;
import cn.js.fan.cache.jcs.RMCache;
public class ThemeMgr {
RMCache rmCache;
final String group = "THEME";
final String ALLTHEME = "ALLTHEME";
static Logger logger;
public final String FILENAME = "theme.xml";
FileUpload fileUpload = null;
public static Document doc = null;
public static Element root = null;
public static String xmlPath;
public static boolean isInited = false;
public static URL confURL;
public ThemeMgr() {
rmCache = RMCache.getInstance();
logger = Logger.getLogger(this.getClass().getName());
confURL = getClass().getClassLoader().getResource(FILENAME);
}
public static void init() {
if (!isInited) {
xmlPath = confURL.getPath();
xmlPath = URLDecoder.decode(xmlPath);
SAXBuilder sb = new SAXBuilder();
try {
FileInputStream fin = new FileInputStream(xmlPath);
doc = sb.build(fin);
root = doc.getRootElement();
fin.close();
isInited = true;
} catch (org.jdom.JDOMException e) {
logger.error(e.getMessage());
} catch (java.io.IOException e) {
logger.error(e.getMessage());
}
}
}
public Element getRootElement() {
if (root == null) {
init();
}
return root;
}
public void reload() {
isInited = false;
try {
rmCache.invalidateGroup(group);
} catch (Exception e) {
logger.error(e.getMessage());
}
}
public Theme getTheme(String code) {
Theme tm = null;
try {
tm = (Theme) rmCache.getFromGroup(code, group);
} catch (Exception e) {
logger.error(e.getMessage());
}
if (tm == null) {
init();
List list = root.getChildren();
if (list != null) {
Iterator ir = list.iterator();
while (ir.hasNext()) {
Element child = (Element) ir.next();
String ecode = child.getAttributeValue("code");
if (ecode.equals(code)) {
String name = child.getChildText("name");
String author = child.getChildText("author");
String path = child.getChildText(
"path");
String banner = child.getChildText("banner");
String height = child.getChildText("height");
tm = new Theme();
tm.setCode(code);
tm.setName(name);
tm.setAuthor(author);
tm.setPath(path);
tm.setBanner(banner);
tm.setHeight(height);
try {
rmCache.putInGroup(code, group,
tm);
} catch (Exception e) {
logger.error("getTheme:" + e.getMessage());
}
return tm;
}
}
}
}
return tm;
}
public Vector getAllTheme() {
Vector v = null;
try {
v = (Vector) rmCache.getFromGroup(ALLTHEME, group);
} catch (Exception e) {
logger.error(e.getMessage());
}
if (v == null) {
v = new Vector();
init();
List list = root.getChildren();
if (list != null) {
Iterator ir = list.iterator();
while (ir.hasNext()) {
Element child = (Element) ir.next();
String code = child.getAttributeValue("code");
v.addElement(getTheme(code));
}
try {
rmCache.putInGroup(ALLTHEME, group, v);
} catch (Exception e) {
logger.error("getAllTheme:" + e.getMessage());
}
}
}
return v;
}
public void writemodify() {
String indent = " ";
Format format = Format.getPrettyFormat();
format.setIndent(indent);
format.setEncoding("utf-8");
XMLOutputter outp = new XMLOutputter(format);
try {
FileOutputStream fout = new FileOutputStream(xmlPath);
outp.output(doc, fout);
fout.close();
} catch (java.io.IOException e) {}
}
public void modify(ServletContext application,
HttpServletRequest request) throws ErrMsgException {
String filename = "", code = "", name = "", height = "", path = "",
banner = "";
doUpload(application, request);
ThemeForm tf = new ThemeForm();
tf.init();
tf.doUpload(application, request);
code = tf.checkCode(request);
name = tf.checkName(request);
height = tf.checkHeight(request);
tf.report();
filename = tf.checkFiles(request);
String picSrc = fileUpload.getUploadFileName("picSrc");
if (!StrUtil.isNumeric(height)) {
throw new ErrMsgException(SkinUtil.LoadString(request,
"res.forum.plugin.ThemeConfig", "height_is_numbic"));
}
if (!filename.equals("")) {
delimg(picSrc);
int lastIndex = filename.indexOf("images");
path = filename.substring(0, lastIndex - 1);
banner = filename.substring(lastIndex);
set(code, "banner", banner);
set(code, "path", "/" + path);
}
set(code, "height", height);
set(code, "name", name);
writemodify();
reload();
}
public FileUpload doUpload(ServletContext application,
HttpServletRequest request) throws
ErrMsgException {
fileUpload = new FileUpload();
String[] extnames = {"gif", "jpg", "png"};
fileUpload.setValidExtname(extnames);
int ret = 0;
try {
ret = fileUpload.doUpload(application, request);
if (ret != fileUpload.RET_SUCCESS) {
throw new ErrMsgException(fileUpload.getErrMessage(request));
}
} catch (IOException e) {
logger.error("doUpload:" + e.getMessage());
}
return fileUpload;
}
public void create(ServletContext application,
HttpServletRequest request) throws ErrMsgException {
String path = "", banner = "";
ThemeForm tf = new ThemeForm();
tf.init();
tf.doUpload(application, request);
String code = tf.checkCode(request);
String name = tf.checkName(request);
String height = tf.checkHeight(request);
tf.report();
String filename = tf.checkFiles(request);
if (filename.equals("") || filename==null) {
throw new ErrMsgException(SkinUtil.LoadString(request, "res.forum.plugin.ThemeConfig", "file_is_null"));
}
int lastIndex = filename.indexOf("images");
path = "/" + filename.substring(0, lastIndex - 1);
banner = filename.substring(lastIndex);
List list = root.getChildren();
Element theme = new Element("theme");
theme.setAttribute(new Attribute("code", code));
Element elementName = new Element("name");
elementName.setText(name);
theme.addContent(elementName);
Element elementPath = new Element("path");
elementPath.setText(path);
theme.addContent(elementPath);
Element elementBanner = new Element("banner");
elementBanner.setText(banner);
theme.addContent(elementBanner);
Element elementHeight = new Element("height");
elementHeight.setText(height);
theme.addContent(elementHeight);
list.add(theme);
writemodify();
reload();
}
public void set(String code, String property, String textValue) {
List list = root.getChildren();
if (list != null) {
Iterator ir = list.listIterator();
while (ir.hasNext()) {
Element child = (Element) ir.next();
String ecode = child.getAttributeValue("code");
if (ecode.equals(code)) {
List list1 = child.getChildren();
if (list1 != null) {
Iterator ir1 = list1.listIterator();
while (ir1.hasNext()) {
Element childContent = (Element) ir1.next();
if (childContent.getName().equals(property)) {
childContent.setText(textValue);
}
}
}
}
}
}
}
public void del(HttpServletRequest request) throws ErrMsgException {
List list = root.getChildren();
String picsrc = "";
String code = ParamUtil.get(request, "code");
if (list != null) {
Iterator ir = list.listIterator();
while (ir.hasNext()) {
Element child = (Element) ir.next();
String ecode = child.getAttributeValue("code");
if (ecode.equals(code)) {
picsrc = Global.getRealPath() + child.getChildText("path") +
"/" + child.getChildText("banner");
root.removeContent(child);
writemodify();
break;
}
}
}
delimg(picsrc);
reload();
}
public void delimg(String src) throws ErrMsgException {
try {
File file = new File(src);
file.delete();
} catch (Exception e) {
logger.error("ThemeUploadMgr modify fail!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -