Coverage Report - net.sf.beanform.prop.BeanPropertySqueezer
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanPropertySqueezer
100% 
N/A 
1.75
 
 1  
 // Copyright 2006 Daniel Gredler
 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 net.sf.beanform.prop;
 16  
 
 17  
 import net.sf.beanform.util.StringCache;
 18  
 
 19  
 import org.apache.hivemind.ApplicationRuntimeException;
 20  
 import org.apache.tapestry.services.DataSqueezer;
 21  
 import org.apache.tapestry.util.io.SqueezeAdaptor;
 22  
 
 23  
 /**
 24  
  * Squeezes {@link BeanProperty} objects into and out of HTML and URLs.
 25  
  *
 26  
  * Since many class names, property names and validator strings will be serialized
 27  
  * to the client, but the number of distinct class names, property names and validator
 28  
  * strings using this squeezer will usually be small, this implementation keeps small
 29  
  * static caches of these strings and their corresponding "abbreviations", which are what
 30  
  * is actually sent to the clients. This is extremely important, as URLs are generally
 31  
  * <a href="http://support.microsoft.com/default.aspx?scid=KB;en-us;q208427">limited</a>
 32  
  * to about 2,000 characters.
 33  
  *
 34  
  * @author Daniel Gredler
 35  
  */
 36  4
 public class BeanPropertySqueezer implements SqueezeAdaptor {
 37  
 
 38  1
     private final static StringCache CLASSNAME_CACHE = new StringCache( 100 );
 39  1
     private final static StringCache PROPERTY_CACHE = new StringCache( 100 );
 40  1
     private final static StringCache VALIDATOR_CACHE = new StringCache( 100 );
 41  1
     private final static StringCache INPUT_CACHE = new StringCache( 10 );
 42  
 
 43  
     private final static String PREFIX = "BP";
 44  
     private final static String DIVIDER = "_";
 45  
 
 46  
     public String getPrefix() {
 47  1
         return PREFIX;
 48  
     }
 49  
 
 50  
     public Class getDataClass() {
 51  1
         return BeanProperty.class;
 52  
     }
 53  
 
 54  
     public String squeeze( DataSqueezer squeezer, Object data ) {
 55  4
         BeanProperty prop = (BeanProperty) data;
 56  4
         String c = CLASSNAME_CACHE.getShortVersion( prop.getOwnerClass().getName() );
 57  4
         String p = PROPERTY_CACHE.getShortVersion( prop.getName() );
 58  4
         String v = VALIDATOR_CACHE.getShortVersion( prop.getValidators() );
 59  4
         String i = INPUT_CACHE.getShortVersion( prop.getInput() );
 60  4
         return PREFIX + DIVIDER + c + DIVIDER + p + DIVIDER + v + DIVIDER + i;
 61  
     }
 62  
 
 63  
     public Object unsqueeze( DataSqueezer squeezer, String string ) {
 64  5
         String[] s = string.split( DIVIDER );
 65  5
         String c = CLASSNAME_CACHE.getRealVersion( s[ 1 ] );
 66  5
         String p = PROPERTY_CACHE.getRealVersion( s[ 2 ] );
 67  5
         String v = VALIDATOR_CACHE.getRealVersion( s[ 3 ] );
 68  5
         String i = INPUT_CACHE.getRealVersion( s[ 4 ] );
 69  
         try {
 70  5
             Class clazz = Class.forName( c );
 71  4
             BeanProperty prop = new BeanProperty( clazz, p, v, i );
 72  4
             return prop;
 73  
         }
 74  1
         catch( ClassNotFoundException e ) {
 75  1
             String msg = BeanPropertyMessages.unsqueezeClassNotFound( string, c );
 76  1
             throw new ApplicationRuntimeException( msg, e );
 77  
         }
 78  
     }
 79  
 
 80  
 }