View Javadoc

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 org.apache.myfaces.custom.navmenu;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.myfaces.component.UserRoleUtils;
24  
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UISelectItems;
27  import javax.faces.context.FacesContext;
28  import javax.faces.el.MethodBinding;
29  import java.util.ArrayList;
30  import java.util.Collection;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  /**
35   * @author Thomas Spiegl (latest modification by $Author: grantsmith $)
36   * @version $Revision: 472638 $ $Date: 2006-11-08 15:54:13 -0500 (Wed, 08 Nov 2006) $
37   */
38  public class NavigationMenuUtils
39  {
40      private static final Log log = LogFactory.getLog(NavigationMenuUtils.class);
41  
42      public static List getNavigationMenuItemList(UIComponent uiComponent)
43      {
44          List list = new ArrayList(uiComponent.getChildCount());
45          for (Iterator children = uiComponent.getChildren().iterator(); children.hasNext(); )
46          {
47              UIComponent child = (UIComponent)children.next();
48              if (child instanceof UINavigationMenuItem)
49              {
50                  NavigationMenuItem item;
51                  Object value = ((UINavigationMenuItem)child).getValue();
52                  if (value != null)
53                  {
54                      //get NavigationMenuItem from model via value binding
55                      if (!(value instanceof NavigationMenuItem))
56                      {
57                          FacesContext facesContext = FacesContext.getCurrentInstance();
58                          throw new IllegalArgumentException("Value binding of UINavigationMenuItem with id " + child.getClientId(facesContext) + " does not reference an Object of type NavigationMenuItem");
59                      }
60                      item = (NavigationMenuItem)value;
61                  }
62                  else
63                  {
64                      UINavigationMenuItem uiItem = (UINavigationMenuItem)child;
65                      String itemLabel = uiItem.getItemLabel();
66                      Object itemValue = uiItem.getItemValue();
67                      if (itemValue == null) itemValue = "";
68                      if (itemLabel == null)
69                      {
70                          itemLabel = itemValue.toString();
71                      }
72                      String actionStr = null;
73                      MethodBinding action = uiItem.getAction();
74                      if (action != null)
75                      {
76                          if (action.getExpressionString() != null)
77                          {
78                              actionStr = action.getExpressionString();
79                          }
80                          else
81                          {
82                              actionStr = action.toString();
83                          }
84                      }
85                      item = new NavigationMenuItem(itemValue,
86                                                    itemLabel,
87                                                    uiItem.getItemDescription(),
88                                                    uiItem.isItemDisabled() || ! UserRoleUtils.isEnabledOnUserRole(uiItem),
89                                                    uiItem.isRendered(),
90                                                    actionStr,
91                                                    uiItem.getIcon(),
92                                                    uiItem.isSplit());
93                      if (uiItem.getActionListener() != null)
94                          item.setActionListener(uiItem.getActionListener().getExpressionString());
95                  }
96  
97                  list.add(item);
98                  if (child.getChildCount() > 0)
99                  {
100                     List l = getNavigationMenuItemList(child);
101                     item.setNavigationMenuItems((NavigationMenuItem[]) l.toArray(new NavigationMenuItem[l.size()]));
102                 }
103             }
104             else if (child instanceof UISelectItems)
105             {
106                 addNavigationMenuItems((UISelectItems) child, list);
107             }
108             else
109             {
110                 FacesContext facesContext = FacesContext.getCurrentInstance();
111                 log.error("Invalid child with id " + child.getClientId(facesContext) + "of component with id : "+uiComponent.getClientId(facesContext)
112                         +" : must be UINavigationMenuItem or UINavigationMenuItems, is of type : "+((child==null)?"null":child.getClass().getName()));
113             }
114         }
115 
116         return list;
117     }
118 
119     public static void addNavigationMenuItems(UISelectItems child, List list)
120     {
121         Object value = child.getValue();
122 
123         if (value == null)
124         {
125             FacesContext facesContext = FacesContext.getCurrentInstance();
126             throw new NullPointerException("Value binding of UINavigationMenuItems with id " + child.getClientId(facesContext) + " is null");
127         }
128 
129         if (value instanceof NavigationMenuItem)
130         {
131             list.add(value);
132         }
133         else if (value instanceof NavigationMenuItem[])
134         {
135             for (int i = 0; i < ((NavigationMenuItem[])value).length; i++)
136             {
137                 list.add(((NavigationMenuItem[])value)[i]);
138             }
139         }
140         else if (value instanceof Collection)
141         {
142             for (Iterator it = ((Collection)value).iterator(); it.hasNext();)
143             {
144                 Object item = it.next();
145                 if (!(item instanceof NavigationMenuItem))
146                 {
147                     FacesContext facesContext = FacesContext.getCurrentInstance();
148                     throw new IllegalArgumentException("Collection referenced by UINavigationMenuItems with id " + child.getClientId(facesContext) + " does not contain Objects of type NavigationMenuItem");
149                 }
150                 list.add(item);
151             }
152         }
153         else
154         {
155             FacesContext facesContext = FacesContext.getCurrentInstance();
156             throw new IllegalArgumentException("Value binding of UINavigationMenuItems with id " + child.getClientId(facesContext) + " does not reference an Object of type NavigationMenuItem, NavigationMenuItem[], Collection or Map");
157         }
158     }
159     
160     
161     /**
162      * Return true if the specified string contains an EL expression.
163      * <p>
164      * This is taken almost verbatim from {@link javax.faces.webapp.UIComponentTag}
165      * in order to remove JSP dependencies from the renderers.
166      */
167     public static boolean isValueReference(String value)
168     {
169         if (value == null) return false;
170 
171         int start = value.indexOf("#{");
172         if (start < 0) return false;
173 
174         int end = value.lastIndexOf('}');
175         return (end >=0 && start < end);
176     }
177 
178 }