View Javadoc

1   /*
2    * $Id: VariableResolver.java,v 1.1 2007/03/23 16:26:33 oeuillot Exp $
3    */
4   package org.rcfaces.core.util;
5   
6   import javax.faces.FacesException;
7   import javax.faces.context.FacesContext;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  import org.rcfaces.core.internal.Constants;
12  
13  /**
14   * 
15   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
16   * @version $Revision: 1.1 $ $Date: 2007/03/23 16:26:33 $
17   */
18  public class VariableResolver {
19  
20      private static final String REVISION = "$Revision: 1.1 $";
21  
22      private static final Log LOG = LogFactory.getLog(VariableResolver.class);
23  
24      /**
25       * Search and instanciate (if necessary) a backing bean by its name.
26       * 
27       * @param variable
28       *            The variable name of the backing bean.
29       * @return An object (can not return <code>null</code>)
30       * @throws FacesException
31       *             if the variable is unknown.
32       */
33      public static Object resolve(String variable) {
34          return resolve(variable, null);
35      }
36  
37      /**
38       * Search and instanciate (if necessary) a backing bean by its name.
39       * 
40       * @param variable
41       *            The variable name of the backing bean.
42       * @param facesContext
43       *            The facesContext or <code>null</code>.
44       * @return An object (can not return <code>null</code>)
45       * @throws FacesException
46       *             if the variable is unknown.
47       */
48      public static Object resolve(String variable, FacesContext facesContext) {
49  
50          if (facesContext == null) {
51              facesContext = FacesContext.getCurrentInstance();
52          }
53  
54          if (Constants.VERIFY_VARIABLE_SYNTAX) {
55              char chs[] = variable.toCharArray();
56  
57              for (int i = 0; i < chs.length; i++) {
58                  if (i == 0) {
59                      if (Character.isJavaIdentifierStart(chs[i]) == false) {
60                          throw new FacesException(
61                                  "Illegal start of variable name '" + variable
62                                          + "'.");
63                      }
64  
65                      continue;
66                  }
67  
68                  if (Character.isJavaIdentifierPart(chs[i]) == false) {
69                      throw new FacesException(
70                              "Illegal character into variable name '" + variable
71                                      + "'. (position=" + i + ")");
72                  }
73              }
74          }
75  
76          if (LOG.isDebugEnabled()) {
77              LOG.debug("Try to resolve variable '" + variable + "'.");
78          }
79  
80          Object ret;
81          if (false /* JSF 1.2 */) {
82              /*
83               * @TODO if JSF 1.2 : faces.getELContext();
84               */
85          } else {
86              javax.faces.el.VariableResolver variableResolver = facesContext
87                      .getApplication().getVariableResolver();
88  
89              ret = variableResolver.resolveVariable(facesContext, variable);
90          }
91  
92          if (LOG.isDebugEnabled()) {
93              LOG.debug("Resolved variable '" + variable + "' = " + ret);
94          }
95  
96          if (ret == null) {
97              throw new FacesException("No variable associated to name '"
98                      + variable + "'.");
99          }
100 
101         return ret;
102     }
103 }