abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r660644 - in /incubator/abdera/java/trunk/dependencies/i18n/src: main/java/org/apache/abdera/i18n/templates/Context.java main/java/org/apache/abdera/i18n/templates/Operation.java test/java/org/apache/abdera/i18n/test/iri/TestTemplate.java
Date Tue, 27 May 2008 18:59:17 GMT
Author: jmsnell
Date: Tue May 27 11:59:13 2008
New Revision: 660644

URL: http://svn.apache.org/viewvc?rev=660644&view=rev
Log:
Update the URI Templates implementation to match the current version of the spec

  http://bitworking.org/projects/URI-Templates/spec/draft-gregorio-uritemplate-03.txt
  
The -listjoin op is now -list
The -append op is now -suffix
-listjoin and -append can still be used for backwards compatibility

-prefix and -suffix substitutions can now be lists

Unicode Normalization Form KC is now required by the spec and is applied automatically

Additional bug fixes have been made

Modified:
    incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Context.java
    incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Operation.java
    incubator/abdera/java/trunk/dependencies/i18n/src/test/java/org/apache/abdera/i18n/test/iri/TestTemplate.java

Modified: incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Context.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Context.java?rev=660644&r1=660643&r2=660644&view=diff
==============================================================================
--- incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Context.java
(original)
+++ incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Context.java
Tue May 27 11:59:13 2008
@@ -44,16 +44,6 @@
   void setIri(boolean isiri);
   
   /**
-   * True if replacement values are to be Unicode NFC normalized
-   */
-  boolean isNormalizing();
-  
-  /**
-   * True if replacement values are to be Unicode NFC normalized
-   */
-  void setNormalizing(boolean normalizing);
-  
-  /**
    * Clear this context
    */
   void clear();

Modified: incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Operation.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Operation.java?rev=660644&r1=660643&r2=660644&view=diff
==============================================================================
--- incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Operation.java
(original)
+++ incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Operation.java
Tue May 27 11:59:13 2008
@@ -75,11 +75,13 @@
     Map<String,Operation> ops = new HashMap<String,Operation>();
     ops.put("", new DefaultOperation());
     ops.put("prefix", new PrefixOperation());
-    ops.put("append", new AppendOperation());
+    ops.put("suffix", new AppendOperation());
     ops.put("join", new JoinOperation());
-    ops.put("listjoin", new ListJoinOperation());
+    ops.put("list", new ListJoinOperation());
     ops.put("opt", new OptOperation());
     ops.put("neg", new NegOperation());
+    ops.put("append", ops.get("suffix")); // for backwards compatibility
+    ops.put("listjoin", ops.get("list")); // for backwards compatibility
     return ops;
   }
   
@@ -105,13 +107,13 @@
     if (value != null) {
       if (value instanceof String) {
         String val = toString(value,context);
-        if (val != null && val.length() > 0)
+        if (val != null)
           buf.append(val);
       } else if (value.getClass().isArray()) {
         Object[] values = (Object[])value;
         for (Object obj : values) {
           String val = toString(obj,context);
-          if (val != null && val.length() > 0) {
+          if (val != null) {
             if (buf.length() > 0) buf.append(sep);
             buf.append(val);
           }
@@ -120,13 +122,13 @@
         Iterable iterable = (Iterable)value;
         for (Object obj : iterable) {
           String val = toString(obj,context);
-          if (val != null && val.length() > 0) {
+          if (val != null) {
             if (buf.length() > 0) buf.append(sep);
             buf.append(val);
           }          
         }
       }
-    }
+    } else return null;
     return buf.toString();
   }
   
@@ -136,7 +138,7 @@
     String def = vardef.length > 1 ? vardef[1] : null;
     Object rep = context.resolve(var);
     String val = toString(rep,context);
-    return val != null && val.length() > 0 ? 
+    return val != null ? 
         val : def != null ? def : null;
   }
   
@@ -147,49 +149,60 @@
         return UrlEncoding.encode((byte[])val);
       } else if (val instanceof char[]) {
         String chars = new String((char[])val);
-        return UrlEncoding.encode(
-            !context.isNormalizing() ? chars : 
+        return UrlEncoding.encode( 
             Normalizer.normalize(
               chars, 
-              Normalizer.Form.C).toString(), 
+              Normalizer.Form.KC).toString(), 
             context.isIri() ? 
               CharUtils.Profile.IUNRESERVED.filter() : 
               CharUtils.Profile.UNRESERVED.filter());
       } else if (val instanceof short[]) {
         StringBuilder buf = new StringBuilder();
         short[] array = (short[]) val;
-        for (short obj : array)
+        for (short obj : array) {
+          if (buf.length() > 0) buf.append("%2C");
           buf.append(String.valueOf(obj));
+        }
         return buf.toString();
       } else if (val instanceof int[]) {
         StringBuilder buf = new StringBuilder();
         int[] array = (int[]) val;
-        for (int obj : array)
+        for (int obj : array) {
+          if (buf.length() > 0) buf.append("%2C");
           buf.append(String.valueOf(obj));
+        }
         return buf.toString();
       } else if (val instanceof long[]) {
         StringBuilder buf = new StringBuilder();
         long[] array = (long[]) val;
-        for (long obj : array)
+        for (long obj : array) {
+          if (buf.length() > 0) buf.append("%2C");
           buf.append(String.valueOf(obj));
+        }
         return buf.toString();
       } else if (val instanceof double[]) {
         StringBuilder buf = new StringBuilder();
         double[] array = (double[]) val;
-        for (double obj : array)
+        for (double obj : array) {
+          if (buf.length() > 0) buf.append("%2C");
           buf.append(String.valueOf(obj));
+        }
         return buf.toString();
       } else if (val instanceof float[]) {
         StringBuilder buf = new StringBuilder();
         float[] array = (float[]) val;
-        for (float obj : array)
+        for (float obj : array) {
+          if (buf.length() > 0) buf.append("%2C");
           buf.append(String.valueOf(obj));
+        }
         return buf.toString();
       } else if (val instanceof boolean[]) {
         StringBuilder buf = new StringBuilder();
         boolean[] array = (boolean[]) val;
-        for (boolean obj : array)
+        for (boolean obj : array) {
+          if (buf.length() > 0) buf.append("%2C");
           buf.append(String.valueOf(obj));
+        }
         return buf.toString();
       } else {
         StringBuilder buf = new StringBuilder();
@@ -220,8 +233,7 @@
     } else if (val instanceof CharSequence) {
       return encode(
         (CharSequence)val,
-        context.isIri(),
-        context.isNormalizing());
+        context.isIri());
     } else if (val instanceof Byte) {
       return UrlEncoding.encode(((Byte)val).byteValue());
     } else if (val instanceof Iterable) {
@@ -235,8 +247,7 @@
         val != null ? 
           val.toString() : 
           null,
-        context.isIri(),
-        context.isNormalizing());
+        context.isIri());
     }
   }
   
@@ -248,21 +259,79 @@
     if (rep != null) {
       StringBuilder buf = new StringBuilder();
       if (rep.getClass().isArray()) {
-        Object[] array = (Object[]) rep;
-        for (Object obj : array) {
-          String val = toString(obj,context);
-          if (val != null && val.length() > 0) {
-            if (buf.length() > 0) buf.append(arg);
+        if (rep instanceof byte[]) {
+          String val = toString(rep,context);
+          if (val != null) {
             buf.append(var);
             buf.append("=");
             buf.append(val);
           }
+        } else if (rep instanceof char[]) {
+          String val = toString(rep,context);
+          if (val != null) {
+            buf.append(var);
+            buf.append("=");
+            buf.append(val);
+          }
+        } else if (rep instanceof short[]) {
+          String val = toString(rep,context);
+          if (val != null) {
+            buf.append(var);
+            buf.append("=");
+            buf.append(val);
+          }
+        } else if (rep instanceof int[]) {
+          String val = toString(rep,context);
+          if (val != null) {
+            buf.append(var);
+            buf.append("=");
+            buf.append(val);
+          }
+        } else if (rep instanceof long[]) {
+          String val = toString(rep,context);
+          if (val != null) {
+            buf.append(var);
+            buf.append("=");
+            buf.append(val);
+          }
+        } else if (rep instanceof double[]) {
+          String val = toString(rep,context);
+          if (val != null) {
+            buf.append(var);
+            buf.append("=");
+            buf.append(val);
+          }
+        } else if (rep instanceof float[]) {
+          String val = toString(rep,context);
+          if (val != null) {
+            buf.append(var);
+            buf.append("=");
+            buf.append(val);
+          }
+        } else if (rep instanceof boolean[]) {
+          String val = toString(rep,context);
+          if (val != null) {
+            buf.append(var);
+            buf.append("=");
+            buf.append(val);
+          }
+        } else {
+          Object[] array = (Object[]) rep;
+          for (Object obj : array) {
+            String val = toString(obj,context);
+            if (val != null) {
+              if (buf.length() > 0) buf.append(arg);
+              buf.append(var);
+              buf.append("=");
+              buf.append(val);
+            }
+          }
         }
       } else if (rep instanceof Iterable) {
         Iterable list = (Iterable)rep;
         for (Object obj : list) {
           String val = toString(obj,context);
-          if (val != null && val.length() > 0) {
+          if (val != null) {
             if (buf.length() > 0) buf.append(arg);
             buf.append(var);
             buf.append("=");
@@ -271,7 +340,7 @@
         }
       } else {
         String val = toString(rep,context);
-        if (val != null && val.length() > 0) {
+        if (val != null) {
           buf.append(var);
           buf.append("=");
           buf.append(val);
@@ -295,20 +364,35 @@
     if (rep == null) rep = def;
     if (rep == null) return false;
     if (rep.getClass().isArray()) {
-      Object[] a = (Object[])rep;
-      return a.length > 0;
-    } else return toString(rep,context).length() > 0;
+      if (rep instanceof byte[]) 
+        return ((byte[])rep).length > 0;
+      else if (rep instanceof short[]) 
+        return ((short[])rep).length > 0;
+      else if (rep instanceof char[]) 
+        return ((char[])rep).length > 0;
+      else if (rep instanceof int[]) 
+        return ((int[])rep).length > 0;
+      else if (rep instanceof long[]) 
+        return ((long[])rep).length > 0;
+      else if (rep instanceof double[]) 
+        return ((double[])rep).length > 0;
+      else if (rep instanceof float[]) 
+        return ((float[])rep).length > 0;
+      else if (rep instanceof boolean[]) 
+        return ((boolean[])rep).length > 0;
+      else if (rep instanceof Object[]) 
+        return ((Object[])rep).length > 0;
+    }
+    return true;
   }
   
   private static String encode(
     CharSequence val, 
-    boolean isiri, 
-    boolean normalizing) {
+    boolean isiri) {
       return UrlEncoding.encode(
-          !normalizing ? val : 
           Normalizer.normalize(
             val, 
-            Normalizer.Form.C).toString(), 
+            Normalizer.Form.KC).toString(), 
           isiri ? 
             CharUtils.Profile.IUNRESERVED.filter() : 
             CharUtils.Profile.UNRESERVED.filter());
@@ -331,7 +415,7 @@
     private static final long serialVersionUID = 2738115969196268525L;
     public PrefixOperation() { super("prefix"); }    
     public String evaluate(String var, String arg, Context context) {
-      String value = eval(var,context);
+      String value = evallist(var,context,arg);
       return value == null || value.length() == 0 ? "" : arg != null ? arg + value : value;
     }
     public void explain(String var, String arg, Appendable buf) throws IOException {
@@ -347,10 +431,10 @@
   
   private static final class AppendOperation extends Operation {
     private static final long serialVersionUID = -2742793539643289075L;
-    public AppendOperation() { super("append"); }    
+    public AppendOperation() { super("suffix"); }    
     public String evaluate(String var, String arg, Context context) {
-      String value = eval(var,context);
-      return value == null || value.length() == 0 ? "" : arg != null ? value + arg : value;
+      String value = evallist(var,context,arg);
+      return value == null || value.length() == 0  ? "" : arg != null ? value + arg : value;
     }
     public void explain(String var, String arg, Appendable buf) throws IOException {
       buf.append("If '")
@@ -373,7 +457,7 @@
       for (int n = 0; n < vardefs.length; n++) {
         String vardef = vardefs[n];
         val = eval(vardef,arg,context);
-        if (val != null && val.length() > 0) {
+        if (val != null) {
           if (buf.length() > 0) buf.append(arg);
           buf.append(val);
         }
@@ -398,7 +482,7 @@
   
   private static final class ListJoinOperation extends Operation {
     private static final long serialVersionUID = -8314383556644740425L;
-    public ListJoinOperation() { super("listjoin"); }
+    public ListJoinOperation() { super("list"); }
     public String evaluate(String var, String arg, Context context) {
       return evallist(var,context,arg);
     }

Modified: incubator/abdera/java/trunk/dependencies/i18n/src/test/java/org/apache/abdera/i18n/test/iri/TestTemplate.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/dependencies/i18n/src/test/java/org/apache/abdera/i18n/test/iri/TestTemplate.java?rev=660644&r1=660643&r2=660644&view=diff
==============================================================================
--- incubator/abdera/java/trunk/dependencies/i18n/src/test/java/org/apache/abdera/i18n/test/iri/TestTemplate.java
(original)
+++ incubator/abdera/java/trunk/dependencies/i18n/src/test/java/org/apache/abdera/i18n/test/iri/TestTemplate.java
Tue May 27 11:59:13 2008
@@ -50,7 +50,7 @@
   
   @Test
   public void test3() throws Exception {
-    String t = "http://bitworking.org/news/{-listjoin|/|entry}";
+    String t = "http://bitworking.org/news/{-list|/|entry}";
     String e = "http://bitworking.org/news/240/Newsqueak";
     HashMapContext c = new HashMapContext();
     c.put("entry", new String[] {"240","Newsqueak"});
@@ -89,7 +89,7 @@
   
   @Test
   public void test7() throws Exception {
-    String t = "bar/{-append|#home|var}";
+    String t = "bar/{-suffix|#home|var}";
     String e = "bar/foo#home";
     HashMapContext c = new HashMapContext();
     c.put("var","foo");
@@ -108,7 +108,7 @@
 
   @Test
   public void test9() throws Exception {
-    String t = "{-listjoin|/|segments}";
+    String t = "{-list|/|segments}";
     String e = "a/b/c";
     HashMapContext c = new HashMapContext();
     c.put("segments",new String[] {"a","b","c"});
@@ -163,7 +163,7 @@
   
   @Test
   public void test14() throws Exception {
-    String t = "{-opt|/-/|categories}{-listjoin|/|categories}";
+    String t = "{-opt|/-/|categories}{-list|/|categories}";
     String e = "/-/A%7C-B/-C";
     HashMapContext c = new HashMapContext();
     c.put("categories", new String[] {"A|-B","-C"});
@@ -172,7 +172,7 @@
   
   @Test
   public void test15() throws Exception {
-    String t = "http://www.google.com/notebook/feeds/{userID}{-prefix|/notebooks/|notebookID}{-opt|/-/|categories}{-listjoin|/|categories}?{-join|&|updated-min,updated-max,alt,start-index,max-results,entryID,orderby}";
+    String t = "http://www.google.com/notebook/feeds/{userID}{-prefix|/notebooks/|notebookID}{-opt|/-/|categories}{-list|/|categories}?{-join|&|updated-min,updated-max,alt,start-index,max-results,entryID,orderby}";
     String e = "http://www.google.com/notebook/feeds/a/notebooks/b?updated-min=c&max-results=d";
     HashMapContext c = new HashMapContext();
     c.put("userID", "a");
@@ -204,7 +204,7 @@
   
   @Test
   public void test18() throws Exception {
-    String t = "http://www.google.com/notebook/feeds/{userID}{-prefix|/notebooks/|notebookID}{-opt|/-/|categories}{-listjoin|/|categories}?{-join|&|updated-min,updated-max,alt,start-index,max-results,entryID,orderby}";
+    String t = "http://www.google.com/notebook/feeds/{userID}{-prefix|/notebooks/|notebookID}{-opt|/-/|categories}{-list|/|categories}?{-join|&|updated-min,updated-max,alt,start-index,max-results,entryID,orderby}";
     Template template = new Template(t);
     String[] variables = template.getVariables();
     assertEquals(variables[0],"userID");
@@ -221,7 +221,7 @@
 
   @Test
   public void test19() throws Exception {
-    String t = "http://www.google.com/notebook/feeds/{userID}{-prefix|/notebooks/|notebookID}{-opt|/-/|categories}{-listjoin|/|categories}?{-join|&|updated-min,updated-max,alt,start-index,max-results,entryID,orderby}";
+    String t = "http://www.google.com/notebook/feeds/{userID}{-prefix|/notebooks/|notebookID}{-opt|/-/|categories}{-list|/|categories}?{-join|&|updated-min,updated-max,alt,start-index,max-results,entryID,orderby}";
     Template template = new Template(t);
     Template t2 = template.clone();
     assertEquals(template,t2);
@@ -253,18 +253,18 @@
     tests.put("http://example.org/{foo=%25}/","http://example.org/%25/");
     tests.put("/{-prefix|#|foo}","/");
     tests.put("./{-prefix|#|str0}","./");
-    tests.put("/{-append|/|a}{-opt|data|points}{-neg|@|a}{-prefix|#|b}","/foo/data#bar");
+    tests.put("/{-suffix|/|a}{-opt|data|points}{-neg|@|a}{-prefix|#|b}","/foo/data#bar");
     tests.put("http://example.org/q={u}","http://example.org/q=%E2%99%94%E2%99%95");
     tests.put("http://example.org/?{-join|&|a,data}","http://example.org/?a=foo&data=10%2C20%2C30");
-    tests.put("http://example.org/?d={-listjoin|,|points}&{-join|&|a,b}","http://example.org/?d=10,20,30&a=foo&b=bar");
-    tests.put("http://example.org/?d={-listjoin|,|list0}&{-join|&|foo}","http://example.org/?d=&");
-    tests.put("http://example.org/?d={-listjoin|&d=|points}","http://example.org/?d=10&d=20&d=30");
+    tests.put("http://example.org/?d={-list|,|points}&{-join|&|a,b}","http://example.org/?d=10,20,30&a=foo&b=bar");
+    tests.put("http://example.org/?d={-list|,|list0}&{-join|&|foo}","http://example.org/?d=&");
+    tests.put("http://example.org/?d={-list|&d=|points}","http://example.org/?d=10&d=20&d=30");
     tests.put("http://example.org/{a}{b}/{a_b}","http://example.org/foobar/baz");
     tests.put("http://example.org/{a}{-prefix|/-/|a}/","http://example.org/foo/-/foo/");
     tests.put("{bytes}","%61%62%63");
     tests.put("{stream}","%61%62%63");
     tests.put("{chars}","abc%2F");
-    tests.put("{ints}","123");
+    tests.put("{ints}","1%2C2%2C3");
     
     for (String t : tests.keySet())
       assertEquals(Template.expand(t,map),tests.get(t));
@@ -272,7 +272,7 @@
   
   @Test
   public void test21() throws Exception {
-    String t = "http://example.org/{foo}/{bar}{-opt|/|categories}{-listjoin|/|categories}?{-join|&|baz,tag}";
+    String t = "http://example.org/{foo}/{bar}{-opt|/|categories}{-list|/|categories}?{-join|&|baz,tag}";
     String e = "http://example.org/abc/xyz/a/b?baz=true&tag=x&tag=y&tag=z";
     assertEquals(Template.expand(t,new MyObject()),e);
   }
@@ -283,7 +283,99 @@
     assertEquals(Template.expandAnnotated(new MyObject()),e);
   }
   
-  @URITemplate("http://example.org/{foo}/{bar}{-opt|/|categories}{-listjoin|/|categories}?{-join|&|baz,tag}")
+  @Test
+  public void test23() throws Exception {
+    Map<String,Object> map = new HashMap<String,Object>();
+    
+    map.put("foo", "\u03d3");
+    map.put("bar", "fred");
+    map.put("baz", new int[] {10,20,30});
+    map.put("qux", new String[] {"10","20","30"});
+    map.put("corge", new String[0]);
+    map.put("garply", "a/b/c");
+    map.put("grault", "");
+    map.put("waldo", "ben & jerrys");
+    map.put("fred", new String[] {"fred", "", "wilma"});
+    map.put("plugh", new String[] {"\u017F\u0307", "\u0073\u0307"});
+    map.put("1-a_b.c", 200);
+
+    String[] templates = {
+      "http://example.org/?q={bar}",
+      "http://example.org/?q=fred",
+
+      "/{xyzzy}",
+      "/",
+
+      "http://example.org/?{-join|&|foo,bar,xyzzy,baz}",
+      "http://example.org/?foo=%CE%8E&bar=fred&baz=10%2C20%2C30",
+
+      "http://example.org/?d={-list|,|qux}",
+      "http://example.org/?d=10,20,30",
+
+      "http://example.org/?d={-list|&d=|qux}",
+      "http://example.org/?d=10&d=20&d=30",
+
+      "http://example.org/{bar}{bar}/{garply}",
+      "http://example.org/fredfred/a%2Fb%2Fc",
+
+      "http://example.org/{bar}{-prefix|/|fred}",
+      "http://example.org/fred/fred//wilma",
+
+      "{-neg|:|corge}{-suffix|:|plugh}",
+      ":%E1%B9%A1:%E1%B9%A1:",
+
+      "../{waldo}/",
+      "../ben%20%26%20jerrys/",
+
+      "telnet:192.0.2.16{-opt|:80|grault}",
+      "telnet:192.0.2.16:80",
+
+      ":{1-a_b.c}:",
+      ":200:"
+    };
+    
+    for (int n = 0; n < templates.length; n = n + 2) {
+      assertEquals(Template.expand(templates[n], map), templates[n+1]);
+    }
+  }
+  
+  @Test 
+  public void test24() throws Exception {
+    Map<String,Object> map = new HashMap<String,Object>();
+    map.put("foo", "fred");
+    map.put("bar", new String[] {"fee","fi","fo","fum"});
+    map.put("baz", new String[0]);
+    String[] templates = {
+      "{-suffix|/|foo}","fred/",
+      "{-suffix|/|bar}","fee/fi/fo/fum/",
+      "{-suffix|/|baz}","",
+      "{-suffix|/|qux}",""
+    };
+      
+      for (int n = 0; n < templates.length; n = n + 2) {
+        assertEquals(Template.expand(templates[n], map), templates[n+1]);
+      }
+  }
+    
+  @Test 
+  public void test25() throws Exception {
+    Map<String,Object> map = new HashMap<String,Object>();
+    map.put("foo", "fred");
+    map.put("bar", new String[] {"fee","fi","fo","fum"});
+    map.put("baz", new String[0]);
+    String[] templates = {
+      "{-prefix|/|foo}","/fred",
+      "{-prefix|/|bar}","/fee/fi/fo/fum",
+      "{-prefix|/|baz}","",
+      "{-prefix|/|qux}",""
+    };
+      
+      for (int n = 0; n < templates.length; n = n + 2) {
+        assertEquals(Template.expand(templates[n], map), templates[n+1]);
+      }
+  }
+  
+  @URITemplate("http://example.org/{foo}/{bar}{-opt|/|categories}{-list|/|categories}?{-join|&|baz,tag}")
   public static class MyObject {
     public String foo = "abc";
     public String getBar() {



Mime
View raw message