[seam-commits] Seam SVN: r12624 - in modules/xml/trunk/impl/src: main/java/org/jboss/seam/xml/parser/namespace and 2 other directories.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Fri Apr 23 19:46:05 EDT 2010
Author: swd847
Date: 2010-04-23 19:46:04 -0400 (Fri, 23 Apr 2010)
New Revision: 12624
Added:
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/FieldValueXmlItem.java
Modified:
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ClassXmlItem.java
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ModelBuilder.java
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/PropertyXmlItem.java
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/XmlItemType.java
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/RootNamespaceElementResolver.java
modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/fieldset/SetFieldValueBeanTest.java
modules/xml/trunk/impl/src/test/resources/org/jboss/seam/xml/test/fieldset/set-field-value-beans.xml
Log:
added shorthand field values
Modified: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ClassXmlItem.java
===================================================================
--- modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ClassXmlItem.java 2010-04-23 23:34:32 UTC (rev 12623)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ClassXmlItem.java 2010-04-23 23:46:04 UTC (rev 12624)
@@ -4,17 +4,24 @@
*/
package org.jboss.seam.xml.model;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
import java.util.HashSet;
+import java.util.Map;
import java.util.Set;
+import java.util.Map.Entry;
+import org.jboss.seam.xml.util.XmlConfigurationException;
+import org.jboss.weld.extensions.util.ReflectionUtils;
+
public class ClassXmlItem extends AbstractXmlItem
{
HashSet<XmlItemType> allowed = new HashSet<XmlItemType>();
- public ClassXmlItem(XmlItem parent, Class<?> c, String document, int lineno)
+ public ClassXmlItem(XmlItem parent, Class<?> c, Map<String, String> attributes, String document, int lineno)
{
- super(XmlItemType.CLASS, parent, c, null, null, document, lineno);
+ super(XmlItemType.CLASS, parent, c, null, attributes, document, lineno);
allowed.add(XmlItemType.ANNOTATION);
allowed.add(XmlItemType.FIELD);
allowed.add(XmlItemType.METHOD);
@@ -27,4 +34,40 @@
return allowed;
}
+ public Set<FieldValueXmlItem> getShorthandFieldValues()
+ {
+ Set<FieldValueXmlItem> values = new HashSet<FieldValueXmlItem>();
+ for (Entry<String, String> e : attributes.entrySet())
+ {
+
+ Field field = ReflectionUtils.getField(getJavaClass(), e.getKey());
+ if (field != null)
+ {
+ values.add(new FieldXmlItem(this, field, e.getValue(), document, lineno));
+ }
+ else
+ {
+ String methodName = "set" + Character.toUpperCase(e.getKey().charAt(0)) + e.getKey().substring(1);
+ if (ReflectionUtils.methodExists(getJavaClass(), methodName))
+ {
+ Set<Method> methods = ReflectionUtils.getMethods(getJavaClass());
+ for (Method m : methods)
+ {
+ if (m.getName().equals(methodName) && m.getParameterTypes().length == 1)
+ {
+ values.add(new PropertyXmlItem(this, e.getKey(), m, e.getValue(), document, lineno));
+ break;
+ }
+ }
+ }
+ else
+ {
+ throw new XmlConfigurationException("Could not resolve field: " + e.getKey(), document, lineno);
+ }
+ }
+
+ }
+ return values;
+ }
+
}
Added: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/FieldValueXmlItem.java
===================================================================
--- modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/FieldValueXmlItem.java (rev 0)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/FieldValueXmlItem.java 2010-04-23 23:46:04 UTC (rev 12624)
@@ -0,0 +1,10 @@
+package org.jboss.seam.xml.model;
+
+import org.jboss.seam.xml.fieldset.FieldValueObject;
+
+public interface FieldValueXmlItem
+{
+ public FieldValueObject getFieldValue();
+
+ public String getFieldName();
+}
Modified: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java
===================================================================
--- modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java 2010-04-23 23:34:32 UTC (rev 12623)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java 2010-04-23 23:46:04 UTC (rev 12624)
@@ -23,7 +23,7 @@
import org.jboss.seam.xml.fieldset.SimpleFieldValue;
import org.jboss.seam.xml.util.XmlConfigurationException;
-public class FieldXmlItem extends AbstractXmlItem
+public class FieldXmlItem extends AbstractXmlItem implements FieldValueXmlItem
{
FieldValueSetter fieldSetter;
@@ -150,4 +150,9 @@
}
return new DirectFieldSetter(field);
}
+
+ public String getFieldName()
+ {
+ return field.getName();
+ }
}
Modified: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ModelBuilder.java
===================================================================
--- modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ModelBuilder.java 2010-04-23 23:34:32 UTC (rev 12623)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ModelBuilder.java 2010-04-23 23:46:04 UTC (rev 12624)
@@ -53,6 +53,9 @@
Map<String, NamespaceElementResolver> resolvers;
+ /**
+ * builds an XML result from a parsed xml document
+ */
public XmlResult build(SaxNode root)
{
@@ -79,6 +82,7 @@
// nodes with a null namespace are whitespace nodes etc
if (node.getNamespaceUri() != null)
{
+ // ignore <alternatives> <interceptors> etc
if (node.getNamespaceUri().equals(BEANS_ROOT_NAMESPACE))
{
continue;
@@ -105,10 +109,13 @@
if (rb.getType() == XmlItemType.CLASS || rb.getType() == XmlItemType.ANNOTATION)
{
ResultType type = getItemType(rb);
+ // if we are configuring a bean
if (type == ResultType.BEAN)
{
- BeanResult<?> tp = buildAnnotatedType((ClassXmlItem) rb);
- if (rb.getJavaClass().isInterface())
+ ClassXmlItem cxml = (ClassXmlItem) rb;
+ // get the AnnotatedType information
+ BeanResult<?> tp = buildAnnotatedType(cxml);
+ if (cxml.getJavaClass().isInterface())
{
ret.addInterface(tp);
}
@@ -116,27 +123,33 @@
{
ret.addBean(tp);
}
+ // <override> or <speciailizes> need to veto the bean
if (tp.getBeanType() != BeanResultType.ADD)
{
ret.addVeto(tp.getType());
}
+ // get all the field values from the bean
+ Set<String> configuredFields = new HashSet<String>();
List<FieldValueObject> fields = new ArrayList<FieldValueObject>();
- for (FieldXmlItem xi : rb.getChildrenOfType(FieldXmlItem.class))
+ for (FieldValueXmlItem xi : cxml.getChildrenOfType(FieldValueXmlItem.class))
{
FieldValueObject f = xi.getFieldValue();
if (f != null)
{
fields.add(f);
+ configuredFields.add(xi.getFieldName());
}
}
- for (PropertyXmlItem xi : rb.getChildrenOfType(PropertyXmlItem.class))
+
+ for (FieldValueXmlItem f : cxml.getShorthandFieldValues())
{
- FieldValueObject f = xi.getFieldValue();
- if (f != null)
+ if (configuredFields.contains(f.getFieldName()))
{
- fields.add(f);
+ throw new XmlConfigurationException("Field configured in two places: " + cxml.getJavaClass().getName() + "." + f.getFieldName(), cxml.getDocument(), cxml.getLineno());
}
+ fields.add(f.getFieldValue());
}
+
if (!fields.isEmpty())
{
if (rb.getJavaClass().isInterface())
@@ -162,15 +175,9 @@
addStereotypeToResult(ret, rb);
}
}
- else if (rb.getType() == XmlItemType.VETO)
- {
- for (XmlItem it : rb.getChildren())
- {
- ret.addVeto(it.getJavaClass());
- }
- }
else if (rb.getType() == XmlItemType.GENERIC_BEAN)
{
+
GenericBeanXmlItem item = (GenericBeanXmlItem) rb;
Set<BeanResult<?>> classes = new HashSet<BeanResult<?>>();
for (ClassXmlItem c : rb.getChildrenOfType(ClassXmlItem.class))
@@ -186,6 +193,9 @@
}
}
+ /**
+ * resolves the appropriate java elements from the xml
+ */
protected XmlItem resolveNode(SaxNode node, XmlItem parent)
{
NamespaceElementResolver resolver = resolveNamepsace(node.getNamespaceUri());
Modified: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/PropertyXmlItem.java
===================================================================
--- modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/PropertyXmlItem.java 2010-04-23 23:34:32 UTC (rev 12623)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/PropertyXmlItem.java 2010-04-23 23:46:04 UTC (rev 12624)
@@ -21,7 +21,7 @@
import org.jboss.seam.xml.fieldset.SimpleFieldValue;
import org.jboss.seam.xml.util.XmlConfigurationException;
-public class PropertyXmlItem extends AbstractXmlItem
+public class PropertyXmlItem extends AbstractXmlItem implements FieldValueXmlItem
{
FieldValueSetter fieldSetter;
@@ -120,4 +120,9 @@
return allowed;
}
+ public String getFieldName()
+ {
+ return name;
+ }
+
}
Modified: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/XmlItemType.java
===================================================================
--- modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/XmlItemType.java 2010-04-23 23:34:32 UTC (rev 12623)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/XmlItemType.java 2010-04-23 23:46:04 UTC (rev 12624)
@@ -6,5 +6,5 @@
public enum XmlItemType
{
- CLASS, METHOD, FIELD, ANNOTATION, VALUE, VETO, ENTRY, KEY, DEPENDENCY, PARAMETERS, PARAMETER, ARRAY, OVERRIDE, SPECIALIZES, TYPE, GENERIC_BEAN;
+ CLASS, METHOD, FIELD, ANNOTATION, VALUE, ENTRY, KEY, DEPENDENCY, PARAMETERS, PARAMETER, ARRAY, OVERRIDE, SPECIALIZES, TYPE, GENERIC_BEAN;
}
Modified: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java
===================================================================
--- modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java 2010-04-23 23:34:32 UTC (rev 12623)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java 2010-04-23 23:46:04 UTC (rev 12624)
@@ -67,7 +67,7 @@
}
else
{
- return new ClassXmlItem(parent, c, node.getDocument(), node.getLineNo());
+ return new ClassXmlItem(parent, c, node.getAttributes(), node.getDocument(), node.getLineNo());
}
}
Modified: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/RootNamespaceElementResolver.java
===================================================================
--- modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/RootNamespaceElementResolver.java 2010-04-23 23:34:32 UTC (rev 12623)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/RootNamespaceElementResolver.java 2010-04-23 23:46:04 UTC (rev 12624)
@@ -121,7 +121,7 @@
}
else
{
- return new ClassXmlItem(parent, primType, node.getDocument(), node.getLineNo());
+ return new ClassXmlItem(parent, primType, node.getAttributes(), node.getDocument(), node.getLineNo());
}
}
Modified: modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/fieldset/SetFieldValueBeanTest.java
===================================================================
--- modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/fieldset/SetFieldValueBeanTest.java 2010-04-23 23:34:32 UTC (rev 12623)
+++ modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/fieldset/SetFieldValueBeanTest.java 2010-04-23 23:46:04 UTC (rev 12624)
@@ -6,6 +6,8 @@
import java.math.BigDecimal;
+import javax.enterprise.util.AnnotationLiteral;
+
import org.jboss.seam.xml.test.AbstractXMLTest;
import org.jboss.seam.xml.test.method.QualifierEnum;
import org.testng.annotations.Test;
@@ -35,4 +37,22 @@
}
+ @Test
+ public void simpleShorthandFieldSetterTest()
+ {
+ FieldValueBean x = getReference(FieldValueBean.class, new AnnotationLiteral<FieldsetQualifier>()
+ {
+ });
+ assert x.bigDecimalValue.compareTo(BigDecimal.TEN) == 0;
+ assert x.bvalue == true;
+ assert x.dvalue == 0;
+ assert x.enumValue == QualifierEnum.A;
+ assert x.fvalue == 0;
+ assert x.getIvalue() == 11;
+ assert x.lvalue == 23;
+ assert x.svalue == 4;
+ assert x.noFieldValue == 7;
+
+ }
+
}
Modified: modules/xml/trunk/impl/src/test/resources/org/jboss/seam/xml/test/fieldset/set-field-value-beans.xml
===================================================================
--- modules/xml/trunk/impl/src/test/resources/org/jboss/seam/xml/test/fieldset/set-field-value-beans.xml 2010-04-23 23:34:32 UTC (rev 12623)
+++ modules/xml/trunk/impl/src/test/resources/org/jboss/seam/xml/test/fieldset/set-field-value-beans.xml 2010-04-23 23:46:04 UTC (rev 12624)
@@ -19,7 +19,11 @@
<test:noField>7</test:noField>
</test:FieldValueBean>
-
-
-
+ <test:FieldValueBean ivalue="10" stringValue="hello world" enumValue="A" fvalue="0" dvalue="0" bvalue="true" lvalue="23" svalue="4" bigDecimalValue="10" noField="7" >
+ <overrides/>
+ <test:init>
+ <Inject/>
+ </test:init>
+ <test:FieldsetQualifier/>
+ </test:FieldValueBean>
</beans>
\ No newline at end of file
More information about the seam-commits
mailing list