001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.message;
029
030import org.apache.http.HeaderElement;
031import org.apache.http.NameValuePair;
032import org.apache.http.util.CharArrayBuffer;
033
034/**
035 * Interface for formatting elements of a header value.
036 * This is the complement to {@link HeaderValueParser}.
037 * Instances of this interface are expected to be stateless and thread-safe.
038 *
039 * <p>
040 * All formatting methods accept an optional buffer argument.
041 * If a buffer is passed in, the formatted element will be appended
042 * and the modified buffer is returned. If no buffer is passed in,
043 * a new buffer will be created and filled with the formatted element.
044 * In both cases, the caller is allowed to modify the returned buffer.
045 * </p>
046 *
047 * @since 4.0
048 */
049public interface HeaderValueFormatter {
050
051    /**
052     * Formats an array of header elements.
053     *
054     * @param buffer    the buffer to append to, or
055     *                  {@code null} to create a new buffer
056     * @param elems     the header elements to format
057     * @param quote     {@code true} to always format with quoted values,
058     *                  {@code false} to use quotes only when necessary
059     *
060     * @return  a buffer with the formatted header elements.
061     *          If the {@code buffer} argument was not {@code null},
062     *          that buffer will be used and returned.
063     */
064    CharArrayBuffer formatElements(CharArrayBuffer buffer,
065                                   HeaderElement[] elems,
066                                   boolean quote);
067
068    /**
069     * Formats one header element.
070     *
071     * @param buffer    the buffer to append to, or
072     *                  {@code null} to create a new buffer
073     * @param elem      the header element to format
074     * @param quote     {@code true} to always format with quoted values,
075     *                  {@code false} to use quotes only when necessary
076     *
077     * @return  a buffer with the formatted header element.
078     *          If the {@code buffer} argument was not {@code null},
079     *          that buffer will be used and returned.
080     */
081    CharArrayBuffer formatHeaderElement(CharArrayBuffer buffer,
082                                        HeaderElement elem,
083                                        boolean quote);
084
085    /**
086     * Formats the parameters of a header element.
087     * That's a list of name-value pairs, to be separated by semicolons.
088     * This method will <i>not</i> generate a leading semicolon.
089     *
090     * @param buffer    the buffer to append to, or
091     *                  {@code null} to create a new buffer
092     * @param nvps      the parameters (name-value pairs) to format
093     * @param quote     {@code true} to always format with quoted values,
094     *                  {@code false} to use quotes only when necessary
095     *
096     * @return  a buffer with the formatted parameters.
097     *          If the {@code buffer} argument was not {@code null},
098     *          that buffer will be used and returned.
099     */
100    CharArrayBuffer formatParameters(CharArrayBuffer buffer,
101                                     NameValuePair[] nvps,
102                                     boolean quote);
103
104    /**
105     * Formats one name-value pair, where the value is optional.
106     *
107     * @param buffer    the buffer to append to, or
108     *                  {@code null} to create a new buffer
109     * @param nvp       the name-value pair to format
110     * @param quote     {@code true} to always format with a quoted value,
111     *                  {@code false} to use quotes only when necessary
112     *
113     * @return  a buffer with the formatted name-value pair.
114     *          If the {@code buffer} argument was not {@code null},
115     *          that buffer will be used and returned.
116     */
117    CharArrayBuffer formatNameValuePair(CharArrayBuffer buffer,
118                                        NameValuePair nvp,
119                                        boolean quote);
120
121}
122