增加会员卡,增加摇一摇部分设备接口。
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
package com.foxinmy.weixin4j.model.card;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
|
||||
/**
|
||||
* 会员卡积分规则
|
||||
*
|
||||
* @auther: Feng Yapeng
|
||||
* @since: 2016/12/15 11:43
|
||||
*/
|
||||
public class MemCardBonusRule {
|
||||
|
||||
/**
|
||||
* 消费金额。以分为单位。
|
||||
*/
|
||||
@JSONField(name = "cost_money_unit")
|
||||
private int costMoneyUnit;
|
||||
/**
|
||||
* 对应增加的积分
|
||||
*/
|
||||
@JSONField(name = "increase_bonus")
|
||||
private int increaseBonus;
|
||||
/**
|
||||
* 用户单次可获取的积分上限。
|
||||
*/
|
||||
@JSONField(name = "max_increase_bonus ")
|
||||
private int maxIncreaseBonus;
|
||||
/**
|
||||
* 初始设置积分【可以理解为开卡积分】
|
||||
*/
|
||||
@JSONField(name = "init_increase_bonus")
|
||||
private int initIncreaseBonus;
|
||||
/**
|
||||
*每使用N 积分
|
||||
*/
|
||||
@JSONField(name = "cost_bonus_unit")
|
||||
private int costBonusUnit;
|
||||
/**
|
||||
* 抵扣多少分
|
||||
*/
|
||||
@JSONField(name = "reduce_money")
|
||||
private int reduceMoney;
|
||||
/**
|
||||
* 抵扣条件,满xx分(这里以分为单位)可用
|
||||
*/
|
||||
@JSONField(name = "least_money_to_use_bonus")
|
||||
private int leastMoneyToUseBonus;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@JSONField(name = "max_reduce_bonus")
|
||||
private int maxReduceBonus;
|
||||
|
||||
|
||||
public int getCostMoneyUnit() {
|
||||
return costMoneyUnit;
|
||||
}
|
||||
|
||||
public void setCostMoneyUnit(int costMoneyUnit) {
|
||||
this.costMoneyUnit = costMoneyUnit;
|
||||
}
|
||||
|
||||
public int getIncreaseBonus() {
|
||||
return increaseBonus;
|
||||
}
|
||||
|
||||
public void setIncreaseBonus(int increaseBonus) {
|
||||
this.increaseBonus = increaseBonus;
|
||||
}
|
||||
|
||||
public int getMaxIncreaseBonus() {
|
||||
return maxIncreaseBonus;
|
||||
}
|
||||
|
||||
public void setMaxIncreaseBonus(int maxIncreaseBonus) {
|
||||
this.maxIncreaseBonus = maxIncreaseBonus;
|
||||
}
|
||||
|
||||
public int getInitIncreaseBonus() {
|
||||
return initIncreaseBonus;
|
||||
}
|
||||
|
||||
public void setInitIncreaseBonus(int initIncreaseBonus) {
|
||||
this.initIncreaseBonus = initIncreaseBonus;
|
||||
}
|
||||
|
||||
public int getCostBonusUnit() {
|
||||
return costBonusUnit;
|
||||
}
|
||||
|
||||
public void setCostBonusUnit(int costBonusUnit) {
|
||||
this.costBonusUnit = costBonusUnit;
|
||||
}
|
||||
|
||||
public int getReduceMoney() {
|
||||
return reduceMoney;
|
||||
}
|
||||
|
||||
public void setReduceMoney(int reduceMoney) {
|
||||
this.reduceMoney = reduceMoney;
|
||||
}
|
||||
|
||||
public int getLeastMoneyToUseBonus() {
|
||||
return leastMoneyToUseBonus;
|
||||
}
|
||||
|
||||
public void setLeastMoneyToUseBonus(int leastMoneyToUseBonus) {
|
||||
this.leastMoneyToUseBonus = leastMoneyToUseBonus;
|
||||
}
|
||||
|
||||
public int getMaxReduceBonus() {
|
||||
return maxReduceBonus;
|
||||
}
|
||||
|
||||
public void setMaxReduceBonus(int maxReduceBonus) {
|
||||
this.maxReduceBonus = maxReduceBonus;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.foxinmy.weixin4j.model.card;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.foxinmy.weixin4j.type.card.FieldNameType;
|
||||
|
||||
/**
|
||||
* 会员卡自定义类型
|
||||
*
|
||||
* @auther: Feng Yapeng
|
||||
* @since: 2016/12/15 10:49
|
||||
*/
|
||||
public class MemCardCustomField {
|
||||
|
||||
/**
|
||||
*会员信息类目半自定义名称,当开发者变更这类类目信息的value值时,可以选择触发系统模板消息通知用户。
|
||||
*/
|
||||
@JSONField(name = "name_type")
|
||||
private FieldNameType nameType;
|
||||
|
||||
/**
|
||||
* 会员信息类目自定义名称,当开发者变更这类类目信息的value值时, 不会触发系统模板消息通知用户
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 点击类目跳转外链url
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* s
|
||||
*/
|
||||
private String tips;
|
||||
|
||||
public MemCardCustomField(FieldNameType fieldNameType, String url) {
|
||||
this.nameType = fieldNameType;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public MemCardCustomField(FieldNameType fieldNameType, String name, String url) {
|
||||
this.nameType = fieldNameType;
|
||||
this.name = name;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public MemCardCustomField(String name, String url, String tips) {
|
||||
this.name = name;
|
||||
this.url = url;
|
||||
this.tips = tips;
|
||||
}
|
||||
|
||||
public FieldNameType getNameType() {
|
||||
return nameType;
|
||||
}
|
||||
|
||||
public void setNameType(FieldNameType nameType) {
|
||||
this.nameType = nameType;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,384 @@
|
||||
package com.foxinmy.weixin4j.model.card;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.foxinmy.weixin4j.type.card.CardType;
|
||||
import com.foxinmy.weixin4j.type.card.FieldNameType;
|
||||
|
||||
/**
|
||||
* 会员卡
|
||||
* @auther: Feng Yapeng
|
||||
* @since: 2016/12/15 10:09
|
||||
*/
|
||||
public class MemberCard extends CardCoupon {
|
||||
|
||||
/**
|
||||
* 会员卡背景图 [商家自定义会员卡背景图,须先调用上传图片接口将背景图上传至CDN,否则报错,
|
||||
* 卡面设计请遵循微信会员卡自定义背景设计规范 ,像素大小控制在1000像素*600像素以下]
|
||||
*/
|
||||
@JSONField(name = "background_pic_url")
|
||||
private String backgroundPicUrl;
|
||||
|
||||
/**
|
||||
* 会员卡特权说明。
|
||||
*/
|
||||
@JSONField(name = "prerogative")
|
||||
private String prerogative;
|
||||
/**
|
||||
* 用户领取会员卡后系统自动将其激活,无需调用激活接口,详情见自动激活。
|
||||
*/
|
||||
@JSONField(name = "auto_activate")
|
||||
private boolean autoActivate;
|
||||
/**
|
||||
* 设置为true时会员卡支持一键开卡,不允许同时传入activate_url字段,否则设置wx_activate失效。
|
||||
*/
|
||||
@JSONField(name = "wx_activate")
|
||||
private boolean wxActivate;
|
||||
/**
|
||||
* 激活会员卡的url。
|
||||
*/
|
||||
@JSONField(name = "activate_url")
|
||||
private String activateUrl;
|
||||
|
||||
/**
|
||||
* 显示积分 【true:积分相关字段均为必填】
|
||||
*/
|
||||
@JSONField(name = "supply_bonus")
|
||||
private boolean supplyBonus;
|
||||
/**
|
||||
* 设置跳转外链查看积分详情。仅适用于积分无法通过激活接口同步的情况下使用该字段
|
||||
*/
|
||||
@JSONField(name = "bonus_url")
|
||||
private String bonusUrl;
|
||||
/**
|
||||
* 显示余额
|
||||
*/
|
||||
@JSONField(name = "supply_balance")
|
||||
private String supplyBalance;
|
||||
/**
|
||||
* 设置跳转外链查看余额详情。仅适用于余额无法通过激活接口同步的情况下使用该字段。
|
||||
*/
|
||||
@JSONField(name = "balance_url")
|
||||
private String balanceUrl;
|
||||
|
||||
/**
|
||||
* 自定义会员信息类目
|
||||
*/
|
||||
@JSONField(name = "custom_field1")
|
||||
private MemCardCustomField customField1;
|
||||
/**
|
||||
* 自定义会员信息类目
|
||||
*/
|
||||
@JSONField(name = "custom_field2")
|
||||
private MemCardCustomField customField2;
|
||||
/**
|
||||
* 自定义会员信息类目
|
||||
*/
|
||||
@JSONField(name = "custom_field3")
|
||||
private MemCardCustomField customField3;
|
||||
|
||||
/**
|
||||
* 积分规则说明
|
||||
*/
|
||||
@JSONField(name = "bonus_rules")
|
||||
private String bonusRules;
|
||||
/**
|
||||
* 储值说明。
|
||||
*/
|
||||
@JSONField(name = "balance_rules")
|
||||
private String balanceRules;
|
||||
/**
|
||||
* 积分清零规则
|
||||
*/
|
||||
@JSONField(name = "bonus_cleared")
|
||||
private String bonusCleared;
|
||||
/**
|
||||
* 自定义会员信息类目,会员卡激活后显示
|
||||
*/
|
||||
@JSONField(name = "custom_cell1")
|
||||
private MemCardCustomField customCell1;
|
||||
|
||||
/**
|
||||
* 折扣【该会员卡享受的折扣优惠,填10就是九折。】
|
||||
*/
|
||||
private int discount;
|
||||
|
||||
@JSONField(name = "bonus_rule")
|
||||
private MemCardBonusRule bonusRule;
|
||||
|
||||
|
||||
/**
|
||||
* 卡券
|
||||
*
|
||||
* @param couponBaseInfo 基础信息
|
||||
*/
|
||||
protected MemberCard(CouponBaseInfo couponBaseInfo, Builder builder) {
|
||||
super(couponBaseInfo);
|
||||
this.activateUrl = builder.activateUrl;
|
||||
this.backgroundPicUrl = builder.backgroundPicUrl;
|
||||
this.prerogative = builder.prerogative;
|
||||
this.autoActivate = builder.autoActivate;
|
||||
this.wxActivate = builder.wxActivate;
|
||||
this.activateUrl = builder.activateUrl;
|
||||
this.supplyBonus = builder.supplyBonus;
|
||||
this.bonusUrl = builder.bonusUrl;
|
||||
this.supplyBalance = builder.supplyBalance;
|
||||
this.balanceUrl = builder.balanceUrl;
|
||||
this.customField1 = builder.customField1;
|
||||
this.customField2 = builder.customField2;
|
||||
this.customField3 = builder.customField3;
|
||||
this.bonusRules = builder.bonusRules;
|
||||
this.balanceRules = builder.balanceRules;
|
||||
this.bonusCleared = builder.bonusCleared;
|
||||
this.customCell1 = builder.customCell1;
|
||||
this.discount = builder.discount;
|
||||
this.bonusRule = builder.bonusRule;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CardType getCardType() {
|
||||
return CardType.MEMBER_CARD;
|
||||
}
|
||||
|
||||
public String getBackgroundPicUrl() {
|
||||
return backgroundPicUrl;
|
||||
}
|
||||
|
||||
public String getPrerogative() {
|
||||
return prerogative;
|
||||
}
|
||||
|
||||
public boolean isAutoActivate() {
|
||||
return autoActivate;
|
||||
}
|
||||
|
||||
public boolean isWxActivate() {
|
||||
return wxActivate;
|
||||
}
|
||||
|
||||
public String getActivateUrl() {
|
||||
return activateUrl;
|
||||
}
|
||||
|
||||
public boolean isSupplyBonus() {
|
||||
return supplyBonus;
|
||||
}
|
||||
|
||||
public String getBonusUrl() {
|
||||
return bonusUrl;
|
||||
}
|
||||
|
||||
public String getSupplyBalance() {
|
||||
return supplyBalance;
|
||||
}
|
||||
|
||||
public String getBalanceUrl() {
|
||||
return balanceUrl;
|
||||
}
|
||||
|
||||
public MemCardCustomField getCustomField1() {
|
||||
return customField1;
|
||||
}
|
||||
|
||||
public MemCardCustomField getCustomField2() {
|
||||
return customField2;
|
||||
}
|
||||
|
||||
public MemCardCustomField getCustomField3() {
|
||||
return customField3;
|
||||
}
|
||||
|
||||
public String getBonusRules() {
|
||||
return bonusRules;
|
||||
}
|
||||
|
||||
public String getBalanceRules() {
|
||||
return balanceRules;
|
||||
}
|
||||
|
||||
public String getBonusCleared() {
|
||||
return bonusCleared;
|
||||
}
|
||||
|
||||
public MemCardCustomField getCustomCell1() {
|
||||
return customCell1;
|
||||
}
|
||||
|
||||
public int getDiscount() {
|
||||
return discount;
|
||||
}
|
||||
|
||||
public MemCardBonusRule getBonusRule() {
|
||||
return bonusRule;
|
||||
}
|
||||
|
||||
|
||||
public static final class Builder {
|
||||
|
||||
/**
|
||||
* 会员卡背景图 [商家自定义会员卡背景图,须先调用上传图片接口将背景图上传至CDN,否则报错,
|
||||
* 卡面设计请遵循微信会员卡自定义背景设计规范 ,像素大小控制在1000像素*600像素以下]
|
||||
*/
|
||||
private String backgroundPicUrl;
|
||||
|
||||
/**
|
||||
* 会员卡特权说明。
|
||||
*/
|
||||
private String prerogative;
|
||||
/**
|
||||
* 用户领取会员卡后系统自动将其激活,无需调用激活接口,详情见自动激活。
|
||||
*/
|
||||
private boolean autoActivate;
|
||||
/**
|
||||
* 设置为true时会员卡支持一键开卡,不允许同时传入activate_url字段,否则设置wx_activate失效。
|
||||
*/
|
||||
private boolean wxActivate;
|
||||
/**
|
||||
* 激活会员卡的url。
|
||||
*/
|
||||
private String activateUrl;
|
||||
|
||||
/**
|
||||
* 显示积分 【true:积分相关字段均为必填】
|
||||
*/
|
||||
private boolean supplyBonus;
|
||||
/**
|
||||
* 设置跳转外链查看积分详情。仅适用于积分无法通过激活接口同步的情况下使用该字段
|
||||
*/
|
||||
private String bonusUrl;
|
||||
/**
|
||||
* 显示余额
|
||||
*/
|
||||
private String supplyBalance;
|
||||
/**
|
||||
* 设置跳转外链查看余额详情。仅适用于余额无法通过激活接口同步的情况下使用该字段。
|
||||
*/
|
||||
private String balanceUrl;
|
||||
/**
|
||||
* 自定义会员信息类目
|
||||
*/
|
||||
private MemCardCustomField customField1;
|
||||
/**
|
||||
* 自定义会员信息类目
|
||||
*/
|
||||
private MemCardCustomField customField2;
|
||||
/**
|
||||
* 自定义会员信息类目
|
||||
*/
|
||||
private MemCardCustomField customField3;
|
||||
/**
|
||||
* 积分规则说明
|
||||
*/
|
||||
private String bonusRules;
|
||||
/**
|
||||
* 储值说明。
|
||||
*/
|
||||
private String balanceRules;
|
||||
/**
|
||||
* 积分清零规则
|
||||
*/
|
||||
private String bonusCleared;
|
||||
/**
|
||||
* 自定义会员信息类目,会员卡激活后显示
|
||||
*/
|
||||
private MemCardCustomField customCell1;
|
||||
/**
|
||||
* 折扣【该会员卡享受的折扣优惠,填10就是九折。】
|
||||
*/
|
||||
private int discount;
|
||||
/**
|
||||
* 积分规则
|
||||
*/
|
||||
private MemCardBonusRule bonusRule;
|
||||
|
||||
|
||||
public Builder setBackgroundPicUrl(String backgroundPicUrl) {
|
||||
this.backgroundPicUrl = backgroundPicUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setPrerogative(String prerogative) {
|
||||
this.prerogative = prerogative;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setAutoActivate(boolean autoActivate) {
|
||||
this.autoActivate = autoActivate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setWxActivate(boolean wxActivate) {
|
||||
this.wxActivate = wxActivate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setActivateUrl(String activateUrl) {
|
||||
this.activateUrl = activateUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setSupplyBonus(boolean supplyBonus) {
|
||||
this.supplyBonus = supplyBonus;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setBonusUrl(String bonusUrl) {
|
||||
this.bonusUrl = bonusUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setSupplyBalance(String supplyBalance) {
|
||||
this.supplyBalance = supplyBalance;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setBalanceUrl(String balanceUrl) {
|
||||
this.balanceUrl = balanceUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setCustomField1(FieldNameType type, String name, String url) {
|
||||
this.customField1 = new MemCardCustomField(type, name, url);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setCustomField2(FieldNameType type, String name, String url) {
|
||||
this.customField2 = new MemCardCustomField(type, name, url);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setCustomField3(FieldNameType type, String name, String url) {
|
||||
this.customField3 = new MemCardCustomField(type, name, url);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setBonusRules(String bonusRules) {
|
||||
this.bonusRules = bonusRules;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setBalanceRules(String balanceRules) {
|
||||
this.balanceRules = balanceRules;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setBonusCleared(String bonusCleared) {
|
||||
this.bonusCleared = bonusCleared;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setCustomCell1(String name, String url, String tips) {
|
||||
this.customCell1 = new MemCardCustomField(name, url, tips);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setDiscount(int discount) {
|
||||
this.discount = discount;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setBonusRule(MemCardBonusRule bonusRule) {
|
||||
this.bonusRule = bonusRule;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.foxinmy.weixin4j.type.card;
|
||||
|
||||
/**
|
||||
* 会员卡的状态
|
||||
*
|
||||
* @auther: Feng Yapeng
|
||||
* @since: 2016/12/19 11:39
|
||||
*/
|
||||
public enum CardStatus {
|
||||
|
||||
/**
|
||||
* 待审核;
|
||||
*/
|
||||
CARD_STATUS_NOT_VERIFY,
|
||||
|
||||
/**
|
||||
* 审核失败
|
||||
*/
|
||||
CARD_STATUS_VERIFY_FAIL,
|
||||
|
||||
/**
|
||||
* 通过审核
|
||||
*/
|
||||
CARD_STATUS_VERIFY_OK,
|
||||
|
||||
/**
|
||||
* 卡券被商户删除
|
||||
*/
|
||||
CARD_STATUS_DELETE,
|
||||
|
||||
/**
|
||||
* 在公众平台投放过的卡券
|
||||
*/
|
||||
CARD_STATUS_DISPATCH;
|
||||
|
||||
}
|
||||
@@ -29,5 +29,10 @@ public enum CardType {
|
||||
/**
|
||||
* 优惠券
|
||||
*/
|
||||
GENERAL_COUPON;
|
||||
GENERAL_COUPON,
|
||||
|
||||
/**
|
||||
* 会员卡
|
||||
*/
|
||||
MEMBER_CARD;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.foxinmy.weixin4j.type.card;
|
||||
|
||||
/**
|
||||
* 会员信息类目半自定义名称
|
||||
*
|
||||
* @auther: Feng Yapeng
|
||||
* @since: 2016/12/15 10:37
|
||||
*/
|
||||
public enum FieldNameType {
|
||||
/**
|
||||
* 等级
|
||||
*/
|
||||
FIELD_NAME_TYPE_LEVEL,
|
||||
/**
|
||||
* 优惠券
|
||||
*/
|
||||
FIELD_NAME_TYPE_COUPON,
|
||||
/**
|
||||
* 印花
|
||||
*/
|
||||
FIELD_NAME_TYPE_STAMP,
|
||||
/**
|
||||
* 折扣
|
||||
*/
|
||||
FIELD_NAME_TYPE_DISCOUNT,
|
||||
/**
|
||||
* 成就
|
||||
*/
|
||||
FIELD_NAME_TYPE_ACHIEVEMEN,
|
||||
/**
|
||||
* 里程
|
||||
*/
|
||||
FIELD_NAME_TYPE_MILEAGE,
|
||||
/**
|
||||
* 集点
|
||||
*/
|
||||
FIELD_NAME_TYPE_SET_POINTS,
|
||||
/**
|
||||
* 次数
|
||||
*/
|
||||
FIELD_NAME_TYPE_TIMS;
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package com.foxinmy.weixin4j.util;
|
||||
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 接口调用错误获取
|
||||
*
|
||||
* @author fengyapeng
|
||||
* @className WeixinErrorUtil2
|
||||
* @date
|
||||
* @see
|
||||
* @since JDK 1.6
|
||||
*/
|
||||
public final class WeixinErrorUtil2 {
|
||||
|
||||
private static byte[] errorXmlByteArray;
|
||||
private final static Map<String, String> errorCacheMap;
|
||||
|
||||
static {
|
||||
errorCacheMap = new ConcurrentHashMap<String, String>();
|
||||
try {
|
||||
errorXmlByteArray = IOUtil.toByteArray(WeixinResponse.class.getResourceAsStream("error.xml"));
|
||||
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
|
||||
ContentHandler textHandler = new ErrorTextHandler(errorCacheMap);
|
||||
xmlReader.setContentHandler(textHandler);
|
||||
xmlReader.parse(new InputSource(new ByteArrayInputStream(errorXmlByteArray)));
|
||||
} catch (IOException e) {
|
||||
;
|
||||
} catch (SAXException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static class ErrorTextHandler extends DefaultHandler {
|
||||
|
||||
private Map<String, String> errorCacheMap;
|
||||
|
||||
public ErrorTextHandler(Map<String, String> errorCacheMap) {
|
||||
this.errorCacheMap = errorCacheMap;
|
||||
}
|
||||
|
||||
private String code;
|
||||
private String text;
|
||||
private boolean codeElement;
|
||||
private boolean textElement;
|
||||
private int isPair = 0;
|
||||
|
||||
@Override
|
||||
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
||||
codeElement = qName.equalsIgnoreCase("code");
|
||||
textElement = qName.equalsIgnoreCase("text");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endElement(String uri, String localName, String qName) throws SAXException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void characters(char[] ch, int start, int length) throws SAXException {
|
||||
String _text = new String(ch, start, length);
|
||||
if (codeElement) {
|
||||
isPair++;
|
||||
code = _text;
|
||||
codeElement = false;
|
||||
}
|
||||
if (textElement) {
|
||||
isPair++;
|
||||
text = _text;
|
||||
textElement = false;
|
||||
}
|
||||
if (isPair == 2) {
|
||||
// 配对成功
|
||||
errorCacheMap.put(code, text);
|
||||
isPair = 0;
|
||||
code = null;
|
||||
text = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getText(String code) throws RuntimeException {
|
||||
return errorCacheMap.get(code);
|
||||
}
|
||||
|
||||
public static String getText(String code, String defaultMsg) throws RuntimeException {
|
||||
String text = getText(code);
|
||||
if (StringUtil.isNotBlank(text)) {
|
||||
return text;
|
||||
}
|
||||
return defaultMsg;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(getText("40001"));
|
||||
System.out.println(getText("30002"));
|
||||
System.out.println(getText("1234"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user