hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rola...@apache.org
Subject svn commit: r569831 - in /jakarta/httpcomponents/httpcore/trunk/module-main/src: main/java/org/apache/http/message/ test/java/org/apache/http/message/
Date Sun, 26 Aug 2007 16:20:49 GMT
Author: rolandw
Date: Sun Aug 26 09:20:45 2007
New Revision: 569831

URL: http://svn.apache.org/viewvc?rev=569831&view=rev
Log:
moved formatting from BasicHeaderElement to BasicHeaderValueFormatter

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueFormatter.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java?rev=569831&r1=569830&r2=569831&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java
Sun Aug 26 09:20:45 2007
@@ -196,60 +196,6 @@
     }
 
 
-    // --------------------------------------------------------- Public Methods
-
-
-    public static void format(
-            final CharArrayBuffer buffer, 
-            final HeaderElement element) {
-        if (buffer == null) {
-            throw new IllegalArgumentException("String buffer may not be null");
-        }
-        if (element == null) {
-            throw new IllegalArgumentException("Header element may not be null");
-        }
-        buffer.append(element.getName());
-        if (element.getValue() != null) {
-            buffer.append("=");
-            buffer.append(element.getValue()); //@@@ quoting?
-        }
-        NameValuePair[] params = element.getParameters();
-        for (int i = 0; i < params.length; i++) {
-            buffer.append("; ");
-            BasicHeaderValueFormatter.DEFAULT.formatNameValuePair
-                (buffer, params[i], false);
-        }
-    }
-    
-    public static String format(final HeaderElement element) {
-        CharArrayBuffer buffer = new CharArrayBuffer(32);
-        format(buffer, element);
-        return buffer.toString();
-    }
-    
-    public static void formatAll(
-            final CharArrayBuffer buffer, 
-            final HeaderElement[] elements) {
-        if (buffer == null) {
-            throw new IllegalArgumentException("String buffer may not be null");
-        }
-        if (elements == null) {
-            throw new IllegalArgumentException("Array of header element may not be null");
-        }
-        for (int i = 0; i < elements.length; i++) {
-            if (i > 0) {
-                buffer.append(", ");
-            }
-            format(buffer, elements[i]);
-        }
-    }
-    
-    public static String formatAll(final HeaderElement[] elements) {
-        CharArrayBuffer buffer = new CharArrayBuffer(64);
-        formatAll(buffer, elements);
-        return buffer.toString();
-    }
-    
     /**
      * Returns parameter with the given name, if found. Otherwise null 
      * is returned

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java?rev=569831&r1=569830&r2=569831&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueFormatter.java
Sun Aug 26 09:20:45 2007
@@ -31,6 +31,7 @@
 
 package org.apache.http.message;
 
+import org.apache.http.HeaderElement;
 import org.apache.http.NameValuePair;
 import org.apache.http.util.CharArrayBuffer;
 
@@ -80,7 +81,161 @@
     // public default constructor
 
 
-    //         throw new UnsupportedOperationException("@@@");
+
+    /**
+     * Formats an array of header elements.
+     *
+     * @param elems     the header elements to format
+     * @param quote     <code>true</code> to always format with quoted values,
+     *                  <code>false</code> to use quotes only when necessary
+     * @param formatter         the formatter to use, or <code>null</code>
+     *                          for the {@link #DEFAULT default}
+     *
+     * @return  the formatted header elements
+     */
+    public final static
+        String formatElements(final HeaderElement[] elems,
+                              final boolean quote,
+                              HeaderValueFormatter formatter) {
+        if (formatter == null)
+            formatter = BasicHeaderValueFormatter.DEFAULT;
+        return formatter.formatElements(null, elems, quote).toString();
+    }
+
+
+    // non-javadoc, see interface HeaderValueFormatter
+    public CharArrayBuffer formatElements(CharArrayBuffer buffer,
+                                          final HeaderElement[] elems,
+                                          final 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;
+    }
+
+
+    /**
+     * Estimates the length of formatted header elements.
+     *
+     * @param elems     the header elements to format, or <code>null</code>
+     *
+     * @return  a length estimate, in number of characters
+     */
+    protected int estimateElementsLen(HeaderElement[] elems) {
+        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;
+    }
+
+
+
+    /**
+     * Formats a header element.
+     *
+     * @param elem      the header element to format
+     * @param quote     <code>true</code> to always format with quoted values,
+     *                  <code>false</code> to use quotes only when necessary
+     * @param formatter         the formatter to use, or <code>null</code>
+     *                          for the {@link #DEFAULT default}
+     *
+     * @return  the formatted header element
+     */
+    public final static
+        String formatHeaderElement(final HeaderElement elem,
+                                   final boolean quote,
+                                   HeaderValueFormatter formatter) {
+        if (formatter == null)
+            formatter = BasicHeaderValueFormatter.DEFAULT;
+        return formatter.formatHeaderElement(null, elem, quote).toString();
+    }
+
+
+    // non-javadoc, see interface HeaderValueFormatter
+    public CharArrayBuffer formatHeaderElement(CharArrayBuffer buffer,
+                                               final HeaderElement elem,
+                                               final 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;
+    }
+
+
+    /**
+     * Estimates the length of a formatted header element.
+     *
+     * @param elem      the header element to format, or <code>null</code>
+     *
+     * @return  a length estimate, in number of characters
+     */
+    protected int estimateHeaderElementLen(HeaderElement elem) {
+        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;
+    }
+
 
 
 
@@ -223,6 +378,7 @@
 
     /**
      * 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 <code>null</code>

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueFormatter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueFormatter.java?rev=569831&r1=569830&r2=569831&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueFormatter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueFormatter.java
Sun Aug 26 09:20:45 2007
@@ -34,7 +34,6 @@
 
 import org.apache.http.HeaderElement;
 import org.apache.http.NameValuePair;
-import org.apache.http.ParseException;
 import org.apache.http.util.CharArrayBuffer;
 
 
@@ -103,110 +102,43 @@
     */
 
 
-    /* *
-     * Parses a single header element.
-     * A header element consist of a semicolon-separate list
-     * of name=value definitions.
-     *
-     * @param buffer    buffer holding the element to parse
+    /**
+     * Formats an array of header elements.
      *
-     * @return  the parsed element
+     * @param buffer    the buffer to append to, or
+     *                  <code>null</code> to create a new buffer
+     * @param elems     the header elements to format
+     * @param quote     <code>true</code> to always format with quoted values,
+     *                  <code>false</code> to use quotes only when necessary
      *
-     * @throws ParseException        in case of a parse error
-     * /
-    HeaderElement parseHeaderElement(CharArrayBuffer buffer,
-                                     int indexFrom,
-                                     int indexTo)
-        throws ParseException
+     * @return  a buffer with the formatted header elements.
+     *          If the <code>buffer</code> argument was not <code>null</code>,
+     *          that buffer will be used and returned.
+     */
+    CharArrayBuffer formatElements(CharArrayBuffer buffer,
+                                   HeaderElement[] elems,
+                                   boolean quote)
         ;
-    */
 
-    /* *
-     * Parses a list of name-value pairs.
-     * These lists are used to specify parameters to a header element.
-     * Parse errors are indicated as <code>RuntimeException</code>.
-     * <p>
-     * This method comforms to the generic grammar and formatting rules
-     * outlined in the 
-     * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2"
-     *   >Section 2.2</a>
-     * and
-     * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6"
-     *   >Section 3.6</a>
-     * of
-     * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.txt">RFC 2616</a>.
-     * </p>
-     * <h>2.2 Basic Rules</h>
-     * <p>
-     * The following rules are used throughout this specification to
-     * describe basic parsing constructs. 
-     * The US-ASCII coded character set is defined by ANSI X3.4-1986.
-     * </p>
-     * <pre>
-     *     OCTET          = <any 8-bit sequence of data>
-     *     CHAR           = <any US-ASCII character (octets 0 - 127)>
-     *     UPALPHA        = <any US-ASCII uppercase letter "A".."Z">
-     *     LOALPHA        = <any US-ASCII lowercase letter "a".."z">
-     *     ALPHA          = UPALPHA | LOALPHA
-     *     DIGIT          = <any US-ASCII digit "0".."9">
-     *     CTL            = <any US-ASCII control character
-     *                      (octets 0 - 31) and DEL (127)>
-     *     CR             = <US-ASCII CR, carriage return (13)>
-     *     LF             = <US-ASCII LF, linefeed (10)>
-     *     SP             = <US-ASCII SP, space (32)>
-     *     HT             = <US-ASCII HT, horizontal-tab (9)>
-     *     <">            = <US-ASCII double-quote mark (34)>
-     * </pre>
-     * <p>
-     * Many HTTP/1.1 header field values consist of words separated
-     * by LWS or special characters. These special characters MUST be
-     * in a quoted string to be used within 
-     * a parameter value (as defined in section 3.6).
-     * <p>
-     * <pre>
-     * token          = 1*<any CHAR except CTLs or separators>
-     * separators     = "(" | ")" | "<" | ">" | "@"
-     *                | "," | ";" | ":" | "\" | <">
-     *                | "/" | "[" | "]" | "?" | "="
-     *                | "{" | "}" | SP | HT
-     * </pre>
-     * <p>
-     * A string of text is parsed as a single word if it is quoted using
-     * double-quote marks.
-     * </p>
-     * <pre>
-     * quoted-string  = ( <"> *(qdtext | quoted-pair ) <"> )
-     * qdtext         = <any TEXT except <">>
-     * </pre>
-     * <p>
-     * The backslash character ("\") MAY be used as a single-character
-     * quoting mechanism only within quoted-string and comment constructs.
-     * </p>
-     * <pre>
-     * quoted-pair    = "\" CHAR
-     * </pre>
-     * <h>3.6 Transfer Codings</h>
-     * <p>
-     * Parameters are in the form of attribute/value pairs.
-     * </p>
-     * <pre>
-     * parameter               = attribute "=" value
-     * attribute               = token
-     * value                   = token | quoted-string
-     * </pre> 
-     *
-     * @param buffer    buffer holding the name-value list to parse
+
+    /**
+     * Formats one header element.
      *
-     * @return  an array holding all items of the name-value list
+     * @param buffer    the buffer to append to, or
+     *                  <code>null</code> to create a new buffer
+     * @param elem      the header element to format
+     * @param quote     <code>true</code> to always format with quoted values,
+     *                  <code>false</code> to use quotes only when necessary
      *
-     * @throws ParseException        in case of a parse error
-     * /
-    NameValuePair[] parseParameters(CharArrayBuffer buffer,
-                                    int indexFrom,
-                                    int indexTo)
-        throws ParseException
+     * @return  a buffer with the formatted header element.
+     *          If the <code>buffer</code> argument was not <code>null</code>,
+     *          that buffer will be used and returned.
+     */
+    CharArrayBuffer formatHeaderElement(CharArrayBuffer buffer,
+                                        HeaderElement elem,
+                                        boolean quote)
         ;
-    */
+
 
 
     /**
@@ -222,7 +154,7 @@
      *
      * @return  a buffer with the formatted parameters.
      *          If the <code>buffer</code> argument was not <code>null</code>,
-     *          that buffer will be returned.
+     *          that buffer will be used and returned.
      */
     CharArrayBuffer formatParameters(CharArrayBuffer buffer,
                                      NameValuePair[] nvps,
@@ -241,7 +173,7 @@
      *
      * @return  a buffer with the formatted name-value pair.
      *          If the <code>buffer</code> argument was not <code>null</code>,
-     *          that buffer will be returned.
+     *          that buffer will be used and returned.
      */
     CharArrayBuffer formatNameValuePair(CharArrayBuffer buffer,
                                         NameValuePair nvp,

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java?rev=569831&r1=569830&r2=569831&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueFormatter.java
Sun Aug 26 09:20:45 2007
@@ -136,7 +136,61 @@
     }
 
 
-    public void testInvalidNullArguments() throws Exception {
+
+    public void testHEFormatting() throws Exception {
+        NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff"); 
+        NameValuePair param2 = new BasicNameValuePair("param", "this\\that"); 
+        NameValuePair param3 = new BasicNameValuePair("param", "this,that");
+        NameValuePair param4 = new BasicNameValuePair("param", null);
+        NameValuePair[] params = new NameValuePair[] {param1, param2, param3, param4};
+        HeaderElement element = new BasicHeaderElement("name", "value", params); 
+        
+        assertEquals("name=value; param=regular_stuff; param=\"this\\\\that\"; param=\"this,that\";
param", 
+                     BasicHeaderValueFormatter.formatHeaderElement(element, false, null));
+    }
+    
+    public void testElementsFormatting() throws Exception {
+        NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff"); 
+        NameValuePair param2 = new BasicNameValuePair("param", "this\\that"); 
+        NameValuePair param3 = new BasicNameValuePair("param", "this,that");
+        NameValuePair param4 = new BasicNameValuePair("param", null);
+        HeaderElement element1 = new BasicHeaderElement("name1", "value1", new NameValuePair[]
{param1}); 
+        HeaderElement element2 = new BasicHeaderElement("name2", "value2", new NameValuePair[]
{param2}); 
+        HeaderElement element3 = new BasicHeaderElement("name3", "value3", new NameValuePair[]
{param3}); 
+        HeaderElement element4 = new BasicHeaderElement("name4", "value4", new NameValuePair[]
{param4}); 
+        HeaderElement element5 = new BasicHeaderElement("name5", null); 
+        HeaderElement[] elements = new HeaderElement[] {element1, element2, element3, element4,
element5}; 
+        
+        assertEquals
+            ("name1=value1; param=regular_stuff, name2=value2; " +
+             "param=\"this\\\\that\", name3=value3; param=\"this,that\", " +
+             "name4=value4; param, name5", 
+             BasicHeaderValueFormatter.formatElements(elements, false, null));
+    }
+
+    
+    public void testInvalidHEArguments() throws Exception {
+        try {
+            BasicHeaderValueFormatter.formatHeaderElement
+                ((HeaderElement) null, false,
+                 BasicHeaderValueFormatter.DEFAULT);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+
+        try {
+            BasicHeaderValueFormatter.formatElements
+                ((HeaderElement[]) null, false,
+                 BasicHeaderValueFormatter.DEFAULT);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+
+
+    public void testInvalidNVArguments() throws Exception {
 
         try {
             BasicHeaderValueFormatter.formatNameValuePair

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java?rev=569831&r1=569830&r2=569831&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java
Sun Aug 26 09:20:45 2007
@@ -195,62 +195,4 @@
             .parseHeaderElement(buffer, 0, buffer.length());
         assertEquals(s, element.toString());
     }
-    
-    public void testElementFormatting() throws Exception {
-        NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff"); 
-        NameValuePair param2 = new BasicNameValuePair("param", "this\\that"); 
-        NameValuePair param3 = new BasicNameValuePair("param", "this,that");
-        NameValuePair param4 = new BasicNameValuePair("param", null);
-        NameValuePair[] params = new NameValuePair[] {param1, param2, param3, param4};
-        HeaderElement element = new BasicHeaderElement("name", "value", params); 
-        
-        assertEquals("name=value; param=regular_stuff; param=\"this\\\\that\"; param=\"this,that\";
param", 
-                BasicHeaderElement.format(element));
-    }
-    
-    public void testElementArrayFormatting() throws Exception {
-        NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff"); 
-        NameValuePair param2 = new BasicNameValuePair("param", "this\\that"); 
-        NameValuePair param3 = new BasicNameValuePair("param", "this,that");
-        NameValuePair param4 = new BasicNameValuePair("param", null);
-        HeaderElement element1 = new BasicHeaderElement("name1", "value1", new NameValuePair[]
{param1}); 
-        HeaderElement element2 = new BasicHeaderElement("name2", "value2", new NameValuePair[]
{param2}); 
-        HeaderElement element3 = new BasicHeaderElement("name3", "value3", new NameValuePair[]
{param3}); 
-        HeaderElement element4 = new BasicHeaderElement("name4", "value4", new NameValuePair[]
{param4}); 
-        HeaderElement element5 = new BasicHeaderElement("name5", null); 
-        HeaderElement[] elements = new HeaderElement[] {element1, element2, element3, element4,
element5}; 
-        
-        assertEquals("name1=value1; param=regular_stuff, name2=value2; " +
-                "param=\"this\\\\that\", name3=value3; param=\"this,that\", " +
-                "name4=value4; param, name5", 
-                BasicHeaderElement.formatAll(elements));
-    }
-    
-    public void testFormatInvalidInput() throws Exception {
-        try {
-            BasicHeaderElement.format(null, new BasicHeaderElement("name1", "value1"));
-            fail("IllegalArgumentException should habe been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.format(new CharArrayBuffer(10), (HeaderElement) null);
-            fail("IllegalArgumentException should habe been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.formatAll(null, new HeaderElement[] {new BasicHeaderElement("name1",
"value1")});
-            fail("IllegalArgumentException should habe been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.formatAll(new CharArrayBuffer(10), (HeaderElement[]) null);
-            fail("IllegalArgumentException should habe been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-    }
-    
 }



Mime
View raw message