本文固定链接:http://www.verydemo.com/demo_c134_i38247.html
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.howbuy.core.util.StringUtil;
import com.howbuy.trade.dto.CorpMerchantDto;
/**
* @ClassName: CorpMerchantUtil
* @Description: 合作商户信息操作工具类
* @date 2013-4-27 上午10:46:48
*
*/
public class CorpMerchantUtil {
/**
* logger
*/
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* instance
*/
private static CorpMerchantUtil instance;
private final static String fileName = "corpMerchant.xml";
private static Map<String, CorpMerchantDto> corpMerchantsMap;
private static String currentVersion = "";
private CorpMerchantUtil() {
loadCorpMerchants();
}
/**
* getInstance
* @return CorpMerchantUtil
*/
public static CorpMerchantUtil getInstance() {
if(null == instance || isUpdateCorpMerchants()) {
instance = new CorpMerchantUtil();
}
return instance;
}
/**
* getCorpMerchantsMap
* @return Map<String, CorpMerchantDto>
*/
public Map<String, CorpMerchantDto> getCorpMerchantsMap() {
return corpMerchantsMap;
}
/**
* 是否存在指定商户代码
* @param corpID 商户代码
* @return boolean true:是,false:否
*/
public boolean isExistsCorpMerchantByCorpID(String corpID) {
if(StringUtil.isEmpty(corpID)) {
return false;
}
return corpMerchantsMap.containsKey(corpID);
}
/**
* 根据商户代码获取商户信息
* @param corpID 商户代码
* @return CorpMerchantDto 商户信息
*/
public CorpMerchantDto getCorpMerchantByCorpID(String corpID) {
if(StringUtil.isEmpty(corpID) || !corpMerchantsMap.containsKey(corpID)) {
return null;
}
return corpMerchantsMap.get(corpID);
}
/**
* 是否需要更新商户列表
* @return boolean
*/
private static boolean isUpdateCorpMerchants() {
boolean isUpdate = false;
SAXReader saxReader = new SAXReader();
Document document;
try {
document = saxReader.read(Thread.currentThread().getContextClassLoader().getResource(fileName));
Element root = document.getRootElement();
Attribute attribute = root.attribute("version");
if(null != attribute) {
String version = attribute.getValue();
if(!currentVersion.equals(version)) {
isUpdate = true;
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
return isUpdate;
}
/**
* 加载查询条件内容
*/
private void loadCorpMerchants() {
logger.info("******************** Load cooperation merchants*********************");
corpMerchantsMap = new HashMap<String, CorpMerchantDto>();
SAXReader saxReader = new SAXReader();
Document document;
try {
document = saxReader.read(Thread.currentThread().getContextClassLoader().getResource(fileName));
Element root = document.getRootElement();
Attribute attribute = root.attribute("version");
String version = attribute.getValue();
currentVersion = version;
for(Iterator iter = root.elementIterator(); iter.hasNext();) {
Element element = (Element)iter.next();
CorpMerchantDto corpMerchantDto = new CorpMerchantDto();
Attribute attributeOfCorpID = element.attribute("corpID");
Attribute attributeOfCoopID = element.attribute("coopID");
Attribute attributeOfActID = element.attribute("actID");
corpMerchantDto.setCorpID(attributeOfCorpID.getValue());
corpMerchantDto.setCoopID(attributeOfCoopID.getValue());
corpMerchantDto.setActID(attributeOfActID.getValue());
corpMerchantDto.setMerchantName(element.getText());
corpMerchantsMap.put(attributeOfCorpID.getValue(), corpMerchantDto);
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
/**
* test
* @param args
* void
*/
public static void main(String[] args) {
Map<String, CorpMerchantDto> map = CorpMerchantUtil.getInstance().getCorpMerchantsMap();
for(Entry<String, CorpMerchantDto> entry : map.entrySet()) {
System.out.println("corpID = " + entry.getValue().getCorpID());
System.out.println("merchantName = " + entry.getValue().getMerchantName());
}
}
}
xml
<?xml version="1.0" encoding="UTF-8"?>
<merchants version="1.0">
<merchant corpID="000001" coopID="W20130701" actID="HD0001">盈盈理财</merchant>
</merchants>
public class CorpMerchantDto implements Serializable {
/**
* @Fields serialVersionUID: 2005927276049639976L
*/
private static final long serialVersionUID = 2005927276049639976L;
/**
* 商户代码
*/
private String corpID;
/**
* 商户名称
*/
private String merchantName;
/**
* 合作代码
*/
private String coopID;
/**
* 活动代码
*/
private String actID;
/**
* @return the corpID
*/
public String getCorpID() {
return corpID;
}
/**
* @param corpID the corpID to set
*/
public void setCorpID(String corpID) {
this.corpID = corpID;
}
/**
* @return the merchantName
*/
public String getMerchantName() {
return merchantName;
}
/**
* @param merchantName the merchantName to set
*/
public void setMerchantName(String merchantName) {
this.merchantName = merchantName;
}
/**
* @return the coopID
*/
public String getCoopID() {
return coopID;
}
/**
* @param coopID the coopID to set
*/
public void setCoopID(String coopID) {
this.coopID = coopID;
}
/**
* @return the actID
*/
public String getActID() {
return actID;
}
/**
* @param actID the actID to set
*/
public void setActID(String actID) {
this.actID = actID;
}