FileDocCategorySizeDatePackage
BasicHeaderValueFormatter.javaAPI DocAndroid 1.5 API14428Wed May 06 22:41:10 BST 2009org.apache.http.message

BasicHeaderValueFormatter

public class BasicHeaderValueFormatter extends Object implements HeaderValueFormatter
Basic implementation for formatting header value elements. Instances of this class are stateless and thread-safe. Derived classes are expected to maintain these properties.
author
Oleg Kalnichevski
author
and others
version
$Revision: 574185 $
since
4.0

Fields Summary
public static final BasicHeaderValueFormatter
DEFAULT
A default instance of this class, for use as default or fallback. Note that {@link BasicHeaderValueFormatter} is not a singleton, there can be many instances of the class itself and of derived classes. The instance here provides non-customized, default behavior.
public static final String
SEPARATORS
Special characters that can be used as separators in HTTP parameters. These special characters MUST be in a quoted string to be used within a parameter value .
public static final String
UNSAFE_CHARS
Unsafe special characters that must be escaped using the backslash character
Constructors Summary
Methods Summary
protected voiddoFormatValue(org.apache.http.util.CharArrayBuffer buffer, java.lang.String value, boolean quote)
Actually formats the value of a name-value pair. This does not include a leading = character. Called from {@link #formatNameValuePair formatNameValuePair}.

param
buffer the buffer to append to, never null
param
value the value to append, never null
param
quote true to always format with quotes, false to use quotes only when necessary


        if (!quote) {
            for (int i = 0; (i < value.length()) && !quote; i++) {
                quote = isSeparator(value.charAt(i));
            }
        }

        if (quote) {
            buffer.append('"");
        }
        for (int i = 0; i < value.length(); i++) {
            char ch = value.charAt(i);
            if (isUnsafe(ch)) {
                buffer.append('\\");
            }
            buffer.append(ch);
        }
        if (quote) {
            buffer.append('"");
        }
    
protected intestimateElementsLen(org.apache.http.HeaderElement[] elems)
Estimates the length of formatted header elements.

param
elems the header elements to format, or null
return
a length estimate, in number of characters

        if ((elems == null) || (elems.length < 1))
            return 0;

        int result = (elems.length-1) * 2; // elements separated by ", "
        for (int i=0; i<elems.length; i++) {
            result += estimateHeaderElementLen(elems[i]);
        }

        return result;
    
protected intestimateHeaderElementLen(org.apache.http.HeaderElement elem)
Estimates the length of a formatted header element.

param
elem the header element to format, or null
return
a length estimate, in number of characters

        if (elem == null)
            return 0;

        int result = elem.getName().length(); // name
        final String value = elem.getValue();
        if (value != null) {
            // assume quotes, but no escaped characters
            result += 3 + value.length(); // ="value"
        }

        final int parcnt = elem.getParameterCount();
        if (parcnt > 0) {
            for (int i=0; i<parcnt; i++) {
                result += 2 +                   // ; <param>
                    estimateNameValuePairLen(elem.getParameter(i));
            }
        }

        return result;
    
protected intestimateNameValuePairLen(org.apache.http.NameValuePair nvp)
Estimates the length of a formatted name-value pair.

param
nvp the name-value pair to format, or null
return
a length estimate, in number of characters

        if (nvp == null)
            return 0;

        int result = nvp.getName().length(); // name
        final String value = nvp.getValue();
        if (value != null) {
            // assume quotes, but no escaped characters
            result += 3 + value.length(); // ="value"
        }
        return result;
    
protected intestimateParametersLen(org.apache.http.NameValuePair[] nvps)
Estimates the length of formatted parameters.

param
nvps the parameters to format, or null
return
a length estimate, in number of characters

        if ((nvps == null) || (nvps.length < 1))
            return 0;

        int result = (nvps.length-1) * 2; // "; " between the parameters
        for (int i=0; i<nvps.length; i++) {
            result += estimateNameValuePairLen(nvps[i]);
        }

        return result;
    
public static final java.lang.StringformatElements(org.apache.http.HeaderElement[] elems, boolean quote, org.apache.http.message.HeaderValueFormatter formatter)
Formats an array of header elements.

param
elems the header elements to format
param
quote true to always format with quoted values, false to use quotes only when necessary
param
formatter the formatter to use, or null for the {@link #DEFAULT default}
return
the formatted header elements




    // public default constructor



                                                                                                                   
      
           
                                
                                
        if (formatter == null)
            formatter = BasicHeaderValueFormatter.DEFAULT;
        return formatter.formatElements(null, elems, quote).toString();
    
public org.apache.http.util.CharArrayBufferformatElements(org.apache.http.util.CharArrayBuffer buffer, org.apache.http.HeaderElement[] elems, boolean quote)

        if (elems == null) {
            throw new IllegalArgumentException
                ("Header element array must not be null.");
        }

        int len = estimateElementsLen(elems);
        if (buffer == null) {
            buffer = new CharArrayBuffer(len);
        } else {
            buffer.ensureCapacity(len);
        }

        for (int i=0; i<elems.length; i++) {
            if (i > 0) {
                buffer.append(", ");
            }
            formatHeaderElement(buffer, elems[i], quote);
        }

        return buffer;
    
public static final java.lang.StringformatHeaderElement(org.apache.http.HeaderElement elem, boolean quote, org.apache.http.message.HeaderValueFormatter formatter)
Formats a header element.

param
elem the header element to format
param
quote true to always format with quoted values, false to use quotes only when necessary
param
formatter the formatter to use, or null for the {@link #DEFAULT default}
return
the formatted header element

        if (formatter == null)
            formatter = BasicHeaderValueFormatter.DEFAULT;
        return formatter.formatHeaderElement(null, elem, quote).toString();
    
public org.apache.http.util.CharArrayBufferformatHeaderElement(org.apache.http.util.CharArrayBuffer buffer, org.apache.http.HeaderElement elem, boolean quote)

        if (elem == null) {
            throw new IllegalArgumentException
                ("Header element must not be null.");
        }

        int len = estimateHeaderElementLen(elem);
        if (buffer == null) {
            buffer = new CharArrayBuffer(len);
        } else {
            buffer.ensureCapacity(len);
        }

        buffer.append(elem.getName());
        final String value = elem.getValue();
        if (value != null) {
            buffer.append('=");
            doFormatValue(buffer, value, quote);
        }

        final int parcnt = elem.getParameterCount();
        if (parcnt > 0) {
            for (int i=0; i<parcnt; i++) {
                buffer.append("; ");
                formatNameValuePair(buffer, elem.getParameter(i), quote);
            }
        }

        return buffer;
    
public static final java.lang.StringformatNameValuePair(org.apache.http.NameValuePair nvp, boolean quote, org.apache.http.message.HeaderValueFormatter formatter)
Formats a name-value pair.

param
nvp the name-value pair to format
param
quote true to always format with a quoted value, false to use quotes only when necessary
param
formatter the formatter to use, or null for the {@link #DEFAULT default}
return
the formatted name-value pair

        if (formatter == null)
            formatter = BasicHeaderValueFormatter.DEFAULT;
        return formatter.formatNameValuePair(null, nvp, quote).toString();
    
public org.apache.http.util.CharArrayBufferformatNameValuePair(org.apache.http.util.CharArrayBuffer buffer, org.apache.http.NameValuePair nvp, boolean quote)

        if (nvp == null) {
            throw new IllegalArgumentException
                ("NameValuePair must not be null.");
        }

        int len = estimateNameValuePairLen(nvp);
        if (buffer == null) {
            buffer = new CharArrayBuffer(len);
        } else {
            buffer.ensureCapacity(len);
        }

        buffer.append(nvp.getName());
        final String value = nvp.getValue();
        if (value != null) {
            buffer.append('=");
            doFormatValue(buffer, value, quote);
        }

        return buffer;
    
public static final java.lang.StringformatParameters(org.apache.http.NameValuePair[] nvps, boolean quote, org.apache.http.message.HeaderValueFormatter formatter)
Formats a set of parameters.

param
nvps the parameters to format
param
quote true to always format with quoted values, false to use quotes only when necessary
param
formatter the formatter to use, or null for the {@link #DEFAULT default}
return
the formatted parameters

        if (formatter == null)
            formatter = BasicHeaderValueFormatter.DEFAULT;
        return formatter.formatParameters(null, nvps, quote).toString();
    
public org.apache.http.util.CharArrayBufferformatParameters(org.apache.http.util.CharArrayBuffer buffer, org.apache.http.NameValuePair[] nvps, boolean quote)

        if (nvps == null) {
            throw new IllegalArgumentException
                ("Parameters must not be null.");
        }

        int len = estimateParametersLen(nvps);
        if (buffer == null) {
            buffer = new CharArrayBuffer(len);
        } else {
            buffer.ensureCapacity(len);
        }

        for (int i = 0; i < nvps.length; i++) {
            if (i > 0) {
                buffer.append("; ");
            }
            formatNameValuePair(buffer, nvps[i], quote);
        }

        return buffer;
    
protected booleanisSeparator(char ch)
Checks whether a character is a {@link #SEPARATORS separator}.

param
ch the character to check
return
true if the character is a separator, false otherwise

        return SEPARATORS.indexOf(ch) >= 0;
    
protected booleanisUnsafe(char ch)
Checks whether a character is {@link #UNSAFE_CHARS unsafe}.

param
ch the character to check
return
true if the character is unsafe, false otherwise

        return UNSAFE_CHARS.indexOf(ch) >= 0;