Chinaunix首页 | 论坛 | 博客
  • 博客访问: 533422
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Java

2010-03-20 19:47:47

  项目需要用到一个映射功能,将一个Bean的属性,条件的映射到另外一个bean的属性里。刚开始的设计是自己定义一套XML标签,解析,执行。但是这样的话,特别麻烦,且不说你设计的XML时候易用,易扩展。
  用Google百度了下,发现使用 BeanShell最为方便, Apache Commons - Jelly 次之(但最接近原有设计)。下面是做的测试例子。

输出结果:

mapped by BeanShell :
[{dep=ssod, nickName=btpka3, age=18, group=nonage, mailServer=163}, {dep=ssod, nickName=mp3f4, age=25, group=adult, mailServer=gmail}]
mapped by Apache Commons - Jelly :
[{dep=ssod, nickName=btpka3, age=18, group=nonage, mailServer=163}, {dep=ssod, nickName=mp3f4, age=25, group=adult, mailServer=gmail}]


TestMapping.java

package jp.co.nttdata.autofill.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.output.NullOutputStream;
import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.XMLOutput;

import bsh.Interpreter;
import edu.emory.mathcs.backport.java.util.Collections;

public class TestMapping {
    public static void main(String[] args) throws Exception {
        System.out.println("mapped by BeanShell :");
        testBeanShell();

        System.out.println("mapped by Apache Commons - Jelly :");
        testJelly();
    }

    public static void testBeanShell() throws Exception {
        List<Map<String, ?>> destList = new ArrayList<Map<String, ?>>();

        List<Map<String, String>> srcList = new ArrayList<Map<String, String>>();
        Map<String, String> srcMap = new HashMap<String, String>();
        srcMap.put("name", "btpka3");
        srcMap.put("age", "18");
        srcMap.put("mail", "btpka3@163.com");
        srcList.add(srcMap);

        srcMap = new HashMap<String, String>();
        srcMap.put("name", "mp3f4");
        srcMap.put("age", "25");
        srcMap.put("sex", "F");
        srcMap.put("mail", "mp3f4@gmail.com");
        srcList.add(srcMap);

        Interpreter intp = new Interpreter();
        intp.set("srcList", Collections.unmodifiableCollection(srcList));
        intp.set("destList", destList);

        String script = "/autofill/jp/co/nttdata/autofill/test/test.bsh";
        intp.source(script);

        System.out.println(destList);

    }

    public static void testJelly() throws Exception {

        List<Map<String, ?>> destList = new ArrayList<Map<String, ?>>();

        List<Map<String, String>> srcList = new ArrayList<Map<String, String>>();
        Map<String, String> srcMap = new HashMap<String, String>();
        srcMap.put("name", "btpka3");
        srcMap.put("age", "18");
        srcMap.put("mail", "btpka3@163.com");
        srcList.add(srcMap);

        srcMap = new HashMap<String, String>();
        srcMap.put("name", "mp3f4");
        srcMap.put("age", "25");
        srcMap.put("sex", "F");
        srcMap.put("mail", "mp3f4@gmail.com");
        srcList.add(srcMap);

        JellyContext context = new JellyContext();
        context.setVariable("srcList", Collections
                .unmodifiableCollection(srcList));
        context.setVariable("destList", destList);
        String script = "/autofill/jp/co/nttdata/autofill/test/test.xml";
        XMLOutput xmlOutput = XMLOutput.createXMLOutput(new NullOutputStream());
        context.runScript(script, xmlOutput);

        System.out.println(destList);
    }
}


test.bsh

import java.util.List;
import java.util.Map;
import java.util.LinkedHashMap;

        for (Map srcBean : srcList) {
            Map destBean = new LinkedHashMap();
            
            // 項目⓪
            destBean.put("dep", "ssod");

            // 項目①
            destBean.put("nickName", srcBean.get("name"));

            // 項目②
            destBean.put("age", srcBean.get("age"));

            // 項目③
            if (Integer.valueOf(destBean.get("age")) <= 18) {
                destBean.put("group", "nonage");
            } else {
                destBean.put("group", "adult");
            }

            // 項目④
            String srcMailAddr = srcBean.get("mail");
            destBean.put("mailServer", srcMailAddr.substring(srcMailAddr.indexOf("@")+1, srcMailAddr.indexOf(".")));
            
            destList.add(destBean);
        }


test.xml

<?xml version="1.0" encoding="UTF-8"?>
<jelly trim="false" xmlns="jelly:core">
  <forEach items="${srcList}" var="srcBean">
    <new var="destBean" className="java.util.LinkedHashMap" />

    <!-- 項目&#9450; -->
    <set target="${destBean}" property="dep" value="ssod" />

    <!-- 項目① -->
    <set target="${destBean}" property="nickName" value="${srcBean.name}" />

    <!-- 項目② -->
    <set target="${destBean}" property="age" value="${srcBean['age']}" />

    <!-- 項目③ -->
    <choose>
      <invokeStatic var="intAge" className="java.lang.Integer"
        method="valueOf">
        <arg type="java.lang.String" value="${srcBean.age}" />
      </invokeStatic>
      <when test="${intAge le 18}">
        <set target="${destBean}" property="group" value="nonage" />
      </when>
      <otherwise>
        <set target="${destBean}" property="group" value="adult" />
      </otherwise>
    </choose>

    <!-- 項目④ -->
    <invokeStatic var="idx" className="java.lang.Long" method="valueOf">
      <arg type="long" value="${srcBean['mail'].indexOf('@')+1}" />
      <!-- NOTICE : "${1}' class is Integer, but '${1+1}' is Long -->
    
    $
{destBean}" property="mailServer"
      value="
${srcBean['mail'].substring(idx.intValue(), srcBean['mail'].indexOf('.'))}" />

    $
{destList.add(destBean)}


阅读(2048) | 评论(0) | 转发(0) |
0

上一篇:大学毕业1年半后的总结

下一篇:JCE - AES

给主人留下些什么吧!~~