Return-Path: Delivered-To: apmail-incubator-abdera-dev-archive@locus.apache.org Received: (qmail 23550 invoked from network); 26 Nov 2007 20:32:19 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 26 Nov 2007 20:32:19 -0000 Received: (qmail 24148 invoked by uid 500); 26 Nov 2007 20:32:06 -0000 Delivered-To: apmail-incubator-abdera-dev-archive@incubator.apache.org Received: (qmail 24120 invoked by uid 500); 26 Nov 2007 20:32:06 -0000 Mailing-List: contact abdera-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: abdera-dev@incubator.apache.org Delivered-To: mailing list abdera-dev@incubator.apache.org Received: (qmail 24111 invoked by uid 99); 26 Nov 2007 20:32:06 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 26 Nov 2007 12:32:06 -0800 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: local policy) Received: from [63.246.7.16] (HELO mail.mulesource.com) (63.246.7.16) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 26 Nov 2007 20:32:06 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by mail.mulesource.com (Postfix) with ESMTP id 844D628310D2 for ; Mon, 26 Nov 2007 14:31:45 -0600 (CST) X-Virus-Scanned: amavisd-new at X-Spam-Score: -2.513 X-Spam-Level: Received: from mail.mulesource.com ([127.0.0.1]) by localhost (mail.mulesource.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 6PhRKS-SKF61 for ; Mon, 26 Nov 2007 14:31:43 -0600 (CST) Received: from [75.218.227.4] (4.sub-75-218-227.myvzw.com [75.218.227.4]) by mail.mulesource.com (Postfix) with ESMTP id 224272831019 for ; Mon, 26 Nov 2007 14:31:41 -0600 (CST) Message-ID: <474B2D06.6050905@mulesource.com> Date: Mon, 26 Nov 2007 15:31:02 -0500 From: Dan Diephouse User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 To: abdera-dev@incubator.apache.org Subject: Re: URI/IRI Templates References: <474B1CFB.6060109@gmail.com> In-Reply-To: <474B1CFB.6060109@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Checked: Checked by ClamAV on apache.org X-Old-Spam-Status: No, score=-2.513 tagged_above=-10 required=6.6 tests=[AWL=0.086, BAYES_00=-2.599] Awesome - this rocks and will be eminently useful! - Dan James M Snell wrote: > I've added support for URI/IRI Templates to the IRI module based on > http://bitworking.org/projects/URI-Templates/draft-gregorio-uritemplate-02.html > > Examples: > > Template instances are immutable and threadsafe. > > private static final Template template = > new > Template("http://example.org{-opt|/~|user}{user}{-opt|/-/|categories}{-listjoin|/|categories}{-opt|?|foo,bar}{-join|&|foo,bar}"); > > > Templates can be resolved from a Map, from Java object getters and > fields, or from a custom Context implementation > > Map map = new HashMap(); > map.put("user","james"); > map.put("categories", new String[] {"a","b","c"}); > map.put("foo", "abc"); > map.put("bar", "xyz"); > System.out.println(template.expand(map)); > > > http://example.org/~james/-/a/b/c?foo=abc&bar=xyz > > > The Java object approach will examine the public fields and getters of > passed in java objects. > > public static class MyObject { > public String user = "james"; > public List getCategories() { > List list = > new ArrayList(); > list.add("a"); > list.add("b"); > list.add("c"); > return list; > } > public Foo[] getFoo() { > return new Foo[] { > new Foo(), > new Foo() > }; > } > public String getBar() { > return "xyz"; > } > } > > private static class Foo { > public String toString() { return "abcæ"; } > } > > MyObject myObject = new MyObject(); > System.out.println(template.expand(myObject,true)); > > > http://example.org/~james/-/a/b/c?foo=abc%C3%A6&foo=abc%C3%A6&bar=xyz > > > The custom Context implementation approach allows template variables to > be resolved dynamically, > > CachingContext context = new CachingContext() { > private static final long serialVersionUID = 4896250661828139020L; > protected T resolveActual(String var) { > if (var.equals("user")) return (T)"james"; > else if (var.equals("categories")) > return (T)new String[] {"a","b","c"}; > else if (var.equals("foo")) return (T)"abc"; > else if (var.equals("bar")) return (T)"xyz"; > else return null; > } > public Iterator iterator() { > return Arrays.asList( > new String[] {"user","categories","foo","bar"}).iterator(); > } > }; > System.out.println(template.expand(context)); > > > http://example.org/~james/-/a/b/c?foo=abc&bar=xyz > > The implementation will continue to evolve as the URI Template spec evolves. > > - James > -- Dan Diephouse MuleSource http://mulesource.com | http://netzooid.com/blog