|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
PageServiceEncoder.java | 100% | 100% | 100% | 100% |
|
1 | // Copyright 2004, 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package org.apache.tapestry.engine.encoders; | |
16 | ||
17 | import org.apache.tapestry.INamespace; | |
18 | import org.apache.tapestry.engine.ServiceEncoder; | |
19 | import org.apache.tapestry.engine.ServiceEncoding; | |
20 | import org.apache.tapestry.services.ServiceConstants; | |
21 | ||
22 | /** | |
23 | * The canonical implementation of {@link org.apache.tapestry.engine.ServiceEncoder}, it encodes | |
24 | * page name and a service name. The page name becomes the servlet path, prefixed with "/" and | |
25 | * suffixed with a dot and a particular extension. In this way, "/app?service=page&page=Home" | |
26 | * becomes simply "Home.html". This is most suitable for the "page" and "external" services. | |
27 | * | |
28 | * @author Howard M. Lewis Ship | |
29 | * @since 4.0 | |
30 | */ | |
31 | public class PageServiceEncoder implements ServiceEncoder | |
32 | { | |
33 | private String _extension; | |
34 | ||
35 | private String _serviceName; | |
36 | ||
37 | 5 | public void encode(ServiceEncoding encoding) |
38 | { | |
39 | 5 | String service = encoding.getParameterValue(ServiceConstants.SERVICE); |
40 | ||
41 | 5 | if (!service.equals(_serviceName)) |
42 | 1 | return; |
43 | ||
44 | 4 | String pageName = encoding.getParameterValue(ServiceConstants.PAGE); |
45 | ||
46 | // Only handle pages in the application namespace (not from a library). | |
47 | ||
48 | 4 | if (pageName.indexOf(INamespace.SEPARATOR) >= 0) |
49 | 1 | return; |
50 | ||
51 | 3 | StringBuffer buffer = new StringBuffer("/"); |
52 | 3 | buffer.append(pageName); |
53 | 3 | buffer.append('.'); |
54 | 3 | buffer.append(_extension); |
55 | ||
56 | 3 | encoding.setServletPath(buffer.toString()); |
57 | ||
58 | 3 | encoding.setParameterValue(ServiceConstants.SERVICE, null); |
59 | 3 | encoding.setParameterValue(ServiceConstants.PAGE, null); |
60 | } | |
61 | ||
62 | 4 | public void decode(ServiceEncoding encoding) |
63 | { | |
64 | 4 | String servletPath = encoding.getServletPath(); |
65 | ||
66 | 4 | int dotx = servletPath.lastIndexOf('.'); |
67 | 4 | if (dotx < 0) |
68 | 1 | return; |
69 | ||
70 | 3 | String extension = servletPath.substring(dotx + 1); |
71 | ||
72 | 3 | if (!extension.equals(_extension)) |
73 | 2 | return; |
74 | ||
75 | // Skip the slash and the dot. | |
76 | ||
77 | 1 | String page = servletPath.substring(1, dotx); |
78 | ||
79 | 1 | encoding.setParameterValue(ServiceConstants.SERVICE, _serviceName); |
80 | 1 | encoding.setParameterValue(ServiceConstants.PAGE, page); |
81 | } | |
82 | ||
83 | 4 | public void setExtension(String extension) |
84 | { | |
85 | 4 | _extension = extension; |
86 | } | |
87 | ||
88 | 6 | public void setServiceName(String serviceName) |
89 | { | |
90 | 6 | _serviceName = serviceName; |
91 | } | |
92 | } |
|