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   */
20  
21  package org.apache.myfaces.orchestra.viewController;
22  
23  import java.util.Arrays;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  /***
28   * <p>Map view-ids to bean names.</p>
29   * <p/>
30   * The strategy of this mapper is as follows:
31   * <ul>
32   * <li>slash ('/') will be removed</li>
33   * <li>every character after a slash will be convertert to uppercase, except the first char</li>
34   * <li>everyting after a dot ('.') will be removed</li>
35   * <li>reserved words {@link #RESERVED_WORDS} are prefixed with an underscore ('_')</li>
36   * <li>result starting with an digit are prefixed too </li>
37   * </ul>
38   * </p>
39   * <p/>
40   * Examples:
41   * <table border="1">
42   * <thead><tr><th>ViewId</th><th>BeanName</th></tr></thead>
43   * <tbody>
44   * <tr><td>/userInfo.jsp</td><td>userInfo</td></tr>
45   * <tr><td>/SecureArea/userPassword.jsp</td><td>secureAreaUserPassword</td></tr>
46   * </tbody>
47   * </table>
48   * </p>
49   */
50  public class DefaultViewControllerNameMapper implements ViewControllerNameMapper
51  {
52  	private static Set RESERVED_WORDS = new HashSet(Arrays.asList(
53  		new String[]
54  			{
55  				"applicationScope",
56  				"cookie",
57  				"facesContext",
58  				"header",
59  				"headerValues",
60  				"initParam",
61  				"param",
62  				"paramValues",
63  				"requestScope",
64  				"sessionScope",
65  				"view"
66  			}
67  	));
68  
69  
70  	public String mapViewId(String viewId)
71  	{
72  		if (viewId == null)
73  		{
74  			return null;
75  		}
76  
77  		boolean nextUpper = false;
78  
79  		StringBuffer sb = new StringBuffer(viewId);
80  		for (int i = 0; i < sb.length(); i++)
81  		{
82  			char c = sb.charAt(i);
83  			if (c == '/')
84  			{
85  				if (i > 0)
86  				{
87  					nextUpper = true;
88  				}
89  				sb.deleteCharAt(i);
90  				i--;
91  			}
92  			else if (c == '.')
93  			{
94  				sb.delete(i, sb.length());
95  				break;
96  			}
97  			else if (nextUpper)
98  			{
99  				sb.setCharAt(i, Character.toUpperCase(c));
100 				nextUpper = false;
101 			}
102 		}
103 
104 		if (sb.length() > 0)
105 		{
106 			sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
107 		}
108 
109 		String beanName = sb.toString();
110 		if (RESERVED_WORDS.contains(beanName))
111 		{
112 			return "_" + beanName;
113 		}
114 
115 		if (beanName.length() > 0 && Character.isDigit(beanName.charAt(0)))
116 		{
117 			return "_" + beanName;
118 		}
119 
120 		return beanName;
121 	}
122 }