2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 public class DefaultViewControllerMapper implements ViewControllerMapper {
73
74
75
76
77
78 /***
79 * <p>Reserved variable names.</p>
80 */
81 private static Set reserved = new HashSet();
82
83 static {
84 reserved.add("applicationScope");
85 reserved.add("cookie");
86 reserved.add("facesContext");
87 reserved.add("header");
88 reserved.add("headerValues");
89 reserved.add("initParam");
90 reserved.add("param");
91 reserved.add("paramValues");
92 reserved.add("requestScope");
93 reserved.add("sessionScope");
94 reserved.add("view");
95 }
96
97
98
99
100
101 /*** {@inheritDoc} */
102 public String mapViewId(String viewId) {
103
104 if (viewId == null) {
105 return null;
106 }
107 if (viewId.startsWith("/")) {
108 viewId = viewId.substring(1);
109 }
110 int slash = viewId.lastIndexOf("/");
111 int period = viewId.lastIndexOf(".");
112 if ((period >= 0) && (period > slash)) {
113 viewId = viewId.substring(0, period);
114 }
115 viewId = viewId.replace('/', '$');
116 if (reserved.contains(viewId)) {
117 return "_" + viewId;
118 } else if ((viewId.length() > 0) && Character.isDigit(viewId.charAt(0))) {
119 return "_" + viewId;
120 } else {
121 return viewId;
122 }
123
124 }
125
126
127 }