public ConfigParser(InputStream input) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(input, this);
}
public void characters(char buffer[], int start, int length) {
accumulator.append(buffer, start, length);
}
public void endDocument() throws SAXException {
super.endDocument();
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
if (!“config”.equals(localName)) { // “config” 是根元素
String key = localName;
String value = accumulator.toString();
result.put(key, value);
}
}
public void startDocument() throws SAXException {
super.startDocument();
accumulator = new StringBuffer();
result = new HashMap
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
accumulator.setLength(0);
}
public HashMap
return result;
}
}
}
写一个常量类
package com.suntek.mobilemeeting.interfaces;
import com.suntek.mobilemeeting.config.ConfigController;
/**
常量类
@author wwj
*/
public interface Const {
String TEL_MOBILE = ConfigController.getInstance().get(“TEL_MOBILE”); // 移动的号码段
String TEL_UNICOM = ConfigController.getInstance().get(“TEL_UNICOM”); // 联通的号码段
String TELECOM = ConfigController.getInstance().get(“TELECOM”); // 电信的号码段
}
判断运营商的方法
/**
Const为常量类或接口
String TEL_MOBILE = config.get(“TEL_MOBILE”, “134,135,136,137,138,139,147,150,151,152,157,158,159,182,183,184,187,188”); //移动的号码段
String TEL_UNICOM = config.get(“TEL_UNICOM”, “130,131,132,155,156,185,186,145,176”); //联通的号码段
截至2013年12月30日 三大运营商号码段
移动:134,135,136,137,138,139,147,150,151,152,157,158,159,182,183,184,187,188
联通:130,131,132,155,156,185,186,145,176
电信:133,153,180,181,189,1700,177
@param tel
@return 运营商 (1=移动、2=联通)
*/
public static byte getTelCompany(String tel) {
String telHead = “”;
if (tel.substring(0, 4).equals(“1700”)) {
telHead = tel.substring(0, 4);
} else {
telHead = tel.substring(0, 3);
}
if (isMobileUnicom(telHead, 1)) {
return 1;
}
if (isMobileUnicom(telHead, 2)) {
return 2;
}
if (isMobileUnicom(telHead, 3)) {
return 3;
}
return -1;
}
/**
判断是哪种类型号码段
@param telHead
@param company
@return
*/
private static boolean isMobileUnicom(String telHead, int company) {
String tel = “”;
switch (company) {
case 1: // 移动号码段
tel = Const.TEL_MOBILE;
break;
case 2: // 联通号码段
tel = Const.TEL_UNICOM;
break;
case 3: // 电信号码段
tel = Const.TELECOM;
break;
default:
return false;
}
// 分割
String[] aTel = tel.split(",");
int iCount = aTel.length;