You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jc...@apache.org on 2007/11/03 16:59:56 UTC

svn commit: r591639 - in /wicket/trunk/jdk-1.4/wicket/src: main/java/org/apache/wicket/util/lang/PropertyResolver.java test/java/org/apache/wicket/util/lang/PropertyResolverTest.java

Author: jcompagner
Date: Sat Nov  3 08:59:56 2007
New Revision: 591639

URL: http://svn.apache.org/viewvc?rev=591639&view=rev
Log:
fix for supporting . in side brackets for map lookup WICKET-988 

Modified:
    wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/lang/PropertyResolver.java
    wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/lang/PropertyResolverTest.java

Modified: wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/lang/PropertyResolver.java
URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/lang/PropertyResolver.java?rev=591639&r1=591638&r2=591639&view=diff
==============================================================================
--- wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/lang/PropertyResolver.java (original)
+++ wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/lang/PropertyResolver.java Sat Nov  3 08:59:56 2007
@@ -107,27 +107,27 @@
 	 *            The convertor to convert the value if needed to the right type.
 	 */
 	public final static void setValue(final String expression, final Object object, Object value,
-			PropertyResolverConverter converter)
+		PropertyResolverConverter converter)
 	{
 		if (expression == null || expression.equals(""))
 		{
 			throw new WicketRuntimeException("Empty expression setting value: " + value +
-					" on object: " + object);
+				" on object: " + object);
 		}
 		if (object == null)
 		{
 			throw new WicketRuntimeException("Null object setting value: " + value +
-					" with expression: " + expression);
+				" with expression: " + expression);
 		}
 
 		ObjectAndGetSetter setter = getObjectAndGetSetter(expression, object, CREATE_NEW_VALUE);
 		if (setter == null)
 		{
 			throw new WicketRuntimeException("Null object returned for expression: " + expression +
-					" for setting value: " + value + " on: " + object);
+				" for setting value: " + value + " on: " + object);
 		}
 		setter.setValue(value, converter == null ? new PropertyResolverConverter(Application.get()
-				.getConverterLocator(), Session.get().getLocale()) : converter);
+			.getConverterLocator(), Session.get().getLocale()) : converter);
 	}
 
 	/**
@@ -141,7 +141,7 @@
 		if (setter == null)
 		{
 			throw new WicketRuntimeException("Null object returned for expression: " + expression +
-					" for getting the target classs of: " + object);
+				" for getting the target classs of: " + object);
 		}
 		return setter.getTargetClass();
 	}
@@ -158,7 +158,7 @@
 		if (setter == null)
 		{
 			throw new WicketRuntimeException("Null object returned for expression: " + expression +
-					" for getting the target classs of: " + object);
+				" for getting the target classs of: " + object);
 		}
 		return setter.getField();
 	}
@@ -175,7 +175,7 @@
 		if (setter == null)
 		{
 			throw new WicketRuntimeException("Null object returned for expression: " + expression +
-					" for getting the target classs of: " + object);
+				" for getting the target classs of: " + object);
 		}
 		return setter.getGetter();
 	}
@@ -192,17 +192,17 @@
 		if (setter == null)
 		{
 			throw new WicketRuntimeException("Null object returned for expression: " + expression +
-					" for getting the target classs of: " + object);
+				" for getting the target classs of: " + object);
 		}
 		return setter.getSetter();
 	}
 
 	private static ObjectAndGetSetter getObjectAndGetSetter(final String expression,
-			final Object object, int tryToCreateNull)
+		final Object object, int tryToCreateNull)
 	{
 		final String expressionBracketsSeperated = Strings.replaceAll(expression, "[", ".[")
-				.toString();
-		int index = expressionBracketsSeperated.indexOf('.');
+			.toString();
+		int index = getNextDotIndex(expressionBracketsSeperated, 0);
 		int lastIndex = 0;
 		Object value = object;
 		Class clz = value.getClass();
@@ -220,11 +220,11 @@
 
 				// expression by it self can't be found. try to find a
 				// setPropertyByIndex(int,value) method
-				index = expressionBracketsSeperated.indexOf('.', index + 1);
+				index = getNextDotIndex(expressionBracketsSeperated, index + 1);
 				if (index != -1)
 				{
 					String indexExpression = expressionBracketsSeperated
-							.substring(lastIndex, index);
+						.substring(lastIndex, index);
 					getAndSetter = getGetAndSetter(indexExpression, clz);
 				}
 				else
@@ -265,7 +265,7 @@
 			}
 
 			lastIndex = index + 1;
-			index = expressionBracketsSeperated.indexOf('.', lastIndex);
+			index = getNextDotIndex(expressionBracketsSeperated, lastIndex);
 			if (index == -1)
 			{
 				exp = expressionBracketsSeperated.substring(lastIndex);
@@ -277,6 +277,28 @@
 	}
 
 
+	private static int getNextDotIndex(String expression, int start)
+	{
+		boolean insideBracket = false;
+		for (int i = start; i < expression.length(); i++)
+		{
+			char ch = expression.charAt(i);
+			if (ch == '.' && !insideBracket)
+			{
+				return i;
+			}
+			else if (ch == '[')
+			{
+				insideBracket = true;
+			}
+			else if (ch == ']')
+			{
+				insideBracket = false;
+			}
+		}
+		return -1;
+	}
+
 	private final static IGetAndSet getGetAndSetter(String exp, Class clz)
 	{
 		Map classesToGetAndSetters = getClassesToGetAndSetters();
@@ -325,7 +347,7 @@
 						if (method != null)
 						{
 							getAndSetter = new MethodGetAndSet(method, MethodGetAndSet.findSetter(
-									method, clz), null);
+								method, clz), null);
 						}
 						else
 						{
@@ -337,10 +359,10 @@
 							else
 							{
 								throw new WicketRuntimeException(
-										"The expression '" +
-												exp +
-												"' is neither an index nor is it a method or field for the list " +
-												clz);
+									"The expression '" +
+										exp +
+										"' is neither an index nor is it a method or field for the list " +
+										clz);
 							}
 						}
 					}
@@ -365,7 +387,7 @@
 						else
 						{
 							throw new WicketRuntimeException("can't parse the exp " + exp +
-									" as an index for an array lookup");
+								" as an index for an array lookup");
 						}
 					}
 				}
@@ -390,7 +412,7 @@
 									// getPropertyIndex(int)
 									// and setPropertyIndex(int, object)
 									String name = Character.toUpperCase(propertyName.charAt(0)) +
-											propertyName.substring(1);
+										propertyName.substring(1);
 									method = clz.getMethod("get" + name, new Class[] { int.class });
 									getAndSetter = new ArrayPropertyGetSet(method, parsedIndex);
 
@@ -398,8 +420,8 @@
 								catch (Exception e)
 								{
 									throw new WicketRuntimeException(
-											"no get method defined for class: " + clz +
-													" expression: " + propertyName);
+										"no get method defined for class: " + clz +
+											" expression: " + propertyName);
 								}
 							}
 							else
@@ -409,14 +431,14 @@
 								// not good
 								// programming with beans patterns
 								throw new WicketRuntimeException(
-										"No get method defined for class: " + clz +
-												" expression: " + exp);
+									"No get method defined for class: " + clz + " expression: " +
+										exp);
 							}
 						}
 						else
 						{
 							getAndSetter = new MethodGetAndSet(method, MethodGetAndSet.findSetter(
-									method, clz), field);
+								method, clz), field);
 						}
 					}
 					else
@@ -429,7 +451,7 @@
 			{
 				field = findField(clz, exp);
 				getAndSetter = new MethodGetAndSet(method, MethodGetAndSet.findSetter(method, clz),
-						field);
+					field);
 			}
 			getAndSetters.put(exp, getAndSetter);
 		}
@@ -608,7 +630,7 @@
 		public Object getValue(final Object object);
 
 		/**
-		 * @return
+		 * @return The target class of the object that as to be set.
 		 */
 		public Class getTargetClass();
 
@@ -626,7 +648,7 @@
 		 * @param converter
 		 */
 		public void setValue(final Object object, final Object value,
-				PropertyResolverConverter converter);
+			PropertyResolverConverter converter);
 
 		/**
 		 * @return Field or null if there is no field
@@ -813,7 +835,7 @@
 			catch (Exception e)
 			{
 				log.warn("Cannot set new value " + value + " at index " + index +
-						" for array holding elements of class " + clzComponentType, e);
+					" for array holding elements of class " + clzComponentType, e);
 			}
 			return value;
 		}
@@ -907,12 +929,12 @@
 			catch (InvocationTargetException ex)
 			{
 				throw new WicketRuntimeException("Error calling index property method: " +
-						getMethod + " on object: " + object, ex.getCause());
+					getMethod + " on object: " + object, ex.getCause());
 			}
 			catch (Exception ex)
 			{
 				throw new WicketRuntimeException("Error calling index property method: " +
-						getMethod + " on object: " + object, ex);
+					getMethod + " on object: " + object, ex);
 			}
 			return ret;
 		}
@@ -933,7 +955,7 @@
 				if (converted == null && value != null)
 				{
 					throw new ConversionException("Can't convert value: " + value + " to class: " +
-							getMethod.getReturnType() + " for setting it on " + object);
+						getMethod.getReturnType() + " for setting it on " + object);
 				}
 				try
 				{
@@ -942,18 +964,18 @@
 				catch (InvocationTargetException ex)
 				{
 					throw new WicketRuntimeException("Error index property calling method: " +
-							setMethod + " on object: " + object, ex.getCause());
+						setMethod + " on object: " + object, ex.getCause());
 				}
 				catch (Exception ex)
 				{
 					throw new WicketRuntimeException("Error index property calling method: " +
-							setMethod + " on object: " + object, ex);
+						setMethod + " on object: " + object, ex);
 				}
 			}
 			else
 			{
 				throw new WicketRuntimeException("no set method defined for value: " + value +
-						" on object: " + object);
+					" on object: " + object);
 			}
 		}
 
@@ -1023,12 +1045,12 @@
 			catch (InvocationTargetException ex)
 			{
 				throw new WicketRuntimeException("Error calling method: " + getMethod +
-						" on object: " + object, ex.getCause());
+					" on object: " + object, ex.getCause());
 			}
 			catch (Exception ex)
 			{
 				throw new WicketRuntimeException("Error calling method: " + getMethod +
-						" on object: " + object, ex);
+					" on object: " + object, ex);
 			}
 			return ret;
 		}
@@ -1039,7 +1061,7 @@
 		 * @param converter
 		 */
 		public final void setValue(final Object object, final Object value,
-				PropertyResolverConverter converter)
+			PropertyResolverConverter converter)
 		{
 			if (setMethod != null)
 			{
@@ -1049,14 +1071,14 @@
 					if (value != null)
 					{
 						throw new ConversionException("Can't convert value: " + value +
-								" to class: " + getMethod.getReturnType() + " for setting it on " +
-								object);
+							" to class: " + getMethod.getReturnType() + " for setting it on " +
+							object);
 					}
 					else if (getMethod.getReturnType().isPrimitive())
 					{
 						throw new ConversionException(
-								"Can't convert null value to a primitive class: " +
-										getMethod.getReturnType() + " for setting it on " + object);
+							"Can't convert null value to a primitive class: " +
+								getMethod.getReturnType() + " for setting it on " + object);
 					}
 				}
 				try
@@ -1066,18 +1088,18 @@
 				catch (InvocationTargetException ex)
 				{
 					throw new WicketRuntimeException("Error calling method: " + setMethod +
-							" on object: " + object, ex.getCause());
+						" on object: " + object, ex.getCause());
 				}
 				catch (Exception ex)
 				{
 					throw new WicketRuntimeException("Error calling method: " + setMethod +
-							" on object: " + object, ex);
+						" on object: " + object, ex);
 				}
 			}
 			else
 			{
 				throw new WicketRuntimeException("no set method defined for value: " + value +
-						" on object: " + object);
+					" on object: " + object);
 			}
 		}
 
@@ -1198,7 +1220,7 @@
 			catch (Exception ex)
 			{
 				throw new WicketRuntimeException("Error getting field value of field " + field +
-						" from object " + object, ex);
+					" from object " + object, ex);
 			}
 		}
 
@@ -1235,7 +1257,7 @@
 			catch (Exception ex)
 			{
 				throw new WicketRuntimeException("Error setting field value of field " + field +
-						" on object " + object + ", value " + value, ex);
+					" on object " + object + ", value " + value, ex);
 			}
 		}
 

Modified: wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/lang/PropertyResolverTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/lang/PropertyResolverTest.java?rev=591639&r1=591638&r2=591639&view=diff
==============================================================================
--- wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/lang/PropertyResolverTest.java (original)
+++ wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/lang/PropertyResolverTest.java Sat Nov  3 08:59:56 2007
@@ -42,7 +42,7 @@
 public class PropertyResolverTest extends TestCase
 {
 	private static final PropertyResolverConverter CONVERTER = new PropertyResolverConverter(
-			new ConverterLocator(), Locale.US);
+		new ConverterLocator(), Locale.US);
 
 	private Person person;
 	private MockWebApplication app;
@@ -151,7 +151,7 @@
 		{
 			PropertyResolver.setValue("country.name", person, "US", CONVERTER);
 			throw new Exception(
-					"name can't be set on a country that doesn't have default constructor");
+				"name can't be set on a country that doesn't have default constructor");
 		}
 		catch (WicketRuntimeException ex)
 		{
@@ -212,6 +212,23 @@
 	/**
 	 * @throws Exception
 	 */
+	public void testMapWithDotLookup() throws Exception
+	{
+		Address address = new Address();
+		HashMap hm = new HashMap();
+		PropertyResolver.setValue("addressMap", person, hm, CONVERTER);
+		PropertyResolver.setValue("addressMap[address.test]", person, address, CONVERTER);
+		assertNotNull(hm.get("address.test"));
+		PropertyResolver.setValue("addressMap[address.test].street", person, "wicket-street",
+			CONVERTER);
+		String street = (String)PropertyResolver
+			.getValue("addressMap[address.test].street", person);
+		assertEquals(street, "wicket-street");
+	}
+
+	/**
+	 * @throws Exception
+	 */
 	public void testListLookup() throws Exception
 	{
 		PropertyResolver.setValue("addressList", person, new ArrayList(), CONVERTER);
@@ -232,7 +249,7 @@
 	public void testArrayLookup() throws Exception
 	{
 		PropertyResolver.setValue("addressArray", person, new Address[] { new Address(), null },
-				CONVERTER);
+			CONVERTER);
 		PropertyResolver.setValue("addressArray.0.street", person, "wicket-street", CONVERTER);
 		String street = (String)PropertyResolver.getValue("addressArray.0.street", person);
 		assertEquals(street, "wicket-street");
@@ -248,7 +265,7 @@
 	public void testArrayLookupByBrackets() throws Exception
 	{
 		PropertyResolver.setValue("addressArray", person, new Address[] { new Address(), null },
-				CONVERTER);
+			CONVERTER);
 		PropertyResolver.setValue("addressArray[0].street", person, "wicket-street", CONVERTER);
 		String street = (String)PropertyResolver.getValue("addressArray[0].street", person);
 		assertEquals(street, "wicket-street");