Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DefaultViewControllerMapper |
|
| 9.0;9 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to you under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | package org.apache.shale.view.impl; | |
19 | ||
20 | import java.util.HashSet; | |
21 | import java.util.Set; | |
22 | ||
23 | import org.apache.shale.view.ViewControllerMapper; | |
24 | ||
25 | /** | |
26 | * <p>{@link DefaultViewControllerMapper} is a default implementation of {@link ViewControllerMapper}. The following | |
27 | * algorithm is implemented:</p> | |
28 | * <ul> | |
29 | * <li>Strip any leading slash ("/") character.</li> | |
30 | * <li>Strip any traling extension (".xxx"), as long as it occurs | |
31 | * after any remaining slash ("/") character.</li> | |
32 | * <li>Convert each instance of a slash ("/") | |
33 | * character into a dollar sign ("$") character.</li> | |
34 | * <li>If the resulting name matches one of the reserved names recognized | |
35 | * by the default <code>VariableResolver</code>, prefix it with an | |
36 | * underscore character ("_"), to avoid problems loading managed beans.</li> | |
37 | * <li>If the resulting name starts with a digit character, prefix it with | |
38 | * an underscore character("_"), to avoid problems evaluating value | |
39 | * binding expressions using it (because this would be treated as a | |
40 | * variable name starting with a digit, and that is not allowed by the | |
41 | * syntax of expression evaluation).</li> | |
42 | * </ul> | |
43 | * | |
44 | * <p>Examples of correct managed bean names for typical JSF view identifiers, | |
45 | * when this mapper is used, would include:</p> | |
46 | * <ul> | |
47 | * <li>For a view identifier <code>/mainmenu.jsp</code>, your managed bean | |
48 | * name <em>must</em> be <code>mainmenu</code> (leading slash and | |
49 | * trailing extension were stripped).</li> | |
50 | * <li>For a view identifier <code>/customer/details.jsp</code>, your managed | |
51 | * bean name <em>must</em> be <code>customer$details</code> (intermediate | |
52 | * slash character also converted).</li> | |
53 | * <li>For a view identifier <code>/header.jsp</code>, your managed bean | |
54 | * name <em>must</em> be <code>_header</code> ("header" is a magic JSF | |
55 | * variable returning a Map of HTTP headers for the current request, so | |
56 | * you cannot use this name for your own managed beans).</li> | |
57 | * </ul> | |
58 | * | |
59 | * | |
60 | * <p>Since the managed bean names also need to be valid variable names in | |
61 | * the expression language, this mapper implementation imposes certain | |
62 | * restrictions on the view identifiers. View identifiers must not contain | |
63 | * characters which have reserved meanings in the expression language, such | |
64 | * as '-' (minus) or '+' (plus). A best practice while using this mapper | |
65 | * is to ensure view identifiers use letters of the English alphabet | |
66 | * in upper or lower case, digits from 0 to 9, '$' (dollar signs) and '_' | |
67 | * (underscores) only.</p> | |
68 | * | |
69 | * $Id: DefaultViewControllerMapper.java 464373 2006-10-16 04:21:54Z rahul $ | |
70 | */ | |
71 | ||
72 | 5 | public class DefaultViewControllerMapper implements ViewControllerMapper { |
73 | ||
74 | ||
75 | // -------------------------------------------------------- Static Variables | |
76 | ||
77 | ||
78 | /** | |
79 | * <p>Reserved variable names.</p> | |
80 | */ | |
81 | 1 | private static Set reserved = new HashSet(); |
82 | ||
83 | static { | |
84 | 1 | reserved.add("applicationScope"); |
85 | 1 | reserved.add("cookie"); |
86 | 1 | reserved.add("facesContext"); |
87 | 1 | reserved.add("header"); |
88 | 1 | reserved.add("headerValues"); |
89 | 1 | reserved.add("initParam"); |
90 | 1 | reserved.add("param"); |
91 | 1 | reserved.add("paramValues"); |
92 | 1 | reserved.add("requestScope"); |
93 | 1 | reserved.add("sessionScope"); |
94 | 1 | reserved.add("view"); |
95 | 1 | } |
96 | ||
97 | ||
98 | // ---------------------------------------------------------- Public Methods | |
99 | ||
100 | ||
101 | /** {@inheritDoc} */ | |
102 | public String mapViewId(String viewId) { | |
103 | ||
104 | 22 | if (viewId == null) { |
105 | return null; | |
106 | } | |
107 | 22 | if (viewId.startsWith("/")) { |
108 | 18 | viewId = viewId.substring(1); |
109 | } | |
110 | 22 | int slash = viewId.lastIndexOf("/"); |
111 | 22 | int period = viewId.lastIndexOf("."); |
112 | 22 | if ((period >= 0) && (period > slash)) { |
113 | 22 | viewId = viewId.substring(0, period); |
114 | } | |
115 | 22 | viewId = viewId.replace('/', '$'); |
116 | 22 | if (reserved.contains(viewId)) { |
117 | 11 | return "_" + viewId; |
118 | 11 | } else if ((viewId.length() > 0) && Character.isDigit(viewId.charAt(0))) { |
119 | 2 | return "_" + viewId; |
120 | } else { | |
121 | 9 | return viewId; |
122 | } | |
123 | ||
124 | } | |
125 | ||
126 | ||
127 | } |