Coverage Report - org.apache.tapestry.util.io.DataSqueezerImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DataSqueezerImpl
0%
0/62
0%
0/38
5
 
 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.util.io;
 16  
 
 17  
 import org.apache.hivemind.lib.util.StrategyRegistry;
 18  
 import org.apache.hivemind.lib.util.StrategyRegistryImpl;
 19  
 import org.apache.tapestry.Tapestry;
 20  
 import org.apache.tapestry.services.DataSqueezer;
 21  
 
 22  
 import java.util.Iterator;
 23  
 import java.util.List;
 24  
 
 25  
 /**
 26  
  * A class used to convert arbitrary objects to Strings and back. This has particular uses involving
 27  
  * HTTP URLs and Cookies.
 28  
  * 
 29  
  * @author Howard Lewis Ship
 30  
  */
 31  
 
 32  0
 public class DataSqueezerImpl implements DataSqueezer
 33  
 {
 34  
     protected static final String NULL_PREFIX = "X";
 35  
 
 36  
     protected static final int ARRAY_SIZE = 90;
 37  
 
 38  
     protected static final int FIRST_ADAPTOR_OFFSET = 33;
 39  
 
 40  
     /**
 41  
      * An array of adaptors; this is used as a cheap lookup-table when unsqueezing. Each adaptor is
 42  
      * identified by a single ASCII character, in the range of 33 ('!') to 122 (the letter 'z'). The
 43  
      * offset into this table is the character minus 33.
 44  
      */
 45  
 
 46  0
     protected SqueezeAdaptor[] _adaptorByPrefix = new SqueezeAdaptor[ARRAY_SIZE];
 47  
 
 48  
     /**
 49  
      * AdaptorRegistry cache of adaptors.
 50  
      */
 51  
 
 52  0
     protected StrategyRegistry _adaptors = new StrategyRegistryImpl();
 53  
 
 54  
     public void setSqueezeAdaptors(List adaptors)
 55  
     {
 56  0
         Iterator i = adaptors.iterator();
 57  
 
 58  0
         while (i.hasNext())
 59  
         {
 60  0
             SqueezeAdaptor adaptor = (SqueezeAdaptor) i.next();
 61  0
             register(adaptor);
 62  0
         }
 63  0
     }
 64  
 
 65  
     /**
 66  
      * Registers the adaptor with one or more single-character prefixes.
 67  
      * <p>
 68  
      * <b>Note</b>: This method should be used for testing purposes only! Squeeze adaptors are
 69  
      * normally injected by HiveMind.
 70  
      * 
 71  
      * @param adaptor
 72  
      *            the adaptor which to be registered.
 73  
      */
 74  
 
 75  
     public synchronized void register(SqueezeAdaptor adaptor)
 76  
     {
 77  0
         if (adaptor == null)
 78  0
             throw new IllegalArgumentException(Tapestry.getMessage("DataSqueezer.null-adaptor"));
 79  
 
 80  0
         String prefix = adaptor.getPrefix();
 81  0
         int prefixLength = prefix.length();
 82  
         int offset;
 83  
 
 84  0
         if (prefixLength < 1)
 85  0
             throw new IllegalArgumentException(Tapestry.getMessage("DataSqueezer.short-prefix"));
 86  
 
 87  0
         Class dataClass = adaptor.getDataClass();
 88  0
         if (dataClass == null)
 89  0
             throw new IllegalArgumentException(Tapestry.getMessage("DataSqueezer.null-class"));
 90  
 
 91  0
         for (int i = 0; i < prefixLength; i++)
 92  
         {
 93  0
             char ch = prefix.charAt(i);
 94  
 
 95  0
             if (ch < '!' | ch > 'z')
 96  0
                 throw new IllegalArgumentException(Tapestry
 97  
                         .getMessage("DataSqueezer.prefix-out-of-range"));
 98  
 
 99  0
             offset = ch - FIRST_ADAPTOR_OFFSET;
 100  
 
 101  0
             if (_adaptorByPrefix[offset] != null)
 102  0
                 throw new IllegalArgumentException(Tapestry.format(
 103  
                         "DataSqueezer.adaptor-prefix-taken",
 104  
                         prefix.substring(i, i)));
 105  
 
 106  0
             _adaptorByPrefix[offset] = adaptor;
 107  
 
 108  
         }
 109  
 
 110  0
         _adaptors.register(dataClass, adaptor);
 111  0
     }
 112  
 
 113  
     /**
 114  
      * Squeezes the data object into a String by locating an appropriate adaptor that can perform
 115  
      * the conversion. data may be null.
 116  
      */
 117  
 
 118  
     public String squeeze(Object data)
 119  
     {
 120  
         SqueezeAdaptor adaptor;
 121  
 
 122  0
         if (data == null)
 123  0
             return NULL_PREFIX;
 124  
 
 125  0
         adaptor = (SqueezeAdaptor) _adaptors.getStrategy(data.getClass());
 126  
 
 127  0
         return adaptor.squeeze(this, data);
 128  
     }
 129  
 
 130  
     /**
 131  
      * A convience; invokes {@link #squeeze(Object)}for each element in the data array. If data is
 132  
      * null, returns null.
 133  
      */
 134  
 
 135  
     public String[] squeeze(Object[] data)
 136  
     {
 137  0
         if (data == null)
 138  0
             return null;
 139  
 
 140  0
         int length = data.length;
 141  
         String[] result;
 142  
 
 143  0
         result = new String[length];
 144  
 
 145  0
         for (int i = 0; i < length; i++)
 146  0
             result[i] = squeeze(data[i]);
 147  
 
 148  0
         return result;
 149  
     }
 150  
 
 151  
     /**
 152  
      * Unsqueezes the string. Note that in a special case, where the first character of the string
 153  
      * is not a recognized prefix, it is assumed that the string is simply a string, and return with
 154  
      * no change.
 155  
      */
 156  
 
 157  
     public Object unsqueeze(String string)
 158  
     {
 159  0
         SqueezeAdaptor adaptor = null;
 160  
 
 161  0
         if (string.equals(NULL_PREFIX))
 162  0
             return null;
 163  0
         else if (string.length() <= 0)
 164  0
             return null;
 165  
         
 166  0
         int offset = string.charAt(0) - FIRST_ADAPTOR_OFFSET;
 167  
 
 168  0
         if (offset >= 0 && offset < _adaptorByPrefix.length)
 169  0
             adaptor = _adaptorByPrefix[offset];
 170  
 
 171  
         // If the adaptor is not otherwise recognized, the it is simply
 172  
         // an encoded String (the StringAdaptor may not have added
 173  
         // a prefix).
 174  
 
 175  0
         if (adaptor == null)
 176  0
             return string;
 177  
 
 178  
         // Adaptor should never be null, because we always supply
 179  
         // an adaptor for String
 180  
 
 181  0
         return adaptor.unsqueeze(this, string);
 182  
     }
 183  
 
 184  
     /**
 185  
      * Convienience method for unsqueezing many strings (back into objects).
 186  
      * <p>
 187  
      * If strings is null, returns null.
 188  
      */
 189  
 
 190  
     public Object[] unsqueeze(String[] strings)
 191  
     {
 192  0
         if (strings == null)
 193  0
             return null;
 194  
 
 195  0
         int length = strings.length;
 196  
         Object[] result;
 197  
 
 198  0
         result = new Object[length];
 199  
 
 200  0
         for (int i = 0; i < length; i++)
 201  0
             result[i] = unsqueeze(strings[i]);
 202  
 
 203  0
         return result;
 204  
     }
 205  
 
 206  
     public String toString()
 207  
     {
 208  
         StringBuffer buffer;
 209  
 
 210  0
         buffer = new StringBuffer();
 211  0
         buffer.append("DataSqueezer[adaptors=<");
 212  0
         buffer.append(_adaptors.toString());
 213  0
         buffer.append(">]");
 214  
 
 215  0
         return buffer.toString();
 216  
     }
 217  
 }