大龙的博客

常用链接

统计

最新评论

初试BeanShell和Apache Commons - Jelly

  项目需要用到一个映射功能,将一个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 -->
    </invokeStatic>
    <set target="
${destBean}" property="mailServer"
      value="
${srcBean['mail'].substring(idx.intValue(), srcBean['mail'].indexOf('.'))}" />

    <expr value="
${destList.add(destBean)}

posted on 2013-06-21 16:33 大龙 阅读(822) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理