DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=13043>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=13043

Adjustment to Iterate logic

           Summary: Adjustment to Iterate logic
           Product: Struts
           Version: 1.0.2 Final
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Custom Tags
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


I would like to propose that the Iterate tag be adjusted to handle the 
situation where a non-Collection object is used as the object to Iterate over.  
Rather than throwing an exception after realizing that this is not a Collection 
or iterate-able object, we could create a pseudo-collection that has just the 
one element and iterate over that.  This way the struts:iterate tag can be used 
even on non-Collection objects and the semantics of that case would be that if 
the object is found, the body will be executed once with respect to that object.

The reason that I suggest this is that I am trying to use Struts as the web 
front-end to another application framework that is UI-agnostic.  When I search 
for a list of objects, this framework will return one data bean for each record 
found.  If there are several, it automatically packages them in a collection.  
The previous, custom-built web framework handled this through a custom tag, but 
I wanted to migrate over to a more sophisticated Struts framework.  I found 
myself having to interogate all of the replies, checking if it was already a 
Collection and if not, create a pseudo-collection in each Action that returned 
a list.  Rather than repeat the logic, in multiple Actions, I was hoping to 
make this semantic shift to Iterate and put the logic there.  I realize that I 
can simple create another custom tag, but I would like to minimize the need for 
that.  I have offered my proposed implementation here. 

        // Construct an iterator for this collection
        if (collection.getClass().isArray()) {
            try {
                // If we're lucky, it is an array of objects
                // that we can iterate over with no copying
                iterator = Arrays.asList((Object[]) collection).iterator();
            } catch (ClassCastException e) {
                // Rats -- it is an array of primitives
                int length = Array.getLength(collection);
                ArrayList c = new ArrayList(length);
                for (int i = 0; i < length; i++) {
                    c.add(Array.get(collection, i));
                }
                iterator = c.iterator();
            }
        } else if (collection instanceof Collection)
            iterator = ((Collection) collection).iterator();
        else if (collection instanceof Iterator)
            iterator = (Iterator) collection;
        else if (collection instanceof Map)
            iterator = ((Map) collection).entrySet().iterator();
        else if (collection instanceof Enumeration)
            iterator = new IteratorAdapter((Enumeration)collection);
        else {
            // This turns a non-Collection into a pseudo-Collection of 1
            Collection pseudoCollection = new Vector();
            pseudoCollection.add(collection);
            iterator = pseudoCollection.iterator();

//          JspException e = new JspException
//              (messages.getMessage("iterate.iterator"));
//            RequestUtils.saveException(pageContext, e);
//            throw e;
        }

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to