[seam-commits] Seam SVN: r12076 - in modules/xml/trunk/src: main/java/org/jboss/seam/xml/parser/namespace and 1 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Thu Feb 25 22:11:51 EST 2010


Author: swd847
Date: 2010-02-25 22:11:50 -0500 (Thu, 25 Feb 2010)
New Revision: 12076

Added:
   modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ParameterXmlItem.java
Modified:
   modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/AbstractXmlItem.java
   modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ArrayXmlItem.java
   modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ClassXmlItem.java
   modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java
   modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/MethodXmlItem.java
   modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ModelBuilder.java
   modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/XmlItem.java
   modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/XmlItemType.java
   modules/xml/trunk/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java
   modules/xml/trunk/src/main/java/org/jboss/seam/xml/parser/namespace/RootNamespaceElementResolver.java
   modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/AbstractXMLTest.java
   modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/NamespaceResolverTest.java
Log:
Cleaned up the XML object model somewhat



Modified: modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/AbstractXmlItem.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/AbstractXmlItem.java	2010-02-26 01:43:39 UTC (rev 12075)
+++ modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/AbstractXmlItem.java	2010-02-26 03:11:50 UTC (rev 12076)
@@ -4,16 +4,12 @@
  */
 package org.jboss.seam.xml.model;
 
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.jboss.seam.xml.fieldset.FieldValueObject;
-
 public abstract class AbstractXmlItem implements XmlItem
 {
    final XmlItemType type;
@@ -82,16 +78,6 @@
       return type;
    }
 
-   public Field getField()
-   {
-      return null;
-   }
-
-   public Method getMethod()
-   {
-      return null;
-   }
-
    public Class<?> getJavaClass()
    {
       return javaClass;
@@ -107,8 +93,16 @@
       return attributes;
    }
 
-   public FieldValueObject getFieldValue()
+   public <T> List<T> getChildrenOfType(Class<T> type)
    {
-      return null;
+      List<T> ret = new ArrayList<T>();
+      for (XmlItem i : children)
+      {
+         if (type.isAssignableFrom(i.getClass()))
+         {
+            ret.add((T) i);
+         }
+      }
+      return ret;
    }
 }

Modified: modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ArrayXmlItem.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ArrayXmlItem.java	2010-02-26 01:43:39 UTC (rev 12075)
+++ modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ArrayXmlItem.java	2010-02-26 03:11:50 UTC (rev 12076)
@@ -6,6 +6,7 @@
 
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
@@ -130,5 +131,17 @@
       }
       return true;
    }
-
+   public <T> List<T> getChildrenOfType(Class<T> type)
+   {
+      List<T> ret = new ArrayList<T>();
+      for(XmlItem i : getChildren())
+      {
+         if(type.isAssignableFrom(i.getClass()))
+         {
+            ret.add((T)i);
+         }
+      }
+      return ret;
+   }
+   
 }

Modified: modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ClassXmlItem.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ClassXmlItem.java	2010-02-26 01:43:39 UTC (rev 12075)
+++ modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ClassXmlItem.java	2010-02-26 03:11:50 UTC (rev 12076)
@@ -25,17 +25,10 @@
    public ClassXmlItem(XmlItem parent, Class<?> c, String document, int lineno)
    {
       super(XmlItemType.CLASS, parent, c, null, null, document, lineno);
-      if (innerText != null)
-      {
-         if (!innerText.trim().equals(""))
-         {
-            attributes.put("value", innerText);
-         }
-      }
       allowed.add(XmlItemType.ANNOTATION);
       allowed.add(XmlItemType.FIELD);
       allowed.add(XmlItemType.METHOD);
-      allowed.add(XmlItemType.CLASS);
+      allowed.add(XmlItemType.PARAMETER);
 
    }
 

Modified: modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java	2010-02-26 01:43:39 UTC (rev 12075)
+++ modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java	2010-02-26 03:11:50 UTC (rev 12076)
@@ -49,7 +49,6 @@
       return field;
    }
 
-   @Override
    public FieldValueObject getFieldValue()
    {
       return fieldValue;

Modified: modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/MethodXmlItem.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/MethodXmlItem.java	2010-02-26 01:43:39 UTC (rev 12075)
+++ modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/MethodXmlItem.java	2010-02-26 03:11:50 UTC (rev 12076)
@@ -44,7 +44,7 @@
       }
       method = found;
       allowed.add(XmlItemType.ANNOTATION);
-      allowed.add(XmlItemType.CLASS);
+      allowed.add(XmlItemType.PARAMETER);
    }
 
    /**
@@ -66,7 +66,7 @@
       List<Class<?>> rtList = new ArrayList<Class<?>>();
       for (XmlItem c : children)
       {
-         if (c.getType() == XmlItemType.CLASS)
+         if (c.getType() == XmlItemType.PARAMETER)
          {
             Class<?> cl = c.getJavaClass();
             rtList.add(cl);

Modified: modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ModelBuilder.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ModelBuilder.java	2010-02-26 01:43:39 UTC (rev 12075)
+++ modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ModelBuilder.java	2010-02-26 03:11:50 UTC (rev 12076)
@@ -96,22 +96,18 @@
             BeanResult<?> tp = buildAnnotatedType(rb);
             ret.getBeans().add(tp);
             List<FieldValueObject> fields = new ArrayList<FieldValueObject>();
-            for (XmlItem xi : rb.getChildren())
+            for (FieldXmlItem xi : rb.getChildrenOfType(FieldXmlItem.class))
             {
-               if (xi.getType() == XmlItemType.FIELD)
+               FieldValueObject f = xi.getFieldValue();
+               if (f != null)
                {
-                  FieldValueObject f = xi.getFieldValue();
-                  if (f != null)
-                  {
-                     fields.add(f);
-                  }
+                  fields.add(f);
                }
             }
             if (!fields.isEmpty())
             {
                ret.getFieldValues().put(tp, fields);
             }
-
          }
          else if (type == ResultType.QUALIFIER)
          {
@@ -123,7 +119,7 @@
          }
          else if (type == ResultType.STEREOTYPE)
          {
-            addSteriotypeToResult(ret, rb);
+            addStereotypeToResult(ret, rb);
          }
       }
       else if (rb.getType() == XmlItemType.VETO)
@@ -243,65 +239,50 @@
       NewAnnotatedTypeBuilder<T> type = result.getBuilder();
       // list of constructor arguments
       List<XmlItem> constList = new ArrayList<XmlItem>();
-      for (XmlItem item : rb.getChildren())
+
+      for (AnnotationXmlItem item : rb.getChildrenOfType(AnnotationXmlItem.class))
       {
-         if (item.getType() == XmlItemType.ANNOTATION)
-         {
+         Annotation a = createAnnotation(item);
+         type.addToClass(a);
+      }
 
-            Annotation a = createAnnotation(item);
-            type.addToClass(a);
-         }
-         else if (item.getType() == XmlItemType.CLASS)
+      for (ParameterXmlItem item : rb.getChildrenOfType(ParameterXmlItem.class))
+      {
+         constList.add(item);
+      }
+      for (FieldXmlItem item : rb.getChildrenOfType(FieldXmlItem.class))
+      {
+         for (AnnotationXmlItem fi : item.getChildrenOfType(AnnotationXmlItem.class))
          {
-            constList.add(item);
-
+            Annotation a = createAnnotation(fi);
+            type.addToField(item.getField(), a);
          }
-         else if (item.getType() == XmlItemType.FIELD)
+      }
+      for (MethodXmlItem item : rb.getChildrenOfType(MethodXmlItem.class))
+      {
+         int paramCount = 0;
+
+         for (AnnotationXmlItem fi : item.getChildrenOfType(AnnotationXmlItem.class))
          {
-            for (XmlItem fi : item.getChildren())
-            {
-               if (fi.getType() == XmlItemType.ANNOTATION)
-               {
-                  Annotation a = createAnnotation(fi);
-                  type.addToField(item.getField(), a);
-               }
-            }
+            Annotation a = createAnnotation(fi);
+            type.addToMethod(item.getMethod(), a);
          }
-         else if (item.getType() == XmlItemType.METHOD)
+         for (ParameterXmlItem fi : item.getChildrenOfType(ParameterXmlItem.class))
          {
-            int paramCount = 0;
-            for (XmlItem fi : item.getChildren())
+            int param = paramCount++;
+            for (AnnotationXmlItem pan : fi.getChildrenOfType(AnnotationXmlItem.class))
             {
-               if (fi.getType() == XmlItemType.ANNOTATION)
-               {
-
-                  // TODO: pass in attribute map
-                  Annotation a = createAnnotation(fi);
-                  type.addToMethod(item.getMethod(), a);
-               }
-               else if (fi.getType() == XmlItemType.CLASS)
-               {
-                  int param = paramCount++;
-                  for (XmlItem pan : fi.getChildren())
-                  {
-                     if (pan.getType() == XmlItemType.ANNOTATION)
-                     {
-                        Annotation a = createAnnotation(pan);
-                        type.addToMethodParameter(item.getMethod(), param, a);
-                     }
-                     else
-                     {
-                        throw new XmlConfigurationException("Method parameters may only have annotations as children in " + item.getJavaClass().getName(), rb.getDocument(), rb.getLineno());
-                     }
-                  }
-               }
+               Annotation a = createAnnotation(pan);
+               type.addToMethodParameter(item.getMethod(), param, a);
             }
          }
-         else if (item.getType() == XmlItemType.DEPENDENCY)
-         {
-            result.getDependencies().add(item.getInnerText());
-         }
+
       }
+      for (DependsXmlItem item : rb.getChildrenOfType(DependsXmlItem.class))
+      {
+         result.addDependency(item.getInnerText());
+      }
+
       if (!constList.isEmpty())
       {
          // the bean defined constructor arguments
@@ -310,7 +291,7 @@
    }
 
    @SuppressWarnings("unchecked")
-   void addSteriotypeToResult(XmlResult ret, XmlItem rb)
+   void addStereotypeToResult(XmlResult ret, XmlItem rb)
    {
 
       Annotation[] values = new Annotation[rb.getChildren().size()];
@@ -319,7 +300,7 @@
       {
          if (item.getType() == XmlItemType.ANNOTATION)
          {
-            Annotation a = createAnnotation(item);
+            Annotation a = createAnnotation((AnnotationXmlItem) item);
             values[count] = a;
          }
          else
@@ -333,7 +314,7 @@
    }
 
    @SuppressWarnings("unchecked")
-   Annotation createAnnotation(XmlItem item)
+   Annotation createAnnotation(AnnotationXmlItem item)
    {
       Map<String, Object> typedVars = new HashMap<String, Object>();
       Class<?> anClass = item.getJavaClass();
@@ -353,7 +334,7 @@
          typedVars.put(mname, XmlObjectConverter.convert(returnType, e.getValue()));
       }
 
-      return ac.get(item.getJavaClass(), typedVars);
+      return ac.get((Class) item.getJavaClass(), typedVars);
    }
 
    public void validateXmlItem(XmlItem item)

Added: modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ParameterXmlItem.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ParameterXmlItem.java	                        (rev 0)
+++ modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/ParameterXmlItem.java	2010-02-26 03:11:50 UTC (rev 12076)
@@ -0,0 +1,37 @@
+/*
+ * Distributed under the LGPL License
+ * 
+ */
+package org.jboss.seam.xml.model;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public class ParameterXmlItem extends AbstractXmlItem
+{
+
+   HashSet<XmlItemType> allowed = new HashSet<XmlItemType>();
+
+   /**
+    * create a class note. If the class is an annotation in can have inner text
+    * to represent the 'value' of the annotation, if can also have atrributes to
+    * represent other properties
+    * 
+    * @param parent
+    * @param c
+    * @param innerText
+    * @param attributes
+    */
+   public ParameterXmlItem(XmlItem parent, Class<?> c, String document, int lineno)
+   {
+      super(XmlItemType.PARAMETER, parent, c, null, null, document, lineno);
+      allowed.add(XmlItemType.ANNOTATION);
+
+   }
+
+   public Set<XmlItemType> getAllowedItem()
+   {
+      return allowed;
+   }
+
+}

Modified: modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/XmlItem.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/XmlItem.java	2010-02-26 01:43:39 UTC (rev 12075)
+++ modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/XmlItem.java	2010-02-26 03:11:50 UTC (rev 12076)
@@ -4,14 +4,9 @@
  */
 package org.jboss.seam.xml.model;
 
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
 
-import org.jboss.seam.xml.fieldset.FieldValueObject;
-
 public interface XmlItem
 {
 
@@ -25,28 +20,22 @@
 
    public XmlItemType getType();
 
-   public Field getField();
-
-   public Method getMethod();
-
    public Class getJavaClass();
 
    /**
-    * attempts to resolve any information that is not availbile at parse time
+    * attempts to resolve any information that is not available at parse time
     * 
     * @param childeren
     * @return
     */
    public boolean resolveChildren();
 
-   public Map<String, String> getAttributes();
-
-   public FieldValueObject getFieldValue();
-
    public Set<XmlItemType> getAllowedItem();
 
    int getLineno();
 
    String getDocument();
 
+   public <T> List<T> getChildrenOfType(Class<T> type);
+
 }
\ No newline at end of file

Modified: modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/XmlItemType.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/XmlItemType.java	2010-02-26 01:43:39 UTC (rev 12075)
+++ modules/xml/trunk/src/main/java/org/jboss/seam/xml/model/XmlItemType.java	2010-02-26 03:11:50 UTC (rev 12076)
@@ -6,5 +6,5 @@
 
 public enum XmlItemType
 {
-   CLASS, METHOD, FIELD, ANNOTATION, VALUE, VETO, ENTRY, KEY, DEPENDENCY;
+   CLASS, METHOD, FIELD, ANNOTATION, VALUE, VETO, ENTRY, KEY, DEPENDENCY,PARAMETER;
 }

Modified: modules/xml/trunk/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java	2010-02-26 01:43:39 UTC (rev 12075)
+++ modules/xml/trunk/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java	2010-02-26 03:11:50 UTC (rev 12076)
@@ -11,6 +11,7 @@
 
 import org.jboss.seam.xml.model.AnnotationXmlItem;
 import org.jboss.seam.xml.model.ClassXmlItem;
+import org.jboss.seam.xml.model.ParameterXmlItem;
 import org.jboss.seam.xml.model.XmlItem;
 import org.jboss.seam.xml.model.XmlItemType;
 import org.jboss.seam.xml.parser.SaxNode;
@@ -52,7 +53,15 @@
          }
          else
          {
-            return new ClassXmlItem(parent, c, node.getDocument(), node.getLineNo());
+            //if it is a method or constructor parameter
+            if(parent != null && (parent.getType() == XmlItemType.METHOD || parent.getType() == XmlItemType.CLASS))
+            {
+               return new ParameterXmlItem(parent, c, node.getDocument(), node.getLineNo());
+            }
+            else
+            {
+               return new ClassXmlItem(parent, c, node.getDocument(), node.getLineNo());
+            }
          }
 
       }

Modified: modules/xml/trunk/src/main/java/org/jboss/seam/xml/parser/namespace/RootNamespaceElementResolver.java
===================================================================
--- modules/xml/trunk/src/main/java/org/jboss/seam/xml/parser/namespace/RootNamespaceElementResolver.java	2010-02-26 01:43:39 UTC (rev 12075)
+++ modules/xml/trunk/src/main/java/org/jboss/seam/xml/parser/namespace/RootNamespaceElementResolver.java	2010-02-26 03:11:50 UTC (rev 12076)
@@ -5,7 +5,6 @@
 package org.jboss.seam.xml.parser.namespace;
 
 import org.jboss.seam.xml.model.ArrayXmlItem;
-import org.jboss.seam.xml.model.DependsXmlItem;
 import org.jboss.seam.xml.model.EntryXmlItem;
 import org.jboss.seam.xml.model.KeyXmlItem;
 import org.jboss.seam.xml.model.ValueXmlItem;
@@ -56,10 +55,7 @@
       {
          return new VetoXmlItem(parent, node.getDocument(), node.getLineNo());
       }
-      else if (item.equals("depends"))
-      {
-         return new DependsXmlItem(parent, node.getInnerText(), node.getDocument(), node.getLineNo());
-      }
+     
       return null;
    }
 

Modified: modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/AbstractXMLTest.java
===================================================================
--- modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/AbstractXMLTest.java	2010-02-26 01:43:39 UTC (rev 12075)
+++ modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/AbstractXMLTest.java	2010-02-26 03:11:50 UTC (rev 12076)
@@ -51,7 +51,13 @@
       }
       else if (beans.size() != 1)
       {
-         throw new RuntimeException("More than one bean found with class: " + clazz + " and bindings " + bindings.toString());
+         StringBuilder bs = new StringBuilder("[");
+         for(Annotation a : bindings)
+         {
+            bs.append(a.toString() + ",");
+         }
+         bs.append("]");
+         throw new RuntimeException("More than one bean found with class: " + clazz + " and bindings " + bs);
       }
       Bean bean = beans.iterator().next();
       return (T) bean.create(manager.createCreationalContext(bean));

Modified: modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/NamespaceResolverTest.java
===================================================================
--- modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/NamespaceResolverTest.java	2010-02-26 01:43:39 UTC (rev 12075)
+++ modules/xml/trunk/src/test/java/org/jboss/seam/xml/test/NamespaceResolverTest.java	2010-02-26 03:11:50 UTC (rev 12076)
@@ -7,6 +7,8 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.jboss.seam.xml.model.FieldXmlItem;
+import org.jboss.seam.xml.model.MethodXmlItem;
 import org.jboss.seam.xml.model.XmlItem;
 import org.jboss.seam.xml.model.XmlItemType;
 import org.jboss.seam.xml.parser.SaxNode;
@@ -48,12 +50,12 @@
 
       method.resolveChildren();
 
-      assert method.getMethod() != null : "Could not resolve method";
-      assert method.getMethod().getParameterTypes().length == 0 : "Wrong method was resolved";
+      assert ((MethodXmlItem) method).getMethod() != null : "Could not resolve method";
+      assert ((MethodXmlItem) method).getMethod().getParameterTypes().length == 0 : "Wrong method was resolved";
 
       XmlItem field = resolver.getItemForNamespace(new SaxNode("field1", null, null, null, null, 0), item);
-      assert field.getType() == XmlItemType.FIELD : "Element of wrong type returned";
-      assert field.getField() != null : "field was not set";
+      assert ((FieldXmlItem) field).getType() == XmlItemType.FIELD : "Element of wrong type returned";
+      assert ((FieldXmlItem) field).getField() != null : "field was not set";
 
    }
 



More information about the seam-commits mailing list