代码
public class CMSVersion {
private CMSVersion() {
}
private static Logger log = LoggerFactory.getLogger(CMSVersion.class);
private static Root root = null;
static {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
root = (Root) jaxbUnmarshaller.unmarshal(Root.class
.getClassLoader().getResourceAsStream("cmsversion.xml"));
} catch (Exception e) {
log.error("通过jaxb解析xml为javaBean报错-------------------------" + e);
}
}
public static Map getCMSVersion(String keyName,
String platformName, String versionNumber) {
log.debug("keyName-------------------------" + keyName);
log.debug("platformName-------------------------" + platformName);
log.debug("versionNumber-------------------------" + versionNumber);
try {
if (StringUtils.isNotEmpty(keyName)
&& StringUtils.isNotEmpty(platformName)
&& StringUtils.isNotEmpty(versionNumber)) {
if (null != root) {
// 1.获取key对象
KeyProms key = getKey(keyName);
log.debug("key-------------------------" + key);
if (null != key) {
// 2.获取平台对象
Platform platform = getPlatform(key, platformName);
log.debug("platform-------------------------"
+ platform);
if (null != platform) {
// 3.获取符合的版本
Version version = getVersion(platform,
versionNumber);
log.debug("version-------------------------"
+ version);
if (null != version) {
return getContent(version);
}
}
}
}
}
} catch (Exception e) {
log.error("返回该版本号下面的所有信息报错-------------------------" + e);
}
return null;
}
private static KeyProms getKey(String keyName) {
List keyList = root.getKeyPromsList();
if (null != keyList && keyList.size() > 0) {
for (KeyProms key : keyList) {
if (keyName.equals(key.getName())) {
return key;
}
}
}
return null;
}
private static Platform getPlatform(KeyProms key, String platformName) {
List platformList = key.getPlatform();
if (null != platformList && platformList.size() > 0) {
for (Platform platform : platformList) {
if (platformName.equals(platform.getName())) {
return platform;
}
}
}
return null;
}
private static Version getVersion(Platform platform, String versionNumber) {
List versionList = platform.getVersion();
if (null != versionList && versionList.size() > 0) {
int currentVersion = Integer.valueOf(versionNumber.split("\\.")[0]);
for (Version version : versionList) {
String versionNumberTemp = version.getVersionNumber();
if (StringUtils.isNotEmpty(versionNumberTemp)) {
String[] versionNumberArray = versionNumberTemp.substring(
1, versionNumberTemp.length() - 1).split("-");
String lowStr = versionNumberArray[0];
String highStr = versionNumberArray.length == 1 ? ""
: versionNumberArray[1];
int low = "".equals(lowStr) ? 0 : Integer.valueOf(lowStr
.split("\\.")[0]);
int high = "".equals(highStr) ? 0 : Integer.valueOf(highStr
.split("\\.")[0]);
// 版本以上
if (high == 0) {
if (currentVersion >= low) {
return version;
}
}
// 版本以下
if (low == 0) {
if (currentVersion <= high) {
return version;
}
}
// 版本之间
if (high != 0 && low != 0) {
if (currentVersion <= high && low <= currentVersion) {
return version;
}
}
}
}
}
return null;
}
private static Map getContent(Version version) {
Map map = null;
List entryList = version.getEntry();
if (null != entryList && entryList.size() > 0) {
map = new LinkedHashMap();
for (Entry entry : entryList) {
map.put(entry.getTempletCode(), entry.getTempletVerson());
}
}
log.debug("content-------------------------" + map);
return map;
}
}
public class Entry {
private String templetCode;
private String templetVerson;
@XmlAttribute
public String getTempletCode() {
return templetCode;
}
public void setTempletCode(String templetCode) {
this.templetCode = templetCode;
}
@XmlAttribute
public String getTempletVerson() {
return templetVerson;
}
public void setTempletVerson(String templetVerson) {
this.templetVerson = templetVerson;
}
@Override
public String toString() {
return "Entry [templetCode=" + templetCode + ", templetVerson="
+ templetVerson + "]";
}
}
public class KeyProms {
private String name;
private List platform;
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public List getPlatform() {
return platform;
}
public void setPlatform(List platform) {
this.platform = platform;
}
@Override
public String toString() {
return "Key [name=" + name + ", platform=" + platform + "]";
}
}
public class Platform {
private String name;
private List version;
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public List getVersion() {
return version;
}
public void setVersion(List version) {
this.version = version;
}
@Override
public String toString() {
return "Platform [name=" + name + ", version=" + version + "]";
}
}
@XmlRootElement(name = "cmsversion")
public class Root {
private List keyPromsList;
@XmlElement(name = "keyproms")
public List getKeyPromsList() {
return keyPromsList;
}
public void setKeyPromsList(List keyPromsList) {
this.keyPromsList = keyPromsList;
}
@Override
public String toString() {
return "Root [keyPromsList=" + keyPromsList + "]";
}
}
public class Version {
private String versionNumber;
private List entry;
@XmlAttribute
public String getVersionNumber() {
return versionNumber;
}
public void setVersionNumber(String versionNumber) {
this.versionNumber = versionNumber;
}
@XmlElementWrapper(name = "content")
@XmlElement
public List getEntry() {
return entry;
}
public void setEntry(List entry) {
this.entry = entry;
}
@Override
public String toString() {
return "Version [versionNumber=" + versionNumber + ", entry=" + entry
+ "]";
}
}
public class CMSVersionTest {
@Test
public void CMSVersion() throws Exception {
System.out.println(CMSVersion.getCMSVersion("key", "iphone", "4.1"));
}
}