skinutil.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 183 行

JAVA
183
字号
package cn.js.fan.web;import java.util.Locale;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import cn.js.fan.base.ISkin;import cn.js.fan.util.ResBundle;import org.apache.log4j.Logger;import cn.js.fan.cache.jcs.RMCache;import java.util.HashMap;import cn.js.fan.util.XMLConfig;import org.jdom.Element;import java.util.List;import java.util.Iterator;import cn.js.fan.util.StrUtil;public class SkinUtil implements ISkin {    static final String resName = "res.common";    static Logger logger = Logger.getLogger("SkinUtil");    public static final String ERR_DB = "err_db";    public static final String PVG_INVALID = "pvg_invalid";    public static final String ERR_SQL = "err_sql";    public static final String ERR_NOT_LOGIN = "err_not_login";    public static final String ERR_ID = "err_id";    public static final String SESSION_LOCALE = "locale";        public SkinUtil() {    }    public static Locale getLocale(HttpServletRequest request) {        if (Global.localeSpecified)            return Global.getLocale();        if (request==null) {                         return Global.locale;        }        HttpSession session = request.getSession(true);        Locale locale = (Locale) session.getAttribute(SESSION_LOCALE);        if (locale != null) {            return locale;        }                String str = request.getHeader("Accept-Language");                 String lang = null;        String country = null;        if (str!=null) {            try {                String[] ary = str.split("-");                lang = ary[0];                country = ary[1];                                int d = country.indexOf(",");                if (d != -1)                    country = country.substring(0, d);                            }            catch (Exception e) {                 logger.error("getLocale: " + StrUtil.trace(e));            }        }        if (lang == null || country == null) {            locale = Global.locale;         }        else {            if (country.equalsIgnoreCase("sg"))                country = "tw";            else if (country.equalsIgnoreCase("hk"))                country = "tw";            else if (country.equalsIgnoreCase("mo"))                country = "tw";            HashMap hm = getSupportLangCountry();                        if (!hm.containsKey(lang.toLowerCase() + "_" + country.toLowerCase())) {                locale = Global.locale;            } else                locale = new Locale(lang, country);        }                session.setAttribute(SESSION_LOCALE, locale);        return locale;    }    public String LoadStr(HttpServletRequest request, String key) {        return LoadString(request, resName, key);    }    public static String LoadString(HttpServletRequest request, String key) {        return LoadString(request, resName, key);    }    public static String LoadString(HttpServletRequest request, String resource, String key) {        String str = "";        try {            ResBundle rb = new ResBundle(resource, getLocale(request));            str = rb.get(key);                    }        catch (Exception e) {            logger.error("LoadString: resource=" + resource + " key=" + key + " " + e.getMessage());            e.printStackTrace();        }        return str;    }    public static String LoadStr(HttpServletRequest request, String resource, String key) {        return LoadString(request, resource, key);    }    public static String makeErrMsg(HttpServletRequest request, String errMsg) {        String str = LoadString(request, "err_display_begin") + StrUtil.toHtml(errMsg) + LoadString(request, "err_display_end");        return str;    }    public static String makeInfo(HttpServletRequest request, String errMsg) {        String str = LoadString(request, "info_display_begin") + StrUtil.toHtml(errMsg) + LoadString(request, "info_display_end");        return str;    }    public static String waitJump(HttpServletRequest request, String msg, int t, String url) {        String str = "";        String spanid = "id" + System.currentTimeMillis();        str = "\n<ol><b><span id=" + spanid + "> 3 </span>";        str += SkinUtil.LoadString(request, "js_waitJump") + "</b></ol>";        str += "<ol>" + msg + "</ol>";        str += "<script language=javascript>\n";        str += "<!--\n";        str += "function tickout(secs) {\n";        str += spanid + ".innerText = secs;\n";        str += "if (--secs > 0) {\n";        str += "  setTimeout('tickout(' +secs + ')', 1000);\n";        str += "}\n";        str += "}\n";        str += "tickout(" + t + ");\n";        str += "-->\n";        str += "</script>\n";        str += "<meta http-equiv=refresh content=" + t + ";url=" + url + ">\n";        return str;    }        public static HashMap getSupportLangCountry() {        String key = "cwbbs_locales";        HashMap hm = null;        try {            hm = (HashMap) RMCache.getInstance().get(key);        }        catch (Exception e) {            logger.error("getSupportLocales:" + e.getMessage());        }        if (hm==null) {            hm = new HashMap();            XMLConfig xc = new XMLConfig("config_i18n.xml", false, "utf-8");            Element root = xc.getRootElement();            Element child = root.getChild("support");            List list = child.getChildren();            if (list != null) {                Iterator ir = list.iterator();                while (ir.hasNext()) {                    Element e = (Element) ir.next();                    hm.put(e.getChildText("lang") + "_" + e.getChildText("country"), "");                }                try {                    RMCache.getInstance().put(key, hm);                }                catch (Exception e) {                    logger.error("getSupportLocales2:" + e.getMessage());                }            }        }        return hm;    }}

⌨️ 快捷键说明

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