Author: swd847
Date: 2010-04-05 04:58:08 -0400 (Mon, 05 Apr 2010)
New Revision: 12380
Added:
modules/xml/trunk/core/src/main/java/org/jboss/seam/xml/model/PropertyXmlItem.java
Modified:
modules/xml/trunk/core/src/main/java/org/jboss/seam/xml/model/ModelBuilder.java
modules/xml/trunk/core/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java
modules/xml/trunk/core/src/test/java/org/jboss/seam/xml/test/fieldset/FieldValueBean.java
modules/xml/trunk/core/src/test/java/org/jboss/seam/xml/test/fieldset/SetFieldValueBeanTest.java
modules/xml/trunk/core/src/test/resources/org/jboss/seam/xml/test/fieldset/set-field-value-beans.xml
Log:
allow setting of values for getter/setter pairs that don't have an underlying field
Modified: modules/xml/trunk/core/src/main/java/org/jboss/seam/xml/model/ModelBuilder.java
===================================================================
---
modules/xml/trunk/core/src/main/java/org/jboss/seam/xml/model/ModelBuilder.java 2010-04-04
19:11:04 UTC (rev 12379)
+++
modules/xml/trunk/core/src/main/java/org/jboss/seam/xml/model/ModelBuilder.java 2010-04-05
08:58:08 UTC (rev 12380)
@@ -121,6 +121,14 @@
fields.add(f);
}
}
+ for (PropertyXmlItem xi : rb.getChildrenOfType(PropertyXmlItem.class))
+ {
+ FieldValueObject f = xi.getFieldValue();
+ if (f != null)
+ {
+ fields.add(f);
+ }
+ }
if (!fields.isEmpty())
{
ret.getFieldValues().put(tp, fields);
Added: modules/xml/trunk/core/src/main/java/org/jboss/seam/xml/model/PropertyXmlItem.java
===================================================================
--- modules/xml/trunk/core/src/main/java/org/jboss/seam/xml/model/PropertyXmlItem.java
(rev 0)
+++
modules/xml/trunk/core/src/main/java/org/jboss/seam/xml/model/PropertyXmlItem.java 2010-04-05
08:58:08 UTC (rev 12380)
@@ -0,0 +1,123 @@
+/*
+ * Distributed under the LGPL License
+ *
+ */
+package org.jboss.seam.xml.model;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.jboss.seam.xml.fieldset.ArrayFieldSet;
+import org.jboss.seam.xml.fieldset.CollectionFieldSet;
+import org.jboss.seam.xml.fieldset.FieldValueObject;
+import org.jboss.seam.xml.fieldset.FieldValueSetter;
+import org.jboss.seam.xml.fieldset.MapFieldSet;
+import org.jboss.seam.xml.fieldset.MethodFieldSetter;
+import org.jboss.seam.xml.fieldset.SimpleFieldValue;
+import org.jboss.seam.xml.util.XmlConfigurationException;
+
+public class PropertyXmlItem extends AbstractXmlItem
+{
+
+ FieldValueSetter fieldSetter;
+ FieldValueObject fieldValue;
+ String name;
+ Class<?> type;
+ HashSet<XmlItemType> allowed = new HashSet<XmlItemType>();
+
+ public PropertyXmlItem(XmlItem parent, String name, Method setter, String innerText,
String document, int lineno)
+ {
+ super(XmlItemType.FIELD, parent, parent.getJavaClass(), innerText, null, document,
lineno);
+ this.name = name;
+ this.type = setter.getParameterTypes()[0];
+ this.fieldSetter = new MethodFieldSetter(setter);
+ if (innerText != null && innerText.length() > 0)
+ {
+ fieldValue = new SimpleFieldValue(parent.getJavaClass(), fieldSetter,
innerText);
+ }
+ allowed.add(XmlItemType.VALUE);
+ }
+
+ public FieldValueObject getFieldValue()
+ {
+ return fieldValue;
+ }
+
+ @Override
+ public boolean resolveChildren()
+ {
+ List<EntryXmlItem> mapEntries = new ArrayList<EntryXmlItem>();
+ List<XmlItem> valueEntries = new ArrayList<XmlItem>();
+ if (fieldValue == null)
+ {
+ for (XmlItem i : children)
+ {
+ if (i.getType() == XmlItemType.VALUE)
+ {
+ valueEntries.add(i);
+ }
+ else if (i.getType() == XmlItemType.ENTRY)
+ {
+ mapEntries.add((EntryXmlItem) i);
+ }
+
+ }
+ }
+ if (!mapEntries.isEmpty() || !valueEntries.isEmpty())
+ {
+ if (Map.class.isAssignableFrom(type))
+ {
+ if (!valueEntries.isEmpty())
+ {
+ throw new XmlConfigurationException("Map fields cannot have
<value> elements as children,only <entry> elements Field:" +
parent.getJavaClass().getName() + '.' + name, getDocument(), getLineno());
+ }
+ if (!mapEntries.isEmpty())
+ {
+ fieldValue = new MapFieldSet(fieldSetter, mapEntries);
+ }
+ }
+ else if (Collection.class.isAssignableFrom(type) || type.isArray())
+ {
+ if (!mapEntries.isEmpty())
+ {
+ throw new XmlConfigurationException("Collection fields must be set
using <value> not <entry> Field:" + parent.getJavaClass().getName() +
'.' + name, getDocument(), getLineno());
+ }
+ if (!valueEntries.isEmpty())
+ {
+ if (type.isArray())
+ {
+ fieldValue = new ArrayFieldSet(fieldSetter, valueEntries);
+ }
+ else
+ {
+ fieldValue = new CollectionFieldSet(fieldSetter, valueEntries);
+ }
+ }
+ }
+ else
+ {
+ if (!mapEntries.isEmpty())
+ {
+ throw new XmlConfigurationException("Only Map fields can be set using
<entry> Field:" + parent.getJavaClass().getName() + '.' + name,
getDocument(), getLineno());
+ }
+ if (valueEntries.size() != 1)
+ {
+ throw new XmlConfigurationException("Non collection fields can only
have a single <value> element Field:" + parent.getJavaClass().getName() +
'.' + name, getDocument(), getLineno());
+ }
+ fieldValue = new SimpleFieldValue(parent.getJavaClass(), fieldSetter,
valueEntries.get(0).getInnerText());
+ }
+ }
+ return true;
+ }
+
+ public Set<XmlItemType> getAllowedItem()
+ {
+ return allowed;
+ }
+
+}
Modified:
modules/xml/trunk/core/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java
===================================================================
---
modules/xml/trunk/core/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java 2010-04-04
19:11:04 UTC (rev 12379)
+++
modules/xml/trunk/core/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java 2010-04-05
08:58:08 UTC (rev 12380)
@@ -5,6 +5,7 @@
package org.jboss.seam.xml.parser.namespace;
import java.lang.reflect.Field;
+import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -15,6 +16,7 @@
import org.jboss.seam.xml.model.FieldXmlItem;
import org.jboss.seam.xml.model.MethodXmlItem;
import org.jboss.seam.xml.model.ParameterXmlItem;
+import org.jboss.seam.xml.model.PropertyXmlItem;
import org.jboss.seam.xml.model.XmlItem;
import org.jboss.seam.xml.model.XmlItemType;
import org.jboss.seam.xml.parser.SaxNode;
@@ -117,6 +119,19 @@
{
return new FieldXmlItem(parent, f, innerText, document, lineno);
}
+
+ String methodName = "set" + Character.toUpperCase(name.charAt(0)) +
name.substring(1);
+ if (ReflectionUtils.methodExists(p, methodName))
+ {
+ Set<Method> methods = ReflectionUtils.getMethods(p);
+ for (Method m : methods)
+ {
+ if (m.getName().equals(methodName) && m.getParameterTypes().length ==
1)
+ {
+ return new PropertyXmlItem(parent, name, m, innerText, document, lineno);
+ }
+ }
+ }
return null;
}
}
Modified:
modules/xml/trunk/core/src/test/java/org/jboss/seam/xml/test/fieldset/FieldValueBean.java
===================================================================
---
modules/xml/trunk/core/src/test/java/org/jboss/seam/xml/test/fieldset/FieldValueBean.java 2010-04-04
19:11:04 UTC (rev 12379)
+++
modules/xml/trunk/core/src/test/java/org/jboss/seam/xml/test/fieldset/FieldValueBean.java 2010-04-05
08:58:08 UTC (rev 12380)
@@ -15,7 +15,7 @@
{
assert ivalue != 20;
}
-
+
private int ivalue = 20;
public String stringValue;
@@ -34,6 +34,8 @@
public long lvalue;
+ int noFieldValue;
+
public void setIvalue(int value)
{
this.ivalue = value + 1;
@@ -44,4 +46,14 @@
return ivalue;
}
+ public int getNoField()
+ {
+ return noFieldValue;
+ }
+
+ public void setNoField(int value)
+ {
+ noFieldValue = value;
+ }
+
}
Modified:
modules/xml/trunk/core/src/test/java/org/jboss/seam/xml/test/fieldset/SetFieldValueBeanTest.java
===================================================================
---
modules/xml/trunk/core/src/test/java/org/jboss/seam/xml/test/fieldset/SetFieldValueBeanTest.java 2010-04-04
19:11:04 UTC (rev 12379)
+++
modules/xml/trunk/core/src/test/java/org/jboss/seam/xml/test/fieldset/SetFieldValueBeanTest.java 2010-04-05
08:58:08 UTC (rev 12380)
@@ -31,6 +31,7 @@
assert x.getIvalue() == 11;
assert x.lvalue == 23;
assert x.svalue == 4;
+ assert x.noFieldValue == 7;
}
Modified:
modules/xml/trunk/core/src/test/resources/org/jboss/seam/xml/test/fieldset/set-field-value-beans.xml
===================================================================
---
modules/xml/trunk/core/src/test/resources/org/jboss/seam/xml/test/fieldset/set-field-value-beans.xml 2010-04-04
19:11:04 UTC (rev 12379)
+++
modules/xml/trunk/core/src/test/resources/org/jboss/seam/xml/test/fieldset/set-field-value-beans.xml 2010-04-05
08:58:08 UTC (rev 12380)
@@ -16,7 +16,10 @@
<test:lvalue>23</test:lvalue>
<test:svalue>4</test:svalue>
<test:bigDecimalValue>10</test:bigDecimalValue>
+ <test:noField>7</test:noField>
</test:FieldValueBean>
+
+
</beans>
\ No newline at end of file