TLV格式和JAVABEAN的转换工具

TLV格式即用TAG标识某个意义,在VALUE中存放该意义对应的值,LENGTH存放该值的长度

JAVABEAN则用对应的属性存放对应属性的值

===============================================

在某些地方常常使用TLV结构,如通信报文中,而将报文解析后常常存在一个JAVABEAN中,这个可以通过报文转换javabean工具完成

 

有时候在数据库中存放的格式也是TLV,如:

CREATE TABLE TBL_TLV_INFO(TAG VARCHAR(2),TAGDESC VARCHAR(100),LEN NUMBER(5),VALUE VARCHAR(100),VALUEDESC VARCHAR(100));

 

在通过Sql语句获取到数据之后,尽管你可以建立一个VO如

public class TlvInfo{

  tag

  tagDesc

  len

  value

  valueDesc

}

来存放数据库信息.

 

但这个VO存放的信息只是数据库中信息的一个镜像,如果需要直观的得到某些信息,该怎么办呢?

================================================

例如:

TAG = '01' 表示姓名

TAG = '02' 表示性别

我可以得到俩个VO信息为:

VO1 =  TAG:G1    TAGDESC:姓名    LEN:2    VALUE:陈本    VALUEDESC:NULL

VO2 =  TAG:G2    TAGDESC:性别    LEN:2    VALUE:01       VALUEDESC:男

 

能不能直接得到一个VO信息为:

VO1 = name:陈本  sex:01 sexDesc:男

 

================================================

1。先由SPRING注入一个TAG和VO属性对应MAP:

 
   
    
     
     
      
       sex
       sexDesc    
      

     

    

   

  

 

2。配置两个VO:

TLV:

   private String tag;
   private String tagName;
   private int length;
   private String value;
   private String valueName;

TOWPROPERTY:

   private String tagvalueproperty;
   private String tagvaluedescproperty;

 

3。转换

public class Map2Obj {

 private static final String TAG_PROPERTY = "tag";
 private static final String LENGTH_PROPERTY = "length";
 private static final String VALUE_PROPERTY = "value";
 private static final String VALUEDESC_PROPERTY = "valueName";

 private HashMap ruleMap = new HashMap();

 /**
  * 根据TLVLIST转成javaBean
  *
  * @param ruleMap
  *            规则Map
  * @param tlvList
  *            tlvList
  * @param obj
  *            转换bean
  * @return
  * @throws IllegalAccessException
  * @throws InvocationTargetException
  * @throws NoSuchMethodException
  */
 public Object map2Object(List tlvList, Object obj)
   throws IllegalAccessException, InvocationTargetException,
   NoSuchMethodException {

//将参数TLV列转成MAP
  HashMap tlvMap = new HashMap();
  for (TLV tlv : tlvList) {
   tlvMap.put(tlv.getTag(), tlv);
  }

//循环遍历SPRING注入的配置map
  for (Iterator> iter = ruleMap.entrySet()
    .iterator(); iter.hasNext();) {
   Entry e = iter.next();
//配置的map的key和value值对

   Object key = e.getKey();
   Object val = ruleMap.get(key);
//在传入的参数里查找,看是否有该配置的TLV,有的情况下,取得该TLV的值,和值描述赋值,若没有,则设成NULL

   TLV tlv = tlvMap.get(key);
   if (tlv != null) {
    String value = tlv.getValue();
    String valueName = tlv.getValueName();

    //若配置的值为list则需要为值和值描述都赋值,若为单值则只需要为值赋值

    if (val instanceof java.lang.String) {
     PropertyUtils.setProperty(obj, val.toString(), value);
    } else {
     List props = (List) val;
     PropertyUtils.setProperty(obj, props.get(0), value);
     PropertyUtils.setProperty(obj, props.get(1), valueName);
    }
   } else {
    String value = "null";
    String valueName = "null";
    if (val instanceof java.lang.String) {
     PropertyUtils.setProperty(obj, val.toString(), value);
    } else {
     List props = (List) val;
     PropertyUtils.setProperty(obj, props.get(0), value);
     PropertyUtils.setProperty(obj, props.get(1), valueName);
    }
   }
  }

  return obj;
 }

 /**
  * 根据javaBean转成TLVLIST,逆向
  *
  * @param ruleMap
  *            规则map
  * @param tlvList
  *            tlvList
  * @param obj
  *            转换BEAN
  * @return
  * @throws IllegalAccessException
  * @throws InvocationTargetException
  * @throws NoSuchMethodException
  */
 public List object2Map(List tlvList, Object obj)
   throws IllegalAccessException, InvocationTargetException,
   NoSuchMethodException {
  TLV tlv = null;
  for (Iterator> iter = ruleMap.entrySet()
    .iterator(); iter.hasNext();) {
   Entry e = iter.next();
   Object key = e.getKey();
   Object val = ruleMap.get(key);

   tlv = new TLV();

   if (val instanceof java.lang.String) {
    Object value = PropertyUtils.getProperty(obj, (String) val);
    if (value != null && !value.equals("")) {
     PropertyUtils.setProperty(tlv, TAG_PROPERTY, key);
     PropertyUtils.setProperty(tlv, LENGTH_PROPERTY, value
       .toString().length());
     PropertyUtils.setProperty(tlv, VALUE_PROPERTY, value);
     tlvList.add(tlv);
    }
   } else {
    List props = (List) val;
    Object value = PropertyUtils.getProperty(obj, props.get(0));
    Object valueName = PropertyUtils.getProperty(obj, props.get(1));
    if (value != null && !value.equals("")) {
     PropertyUtils.setProperty(tlv, TAG_PROPERTY, key);
     PropertyUtils.setProperty(tlv, LENGTH_PROPERTY, value
       .toString().length());
     PropertyUtils.setProperty(tlv, VALUE_PROPERTY, value);
     PropertyUtils.setProperty(tlv, VALUEDESC_PROPERTY,
       valueName);
     tlvList.add(tlv);
    }
   }
  }

  return tlvList;
 }

 public HashMap getRuleMap() {
  return ruleMap;
 }

 public void setRuleMap(HashMap ruleMap) {
  this.ruleMap = ruleMap;
 }
}

 

你可能感兴趣的:(TLV格式和JAVABEAN的转换工具)