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.view.facelets.util;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import javax.faces.FacesException;
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.context.FacesContext;
27  import org.apache.myfaces.shared.renderkit.JSFAttr;
28  import org.apache.myfaces.shared.util.ClassUtils;
29  
30  /**
31   * Utility methods used in FaceletsViewDeclarationLanguage
32   *
33   * @author Leonardo Uribe
34   */
35  public class FaceletsViewDeclarationLanguageUtils
36  {
37      public static Class getReturnType(String signature)
38      {
39          int endName = signature.indexOf('(');
40          if (endName < 0)
41          {
42              throw new FacesException("Invalid method signature:" + signature);
43          }
44          int end = signature.lastIndexOf(' ', endName);
45          if (end < 0)
46          {
47              throw new FacesException("Invalid method signature:" + signature);
48          }
49          try
50          {
51              return ClassUtils.javaDefaultTypeToClass(signature.substring(0, end));
52          }
53          catch (ClassNotFoundException e)
54          {
55              throw new FacesException("Invalid method signature:" + signature);
56          }
57      }
58  
59      /**
60       * Get the parameters types from the function signature.
61       *
62       * @return An array of parameter class names
63       */
64      public static Class[] getParameters(String signature) throws FacesException
65      {
66          ArrayList<Class> params = new ArrayList<Class>();
67          // Signature is of the form
68          // <return-type> S <method-name S? '('
69          // < <arg-type> ( ',' <arg-type> )* )? ')'
70          int start = signature.indexOf('(') + 1;
71          boolean lastArg = false;
72          while (true)
73          {
74              int p = signature.indexOf(',', start);
75              if (p < 0)
76              {
77                  p = signature.indexOf(')', start);
78                  if (p < 0)
79                  {
80                      throw new FacesException("Invalid method signature:" + signature);
81                  }
82                  lastArg = true;
83              }
84              String arg = signature.substring(start, p).trim();
85              if (!"".equals(arg))
86              {
87                  try
88                  {
89                      params.add(ClassUtils.javaDefaultTypeToClass(arg));
90                  }
91                  catch (ClassNotFoundException e)
92                  {
93                      throw new FacesException("Invalid method signature:" + signature);
94                  }
95              }
96              if (lastArg)
97              {
98                  break;
99              }
100             start = p + 1;
101         }
102         return params.toArray(new Class[params.size()]);
103     }
104 
105     /**
106      * Called on restoreView(...) to mark resources as rendered.
107      * 
108      * @since 2.3
109      * @param facesContext
110      * @param view 
111      */
112     public static void markRenderedResources(FacesContext facesContext, UIViewRoot view)
113     {
114         if (view != null && facesContext.getPartialViewContext().isAjaxRequest())
115         {
116             markRenderedResources(facesContext, view, view.getComponentResources(facesContext, "head"));
117             markRenderedResources(facesContext, view, view.getComponentResources(facesContext, "body"));
118         }
119     }
120 
121     private static void markRenderedResources(FacesContext facesContext, UIViewRoot view, 
122             List<UIComponent> componentResources)
123     {
124         if (componentResources != null)
125         {
126             for (UIComponent component : componentResources)
127             {
128                 if ("javax.faces.resource.Script".equals(component.getRendererType()) ||
129                     "javax.faces.resource.Stylesheet".equals(component.getRendererType()))
130                 {
131                     String resourceName = (String) component.getAttributes().get(JSFAttr.NAME_ATTR);
132                     String libraryName = (String) component.getAttributes().get(JSFAttr.LIBRARY_ATTR);
133                     
134                     if (resourceName == null)
135                     {
136                         continue;
137                     }
138                     if ("".equals(resourceName))
139                     {
140                         continue;
141                     }
142 
143                     int index = resourceName.indexOf('?');
144                     if (index >= 0)
145                     {
146                         resourceName = resourceName.substring(0, index);
147                     }
148                     
149                     facesContext.getApplication().getResourceHandler().markResourceRendered(
150                             facesContext, resourceName, libraryName);
151                 }
152             }
153         }
154     }
155 }