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 javax.faces.FacesException;
23  import org.apache.myfaces.shared.util.ClassUtils;
24  
25  /**
26   * Utility methods used in FaceletsViewDeclarationLanguage
27   *
28   * @author Leonardo Uribe
29   */
30  public class FaceletsViewDeclarationLanguageUtils
31  {
32      public static Class getReturnType(String signature)
33      {
34          int endName = signature.indexOf('(');
35          if (endName < 0)
36          {
37              throw new FacesException("Invalid method signature:" + signature);
38          }
39          int end = signature.lastIndexOf(' ', endName);
40          if (end < 0)
41          {
42              throw new FacesException("Invalid method signature:" + signature);
43          }
44          try
45          {
46              return ClassUtils.javaDefaultTypeToClass(signature.substring(0, end));
47          }
48          catch (ClassNotFoundException e)
49          {
50              throw new FacesException("Invalid method signature:" + signature);
51          }
52      }
53  
54      /**
55       * Get the parameters types from the function signature.
56       *
57       * @return An array of parameter class names
58       */
59      public static Class[] getParameters(String signature) throws FacesException
60      {
61          ArrayList<Class> params = new ArrayList<Class>();
62          // Signature is of the form
63          // <return-type> S <method-name S? '('
64          // < <arg-type> ( ',' <arg-type> )* )? ')'
65          int start = signature.indexOf('(') + 1;
66          boolean lastArg = false;
67          while (true)
68          {
69              int p = signature.indexOf(',', start);
70              if (p < 0)
71              {
72                  p = signature.indexOf(')', start);
73                  if (p < 0)
74                  {
75                      throw new FacesException("Invalid method signature:" + signature);
76                  }
77                  lastArg = true;
78              }
79              String arg = signature.substring(start, p).trim();
80              if (!"".equals(arg))
81              {
82                  try
83                  {
84                      params.add(ClassUtils.javaDefaultTypeToClass(arg));
85                  }
86                  catch (ClassNotFoundException e)
87                  {
88                      throw new FacesException("Invalid method signature:" + signature);
89                  }
90              }
91              if (lastArg)
92              {
93                  break;
94              }
95              start = p + 1;
96          }
97          return params.toArray(new Class[params.size()]);
98      }
99  
100 
101 }