com.anysoft.cache.SimpleModel Maven / Gradle / Ivy
package com.anysoft.cache;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.anysoft.cache.Cacheable;
import com.anysoft.util.JsonTools;
import com.anysoft.util.Properties;
import com.anysoft.util.XmlTools;
import com.anysoft.util.code.Coder;
import com.anysoft.util.code.CoderFactory;
/**
* 简单缓存对象的实现
*
* @author duanyy
*
* @since 1.0.12
* @version 1.3.0 [20140727 duanyy]
* - Cachable修正类名为Cacheable
*
* @version 1.4.4 [20140912 duanyy]
* - JsonSerializer中Map参数化
*
* @version 1.6.3.2 [20150213 duanyy]
* - 接口{@link com.anysoft.cache.Cacheable Cacheable}增加了{@link com.anysoft.cache.Cacheable#expire() Cacheable.expire}方法
*
* @version 1.6.4.17 [20151216 duanyy]
* - 根据sonar建议优化代码
*
* @version 1.6.4.20 [20151222 duanyy]
* - 根据sonar建议优化代码
*/
public class SimpleModel extends Properties implements Cacheable {
protected String id = "";
protected HashMap fields = new HashMap(); // NOSONAR
public SimpleModel(String theId){
id = theId;
}
@Override
public void fromXML(Element root) {
fields.clear();
NodeList nodeList = XmlTools.getNodeListByPath(root, "fields/field");
if (nodeList != null && nodeList.getLength() > 0){
int length = nodeList.getLength();
for (int i = 0; i < length ; i ++){
Node n = nodeList.item(i);
if (n.getNodeType() != Node.ELEMENT_NODE){
continue;
}
Element e = (Element)n;
String name = e.getAttribute("id");
String value = e.getAttribute("value"); // NOSONAR
String coder = e.getAttribute("coder"); // NOSONAR
coder = StringUtils.isEmpty(coder) ? "Default":coder; // NOSONAR
if (StringUtils.isNotBlank(name) && StringUtils.isNotEmpty(value)){
Field field = new Field();
field.name = name;
field.coder = coder;
String isRaw = e.getAttribute("isRaw");
Coder aCoder = CoderFactory.newCoder(field.coder);
if (aCoder == null || BooleanUtils.toBoolean(isRaw)){ // NOSONAR
field.value = value;
}else{
String key = e.getAttribute("key");
field.value = aCoder.decode(value,key);
}
fields.put(field.name, field);
}
}
}
}
@Override
public void toXML(Element root) {
root.setAttribute("id", id);
Collection collection = fields.values();
if (!collection.isEmpty()){
Document doc = root.getOwnerDocument();
Element fieldsElem = doc.createElement("fields"); // NOSONAR
for (Field field:collection){
Element fieldElem = doc.createElement("field");
fieldElem.setAttribute("id", field.name);
fieldElem.setAttribute("coder", field.coder);
Coder coder = CoderFactory.newCoder(field.coder);
if (coder != null){
String key = coder.createKey();
fieldElem.setAttribute("value", coder.encode(field.value,key));
if (StringUtils.isNotEmpty(key)){ // NOSONAR
fieldElem.setAttribute("key", key);
}
}else{
fieldElem.setAttribute("value", field.value);
}
fieldsElem.appendChild(fieldElem);
}
root.appendChild(fieldsElem);
}
}
@SuppressWarnings("unchecked")
@Override
public void fromJson(Map root) { // NOSONAR
fields.clear();
Object fieldsObject = root.get("fields");
if (fieldsObject != null && fieldsObject instanceof List){
for (Object fieldObject:(List