You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2007/11/27 00:32:08 UTC

svn commit: r598463 - in /incubator/abdera/java/trunk/dependencies/i18n/src: main/java/org/apache/abdera/i18n/templates/Template.java main/java/org/apache/abdera/i18n/templates/URITemplate.java test/java/org/apache/abdera/i18n/test/iri/TestTemplate.java

Author: jmsnell
Date: Mon Nov 26 15:32:00 2007
New Revision: 598463

URL: http://svn.apache.org/viewvc?rev=598463&view=rev
Log:
Provide an annotation that makes it possible to associate a URI Template with a Class. Template can
then expand those objects into a URI based on it's public fields and getters

Added:
    incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/URITemplate.java
Modified:
    incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Template.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/Template.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Template.java?rev=598463&r1=598462&r2=598463&view=diff
==============================================================================
--- incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Template.java (original)
+++ incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Template.java Mon Nov 26 15:32:00 2007
@@ -263,6 +263,7 @@
   }
   
   public static String expand(String pattern, Context context) {
+    if (context == null || pattern == null) throw new IllegalArgumentException();
     Template template = new Template(pattern);
     return template.expand(context);
   }
@@ -272,8 +273,24 @@
   }
   
   public static String expand(String pattern, Object object, boolean isiri) {
+    if (object == null || pattern == null) throw new IllegalArgumentException();
     Template template = new Template(pattern);
     return template.expand(object,isiri);
+  }
+  
+  /**
+   * Use an Object annotated with the URITemplate annotation to expand a
+   * template
+   */
+  public static String expandAnnotated(Object object) {
+    if (object == null) throw new IllegalArgumentException();
+    Class _class = object.getClass();
+    URITemplate uritemplate = (URITemplate) _class.getAnnotation(URITemplate.class);
+    if (uritemplate != null) {
+      return expand(uritemplate.value(),object,uritemplate.isiri());
+    } else {
+      throw new IllegalArgumentException("No URI Template provided");
+    }
   }
   
   public static String explain(String pattern) {

Added: incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/URITemplate.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/URITemplate.java?rev=598463&view=auto
==============================================================================
--- incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/URITemplate.java (added)
+++ incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/URITemplate.java Mon Nov 26 15:32:00 2007
@@ -0,0 +1,31 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.i18n.templates;
+
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+@Retention(RUNTIME)
+@Target(TYPE)
+public @interface URITemplate {
+  String value();
+  boolean isiri() default false;
+}

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=598463&r1=598462&r2=598463&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 Mon Nov 26 15:32:00 2007
@@ -26,6 +26,7 @@
 
 import org.apache.abdera.i18n.templates.HashMapContext;
 import org.apache.abdera.i18n.templates.Template;
+import org.apache.abdera.i18n.templates.URITemplate;
 
 public final class TestTemplate 
   extends TestCase {
@@ -246,7 +247,13 @@
     String e = "http://example.org/abc/xyz/a/b?baz=true&tag=x&tag=y&tag=z";
     assertEquals(Template.expand(t,new MyObject()),e);
   }
+
+  public static void test22() throws Exception {
+    String e = "http://example.org/abc/xyz/a/b?baz=true&tag=x&tag=y&tag=z";
+    assertEquals(Template.expandAnnotated(new MyObject()),e);
+  }
   
+  @URITemplate("http://example.org/{foo}/{bar}{-opt|/|categories}{-listjoin|/|categories}?{-join|&|baz,tag}")
   public static class MyObject {
     public String foo = "abc";
     public String getBar() {