KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > engine > encoders > PageServiceEncoder


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 JavaDoc _extension;
34
35     private String JavaDoc _serviceName;
36
37     public void encode(ServiceEncoding encoding)
38     {
39         String JavaDoc service = encoding.getParameterValue(ServiceConstants.SERVICE);
40
41         if (!service.equals(_serviceName))
42             return;
43
44         String JavaDoc pageName = encoding.getParameterValue(ServiceConstants.PAGE);
45
46         // Only handle pages in the application namespace (not from a library).
47

48         if (pageName.indexOf(INamespace.SEPARATOR) >= 0)
49             return;
50
51         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("/");
52         buffer.append(pageName);
53         buffer.append('.');
54         buffer.append(_extension);
55
56         encoding.setServletPath(buffer.toString());
57
58         encoding.setParameterValue(ServiceConstants.SERVICE, null);
59         encoding.setParameterValue(ServiceConstants.PAGE, null);
60     }
61
62     public void decode(ServiceEncoding encoding)
63     {
64         String JavaDoc servletPath = encoding.getServletPath();
65
66         int dotx = servletPath.lastIndexOf('.');
67         if (dotx < 0)
68             return;
69
70         String JavaDoc extension = servletPath.substring(dotx + 1);
71
72         if (!extension.equals(_extension))
73             return;
74
75         // Skip the slash and the dot.
76

77         String JavaDoc page = servletPath.substring(1, dotx);
78
79         encoding.setParameterValue(ServiceConstants.SERVICE, _serviceName);
80         encoding.setParameterValue(ServiceConstants.PAGE, page);
81     }
82
83     public void setExtension(String JavaDoc extension)
84     {
85         _extension = extension;
86     }
87
88     public void setServiceName(String JavaDoc serviceName)
89     {
90         _serviceName = serviceName;
91     }
92 }
Popular Tags