rdonkin 2003/08/27 16:28:07
Modified: beanutils/src/java/org/apache/commons/beanutils
BeanUtilsBean.java PropertyUtilsBean.java
beanutils/src/test/org/apache/commons/beanutils
BeanUtilsTestCase.java
Log:
Fixed bug with parsing mapped properties which contained values with dots.
Revision Changes Path
1.12 +36 -5
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtilsBean.java
Index: BeanUtilsBean.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtilsBean.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- BeanUtilsBean.java 7 Jul 2003 22:00:02 -0000 1.11
+++ BeanUtilsBean.java 27 Aug 2003 23:28:07 -0000 1.12
@@ -919,7 +919,7 @@
// Resolve any nested expression to get the actual target bean
Object target = bean;
- int delim = name.lastIndexOf(PropertyUtils.NESTED_DELIM);
+ int delim = findLastNestedIndex(name);
if (delim >= 0) {
try {
target =
@@ -1067,6 +1067,37 @@
(e, "Cannot set " + propName);
}
+ }
+
+ private int findLastNestedIndex(String expression)
+ {
+ // walk back from the end to the start
+ // and find the first index that
+ int bracketCount = 0;
+ for (int i = expression.length() - 1; i>=0 ; i--) {
+ char at = expression.charAt(i);
+ switch (at) {
+ case PropertyUtils.NESTED_DELIM:
+ if (bracketCount < 1) {
+ return i;
+ }
+ break;
+
+ case PropertyUtils.MAPPED_DELIM:
+ case PropertyUtils.INDEXED_DELIM:
+ // not bothered which
+ --bracketCount;
+ break;
+
+ case PropertyUtils.MAPPED_DELIM2:
+ case PropertyUtils.INDEXED_DELIM2:
+ // not bothered which
+ ++bracketCount;
+ break;
+ }
+ }
+ // can't find any
+ return -1;
}
/**
1.10 +40 -7
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java
Index: PropertyUtilsBean.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- PropertyUtilsBean.java 3 Aug 2003 00:43:52 -0000 1.9
+++ PropertyUtilsBean.java 27 Aug 2003 23:28:07 -0000 1.10
@@ -799,8 +799,8 @@
}
// Resolve nested references
- while (true) {
- int period = name.indexOf(PropertyUtils.NESTED_DELIM);
+ while (true) {
+ int period = findNextNestedIndex(name);
if (period < 0) {
break;
}
@@ -841,9 +841,10 @@
if ((bean == null) || (name == null)) {
return (null);
}
-
+
PropertyDescriptor descriptors[] = getPropertyDescriptors(bean);
if (descriptors != null) {
+
for (int i = 0; i < descriptors.length; i++) {
if (name.equals(descriptors[i].getName()))
return (descriptors[i]);
@@ -870,8 +871,40 @@
mappedDescriptors.put(name, result);
}
}
+
return result;
+ }
+
+ private int findNextNestedIndex(String expression)
+ {
+ // walk back from the end to the start
+ // and find the first index that
+ int bracketCount = 0;
+ for (int i=0, size=expression.length(); i<size ; i++) {
+ char at = expression.charAt(i);
+ switch (at) {
+ case PropertyUtils.NESTED_DELIM:
+ if (bracketCount < 1) {
+ return i;
+ }
+ break;
+
+ case PropertyUtils.MAPPED_DELIM:
+ case PropertyUtils.INDEXED_DELIM:
+ // not bothered which
+ ++bracketCount;
+ break;
+
+ case PropertyUtils.MAPPED_DELIM2:
+ case PropertyUtils.INDEXED_DELIM2:
+ // not bothered which
+ --bracketCount;
+ break;
+ }
+ }
+ // can't find any
+ return -1;
}
1.25 +14 -5
jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java
Index: BeanUtilsTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -r1.24 -r1.25
--- BeanUtilsTestCase.java 26 Mar 2003 19:51:46 -0000 1.24
+++ BeanUtilsTestCase.java 27 Aug 2003 23:28:07 -0000 1.25
@@ -1344,5 +1344,14 @@
}
}
-
+ public void testMappedProperty() throws Exception {
+ MappedPropertyTestBean bean = new MappedPropertyTestBean();
+
+ BeanUtils.setProperty(bean, "mapproperty(this.that.the-other)",
"some.dotty.value");
+
+ assertEquals(
+ "Mapped property set correctly",
+ "some.dotty.value",
+ bean.getMapproperty("this.that.the-other"));
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]