Coverage Report - org.apache.tapestry.engine.encoders.PageServiceEncoder
 
Classes in this File Line Coverage Branch Coverage Complexity
PageServiceEncoder
0%
0/26
0%
0/8
3
 
 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
 24  
  * {@link org.apache.tapestry.engine.ServiceEncoder}, it encodes page name and
 25  
  * a service name. The page name becomes the servlet path, prefixed with "/" and
 26  
  * suffixed with a dot and a particular extension. In this way,
 27  
  * "/app?service=page&page=Home" becomes simply "Home.html". This is most
 28  
  * suitable for the "page" and "external" services.
 29  
  * 
 30  
  * @author Howard M. Lewis Ship
 31  
  * @since 4.0
 32  
  */
 33  0
 public class PageServiceEncoder implements ServiceEncoder
 34  
 {
 35  
 
 36  
     private String _extension;
 37  
 
 38  
     private String _serviceName;
 39  
 
 40  
     public void encode(ServiceEncoding encoding)
 41  
     {
 42  0
         String service = encoding.getParameterValue(ServiceConstants.SERVICE);
 43  
 
 44  0
         if (!service.equals(_serviceName)) return;
 45  
 
 46  0
         String pageName = encoding.getParameterValue(ServiceConstants.PAGE);
 47  
 
 48  
         // Only handle pages in the application namespace (not from a library).
 49  
 
 50  0
         if (pageName.indexOf(INamespace.SEPARATOR) >= 0) return;
 51  
 
 52  0
         StringBuffer buffer = new StringBuffer("/");
 53  0
         buffer.append(pageName);
 54  0
         buffer.append('.');
 55  0
         buffer.append(_extension);
 56  
 
 57  0
         encoding.setServletPath(buffer.toString());
 58  
 
 59  0
         encoding.setParameterValue(ServiceConstants.SERVICE, null);
 60  0
         encoding.setParameterValue(ServiceConstants.PAGE, null);
 61  0
     }
 62  
 
 63  
     public void decode(ServiceEncoding encoding)
 64  
     {
 65  0
         String servletPath = encoding.getServletPath();
 66  
 
 67  0
         int dotx = servletPath.lastIndexOf('.');
 68  0
         if (dotx < 0) return;
 69  
 
 70  0
         String extension = servletPath.substring(dotx + 1);
 71  
 
 72  0
         if (!extension.equals(_extension)) return;
 73  
 
 74  
         // Skip the slash and the dot.
 75  
 
 76  0
         String page = servletPath.substring(1, dotx);
 77  
 
 78  0
         encoding.setParameterValue(ServiceConstants.SERVICE, _serviceName);
 79  0
         encoding.setParameterValue(ServiceConstants.PAGE, page);
 80  0
     }
 81  
 
 82  
     public void setExtension(String extension)
 83  
     {
 84  0
         _extension = extension;
 85  0
     }
 86  
 
 87  
     public void setServiceName(String serviceName)
 88  
     {
 89  0
         _serviceName = serviceName;
 90  0
     }
 91  
 }