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.shared.application;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.faces.context.FacesContext;
27  
28  public class NavigationUtils
29  {
30      /**
31       * Evaluate all EL expressions found as parameters and return a map that can be used for 
32       * redirect or render bookmark links
33       * 
34       * @param parameters parameter map retrieved from NavigationCase.getParameters()
35       * @return
36       */
37      public static Map<String, List<String> > getEvaluatedNavigationParameters(
38              FacesContext facesContext, 
39              Map<String, List<String> > parameters)
40      {
41          Map<String,List<String>> evaluatedParameters = null;
42          if (parameters != null && parameters.size() > 0)
43          {
44              evaluatedParameters = new HashMap<String, List<String>>();
45              for (Map.Entry<String, List<String>> pair : parameters.entrySet())
46              {
47                  boolean containsEL = false;
48                  for (String value : pair.getValue())
49                  {
50                      if (_isExpression(value))
51                      {
52                          containsEL = true;
53                          break;
54                      }
55                  }
56                  if (containsEL)
57                  {
58                      evaluatedParameters.put(pair.getKey(), 
59                              _evaluateValueExpressions(facesContext, pair.getValue()));
60                  }
61                  else
62                  {
63                      evaluatedParameters.put(pair.getKey(), pair.getValue());
64                  }
65              }
66          }
67          else
68          {
69              evaluatedParameters = parameters;
70          }
71          return evaluatedParameters;
72      }
73      
74      /**
75       * Checks the Strings in the List for EL expressions and evaluates them.
76       * Note that the returned List will be a copy of the given List, because
77       * otherwise it will have unwanted side-effects.
78       * @param values
79       * @return
80       */
81      private static List<String> _evaluateValueExpressions(FacesContext context, List<String> values)
82      {
83          // note that we have to create a new List here, because if we
84          // change any value on the given List, it will be changed in the
85          // NavigationCase too and the EL expression won't be evaluated again
86          List<String> target = new ArrayList<String>(values.size());
87          for (String value : values)
88          {
89              if (_isExpression(value))
90              {
91                  // evaluate the ValueExpression
92                  value = context.getApplication().evaluateExpressionGet(context, value, String.class);
93              }
94              target.add(value);
95          }
96          return target;
97      }
98      
99      private static boolean _isExpression(String text)
100     {
101         return text.indexOf("#{") != -1;
102     }
103 
104 }