Coverage Report - javax.faces.component._SelectItemsIterator
 
Classes in this File Line Coverage Branch Coverage Complexity
_SelectItemsIterator
0%
0/98
0%
0/56
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package javax.faces.component;
 20  
 
 21  
 import java.util.*;
 22  
 import javax.faces.el.ValueBinding;
 23  
 import javax.faces.model.SelectItem;
 24  
 
 25  
 
 26  
 /**
 27  
  * @author Mathias Broekelmann (latest modification by $Author: lu4242 $)
 28  
  * @version $Revision: 682489 $ $Date: 2008-08-04 15:22:15 -0500 (Mon, 04 Aug 2008) $
 29  
  */
 30  
 class _SelectItemsIterator implements Iterator
 31  
 {
 32  
     private final Iterator _childs;
 33  
     private Iterator<SelectItem> _nestedItems;    
 34  
     private Object _nextItem;
 35  
     private String _collectionLabel;
 36  
     private UISelectItems _currentUISelectItems;
 37  
 
 38  
     public _SelectItemsIterator(UIComponent selectItemsParent)
 39  0
     {
 40  0
         _childs = selectItemsParent.getChildren().iterator();
 41  0
     }
 42  
 
 43  
     public boolean hasNext()
 44  
     {
 45  0
         if(_nextItem != null)
 46  
         {
 47  0
             return true;
 48  
         }
 49  0
         if(_nestedItems != null)
 50  
         {
 51  0
             if(_nestedItems.hasNext())
 52  
             {
 53  0
                 return true;
 54  
             }
 55  0
             _nestedItems = null;
 56  
         }            
 57  0
         if (_childs.hasNext())
 58  
         {
 59  0
             UIComponent child = (UIComponent) _childs.next();
 60  
             // When there is other components nested that does
 61  
             // not extends from UISelectItem or UISelectItems
 62  
             // the behavior for this iterator is just skip this
 63  
             // element(s) until an element that extends from these
 64  
             // classes are found. If there is no more elements
 65  
             // that conform this condition, just return false.
 66  
             while (!(child instanceof UISelectItem)
 67  0
                     && !(child instanceof UISelectItems))
 68  
             {
 69  
                 //Try to skip it
 70  0
                 if (_childs.hasNext())
 71  
                 {
 72  
                     //Skip and do the same check
 73  0
                     child = (UIComponent) _childs.next();
 74  
                 }
 75  
                 else
 76  
                 {
 77  
                     //End loop, so the final result is return false,
 78  
                     //since there are no more components to iterate.
 79  0
                     return false;
 80  
                 }
 81  
             }
 82  0
             if (child instanceof UISelectItem)
 83  
             {
 84  0
                 UISelectItem uiSelectItem = (UISelectItem) child;
 85  0
                 Object item = uiSelectItem.getValue();
 86  0
                 if (item == null)
 87  
                 {
 88  0
                     Object itemValue = ((UISelectItem) child).getItemValue();
 89  0
                     String label = ((UISelectItem) child).getItemLabel();
 90  0
                     String description = ((UISelectItem) child)
 91  
                                     .getItemDescription();
 92  0
                     boolean disabled = ((UISelectItem) child).isItemDisabled();
 93  0
                     if (label == null)
 94  
                     {
 95  0
                         label = itemValue.toString();
 96  
                     }
 97  0
                     item = new SelectItem(itemValue, label, description,
 98  
                                     disabled);
 99  0
                 }
 100  0
                 else if (!(item instanceof SelectItem))
 101  
                 {
 102  0
                     ValueBinding binding = ((UISelectItem) child)
 103  
                                     .getValueBinding("value");
 104  0
                     throw new IllegalArgumentException(
 105  
                                     "Value binding '"
 106  
                                     + (binding == null ? null : binding.getExpressionString())
 107  
                                     + "' of UISelectItem : "
 108  
                                     + getPathToComponent(child)
 109  
                                     + " does not reference an Object of type SelectItem");
 110  
                 }
 111  0
                 _nextItem = item;
 112  0
                 return true;
 113  
             }
 114  0
             else if (child instanceof UISelectItems)
 115  
             {
 116  0
                 _currentUISelectItems = ((UISelectItems) child);
 117  0
                 Object value = _currentUISelectItems.getValue();
 118  
 
 119  0
                 if (value instanceof SelectItem)
 120  
                 {
 121  0
                     _nextItem = value;
 122  0
                     return true;
 123  
                 }
 124  0
                 else if (value instanceof SelectItem[])
 125  
                 {
 126  0
                     _nestedItems = Arrays.asList((SelectItem[]) value)
 127  
                                     .iterator();
 128  0
                     _collectionLabel = "Array";
 129  0
                     return hasNext();
 130  
                 }
 131  0
                 else if (value instanceof Collection)
 132  
                 {
 133  0
                     _nestedItems = ((Collection<SelectItem>)value).iterator();
 134  0
                     _collectionLabel = "Collection";
 135  0
                     return hasNext();
 136  
                 }
 137  0
                 else if (value instanceof Map)
 138  
                 {
 139  0
                     Map map = ((Map) value);
 140  0
                     Collection<SelectItem> items = new ArrayList<SelectItem>(map.size()); 
 141  0
                     for (Iterator it = map.entrySet().iterator(); it
 142  0
                                     .hasNext();)
 143  
                     {
 144  0
                         Map.Entry entry = (Map.Entry) it.next();
 145  0
                         items.add(new SelectItem(entry.getValue(), entry
 146  
                                         .getKey().toString()));
 147  0
                     }
 148  0
                     _nestedItems = items.iterator();
 149  0
                     _collectionLabel = "Map";
 150  0
                     return hasNext();
 151  
                 }
 152  
                 else
 153  
                 {
 154  0
                     ValueBinding binding = _currentUISelectItems.getValueBinding("value");
 155  
 
 156  0
                     throw new IllegalArgumentException(
 157  
                         "Value binding '"
 158  
                         + (binding == null ? null : binding
 159  
                                         .getExpressionString())
 160  
                         + "'of UISelectItems with component-path "
 161  
                         + getPathToComponent(child)
 162  
                         + " does not reference an Object of type SelectItem, SelectItem[], Collection or Map but of type : "
 163  
                         + ((value == null) ? null : value
 164  
                                         .getClass()
 165  
                                         .getName()));
 166  
                 }
 167  
             }
 168  
         }
 169  0
         return false;
 170  
     }
 171  
 
 172  
     public Object next()
 173  
     {
 174  0
         if (!hasNext())
 175  
         {
 176  0
             throw new NoSuchElementException();
 177  
         }
 178  0
         if(_nextItem != null)
 179  
         {
 180  0
             Object value = _nextItem;
 181  0
             _nextItem = null;
 182  0
             return value;
 183  
         }        
 184  0
         if (_nestedItems != null)
 185  
         {
 186  0
             Object item = _nestedItems.next();
 187  0
             if (!(item instanceof SelectItem))
 188  
             {
 189  0
                 ValueBinding binding = _currentUISelectItems
 190  
                                 .getValueBinding("value");
 191  0
                 throw new IllegalArgumentException(
 192  
                 _collectionLabel + " referenced by UISelectItems with binding '"
 193  
                 + binding.getExpressionString()
 194  
                 + "' and Component-Path : " + getPathToComponent(_currentUISelectItems)
 195  
                 + " does not contain Objects of type SelectItem");
 196  
             }
 197  0
             return item;
 198  
         }
 199  0
         throw new NoSuchElementException();
 200  
     }
 201  
 
 202  
     public void remove()
 203  
     {
 204  0
         throw new UnsupportedOperationException();
 205  
     }
 206  
 
 207  
 
 208  
     private String getPathToComponent(UIComponent component)
 209  
     {
 210  0
         StringBuffer buf = new StringBuffer();
 211  
 
 212  0
         if(component == null)
 213  
         {
 214  0
             buf.append("{Component-Path : ");
 215  0
             buf.append("[null]}");
 216  0
             return buf.toString();
 217  
         }
 218  
 
 219  0
         getPathToComponent(component,buf);
 220  
 
 221  0
         buf.insert(0,"{Component-Path : ");
 222  0
         buf.append("}");
 223  
 
 224  0
         return buf.toString();
 225  
     }
 226  
 
 227  
     private void getPathToComponent(UIComponent component, StringBuffer buf)
 228  
     {
 229  0
         if(component == null)
 230  0
             return;
 231  
 
 232  0
         StringBuffer intBuf = new StringBuffer();
 233  
 
 234  0
         intBuf.append("[Class: ");
 235  0
         intBuf.append(component.getClass().getName());
 236  0
         if(component instanceof UIViewRoot)
 237  
         {
 238  0
             intBuf.append(",ViewId: ");
 239  0
             intBuf.append(((UIViewRoot) component).getViewId());
 240  
         }
 241  
         else
 242  
         {
 243  0
             intBuf.append(",Id: ");
 244  0
             intBuf.append(component.getId());
 245  
         }
 246  0
         intBuf.append("]");
 247  
 
 248  0
         buf.insert(0,intBuf);
 249  
 
 250  0
         if(component!=null)
 251  
         {
 252  0
             getPathToComponent(component.getParent(),buf);
 253  
         }
 254  0
     }
 255  
 }