1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.application;
20
21 import java.net.MalformedURLException;
22 import java.net.URL;
23 import java.util.List;
24 import java.util.Map;
25
26 import javax.el.ExpressionFactory;
27 import javax.el.ValueExpression;
28 import javax.faces.context.ExternalContext;
29 import javax.faces.context.FacesContext;
30
31
32
33
34
35
36
37 public class NavigationCase
38 {
39 private String _condition;
40 private String _fromAction;
41 private String _fromOutcome;
42 private String _fromViewId;
43 private String _toViewId;
44 private boolean _includeViewParams;
45 private boolean _redirect;
46 private Map<String,List<String>> _parameters;
47
48 private ValueExpression _conditionExpression;
49 private ValueExpression _toViewIdExpression;
50
51 public NavigationCase(String fromViewId, String fromAction, String fromOutcome, String condition, String toViewId,
52 Map<String,List<String>> parameters, boolean redirect, boolean includeViewParams)
53 {
54 _condition = condition;
55 _fromViewId = fromViewId;
56 _fromAction = fromAction;
57 _fromOutcome = fromOutcome;
58 _toViewId = toViewId;
59 _redirect = redirect;
60 _includeViewParams = includeViewParams;
61 _parameters = parameters;
62 }
63
64
65
66
67 @Override
68 public boolean equals(Object o)
69 {
70 if (o == this)
71 {
72 return true;
73 }
74 else if (o instanceof NavigationCase)
75 {
76 NavigationCase c = (NavigationCase) o;
77
78 return equals(_fromViewId, c._fromViewId) && equals(_fromAction, c._fromAction)
79 && equals(_fromOutcome, c._fromOutcome) && equals(_condition, c._condition)
80 && equals(_toViewId, c._toViewId) && _redirect == c._redirect
81 && _includeViewParams == c._includeViewParams;
82 }
83 else
84 {
85 return false;
86 }
87 }
88
89
90
91
92 @Override
93 public int hashCode()
94 {
95 return hash(_fromViewId) << 4 ^ hash(_fromAction) << 8 ^ hash(_fromOutcome) << 12 ^ hash(_condition) << 16
96 ^ hash(_toViewId) << 20 ^ hash(Boolean.valueOf(_redirect)) << 24
97 ^ hash(Boolean.valueOf(_includeViewParams));
98 }
99
100 public URL getActionURL(FacesContext context) throws MalformedURLException
101 {
102 ExternalContext externalContext = context.getExternalContext();
103 return new URL(externalContext.getRequestScheme(), externalContext.getRequestServerName(),
104 externalContext.getRequestServerPort(),
105 context.getApplication().getViewHandler().getActionURL(context, getToViewId(context)));
106 }
107
108 public Boolean getCondition(FacesContext context)
109 {
110 if (_condition == null)
111 {
112 return null;
113 }
114
115 ValueExpression expression = _getConditionExpression(context);
116
117 return Boolean.TRUE.equals(expression.getValue(context.getELContext()));
118 }
119
120 public String getFromAction()
121 {
122 return _fromAction;
123 }
124
125 public String getFromOutcome()
126 {
127 return _fromOutcome;
128 }
129
130 public String getFromViewId()
131 {
132 return _fromViewId;
133 }
134
135 public URL getBookmarkableURL(FacesContext context) throws MalformedURLException
136 {
137 ExternalContext externalContext = context.getExternalContext();
138 return new URL(externalContext.getRequestScheme(),
139 externalContext.getRequestServerName(),
140 externalContext.getRequestServerPort(),
141 context.getApplication().getViewHandler().getBookmarkableURL(context, getToViewId(context),
142 _NavigationUtils.getEvaluatedNavigationParameters(context,
143 getParameters()), isIncludeViewParams()));
144 }
145
146 public URL getResourceURL(FacesContext context) throws MalformedURLException
147 {
148 ExternalContext externalContext = context.getExternalContext();
149 return new URL(externalContext.getRequestScheme(), externalContext.getRequestServerName(),
150 externalContext.getRequestServerPort(),
151 context.getApplication().getViewHandler().getResourceURL(context, getToViewId(context)));
152 }
153
154 public URL getRedirectURL(FacesContext context) throws MalformedURLException
155 {
156 ExternalContext externalContext = context.getExternalContext();
157 return new URL(externalContext.getRequestScheme(),
158 externalContext.getRequestServerName(),
159 externalContext.getRequestServerPort(),
160 context.getApplication().getViewHandler().getRedirectURL(context, getToViewId(context),
161 _NavigationUtils.getEvaluatedNavigationParameters(context,
162 getParameters()), isIncludeViewParams()));
163 }
164
165 public Map<String,List<String>> getParameters()
166 {
167 return _parameters;
168 }
169
170 public String getToViewId(FacesContext context)
171 {
172 if (_toViewId == null)
173 {
174 return null;
175 }
176
177 ValueExpression expression = _getToViewIdExpression(context);
178
179 return (String)( (expression.isLiteralText()) ?
180 expression.getExpressionString() : expression.getValue(context.getELContext()) );
181 }
182
183 public boolean hasCondition()
184 {
185 return _condition != null && _condition.length() > 0;
186 }
187
188 public boolean isIncludeViewParams()
189 {
190 return _includeViewParams;
191 }
192
193 public boolean isRedirect()
194 {
195 return _redirect;
196 }
197
198
199
200
201 @Override
202 public String toString()
203 {
204 StringBuilder builder = new StringBuilder(128);
205 builder.append("<navigation-case>\n");
206
207 if (_fromViewId != null)
208 {
209 builder.append(" ");
210 builder.append("<from-view-id>");
211 builder.append(_fromViewId);
212 builder.append("</from-view-id>\n");
213 }
214
215 if (_fromAction != null)
216 {
217 builder.append(" ");
218 builder.append("<from-action>");
219 builder.append(_fromAction);
220 builder.append("</from-action>\n");
221 }
222
223 if (_fromOutcome != null)
224 {
225 builder.append(" ");
226 builder.append("<from-outcome>");
227 builder.append(_fromOutcome);
228 builder.append("</from-outcome>\n");
229 }
230
231 if (_condition != null)
232 {
233 builder.append(" ");
234 builder.append("<if>");
235 builder.append(_condition);
236 builder.append("</if>\n");
237 }
238
239 builder.append(" ");
240 builder.append("<to-view-id>");
241 builder.append(_toViewId);
242 builder.append("</to-view-id>\n");
243
244 if (_redirect)
245 {
246 builder.append(" ");
247 builder.append("<redirect include-view-params=\"");
248 builder.append(_includeViewParams);
249 if (_parameters != null && _parameters.size() != 0)
250 {
251 builder.append("\">\n");
252 for (Map.Entry<String, List<String>> entry : _parameters.entrySet())
253 {
254 String name = entry.getKey();
255 for (String value : entry.getValue())
256 {
257 builder.append(" <view-param>\n");
258 builder.append(" <name>");
259 builder.append(name);
260 builder.append("</name>\n");
261 builder.append(" <value>");
262 builder.append(value);
263 builder.append("</value>\n");
264 builder.append(" </view-param>\n");
265 }
266 }
267 builder.append(" </redirect>\n");
268 }
269 else
270 {
271 builder.append("\"/>\n");
272 }
273 }
274
275 builder.append("</navigation-case>");
276
277 return builder.toString();
278 }
279
280 private ValueExpression _getConditionExpression(FacesContext context)
281 {
282 assert _condition != null;
283
284 if (_conditionExpression == null)
285 {
286 ExpressionFactory factory = context.getApplication().getExpressionFactory();
287 _conditionExpression = factory.createValueExpression(context.getELContext(), _condition, Boolean.class);
288 }
289
290 return _conditionExpression;
291 }
292
293 private ValueExpression _getToViewIdExpression(FacesContext context)
294 {
295 assert _toViewId != null;
296
297 if (_toViewIdExpression == null)
298 {
299 ExpressionFactory factory = context.getApplication().getExpressionFactory();
300 _toViewIdExpression = factory.createValueExpression(context.getELContext(), _toViewId, String.class);
301 }
302
303 return _toViewIdExpression;
304 }
305
306 private boolean equals(Object o1, Object o2)
307 {
308 if (o1 == null)
309 {
310 return o2 == null;
311 }
312 else
313 {
314 return o1.equals(o2);
315 }
316 }
317
318 private int hash(Object o)
319 {
320 return o == null ? 0 : o.hashCode();
321 }
322 }