View Javadoc

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  public class BeanPropertySqueezer implements SqueezeAdaptor {
37  
38      private final static StringCache CLASSNAME_CACHE = new StringCache( 100 );
39      private final static StringCache PROPERTY_CACHE = new StringCache( 100 );
40      private final static StringCache VALIDATOR_CACHE = new StringCache( 100 );
41      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          return PREFIX;
48      }
49  
50      public Class getDataClass() {
51          return BeanProperty.class;
52      }
53  
54      public String squeeze( DataSqueezer squeezer, Object data ) {
55          BeanProperty prop = (BeanProperty) data;
56          String c = CLASSNAME_CACHE.getShortVersion( prop.getOwnerClass().getName() );
57          String p = PROPERTY_CACHE.getShortVersion( prop.getName() );
58          String v = VALIDATOR_CACHE.getShortVersion( prop.getValidators() );
59          String i = INPUT_CACHE.getShortVersion( prop.getInput() );
60          return PREFIX + DIVIDER + c + DIVIDER + p + DIVIDER + v + DIVIDER + i;
61      }
62  
63      public Object unsqueeze( DataSqueezer squeezer, String string ) {
64          String[] s = string.split( DIVIDER );
65          String c = CLASSNAME_CACHE.getRealVersion( s[ 1 ] );
66          String p = PROPERTY_CACHE.getRealVersion( s[ 2 ] );
67          String v = VALIDATOR_CACHE.getRealVersion( s[ 3 ] );
68          String i = INPUT_CACHE.getRealVersion( s[ 4 ] );
69          try {
70              Class clazz = Class.forName( c );
71              BeanProperty prop = new BeanProperty( clazz, p, v, i );
72              return prop;
73          }
74          catch( ClassNotFoundException e ) {
75              String msg = BeanPropertyMessages.unsqueezeClassNotFound( string, c );
76              throw new ApplicationRuntimeException( msg, e );
77          }
78      }
79  
80  }