Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 package org.apache.myfaces.shared.util;
 
 import java.util.Map;
 
 
 
 // ATTENTION
 // This class is associated with javax.faces.component._SelectItemsIterator.
 // Changes here should also be applied to this class.
 

Author(s):
Mathias Broekelmann (latest modification by $Author: lu4242 $)
Jakob Korherr (jsf 2.0)
Version:
$Revision: 1299896 $ $Date: 2012-03-12 16:56:05 -0500 (Mon, 12 Mar 2012) $
 
 public class SelectItemsIterator implements Iterator<SelectItem>
 {
     private static final Logger log = Logger.getLogger(SelectItemsIterator.class.getName());
     
     private static final String VAR_PROP = .;
     private static final String ITEM_VALUE_PROP = .;
     private static final String ITEM_LABEL_PROP = .;
     private static final String ITEM_DESCRIPTION_PROP = .;
     private static final String ITEM_DISABLED_PROP = .;
     
     private static final Iterator<UIComponent_EMPTY_UICOMPONENT_ITERATOR = new _EmptyIterator<UIComponent>();
     
     private final Iterator<UIComponent_children;
     private Iterator<? extends Object_nestedItems;
     private SelectItem _nextItem;
     private FacesContext _facesContext;
 
     public SelectItemsIterator(UIComponent selectItemsParentFacesContext facesContext)
     {
          = selectItemsParent.getChildCount() > 0
                 ? selectItemsParent.getChildren().iterator()
                 : ;
          = facesContext;
     }
 
     @SuppressWarnings("unchecked")
     public boolean hasNext()
     {
         if ( != null)
         {
             return true;
         }
         if ( != null)
         {
             if (.hasNext())
             {
                 return true;
             }
              = null;
         }
         if (.hasNext())
         {
             UIComponent child = .next();
             // When there is other components nested that does
             // not extends from UISelectItem or UISelectItems
             // the behavior for this iterator is just skip this
             // element(s) until an element that extends from these
             // classes are found. If there is no more elements
            // that conform this condition, just return false.
            while (!(child instanceof UISelectItem) && !(child instanceof UISelectItems))
            {
                // Try to skip it
                if (.hasNext())
                {
                    // Skip and do the same check
                    child = .next();
                }
                else
                {
                    // End loop, so the final result is return false,
                    // since there are no more components to iterate.
                    return false;
                }
            }
            if (child instanceof UISelectItem)
            {
                UISelectItem uiSelectItem = (UISelectItemchild;
                Object item = uiSelectItem.getValue();
                if (item == null)
                {
                    // no value attribute --> create the SelectItem out of the other attributes
                    Object itemValue = uiSelectItem.getItemValue();
                    String label = uiSelectItem.getItemLabel();
                    String description = uiSelectItem.getItemDescription();
                    boolean disabled = uiSelectItem.isItemDisabled();
                    boolean escape = uiSelectItem.isItemEscaped();
                    boolean noSelectionOption = uiSelectItem.isNoSelectionOption();
                    if (label == null)
                    {
                        label = itemValue.toString();
                    }
                    item = new SelectItem(itemValuelabeldescriptiondisabledescapenoSelectionOption);
                }
                else if (!(item instanceof SelectItem))
                {
                    ValueExpression expression = uiSelectItem.getValueExpression("value");
                    throw new IllegalArgumentException("ValueExpression '"
                            + (expression == null ? null : expression.getExpressionString()) + "' of UISelectItem : "
                            + DebugUtils.getPathToComponent(child)
                            + " does not reference an Object of type SelectItem");
                }
                 = (SelectItemitem;
                return true;
            }
            else if (child instanceof UISelectItems)
            {
                 = ((UISelectItemschild);
                Object value = .getValue();
                if (value instanceof SelectItem)
                {
                     = (SelectItemvalue;
                    return true;
                }
                else if (value != null && value.getClass().isArray())
                {
                    // value is any kind of array (primitive or non-primitive)
                    // --> we have to use class Array to get the values
                    final int length = Array.getLength(value);
                    Collection<Objectitems = new ArrayList<Object>(length);
                    for (int i = 0; i < lengthi++)
                    {
                        items.add(Array.get(valuei));
                    }
                     = items.iterator();
                    return hasNext();
                }
                else if (value instanceof Iterable)
                {
                    // value is Iterable --> Collection, DataModel,...
                     = ((Iterable<?>) value).iterator();
                    return hasNext();
                }
                else if (value instanceof Map)
                {
                    Map<ObjectObjectmap = ((Map<ObjectObject>) value);
                    Collection<SelectItemitems = new ArrayList<SelectItem>(map.size());
                    for (Map.Entry<ObjectObjectentry : map.entrySet())
                    {
                        items.add(new SelectItem(entry.getValue(), entry.getKey().toString()));
                    }
                    
                     = items.iterator();
                    return hasNext();
                }
                else
                {
                    Level level = .;
                    if (!.isProjectStage(.))
                    {
                        level = .;
                    }
                    if (.isLoggable(level))
                    {
                        ValueExpression expression = .getValueExpression("value");
                        .log(level"ValueExpression {0} of UISelectItems with component-path {1}"
                                + " does not reference an Object of type SelectItem,"
                                + " array, Iterable or Map, but of type: {2}",
                                new Object[] {
                                    (expression == null ? null : expression.getExpressionString()),
                                    DebugUtils.getPathToComponent(child),
                                    (value == null ? null : value.getClass().getName()) 
                                });
                    }
                }
            }
        }
        return false;
    }
    public SelectItem next()
    {
        if (!hasNext())
        {
            throw new NoSuchElementException();
        }
        if ( != null)
        {
            SelectItem value = ;
             = null;
            return value;
        }
        if ( != null)
        {
            Object item = .next();
            
            if (!(item instanceof SelectItem))
            {
                // check new params of SelectItems (since 2.0): itemValue, itemLabel, itemDescription,...
                // Note that according to the spec UISelectItems does not provide Getter and Setter 
                // methods for this values, so we have to use the attribute map
                Map<StringObjectattributeMap = .getAttributes();
                
                // write the current item into the request map under the key listed in var, if available
                boolean wroteRequestMapVarValue = false;
                Object oldRequestMapVarValue = null;
                final String var = (StringattributeMap.get();
                if(var != null && !"".equals(var))
                {
                    // save the current value of the key listed in var from the request map
                    oldRequestMapVarValue = .getExternalContext().getRequestMap().put(varitem);
                    wroteRequestMapVarValue = true;
                }
                
                // check the itemValue attribute
                Object itemValue = attributeMap.get();
                if (itemValue == null)
                {
                    // the itemValue attribute was not provided
                    // --> use the current item as the itemValue
                    itemValue = item;
                }
                
                // Spec: When iterating over the select items, toString() 
                // must be called on the string rendered attribute values
                Object itemLabel = attributeMap.get();
                if (itemLabel == null)
                {
                    itemLabel = itemValue.toString();
                }
                else
                {
                    itemLabel = itemLabel.toString();
                }
                Object itemDescription = attributeMap.get();
                if (itemDescription != null)
                {
                    itemDescription = itemDescription.toString();
                }
                Boolean itemDisabled = getBooleanAttribute(false);
                Boolean itemLabelEscaped = getBooleanAttribute(true);
                Object noSelectionValue = attributeMap.get();
                item = new SelectItem(itemValue,
                        (StringitemLabel,
                        (StringitemDescription,
                        itemDisabled,
                        itemLabelEscaped,
                        itemValue.equals(noSelectionValue)); 
                    
                // remove the value with the key from var from the request map, if previously written
                if(wroteRequestMapVarValue)
                {
                    // If there was a previous value stored with the key from var in the request map, restore it
                    if (oldRequestMapVarValue != null)
                    {
                        .getExternalContext()
                                .getRequestMap().put(varoldRequestMapVarValue);
                    }
                    else
                    {
                        .getExternalContext()
                                .getRequestMap().remove(var);
                    }
                } 
            }
            return (SelectItemitem;
        }
        throw new NoSuchElementException();
    }
    public void remove()
    {
        throw new UnsupportedOperationException();
    }
    
    private boolean getBooleanAttribute(UIComponent componentString attrNameboolean defaultValue)
    {
        Object value = component.getAttributes().get(attrName);
        if (value == null)
        {
            return defaultValue;
        }
        else if (value instanceof Boolean)
        {
            return (Booleanvalue;
        }
        else
        {
            // If the value is a String, parse the boolean.
            // This makes the following code work: <tag attribute="true" />,
            // otherwise you would have to write <tag attribute="#{true}" />.
            return Boolean.valueOf(value.toString());
        }
    }
New to GrepCode? Check out our FAQ X