hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r348803 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/Header.java java/org/apache/http/HeaderElement.java test/org/apache/http/TestHeaderElement.java
Date Thu, 24 Nov 2005 21:06:56 GMT
Author: olegk
Date: Thu Nov 24 13:06:48 2005
New Revision: 348803

URL: http://svn.apache.org/viewcvs?rev=348803&view=rev
Log:
HeaderElement parsing code refactored to make use of CharArrayBuffer

Modified:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeaderElement.java

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java?rev=348803&r1=348802&r2=348803&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java Thu Nov 24
13:06:48 2005
@@ -176,8 +176,7 @@
      * @since 3.0
      */
     public HeaderElement[] getElements() {
-        return HeaderElement.parseElements(
-                this.buffer.internBuffer(), this.posValue, this.buffer.length());
+        return HeaderElement.parseElements(this.buffer, this.posValue, this.buffer.length());
     }
     
 }

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java?rev=348803&r1=348802&r2=348803&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java Thu
Nov 24 13:06:48 2005
@@ -124,47 +124,6 @@
     }
 
     /**
-     * Constructor with array of characters.
-     *
-     * @param chars the array of characters
-     * @param offset - the initial offset.
-     * @param length - the length.
-     * 
-     * @since 3.0
-     */
-    public HeaderElement(char[] chars, int offset, int length) {
-        super();
-        ParameterParser parser = new ParameterParser();
-        List params = parser.parse(chars, offset, length, ';');
-        if (params.size() > 0) {
-            NameValuePair element = (NameValuePair) params.remove(0);
-            this.name = element.getName();
-            this.value = element.getValue();
-            if (params.size() > 0) {
-                this.parameters = (NameValuePair[])
-                    params.toArray(new NameValuePair[params.size()]);    
-            } else {
-                this.parameters = new NameValuePair[] {};
-            }
-        } else {
-            this.name = "";
-            this.value = null;
-            this.parameters = new NameValuePair[] {};
-        }
-    }
-
-    /**
-     * Constructor with array of characters.
-     *
-     * @param chars the array of characters
-     * 
-     * @since 3.0
-     */
-    public HeaderElement(char[] chars) {
-        this(chars, 0, chars.length);
-    }
-
-    /**
      * Returns the name.
      *
      * @return String name The name
@@ -204,24 +163,33 @@
      * 
      * @since 3.0
      */
-    public static final HeaderElement[] parseElements(char[] headerValue, int indexFrom,
int indexTo) {
-        if (headerValue == null) {
-            return new HeaderElement[] {};
+    public static final HeaderElement[] parseElements(final CharArrayBuffer buffer, int indexFrom,
int indexTo) {
+        if (buffer == null) {
+            throw new IllegalArgumentException("Char array buffer may not be null");
+        }
+        if (indexFrom < 0) {
+            throw new IndexOutOfBoundsException();
+        }
+        if (indexTo > buffer.length()) {
+            throw new IndexOutOfBoundsException();
+        }
+        if (indexFrom > indexTo) {
+            throw new IndexOutOfBoundsException();
         }
         List elements = new ArrayList(); 
         int i = indexFrom;
         boolean qouted = false;
         while (i < indexTo) {
-            char ch = headerValue[i];
+            char ch = buffer.charAt(i);
             if (ch == '"') {
                 qouted = !qouted;
             }
             HeaderElement element = null;
             if ((!qouted) && (ch == ',')) {
-                element = new HeaderElement(headerValue, indexFrom, i);
+                element = parse(buffer, indexFrom, i);
                 indexFrom = i + 1;
             } else if (i == indexTo - 1) {
-                element = new HeaderElement(headerValue, indexFrom, indexTo);
+                element = parse(buffer, indexFrom, indexTo);
             }
             if (element != null && !element.getName().equals("")) {
                 elements.add(element);
@@ -236,17 +204,57 @@
      * This parses the value part of a header. The result is an array of
      * HeaderElement objects.
      *
-     * @param headerValue  the string representation of the header value
+     * @param s  the string representation of the header value
      *                     (as received from the web server).
      * @return array of {@link HeaderElement}s.
      * 
      * @since 3.0
      */
-    public static final HeaderElement[] parseElements(final String headerValue) {
-        if (headerValue == null) {
-            return new HeaderElement[] {};
+    public static final HeaderElement[] parseElements(final String s) {
+        if (s == null) {
+            throw new IllegalArgumentException("String may not be null");
+        }
+        CharArrayBuffer buffer = new CharArrayBuffer(s.length()); 
+        buffer.append(s);
+        return parseElements(buffer, 0, buffer.length());
+    }
+
+    public static HeaderElement parse(final CharArrayBuffer buffer, int indexFrom, int indexTo)
{
+        if (buffer == null) {
+            throw new IllegalArgumentException("Char array buffer may not be null");
+        }
+        if (indexFrom < 0) {
+            throw new IndexOutOfBoundsException();
+        }
+        if (indexTo > buffer.length()) {
+            throw new IndexOutOfBoundsException();
+        }
+        if (indexFrom > indexTo) {
+            throw new IndexOutOfBoundsException();
+        }
+        ParameterParser parser = new ParameterParser();
+        List paramlist = parser.parse(buffer.internBuffer(), indexFrom, indexTo, ';');
+        if (paramlist.size() > 0) {
+            NameValuePair element = (NameValuePair) paramlist.remove(0);
+            String name = element.getName();
+            String value = element.getValue();
+            NameValuePair[] params = null;
+            if (paramlist.size() > 0) {
+                params = (NameValuePair[]) paramlist.toArray(new NameValuePair[paramlist.size()]);
   
+            }
+            return new HeaderElement(name, value, params);
+        } else {
+            return new HeaderElement("", null, null);
+        }
+    }
+
+    public static final HeaderElement parse(final String s) {
+        if (s == null) {
+            throw new IllegalArgumentException("String may not be null");
         }
-        return parseElements(headerValue.toCharArray(), 0, headerValue.length());
+        CharArrayBuffer buffer = new CharArrayBuffer(s.length());
+        buffer.append(s);
+        return parse(buffer, 0, buffer.length());
     }
 
     /**

Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeaderElement.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeaderElement.java?rev=348803&r1=348802&r2=348803&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeaderElement.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeaderElement.java
Thu Nov 24 13:06:48 2005
@@ -30,6 +30,8 @@
 
 package org.apache.http;
 
+import org.apache.http.io.CharArrayBuffer;
+
 import junit.framework.*;
 
 /**
@@ -82,7 +84,7 @@
 
     public void testCharArrayConstructor() throws Exception {
         String s = "name = value; param1 = value1";
-        HeaderElement element = new HeaderElement(s.toCharArray()); 
+        HeaderElement element = HeaderElement.parse(s); 
         assertEquals("name", element.getName());
         assertEquals("value", element.getValue());
         assertEquals(1, element.getParameters().length);
@@ -149,19 +151,75 @@
         HeaderElement[] elements = HeaderElement.parseElements(headerValue);
         assertEquals("Number of elements", 0, elements.length);
     }
-    
-    public void testNullInput() throws Exception {
-        HeaderElement[] elements = HeaderElement.parseElements((char [])null, 0, 0);
-        assertNotNull(elements);
-        assertEquals("Number of elements", 0, elements.length);
-        elements = HeaderElement.parseElements((String)null);
-        assertNotNull(elements);
-        assertEquals("Number of elements", 0, elements.length);
-    }
 
+    public void testInvalidInput() throws Exception {
+        CharArrayBuffer buffer = new CharArrayBuffer(32);
+        buffer.append("name = value");
+        try {
+            HeaderElement.parseElements(null, 0, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            HeaderElement.parseElements(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            HeaderElement.parseElements(buffer, -1, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            HeaderElement.parseElements(buffer, 0, 1000);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            HeaderElement.parseElements(buffer, 2, 1);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            HeaderElement.parse(null, 0, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            HeaderElement.parse(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            HeaderElement.parse(buffer, -1, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            HeaderElement.parse(buffer, 0, 1000);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            HeaderElement.parse(buffer, 2, 1);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+    }
+    
     public void testParamByName() throws Exception {
         String s = "name = value; param1 = value1; param2 = value2";
-        HeaderElement element = new HeaderElement(s.toCharArray()); 
+        HeaderElement element = HeaderElement.parse(s); 
         assertEquals("value1", element.getParameterByName("param1").getValue());
         assertEquals("value2", element.getParameterByName("param2").getValue());
         assertNull(element.getParameterByName("param3"));
@@ -228,10 +286,10 @@
     
     public void testToString() {
         String s = "name = value; param1 = value1; param2 = value2";
-        HeaderElement element = new HeaderElement(s.toCharArray());
+        HeaderElement element = HeaderElement.parse(s);
         assertEquals(s, element.toString());
         s = "name; param1 = value1; param2 = value2";
-        element = new HeaderElement(s.toCharArray());
+        element = HeaderElement.parse(s);
         assertEquals(s, element.toString());
     }
     



Mime
View raw message