Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2004, 2005 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package org.apache.tapestry.util.io;
 
 import  org.apache.hivemind.lib.util.StrategyRegistry;
 import  org.apache.hivemind.lib.util.StrategyRegistryImpl;
 
 import java.util.List;

A class used to convert arbitrary objects to Strings and back. This has particular uses involving HTTP URLs and Cookies.

Author(s):
Howard Lewis Ship
 
 
 public class DataSqueezerImpl implements DataSqueezer
 {
     protected static final String NULL_PREFIX = "X";
 
     protected static final int ARRAY_SIZE = 90;
 
     protected static final int FIRST_ADAPTOR_OFFSET = 33;

    
An array of adaptors; this is used as a cheap lookup-table when unsqueezing. Each adaptor is identified by a single ASCII character, in the range of 33 ('!') to 122 (the letter 'z'). The offset into this table is the character minus 33.
 
 
     protected SqueezeAdaptor[] _adaptorByPrefix = new SqueezeAdaptor[];

    
AdaptorRegistry cache of adaptors.
 
 
     protected StrategyRegistry _adaptors = new StrategyRegistryImpl();
 
     public void setSqueezeAdaptors(List adaptors)
     {
         Iterator i = adaptors.iterator();
 
         while (i.hasNext())
         {
             SqueezeAdaptor adaptor = (SqueezeAdaptori.next();
             register(adaptor);
         }
     }

    
Registers the adaptor with one or more single-character prefixes.

Note: This method should be used for testing purposes only! Squeeze adaptors are normally injected by HiveMind.

Parameters:
adaptor the adaptor which to be registered.
 
 
     public synchronized void register(SqueezeAdaptor adaptor)
     {
         if (adaptor == null)
             throw new IllegalArgumentException(Tapestry.getMessage("DataSqueezer.null-adaptor"));
 
         String prefix = adaptor.getPrefix();
         int prefixLength = prefix.length();
         int offset;
 
         if (prefixLength < 1)
             throw new IllegalArgumentException(Tapestry.getMessage("DataSqueezer.short-prefix"));
 
         Class dataClass = adaptor.getDataClass();
         if (dataClass == null)
             throw new IllegalArgumentException(Tapestry.getMessage("DataSqueezer.null-class"));
 
         for (int i = 0; i < prefixLengthi++)
         {
             char ch = prefix.charAt(i);
 
             if (ch < '!' | ch > 'z')
                 throw new IllegalArgumentException(Tapestry
                         .getMessage("DataSqueezer.prefix-out-of-range"));
 
             offset = ch - ;
            if ([offset] != null)
                throw new IllegalArgumentException(Tapestry.format(
                        "DataSqueezer.adaptor-prefix-taken",
                        prefix.substring(ii)));
            [offset] = adaptor;
        }
        .register(dataClassadaptor);
    }

    
Squeezes the data object into a String by locating an appropriate adaptor that can perform the conversion. data may be null.
    public String squeeze(Object data)
    {
        SqueezeAdaptor adaptor;
        if (data == null)
            return ;
        adaptor = (SqueezeAdaptor.getStrategy(data.getClass());
        return adaptor.squeeze(thisdata);
    }

    
A convience; invokes squeeze(Object)for each element in the data array. If data is null, returns null.
    public String[] squeeze(Object[] data)
    {
        if (data == null)
            return null;
        int length = data.length;
        String[] result;
        result = new String[length];
        for (int i = 0; i < lengthi++)
            result[i] = squeeze(data[i]);
        return result;
    }

    
Unsqueezes the string. Note that in a special case, where the first character of the string is not a recognized prefix, it is assumed that the string is simply a string, and return with no change.
    public Object unsqueeze(String string)
    {
        SqueezeAdaptor adaptor = null;
        if (string.equals())
            return null;
        else if (string.length() <= 0)
            return null;
        
        int offset = string.charAt(0) - ;
        if (offset >= 0 && offset < .)
            adaptor = [offset];
        // If the adaptor is not otherwise recognized, the it is simply
        // an encoded String (the StringAdaptor may not have added
        // a prefix).
        if (adaptor == null)
            return string;
        // Adaptor should never be null, because we always supply
        // an adaptor for String
        return adaptor.unsqueeze(thisstring);
    }

    
Convienience method for unsqueezing many strings (back into objects).

If strings is null, returns null.

    public Object[] unsqueeze(String[] strings)
    {
        if (strings == null)
            return null;
        int length = strings.length;
        Object[] result;
        result = new Object[length];
        for (int i = 0; i < lengthi++)
            result[i] = unsqueeze(strings[i]);
        return result;
    }
    public String toString()
    {
        StringBuffer buffer;
        buffer = new StringBuffer();
        buffer.append("DataSqueezer[adaptors=<");
        buffer.append(.toString());
        buffer.append(">]");
        return buffer.toString();
    }
New to GrepCode? Check out our FAQ X