abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1221908 - in /abdera/abdera2: common/src/main/java/org/apache/abdera2/common/http/Preference.java test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java
Date Wed, 21 Dec 2011 22:16:53 GMT
Author: jmsnell
Date: Wed Dec 21 22:16:52 2011
New Revision: 1221908

URL: http://svn.apache.org/viewvc?rev=1221908&view=rev
Log:
Fixes...

Modified:
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/Preference.java
    abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/Preference.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/Preference.java?rev=1221908&r1=1221907&r2=1221908&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/Preference.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/Preference.java Wed
Dec 21 22:16:52 2011
@@ -159,6 +159,10 @@ public class Preference implements Seria
       return this;
     }
     
+    public Builder param(String key) {
+      return param(key,"");
+    }
+    
     public Builder param(String key, String val) {
       checkNotNull(key);
       key = key.toLowerCase(Locale.US);
@@ -306,7 +310,7 @@ public class Preference implements Seria
     StringBuilder buf = new StringBuilder();
     buf.append(token);
     
-    if (value != null) {
+    if (value != null && value.length() > 0) {
       String encval = Codec.encode(value, Codec.STAR);
       if (value.equals(encval)) {
         buf.append('=')
@@ -320,16 +324,18 @@ public class Preference implements Seria
 
    for (Map.Entry<String, String> entry : params.entrySet()) {
      String val = entry.getValue();
-     String encval = Codec.encode(val,Codec.STAR);
+     String encval = val != null ? Codec.encode(val,Codec.STAR) : null;
      buf.append(';')
         .append(entry.getKey());
-     if (!val.equals(encval)) {
-       buf.append('*')
-          .append('=')
-          .append(encval);
-     } else {
-       buf.append('=')
-          .append(quotedIfNotToken(val));
+     if (val != null && val.length() > 0) {
+       if (!val.equals(encval)) {
+         buf.append('*')
+            .append('=')
+            .append(encval);
+       } else {
+         buf.append('=')
+            .append(quotedIfNotToken(val));
+       }
      }
    }
     
@@ -337,7 +343,7 @@ public class Preference implements Seria
   }
   
   private final static String TOKEN = "[\\!\\#\\$\\%\\&\\'\\*\\+\\-\\.\\^\\_\\`\\|\\~a-zA-Z0-9]+";
-  private final static String PREF = TOKEN+"(?:\\s*=\\s*(?:(?:\"[^\"]+\")|(?:"+TOKEN+")))?";
+  private final static String PREF = TOKEN+"(?:\\s*=\\s*(?:(?:\"[^\"]+\")|(?:"+TOKEN+"))?)?";
   private final static String PARAMS = "(?:\\s*;\\s*" + PREF + ")*";
   private final static String PATTERN = "("+PREF+")(" + PARAMS + ")";
 
@@ -347,33 +353,35 @@ public class Preference implements Seria
     Pattern.compile("("+PREF+")");
   
   public static Iterable<Preference> parse(String text) {
-    ImmutableList.Builder<Preference> prefs = ImmutableList.builder();
-    Matcher matcher = pattern.matcher(text);
-    while (matcher.find()) {
-      String pref = matcher.group(1);
-      String params = matcher.group(2);
-      String token = null, tokenval = null;
-      
-      if (pref != null) {
-        String[] ps = pref.split("\\s*=\\s*", 2);
-        token = ps[0].trim();
-        if (ps.length == 2)
-          tokenval = Codec.decode(CharUtils.unquote(ps[1]));
-      }
-      
-      Preference.Builder maker = 
-        Preference.make().token(token).value(tokenval);   
-      if (params != null) {
-        Matcher mparams = param.matcher(params);
-        while(mparams.find()) {
-          String p = mparams.group(1);
-          String[] ps = p.split("\\s*=\\s*", 2);
-          maker.param(ps[0], Codec.decode(CharUtils.unquote(ps[1])));
+      ImmutableList.Builder<Preference> prefs = ImmutableList.builder();
+      Matcher matcher = pattern.matcher(text);
+      while (matcher.find()) {
+        String pref = matcher.group(1);
+        String params = matcher.group(2);
+        String token = null, tokenval = null;
+        
+        if (pref != null) {
+          String[] ps = pref.split("\\s*\\*?=\\s*", 2);
+          token = ps[0].trim();
+          if (ps.length == 2)
+            tokenval = Codec.decode(CharUtils.unquote(ps[1]));
         }
+        
+        Preference.Builder maker = 
+          Preference.make().token(token).value(tokenval);   
+        if (params != null) {
+          Matcher mparams = param.matcher(params);
+          while(mparams.find()) {
+            String p = mparams.group(1);
+            String[] ps = p.split("\\s*\\*?=\\s*", 2);
+            if (ps.length == 2)
+              maker.param(ps[0], Codec.decode(CharUtils.unquote(ps[1])));
+            else maker.param(ps[0]);
+          }
+        }
+        prefs.add(maker.get());
       }
-      prefs.add(maker.get());
-    }
-    return prefs.build();
+      return prefs.build();
   }
   
   public static String toString(

Modified: abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java?rev=1221908&r1=1221907&r2=1221908&view=diff
==============================================================================
--- abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java
(original)
+++ abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/http/PreferenceTest.java
Wed Dec 21 22:16:52 2011
@@ -7,6 +7,8 @@ import static org.junit.Assert.assertTru
 
 import org.junit.Test;
 
+import com.google.common.collect.Iterables;
+
 public class PreferenceTest {
 
   @Test
@@ -17,9 +19,9 @@ public class PreferenceTest {
         PREF_LENIENT, 
         WAIT(10), 
         PREF_RETURN_ASYNCH,
-        make("A","B").param("B", "c").param("C","foo bar baz").get());
+        make("A","B").param("B", "c").param("C","fo\u00F6 bar baz").get());
 
-    assertEquals("lenient,wait=10,return-asynch,a=B;b=c;c=\"foo bar baz\"",prefs);
+    assertEquals("lenient,wait=10,return-asynch,a=B;b=c;c*=UTF-8''fo%C3%B6%20bar%20baz",prefs);
     
     Iterable<Preference> list = parse(prefs);
     assertTrue(contains(list, WAIT(10)));
@@ -34,9 +36,9 @@ public class PreferenceTest {
     assertEquals("B",a.getValue());
     assertTrue(a.hasParam("b"));
     assertEquals("c",a.getParam("B"));
-    
-    System.out.println(a.getParam("b"));
-    System.out.println(a.getParam("c"));
+    assertEquals("fo\u00F6 bar baz", a.getParam("C"));    
+    list = parse("lenient , wait=;foo=");
+    assertEquals(2,Iterables.size(list));
     
   }
   



Mime
View raw message