hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rola...@apache.org
Subject svn commit: r568044 - in /jakarta/httpcomponents/httpcore/trunk/module-main/src: main/java/org/apache/http/message/ test/java/org/apache/http/message/
Date Tue, 21 Aug 2007 09:55:14 GMT
Author: rolandw
Date: Tue Aug 21 02:55:13 2007
New Revision: 568044

URL: http://svn.apache.org/viewvc?rev=568044&view=rev
Log:
removed static parsing code from BasicNameValuePair, adjusted test cases

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicNameValuePair.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestNameValuePair.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java?rev=568044&r1=568043&r2=568044&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
Tue Aug 21 02:55:13 2007
@@ -300,6 +300,33 @@
 
 
     /**
+     * Parses a name-value-pair with the given parser.
+     *
+     * @param value     the NVP to parse
+     * @param parser    the parser to use, or <code>null</code> for default
+     *
+     * @return  the parsed name-value pair
+     */
+    public final static
+       NameValuePair parseNameValuePair(String value,
+                                        HeaderValueParser parser)
+        throws ParseException {
+
+        if (value == null) {
+            throw new IllegalArgumentException
+                ("Value to parse may not be null.");
+        }
+
+        if (parser == null)
+            parser = BasicHeaderValueParser.DEFAULT;
+
+        CharArrayBuffer buffer = new CharArrayBuffer(value.length());
+        buffer.append(value);
+        return parser.parseNameValuePair(buffer, 0, buffer.length());
+    }
+
+
+    /**
      * Parses a name=value specification, where the = and value are optional.
      *
      * @param buffer    the buffer holding the name-value pair to parse
@@ -307,9 +334,9 @@
      * @return  the name-value pair, where the value is <code>null</code>
      *          if no value is specified
      */
-    protected NameValuePair parseNameValuePair(final CharArrayBuffer buffer,
-                                               final int indexFrom,
-                                               final int indexTo) {
+    public NameValuePair parseNameValuePair(final CharArrayBuffer buffer,
+                                            final int indexFrom,
+                                            final int indexTo) {
 
         if (buffer == null) {
             throw new IllegalArgumentException
@@ -354,7 +381,7 @@
 
     /**
      * Creates a name-value pair.
-     * Called from {@link #parseNameValuePair}.
+     * Called from {@link #parseNameValue}.
      *
      * @param name      the name
      * @param value     the value, or <code>null</code>

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicNameValuePair.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicNameValuePair.java?rev=568044&r1=568043&r2=568044&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicNameValuePair.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicNameValuePair.java
Tue Aug 21 02:55:13 2007
@@ -145,107 +145,6 @@
         return this.value;
     }
 
-    public static final NameValuePair[] parseAll(
-            final CharArrayBuffer buffer, final int indexFrom, final 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 params = new ArrayList(); 
-        int cur = indexFrom;
-        int from = indexFrom;
-        boolean qouted = false;
-        boolean escaped = false;
-        while (cur < indexTo) {
-            char ch = buffer.charAt(cur);
-            if (ch == '"' && !escaped) {
-                qouted = !qouted;
-            }
-            NameValuePair param = null;
-            if (!qouted && ch == ';') {
-                param = parse(buffer, from, cur);
-                from = cur + 1;
-            } else if (cur == indexTo - 1) {
-                param = parse(buffer, from, indexTo);
-            }
-            if (param != null && !(param.getName().length() == 0 && param.getValue()
== null)) {
-                params.add(param);
-            }
-            if (escaped) {
-                escaped = false;
-            } else {
-                escaped = qouted && ch == '\\';
-            }
-            cur++;
-        }
-        return (NameValuePair[]) params.toArray(new NameValuePair[params.size()]);
-    }
-    
-    public static final NameValuePair[] parseAll(final String s) {
-        if (s == null) {
-            throw new IllegalArgumentException("String may not be null");
-        }
-        CharArrayBuffer buffer = new CharArrayBuffer(s.length());
-        buffer.append(s);
-        return parseAll(buffer, 0, buffer.length());
-    }
-
-    public static NameValuePair parse(
-            final CharArrayBuffer buffer, final int indexFrom, final 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();
-        }
-        int eq = buffer.indexOf('=', indexFrom, indexTo);
-        if (eq < 0) {
-            return new BasicNameValuePair(buffer.substringTrimmed(indexFrom, indexTo), null);
-        }
-        String name = buffer.substringTrimmed(indexFrom, eq);
-        int i1 = eq + 1;
-        int i2 = indexTo;
-        // Trim leading white spaces
-        while (i1 < i2 && (HTTP.isWhitespace(buffer.charAt(i1)))) {
-            i1++;
-        }
-        // Trim trailing white spaces
-        while ((i2 > i1) && (HTTP.isWhitespace(buffer.charAt(i2 - 1)))) {
-            i2--;
-        }
-        // Strip away quotes if necessary
-        if (((i2 - i1) >= 2) 
-            && (buffer.charAt(i1) == '"') 
-            && (buffer.charAt(i2 - 1) == '"')) {
-            i1++;
-            i2--;
-        }
-        String value = buffer.substring(i1, i2);
-        return new BasicNameValuePair(name, value);
-    }
-
-    public static final NameValuePair parse(final String s) {
-        if (s == null) {
-            throw new IllegalArgumentException("String may not be null");
-        }
-        CharArrayBuffer buffer = new CharArrayBuffer(s.length());
-        buffer.append(s);
-        return parse(buffer, 0, buffer.length());
-    }
 
     /**
      * Special characters that can be used as separators in HTTP parameters.

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java?rev=568044&r1=568043&r2=568044&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java
Tue Aug 21 02:55:13 2007
@@ -197,5 +197,22 @@
         throws ParseException
         ;
 
+
+    /**
+     * Parses a name=value specification, where the = and value are optional.
+     *
+     * @param buffer    the buffer holding the name-value pair to parse
+     *
+     * @return  the name-value pair, where the value is <code>null</code>
+     *          if no value is specified
+     *
+     * @throws ParseException        in case of a parse error
+     */
+    NameValuePair parseNameValuePair(CharArrayBuffer buffer,
+                                     int indexFrom,
+                                     int indexTo)
+        throws ParseException
+        ;
+
 }
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java?rev=568044&r1=568043&r2=568044&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
Tue Aug 21 02:55:13 2007
@@ -101,7 +101,7 @@
         assertEquals(" value8",elements[2].getParameters()[1].getValue());
     }
 
-    public void testParseEscaped() {
+    public void testParseHEEscaped() {
         String s = 
           "test1 =  \"\\\"stuff\\\"\", test2= \"\\\\\", test3 = \"stuff, stuff\"";
         HeaderElement[] elements = BasicHeaderValueParser.parseElements(s, null);
@@ -114,25 +114,25 @@
         assertEquals("stuff, stuff", elements[2].getValue());
     }
 
-    public void testFringeCase1() throws Exception {
+    public void testHEFringeCase1() throws Exception {
         String headerValue = "name1 = value1,";
         HeaderElement[] elements = BasicHeaderValueParser.parseElements(headerValue, null);
         assertEquals("Number of elements", 1, elements.length);
     }
 
-    public void testFringeCase2() throws Exception {
+    public void testHEFringeCase2() throws Exception {
         String headerValue = "name1 = value1, ";
         HeaderElement[] elements = BasicHeaderValueParser.parseElements(headerValue, null);
         assertEquals("Number of elements", 1, elements.length);
     }
 
-    public void testFringeCase3() throws Exception {
+    public void testHEFringeCase3() throws Exception {
         String headerValue = ",, ,, ,";
         HeaderElement[] elements = BasicHeaderValueParser.parseElements(headerValue, null);
         assertEquals("Number of elements", 0, elements.length);
     }
 
-    public void testInvalidInput() throws Exception {
+    public void testHEInvalidInput() throws Exception {
         CharArrayBuffer buffer = new CharArrayBuffer(32);
         buffer.append("name = value");
         try {
@@ -188,6 +188,151 @@
         }
         try {
             BasicHeaderValueParser.DEFAULT.parseHeaderElement(buffer, 2, 1);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+    }
+
+
+    
+    public void testNVParse() {
+        String s = "test";
+        NameValuePair param =
+            BasicHeaderValueParser.parseNameValuePair(s, null);
+        assertEquals("test", param.getName());
+        assertEquals(null, param.getValue());
+
+        s = "test=stuff";
+        param = BasicHeaderValueParser.parseNameValuePair(s, null);
+        assertEquals("test", param.getName());
+        assertEquals("stuff", param.getValue());
+        
+        s = "   test  =   stuff ";
+        param = BasicHeaderValueParser.parseNameValuePair(s, null);
+        assertEquals("test", param.getName());
+        assertEquals("stuff", param.getValue());
+        
+        s = "test  = \"stuff\"";
+        param = BasicHeaderValueParser.parseNameValuePair(s, null);
+        assertEquals("test", param.getName());
+        assertEquals("stuff", param.getValue());
+        
+        s = "test  = \"  stuff\\\"\"";
+        param = BasicHeaderValueParser.parseNameValuePair(s, null);
+        assertEquals("test", param.getName());
+        assertEquals("  stuff\\\"", param.getValue());
+        
+        s = "  test";
+        param = BasicHeaderValueParser.parseNameValuePair(s, null);
+        assertEquals("test", param.getName());
+        assertEquals(null, param.getValue());
+
+        s = "  ";
+        param = BasicHeaderValueParser.parseNameValuePair(s, null);
+        assertEquals("", param.getName());
+        assertEquals(null, param.getValue());
+
+        s = " = stuff ";
+        param = BasicHeaderValueParser.parseNameValuePair(s, null);
+        assertEquals("", param.getName());
+        assertEquals("stuff", param.getValue());
+    }
+
+    public void testNVParseAll() {
+        String s = 
+          "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff";
+        NameValuePair[] params =
+            BasicHeaderValueParser.parseParameters(s, null);
+        assertEquals("test", params[0].getName());
+        assertEquals(null, params[0].getValue());
+        assertEquals("test1", params[1].getName());
+        assertEquals("stuff", params[1].getValue());
+        assertEquals("test2", params[2].getName());
+        assertEquals("stuff; stuff", params[2].getValue());
+        assertEquals("test3", params[3].getName());
+        assertEquals("\"stuff", params[3].getValue());
+
+        s = "  ";
+        params = BasicHeaderValueParser.parseParameters(s, null);
+        assertEquals(0, params.length);
+    }
+
+    public void testNVParseEscaped() {
+        String s = 
+          "test1 =  \"\\\"stuff\\\"\"; test2= \"\\\\\"; test3 = \"stuff; stuff\"";
+        NameValuePair[] params =
+            BasicHeaderValueParser.parseParameters(s, null);
+        assertEquals(3, params.length);
+        assertEquals("test1", params[0].getName());
+        assertEquals("\\\"stuff\\\"", params[0].getValue());
+        assertEquals("test2", params[1].getName());
+        assertEquals("\\\\", params[1].getValue());
+        assertEquals("test3", params[2].getName());
+        assertEquals("stuff; stuff", params[2].getValue());
+    }
+
+    public void testNVParseInvalidInput() throws Exception {
+        CharArrayBuffer buffer = new CharArrayBuffer(32);
+        buffer.append("name = value");
+
+        try {
+            BasicHeaderValueParser.parseParameters(null, null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            BasicHeaderValueParser.DEFAULT.parseParameters(null, 0, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            BasicHeaderValueParser.DEFAULT.parseParameters(buffer, -1, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            BasicHeaderValueParser.DEFAULT.parseParameters(buffer, 0, 1000);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            BasicHeaderValueParser.DEFAULT.parseParameters(buffer, 2, 1);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+
+        try {
+            BasicHeaderValueParser.parseNameValuePair(null, null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            BasicHeaderValueParser.DEFAULT.parseNameValuePair(null, 0, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            BasicHeaderValueParser.DEFAULT.parseNameValuePair(buffer, -1, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            BasicHeaderValueParser.DEFAULT.parseNameValuePair(buffer, 0, 1000);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            BasicHeaderValueParser.DEFAULT.parseNameValuePair(buffer, 2, 1);
             fail("IllegalArgumentException should have been thrown");
         } catch (IndexOutOfBoundsException ex) {
             // expected

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestNameValuePair.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestNameValuePair.java?rev=568044&r1=568043&r2=568044&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestNameValuePair.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestNameValuePair.java
Tue Aug 21 02:55:13 2007
@@ -100,144 +100,6 @@
         NameValuePair param2 = new BasicNameValuePair("name1", null);
         assertEquals("name1", param2.toString());
     }
-    
-    public void testParse() {
-        String s = "test";
-        NameValuePair param = BasicNameValuePair.parse(s);
-        assertEquals("test", param.getName());
-        assertEquals(null, param.getValue());
-
-        s = "test=stuff";
-        param = BasicNameValuePair.parse(s);
-        assertEquals("test", param.getName());
-        assertEquals("stuff", param.getValue());
-        
-        s = "   test  =   stuff ";
-        param = BasicNameValuePair.parse(s);
-        assertEquals("test", param.getName());
-        assertEquals("stuff", param.getValue());
-        
-        s = "test  = \"stuff\"";
-        param = BasicNameValuePair.parse(s);
-        assertEquals("test", param.getName());
-        assertEquals("stuff", param.getValue());
-        
-        s = "test  = \"  stuff\\\"\"";
-        param = BasicNameValuePair.parse(s);
-        assertEquals("test", param.getName());
-        assertEquals("  stuff\\\"", param.getValue());
-        
-        s = "  test";
-        param = BasicNameValuePair.parse(s);
-        assertEquals("test", param.getName());
-        assertEquals(null, param.getValue());
-
-        s = "  ";
-        param = BasicNameValuePair.parse(s);
-        assertEquals("", param.getName());
-        assertEquals(null, param.getValue());
-
-        s = " = stuff ";
-        param = BasicNameValuePair.parse(s);
-        assertEquals("", param.getName());
-        assertEquals("stuff", param.getValue());
-    }
-
-    public void testParseAll() {
-        String s = 
-          "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff";
-        NameValuePair[] params = BasicNameValuePair.parseAll(s);
-        assertEquals("test", params[0].getName());
-        assertEquals(null, params[0].getValue());
-        assertEquals("test1", params[1].getName());
-        assertEquals("stuff", params[1].getValue());
-        assertEquals("test2", params[2].getName());
-        assertEquals("stuff; stuff", params[2].getValue());
-        assertEquals("test3", params[3].getName());
-        assertEquals("\"stuff", params[3].getValue());
-
-        s = "  ";
-        params = BasicNameValuePair.parseAll(s);
-        assertEquals(0, params.length);
-    }
-
-    public void testParseEscaped() {
-        String s = 
-          "test1 =  \"\\\"stuff\\\"\"; test2= \"\\\\\"; test3 = \"stuff; stuff\"";
-        NameValuePair[] params = BasicNameValuePair.parseAll(s);
-        assertEquals(3, params.length);
-        assertEquals("test1", params[0].getName());
-        assertEquals("\\\"stuff\\\"", params[0].getValue());
-        assertEquals("test2", params[1].getName());
-        assertEquals("\\\\", params[1].getValue());
-        assertEquals("test3", params[2].getName());
-        assertEquals("stuff; stuff", params[2].getValue());
-    }
-
-    public void testParseInvalidInput() throws Exception {
-        CharArrayBuffer buffer = new CharArrayBuffer(32);
-        buffer.append("name = value");
-        try {
-            BasicNameValuePair.parseAll(null, 0, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicNameValuePair.parseAll(null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicNameValuePair.parseAll(buffer, -1, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicNameValuePair.parseAll(buffer, 0, 1000);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicNameValuePair.parseAll(buffer, 2, 1);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicNameValuePair.parse(null, 0, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicNameValuePair.parse(null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicNameValuePair.parse(buffer, -1, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicNameValuePair.parse(buffer, 0, 1000);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicNameValuePair.parse(buffer, 2, 1);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-    }
 
     public void testBasicFormatting() throws Exception {
         NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff"); 



Mime
View raw message