org.apache.myfaces.shared.util.SelectItemsIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of myfaces-impl Show documentation
Show all versions of myfaces-impl Show documentation
The private implementation classes of the Apache MyFaces Core JSF-2.1 Implementation
/*
* 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.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.el.ValueExpression;
import javax.faces.application.ProjectStage;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItem;
import javax.faces.component.UISelectItems;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.apache.myfaces.shared.renderkit.JSFAttr;
import org.apache.myfaces.shared.renderkit.RendererUtils;
// ATTENTION
// This class is associated with javax.faces.component._SelectItemsIterator.
// Changes here should also be applied to this class.
/**
* @author Mathias Broekelmann (latest modification by $Author: lu4242 $)
* @author Jakob Korherr (jsf 2.0)
* @version $Revision: 1200044 $ $Date: 2011-11-09 18:44:02 -0500 (Wed, 09 Nov 2011) $
*/
public class SelectItemsIterator implements Iterator
{
private static final Logger log = Logger.getLogger(SelectItemsIterator.class.getName());
private static final String VAR_ATTR = JSFAttr.VAR_ATTR;
private static final String ITEM_VALUE_ATTR = JSFAttr.ITEM_VALUE_ATTR;
private static final String ITEM_LABEL_ATTR = JSFAttr.ITEM_LABEL_ATTR;
private static final String ITEM_DESCRIPTION_ATTR = JSFAttr.ITEM_DESCRIPTION_ATTR;
private static final String ITEM_DISABLED_ATTR = JSFAttr.ITEM_DISABLED_ATTR;
private static final String ITEM_LABEL_ESCAPED_ATTR = JSFAttr.ITEM_LABEL_ESCAPED_ATTR;
private static final String NO_SELECTION_VALUE_ATTR = JSFAttr.NO_SELECTION_VALUE_ATTR;
private static final Iterator _EMPTY_UICOMPONENT_ITERATOR = new _EmptyIterator();
private final Iterator _children;
private Iterator _nestedItems;
private SelectItem _nextItem;
private UISelectItems _currentUISelectItems;
private FacesContext _facesContext;
public SelectItemsIterator(UIComponent selectItemsParent, FacesContext facesContext)
{
_children = selectItemsParent.getChildCount() > 0 ?
selectItemsParent.getChildren().iterator() : _EMPTY_UICOMPONENT_ITERATOR;
_facesContext = facesContext;
}
@SuppressWarnings("unchecked")
public boolean hasNext()
{
if (_nextItem != null)
{
return true;
}
if (_nestedItems != null)
{
if (_nestedItems.hasNext())
{
return true;
}
_nestedItems = null;
}
if (_children.hasNext())
{
UIComponent child = _children.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 (_children.hasNext())
{
// Skip and do the same check
child = _children.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 = (UISelectItem) child;
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(itemValue, label, description, disabled, escape, noSelectionOption);
}
else if (!(item instanceof SelectItem))
{
ValueExpression expression = uiSelectItem.getValueExpression("value");
throw new IllegalArgumentException("ValueExpression '"
+ (expression == null ? null : expression.getExpressionString()) + "' of UISelectItem : "
+ RendererUtils.getPathToComponent(child) +
" does not reference an Object of type SelectItem");
}
_nextItem = (SelectItem) item;
return true;
}
else if (child instanceof UISelectItems)
{
_currentUISelectItems = ((UISelectItems) child);
Object value = _currentUISelectItems.getValue();
if (value instanceof SelectItem)
{
_nextItem = (SelectItem) value;
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