XML解析

来源:互联网 发布:学霸通软件安装 编辑:程序博客网 时间:2024/06/14 05:19
package com.******.boss.inf.config;


import java.util.concurrent.ConcurrentHashMap;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


import******
import com.****.biss.uls.Logger;
import com.******.boss.inf.interfaces.ReplyCode;
import com.******.boss.inf.trade.TradeException;


/**
 * 
 * 
 */
class SmsLangConfig {
String name;
public ConcurrentHashMap<String, String> langConfigMap = new ConcurrentHashMap<String, String>();

public SmsLangConfig(String name) {
this.name = name;
}
}


class GatewayConfig {
String name;
public ConcurrentHashMap<String, String> gatewayConfigMap = new ConcurrentHashMap<String, String>();

public GatewayConfig(String name) {
this.name = name;
}
}


class CountryConfig {
String name;
public CountryConfig(String name) {
this.name = name;
}

public ConcurrentHashMap<String, String> countryConfigMap = new ConcurrentHashMap<String, String>();
}
/**
 * 
 * @Package com.gigaset.boss.inf
 * @Class SmsConfigHelper.java
 * @Description(短信模板解析)
 * @Date 2015年4月17日 下午6:08:12
 * @Revision v1.0
 */
public class SmsConfigHelper {


static final Logger logger = Logger.getLogger(SmsConfigHelper.class);
/**
* 基础模板
*/
static ConcurrentHashMap<String, String> configMap = new ConcurrentHashMap<String, String>();
/**
* 短信模板
*/
static ConcurrentHashMap<String, SmsLangConfig> smsTemplateLangConfigMap = new ConcurrentHashMap<String, SmsLangConfig>();
/**
* 网关
*/
static ConcurrentHashMap<String, GatewayConfig> gatewayConfigMap = new ConcurrentHashMap<String, GatewayConfig>();


/**
* 国家
*/
static ConcurrentHashMap<String, String> countryConfigMap = new ConcurrentHashMap<String, String>();

static {
try {
loadConfig(GigasetUtil.getConfigPath() + "boss-config.xml");
} catch (Throwable e) {
logger.error(e.getMessage(), e);
}
}


public static void loadConfig(String configFile) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
builder = factory.newDocumentBuilder();
Document doc = builder.parse(configFile);
NodeList lss = doc.getElementsByTagName("sms-config").item(0).getChildNodes();
for (int i = 0; i < lss.getLength(); i++) {
Node item = lss.item(i);
if (!(item instanceof Element)) {
continue;
}
Element field = (Element) item;
if (field.hasChildNodes() && !field.getTagName().equals("template-config")) {
configMap.put(field.getTagName(), field.getFirstChild().getNodeValue().trim());
}
}

NodeList list = doc.getElementsByTagName("sms-config");
Element node = (Element) list.item(0);
//template-config
NodeList list2 = node.getElementsByTagName("template-config");
Element node1 = (Element) list2.item(0);
NodeList ls = node1.getElementsByTagName("template");
for(int i = 0; i < ls.getLength(); i++)  {
Element node2 = (Element) ls.item(i);
String lang = node2.getAttribute("lang");
SmsLangConfig langConfig = new SmsLangConfig(lang);
smsTemplateLangConfigMap.put(lang, langConfig);
NodeList list2_1_1 = node1.getElementsByTagName("template").item(i).getChildNodes();
for (int n = 0; n < list2_1_1.getLength(); n++) {
Node item1 = list2_1_1.item(n);
if (!(item1 instanceof Element)) {
continue;
}
Element field1 = (Element) item1;
if (field1.hasChildNodes()) {
langConfig.langConfigMap.put(field1.getTagName(), field1.getFirstChild().getNodeValue().trim());
}
}
}

//gateway-config
NodeList list3 = node.getElementsByTagName("gateway-config");
Element node3 = (Element) list3.item(0);
NodeList ls3 = node3.getElementsByTagName("gateway");
for(int y = 0; y < ls3.getLength(); y++) {
Element node3_1 = (Element) ls3.item(y);
GatewayConfig gatewayConfig = new GatewayConfig(node3_1.getAttribute("name"));
gatewayConfigMap.put(node3_1.getAttribute("name"), gatewayConfig);
gatewayConfig.gatewayConfigMap.put("service", node3_1.getAttribute("service"));
gatewayConfig.gatewayConfigMap.put("from", node3_1.getAttribute("from"));
gatewayConfig.gatewayConfigMap.put("username", node3_1.getAttribute("username"));
gatewayConfig.gatewayConfigMap.put("password", node3_1.getAttribute("password"));
}

//country-config
NodeList list4 = node.getElementsByTagName("country-config");
Element node4 = (Element) list4.item(0);
NodeList ls4 = node4.getElementsByTagName("country");
for (int m = 0; m < ls4.getLength(); m++) {
Element node4_1 = (Element) ls4.item(m);
countryConfigMap.put(node4_1.getAttribute("name"), node4_1.getAttribute("gateway"));
}
} catch (Throwable e) {
logger.error("init template failure:", e);
}
}


public static String getString(String key) {
return configMap.get(key);
}


public static String getString(String key, String defaultValue) {
if (configMap.containsKey(key))
return configMap.get(key);
else
return defaultValue;
}


public static int getInt(String key) {
return Integer.parseInt(configMap.get(key));
}


public static int getInt(String key, int defaultValue) {
try {
if (configMap.containsKey(key)) {
return Integer.parseInt(configMap.get(key));
}
} catch (Exception e) {
}
return defaultValue;
}


public static boolean getBoolean(String key) {
return Boolean.parseBoolean(configMap.get(key));
}


public static boolean getBoolean(String key, boolean defaultValue) {
try {
if (configMap.containsKey(key)) {
return Boolean.parseBoolean(configMap.get(key));
}
} catch (Exception e) {
}
return defaultValue;
}


public static long getLong(String key) {
return Long.parseLong(configMap.get(key));
}


public static long getLong(String key, long defaultValue) {
try {
if (configMap.containsKey(key)) {
return Long.parseLong(configMap.get(key));
}
} catch (Exception e) {
}
return defaultValue;
}

/**
* 根据语言和模板可以取短信模板
* @param lang
* @param key
* @return
*/
public static String getSmsTemplate(String lang, String key) {
logger.info("******************lang: " + lang + "--" + "key:" + key);
//如果语言模板为空 则默认读取中文模板
try {
return SmsConfigHelper.smsTemplateLangConfigMap.get(lang == null || lang == "" ? "zh_CN" : lang).langConfigMap.get(key);
} catch (Exception e) {
throw new TradeException(ReplyCode.Idm.CODE_105, "not exist language " + lang);
}
}

/**

* @param lang
* @param key
* @return
*/
public static String getGatewayTemplate(String region, String key) {
return SmsConfigHelper.gatewayConfigMap.get(region).gatewayConfigMap.get(key);
}

public static String getCountryGateway(String country) {
return SmsConfigHelper.countryConfigMap.get(country);
}

public static void main(String[] args) {
// System.out.println(SmsConfigHelper.configMap.get("sms-max-resend-times"));
// System.out.println(SmsConfigHelper.getInt("sms-max-resend-times"));
System.out.println(SmsConfigHelper.getSmsTemplate("en","wrong-password-content"));
// System.out.println(SmsConfigHelper.smsTemplateLangConfigMap.get("EN").langConfigMap.get("wrong-password-content"));
System.out.println(SmsConfigHelper.gatewayConfigMap.get(SmsConfigHelper.countryConfigMap.get("US")).gatewayConfigMap.get("service"));
System.out.println(SmsConfigHelper.gatewayConfigMap.get(SmsConfigHelper.countryConfigMap.get("US")).gatewayConfigMap.get("from"));
System.out.println(SmsConfigHelper.gatewayConfigMap.get(SmsConfigHelper.countryConfigMap.get("US")).gatewayConfigMap.get("username"));
System.out.println(SmsConfigHelper.gatewayConfigMap.get(SmsConfigHelper.countryConfigMap.get("US")).gatewayConfigMap.get("password"));
System.out.println(SmsConfigHelper.countryConfigMap.get("CN"));
}

}



<sms-config>

<https://52.28.65.237/api/sms/v2>

<6592384918>

<123456>
<!-- 短信发送服务线程数 -->  
<sms-pool-size>1</sms-pool-size>
<!-- 短信发送流量控制上限(m条/n秒)- 计数条数上限 -->  
<sms-flow-control-count>100</sms-flow-control-count>
<!-- 短信发送流量控制上限(m条/n秒) - 计数周期(单位:秒) -->  
<sms-flow-control-period>600</sms-flow-control-period>
<gateway-config>
<gateway name="default" service="***************" from=""  username="test" password="" />
</gateway-config>
<country-config>
<country name="CN" gateway="default" />
<country name="US" gateway="default" />
<country name="AT" gateway="default" />
<country name="CH" gateway="default" />
<country name="CZ" gateway="default" />
<country name="DE" gateway="default" />
<country name="FR" gateway="default" />
<country name="IT" gateway="default" />
<country name="NL" gateway="default" />
<country name="SE" gateway="default" />
<country name="GB" gateway="default" />
</country-config>

<template-config default="zh_CN">
<template lang="zh_CN">
<!-- 欢迎内容(已屏蔽) -->
<welcome-content>您的帐户现在已激活。欢迎使用******服务。感谢您的加入。</welcome-content>
<!-- 密码错误提醒短信内容(已屏蔽) -->
<wrong-password-content>您的****账户{0}密码错误已达{1}次,超过{2}次账号将被锁定。</wrong-password-content>  
<!-- 用户信息变更通知模板((已屏蔽)) -->  
<modify-account-content>您的******账户{0}已更新为{1},如果不是您本人的操作,请尽快修改您的账户信息,设置更为安全的密码。</modify-account-content>  
<!-- 密码变更通知内容(已屏蔽) -->
<modify-password-content>您的******账户密码已被更新,如果不是您本人的操作,请尽快修改您的账户信息,设置更为安全的密码。</modify-password-content>
<!-- 找回密码短信内容 -->  
<retrive-password-content>您本次业务的验证码为{0},请在{1}秒内正确输入验证码。【******】</retrive-password-content>  
<!-- 重置密码成功通知(已屏蔽) -->  
<reset-password-content>*******密码重置成功</reset-password-content> 
<!-- 获取短信验证码 -->
<retrive-notice-sms>您本次业务的验证码为{0},请在{1}秒内正确输入验证码。【*******】</retrive-notice-sms> 
</template>

</template-config>
</sms-config>

0 0
原创粉丝点击