论坛首页 Java企业应用论坛

用测试理解如何使用ognl: 读取和设置包含筛选条件的列表元素的属性。

浏览 2991 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-11-20   最后修改:2009-11-20

先转一篇介绍

OGNL 语言介绍与实践 

 

官网上的资料比较精炼,例子很少,还是写一些测试代码来验证用法:

 

包装一下:

package org.airlinkmatrix.test.ognl;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

public class OgnlExpression {
	
	private static final OgnlContext DEFAULT_CONTEXT = (OgnlContext)Ognl.createDefaultContext(null);
	
	private Object expression;
	private OgnlContext context;

	public void setContext(OgnlContext context) {
		this.context = context;
	}

	public OgnlContext getContext() {
		if (context==null){
			context = DEFAULT_CONTEXT;
		}
		return context;
	}
	
	public OgnlExpression(String expressionString) throws OgnlException
	{
		super();
		expression = Ognl.parseExpression(expressionString);
	}

	public Object getExpression()
	{
		return expression;
	}

	public Object getValue(Object rootObject) throws OgnlException
	{
		return Ognl.getValue(getExpression(), getContext(), rootObject);
	}

	public void setValue(Object rootObject, Object value) throws OgnlException
	{
		Ognl.setValue(getExpression(), getContext(), rootObject, value);
	}

}

 测试类:

package org.airlinkmatrix.test.ognl;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.airlinkmatrix.test.ognl.OgnlExpression;

import junit.framework.TestCase;
import ognl.OgnlException;

public class OgnlExpressionTest extends TestCase {

	class Root{
		private Object[] array;
		public Object[] getArray() {
			return array;
		}
		public void setArray(Object[] array) {
			this.array = array;
		}
		
		private Vector vector;
		public Vector getVector() {
			return vector;
		}
		public void setVector(Vector vector) {
			this.vector = vector;
		}
		public Root() {
			vector = new Vector();
			vector.add(new Part("Michael","002"));
			vector.add(new Part("Alex","001"));
			vector.add(new Part("Joseph","003"));
			vector.add(new Part("Alex","004"));
			
			array = new Part[4];
			array[0] = new Part("Michael","002");
			array[1] = new Part("Alex","001");
			array[2] = new Part("Joseph","003");
			array[3] = new Part("Alex","004");
		}
	}
	
	class Part{
		private String id;
		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		private String name;

		public Part(String name,String id) {
			super();
			this.name = name;
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String toString() {
//			return "name='"+name+"',id='"+id+"'";
			return name;
		}

		public boolean equals(Object obj) {
			if (!(obj instanceof Part)){
				return false;
			}
			Part anotherPart = (Part)obj;
			if (this.name==null||this.id==null){
				return false;
			}
			if (!this.name.equals(anotherPart.getName())){
				return false;
			}
			if (!this.id.equals(anotherPart.getId())){
				return false;
			}
			return true;
		}
		
	}
	
	private Root ROOT;

	protected void setUp() throws Exception {
		super.setUp();
		ROOT = new Root();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	public void testGetValue_ROOT_vector() {
		try {
			OgnlExpression expression = null;
			ArrayList valueList = null;
			
			//?号表示匹配所有记录,
			expression = new OgnlExpression("vector.{? #this.name == 'Alex' }");
			valueList = (ArrayList)expression.getValue(ROOT);
			assertEquals(new Part("Alex","001"),valueList.get(0));
			assertEquals(new Part("Alex","004"),valueList.get(1));
			
			//^号表示匹配第一条记录
			expression = new OgnlExpression("vector.{^ #this.name == 'Alex' }");
			valueList = (ArrayList)expression.getValue(ROOT);
			assertEquals(new Part("Alex","001"),valueList.get(0));

			//$号表示匹配最后一条记录
			expression = new OgnlExpression("vector.{$ #this.name == 'Alex' }");
			valueList = (ArrayList)expression.getValue(ROOT);
			assertEquals(new Part("Alex","004"),valueList.get(0));

			//?号匹配多条记录,所以有可能选到第二条记录
			expression = new OgnlExpression("vector.{? #this.name == 'Alex' }[1].id");
			assertEquals("004",expression.getValue(ROOT));

			//可以接着取记录属性
			expression = new OgnlExpression("vector.{^ #this.name == 'Alex' }[0].id");
			assertEquals("001",expression.getValue(ROOT));

			try{
				//^号匹配单条记录,第二条记录一定是空的,会报IndexOutOfBoundsException
				expression = new OgnlExpression("vector.{^ #this.name == 'Alex' }[1].id");
				expression.getValue(ROOT);
				fail("expect IndexOutOfBoundsException");
			}catch (IndexOutOfBoundsException ex){
				
			}catch (Exception otherEx){
				fail("expect IndexOutOfBoundsException");
			}
			
			try{
				//^号匹配单条记录,如果匹配不到数据,仍然会报IndexOutOfBoundsException
				//这个与官网的文档是有出入的,^号并不能避免IndexOutOfBoundsException
				expression = new OgnlExpression("vector.{^ #this.name == 'Wrong Name' }[0].id");
				expression.getValue(ROOT);
				fail("expect IndexOutOfBoundsException");
			}catch (IndexOutOfBoundsException ex){
				
			}catch (Exception otherEx){
				fail("expect IndexOutOfBoundsException");
			}
			
		} catch (OgnlException e) {
			e.printStackTrace();
			fail("oops!");
		}
	}

	
	public void testGetValue_ROOT_array() {
		
		try {
			OgnlExpression expression = null;
			ArrayList valueList = null;
			
			//?号表示匹配所有记录,
			expression = new OgnlExpression("array.{? #this.name == 'Alex' }");
			valueList = (ArrayList)expression.getValue(ROOT);
			assertEquals(new Part("Alex","001"),valueList.get(0));
			assertEquals(new Part("Alex","004"),valueList.get(1));
			
			//^号表示匹配第一条记录
			expression = new OgnlExpression("array.{^ #this.name == 'Alex' }");
			valueList = (ArrayList)expression.getValue(ROOT);
			assertEquals(new Part("Alex","001"),valueList.get(0));

			//$号表示匹配最后一条记录
			expression = new OgnlExpression("array.{$ #this.name == 'Alex' }");
			valueList = (ArrayList)expression.getValue(ROOT);
			assertEquals(new Part("Alex","004"),valueList.get(0));

			//?号匹配多条记录,所以有可能选到第二条记录
			expression = new OgnlExpression("array.{? #this.name == 'Alex' }[1].id");
			assertEquals("004",expression.getValue(ROOT));

			//可以接着取记录属性
			expression = new OgnlExpression("array.{^ #this.name == 'Alex' }[0].id");
			assertEquals("001",expression.getValue(ROOT));

			try{
				//^号匹配单条记录,第二条记录一定是空的,会报IndexOutOfBoundsException
				expression = new OgnlExpression("array.{^ #this.name == 'Alex' }[1].id");
				expression.getValue(ROOT);
				fail("expect IndexOutOfBoundsException");
			}catch (IndexOutOfBoundsException ex){
				
			}catch (Exception otherEx){
				fail("expect IndexOutOfBoundsException");
			}
			
			try{
				//^号匹配单条记录,如果匹配不到数据,仍然会报IndexOutOfBoundsException
				//这个与官网的文档是有出入的,^号并不能避免IndexOutOfBoundsException
				expression = new OgnlExpression("array.{^ #this.name == 'Wrong Name' }[0].id");
				expression.getValue(ROOT);
				fail("expect IndexOutOfBoundsException");
			}catch (IndexOutOfBoundsException ex){
				
			}catch (Exception otherEx){
				fail("expect IndexOutOfBoundsException");
			}
			
		} catch (OgnlException e) {
			e.printStackTrace();
			fail("oops!");
		}
	}
	
	public void testSetValue_ROOT_Normal() {
		
		try {
			OgnlExpression expression = null;
			
			expression = new OgnlExpression("array.{^ #this.name == 'Alex' }[0].id");
			expression.setValue(ROOT,"007");
			
			assertEquals("007",((Part)ROOT.getArray()[1]).getId());
			
		} catch (OgnlException e) {
			e.printStackTrace();
			fail("oops!");
		}
	}
	
	
}
 

读取、设置对象图内部的一个值没有多大问题了。但,ognl 组件是否能够配置为设置列表中每一个元素的某字段为同一值呢?

例如,对象图如下:

ROOT

   Part

     id="1"

   Part

     id="2"

   Part

     id="3"

 

其中ROOT拥有Part的一个列表(java.util.List)

 

有没有一种写法,能利用ognl表达式一次将上述对象的值改为:

ROOT

   Part

     id="007"

   Part

     id= "007"

   Part

     id= "007"

 

在ognl语言参考中没有找到。

尝试自己扩展一个吧。

   发表时间:2009-11-21   最后修改:2009-11-21

写一个测试当用例:

 

	/**
	 * 扩展OgnlExpression setValue语法,例如
	 * "array.{^ #this.name == 'Alex' }[].id" 
	 * 表示将设置列表中name等于Alex的所有元素的id属性
	 */
	public void testSetValue_ROOT_Multiple_Normal() {
		
		try {
			OgnlExpression expression = null;
			
			expression = new OgnlExpression("array.{^ #this.name == 'Alex' }[].id");
			expression.setValue(ROOT, "007");
			
			//匹配的第二第四条记录值改变了
			assertEquals("007",((Part)ROOT.getArray()[1]).getId());
			assertEquals("007",((Part)ROOT.getArray()[3]).getId());
			
			//匹配的第一第三条记录值没变
			assertEquals("002",((Part)ROOT.getArray()[0]).getId());
			assertEquals("003",((Part)ROOT.getArray()[2]).getId());
		} catch (OgnlException e) {
			e.printStackTrace();
			fail("oops!");
		}
	}
 

 

 

0 请登录后投票
   发表时间:2009-11-21   最后修改:2009-11-21

继续,为了保留原来的类和测试功能作为参考,我新写了一个ExtendedOgnlExpression包装类和相应的测试ExtendedOgnlExpressionTest

 

ExtendedOgnlExpressionTest如下:

 

package org.airlinkmatrix.test.ognl;

import java.util.Vector;

import junit.framework.TestCase;
import ognl.OgnlException;

public class ExtendedOgnlExpressionTest extends TestCase {

	class Root{
		private Object[] array;
		public Object[] getArray() {
			return array;
		}
		public void setArray(Object[] array) {
			this.array = array;
		}
		
		private Vector vector;
		public Vector getVector() {
			return vector;
		}
		public void setVector(Vector vector) {
			this.vector = vector;
		}
		public Root() {
			vector = new Vector();
			vector.add(new Part("Michael","002"));
			vector.add(new Part("Alex","001"));
			vector.add(new Part("Joseph","003"));
			vector.add(new Part("Alex","004"));
			
			array = new Part[4];
			array[0] = new Part("Michael","002");
			array[1] = new Part("Alex","001");
			array[2] = new Part("Joseph","003");
			array[3] = new Part("Alex","004");
		}
	}
	
	class Part{
		private String id;
		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		private String name;

		public Part(String name,String id) {
			super();
			this.name = name;
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String toString() {
//			return "name='"+name+"',id='"+id+"'";
			return name;
		}

		public boolean equals(Object obj) {
			if (!(obj instanceof Part)){
				return false;
			}
			Part anotherPart = (Part)obj;
			if (this.name==null||this.id==null){
				return false;
			}
			if (!this.name.equals(anotherPart.getName())){
				return false;
			}
			if (!this.id.equals(anotherPart.getId())){
				return false;
			}
			return true;
		}
		
	}
	
	private Root ROOT;

	protected void setUp() throws Exception {
		super.setUp();
		ROOT = new Root();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	public void testSetValue_ROOT_Normal() {
		
		try {
			OgnlExpression expression = null;
			
			expression = new OgnlExpression("array.{^ #this.name == 'Alex' }[0].id");
			expression.setValue(ROOT,"007");
			
			assertEquals("007",((Part)ROOT.getArray()[1]).getId());
			
		} catch (OgnlException e) {
			e.printStackTrace();
			fail("oops!");
		}
	}
	/**
	 * 扩展OgnlExpression setValue语法,例如
	 * "array.{^ #this.name == 'Alex' }[].id" 
	 * 表示将设置列表中name等于Alex的所有元素的id属性
	 */
	public void testSetValue_ROOT_Multiple_Normal() {
		
		try {
			ExtendedOgnlExpression expression = null;
			
			expression = new ExtendedOgnlExpression("array.{? #this.name == 'Alex' }[].id");
			expression.setValue(ROOT, "007");
			
			//匹配的第二第四条记录值改变了
			assertEquals("007",((Part)ROOT.getArray()[1]).getId());
			assertEquals("007",((Part)ROOT.getArray()[3]).getId());
			
			//没匹配的第一第三条记录值没变
			assertEquals("002",((Part)ROOT.getArray()[0]).getId());
			assertEquals("003",((Part)ROOT.getArray()[2]).getId());
		} catch (OgnlException e) {
			e.printStackTrace();
			fail("oops!");
		}
	}
}

 简单起见,做了一些copy&paste。 修正测试用例的一个小错误, ^  改为 ? ,即

new ExtendedOgnlExpression("array.{? #this.name == 'Alex' }[].id");

 

实现思路是在[]中填充index,然后依赖IndexOutOfBoundsException来判断结果是否已经遍历结束(有点dirty,但貌似没其他办法)。

 

实现如下:

package org.airlinkmatrix.test.ognl;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

public class ExtendedOgnlExpression {
	
	private static final OgnlContext DEFAULT_CONTEXT = (OgnlContext)Ognl.createDefaultContext(null);
	
	private OgnlContext context;

	private String expressionString;

	public void setContext(OgnlContext context) {
		this.context = context;
	}

	public OgnlContext getContext() {
		if (context==null){
			context = DEFAULT_CONTEXT;
		}
		return context;
	}
	
	public ExtendedOgnlExpression(String expressionString) 
	{
		super();
		this.expressionString = expressionString;
	}

	public Object getExpression(String expressionString) throws OgnlException
	{
		return Ognl.parseExpression(expressionString);
	}

	public Object getValue(Object rootObject) throws OgnlException
	{
		if (expressionString.indexOf("[]")>0){
			throw new OgnlException("getValue do not support []");
		}
		return Ognl.getValue(getExpression(expressionString), getContext(), rootObject);
	}

	public void setValue(Object rootObject, Object value) throws OgnlException
	{
		if (expressionString.indexOf("[]")>0){
			setMultipleValue(rootObject, value);
				
		}else{
			setValue(rootObject, value, expressionString);
		}
		
	}

	private void setMultipleValue(Object rootObject, Object value)
			throws OgnlException {
		//依次在[]中填入0,1,2,...,设置目标值,直到超出匹配列表的最后一个,发生IndexOutOfBoundsException为止
		int i = 0;
		while(true){
			try {
				setValue(rootObject, value, addIndex(expressionString,i));
			} catch (IndexOutOfBoundsException e) {
				e.printStackTrace();
				break;
			}
			i++;
		}
	}

	private void setValue(Object rootObject, Object value,String expressionString) throws OgnlException {
		Ognl.setValue(getExpression(expressionString), getContext(), rootObject, value);
	}

	private String addIndex(String expressionString, int i) {
		int index = expressionString.indexOf("[]");
		String str = expressionString.substring(0,index+1)
						+String.valueOf(i)
						+expressionString.substring(index+1);
			
		return str;
	}
	
}

 

运行测试,成功:)

 

 

 

0 请登录后投票
   发表时间:2009-11-21  
这样就支持了出现单个列表[]符号的多记录赋值。

下一步可以考虑支持多个列表[]符号的两重或多重列表记录的赋值。

0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics