You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@abdera.apache.org by James M Snell <ja...@gmail.com> on 2007/08/06 06:21:43 UTC

Annotations

Ok, so I have a basic preliminary implementation of an annotation-driven
Java-to-Atom converter.

Here's how it works:

Let's suppose that I have a java class.

  class Foo {}

I want to serialize this as an Atom Entry document

  @AtomEntry class Foo {}

I want to specify that the xml:lang for the feed is "en-US"

  @AtomEntry @Lang("en-US") class Foo {}

Now I want to add an atom:id, atom:title, atom:updated and atom:author
to the entry

  @AtomEntry @Lang("en-US") class Foo {

    @AtomID String getId() { ... }

    @AtomTitle ( Text.Type.TEXT ) String getTitle() { ... }

    @AtomUpdated Date getUpdated() { ... }

    @AtomAuthor FooPerson getAuthor() { ... }

  }

I want to have FooPerson automatically converted into an atom:author
object, so I need to annotate it also.

  @AtomPerson @AtomAuthor class FooPerson {

    @Value String getName() { ... }

    @Email String getEmail() { ... }

  }

Now I want to add a list of categories to Foo:

  @AtomEntry @Lang("en-US") class Foo {

    @AtomID String getId() { ... }

    @AtomTitle ( Text.Type.TEXT ) String getTitle() { ... }

    @AtomAuthor FooPerson getAuthor() { ... }

    @List(AtomCategory.class) List<FooTag> getCategories() { ... }
  }

Each FooTag needs to be annotated

  @AtomCategory (scheme="http://example.org/foo" ) class FooTag { ... }

Now I want to add XHTML content to the entry and a single simple extension.

  @AtomEntry @Lang("en-US") class Foo {

    @AtomID String getId() { ... }

    @AtomTitle ( Text.Type.TEXT ) String getTitle() { ... }

    @AtomAuthor FooPerson getAuthor() { ... }

    @List(AtomCategory.class) List<FooTag> getCategories() { ... }

    @AtomContent ( Content.Type.XHTML ) String getContent() { ... }

    @Extension (uri="http://example.org/foo",
                localname="foo", prefix="b") String getFoo() { ... }
  }

Now that I've annotated my classes, I can create an instance and convert
those to Atom...

    ConversionContext context = new ConversionContext();
    DefaultConverter conv = new DefaultConverter();
    Foo foo = new Foo();
    // set foo properties
    Entry entry = conv.convert(test,context);
    System.out.println(entry);

Now, suppose I want to have more control over how some object is
converted.  I can use a CustomConverter annotation, either on the method
or the class, e.g.,

    @AtomAuthor @CustomConverter(MyConverter.class)
    FooPerson getAuthor();

Now, FooPerson will be passed to MyConverter rather than the
DefaultConverter.

The implementation is functional but is far from complete.  I'm also not
happy with how complicated the internal implementation details are.  It
seems way more complex that it really should be.  However, it does work
reasonably well.

I can drop this into the contrib module if folks want to play around
with it, or I can just upload a zip to my personal site for folks to
play around with.

- James

Re: Annotations

Posted by Dan Diephouse <da...@envoisolutions.com>.
On 8/7/07, James M Snell <ja...@gmail.com> wrote:
>
>
> > Along these lines I was wondering if it was possible to leverage
> existing
> > annotations from JAXB & JPA for those who have using those technologies.
> We
> > could automatically create workspaces/collections/entries based on the
> JPA
> > relationships. Then we could use JAXB to do the serialization. Not sure
> yet
> > if thats a stupid idea... Thoughts?
> >
>
> Hmm.. I'd rather limit the number of external things we'd be depending
> on... Abdera already has way too many external deps.  What would be
> possible, however, is to provide some way of extending the default
> converter so that these additional things can be handled.


Yeah, I didn't mean to suggest it should be included in the core. Just could
be an interesting add-on...

- Dan


-- 
Dan Diephouse
Envoi Solutions
http://envoisolutions.com | http://netzooid.com/blog

Re: Annotations

Posted by James M Snell <ja...@gmail.com>.

Dan Diephouse wrote:
> Very cool James.
> 
> One suggestion: conventions! I.e. the default ID should be getId(). The
> default Author is getAuthor(). etc etc.
> 

Yep. This could be done easily.

> Along these lines I was wondering if it was possible to leverage existing
> annotations from JAXB & JPA for those who have using those technologies. We
> could automatically create workspaces/collections/entries based on the JPA
> relationships. Then we could use JAXB to do the serialization. Not sure yet
> if thats a stupid idea... Thoughts?
> 

Hmm.. I'd rather limit the number of external things we'd be depending
on... Abdera already has way too many external deps.  What would be
possible, however, is to provide some way of extending the default
converter so that these additional things can be handled.

- James

> - Dan
> 
> On 8/6/07, James M Snell <ja...@gmail.com> wrote:
>> Ok, so I have a basic preliminary implementation of an annotation-driven
>> Java-to-Atom converter.
>>
>> Here's how it works:
>>
>> Let's suppose that I have a java class.
>>
>>   class Foo {}
>>
>> I want to serialize this as an Atom Entry document
>>
>>   @AtomEntry class Foo {}
>>
>> I want to specify that the xml:lang for the feed is "en-US"
>>
>>   @AtomEntry @Lang("en-US") class Foo {}
>>
>> Now I want to add an atom:id, atom:title, atom:updated and atom:author
>> to the entry
>>
>>   @AtomEntry @Lang("en-US") class Foo {
>>
>>     @AtomID String getId() { ... }
>>
>>     @AtomTitle ( Text.Type.TEXT ) String getTitle() { ... }
>>
>>     @AtomUpdated Date getUpdated() { ... }
>>
>>     @AtomAuthor FooPerson getAuthor() { ... }
>>
>>   }
>>
>> I want to have FooPerson automatically converted into an atom:author
>> object, so I need to annotate it also.
>>
>>   @AtomPerson @AtomAuthor class FooPerson {
>>
>>     @Value String getName() { ... }
>>
>>     @Email String getEmail() { ... }
>>
>>   }
>>
>> Now I want to add a list of categories to Foo:
>>
>>   @AtomEntry @Lang("en-US") class Foo {
>>
>>     @AtomID String getId() { ... }
>>
>>     @AtomTitle ( Text.Type.TEXT ) String getTitle() { ... }
>>
>>     @AtomAuthor FooPerson getAuthor() { ... }
>>
>>     @List(AtomCategory.class) List<FooTag> getCategories() { ... }
>>   }
>>
>> Each FooTag needs to be annotated
>>
>>   @AtomCategory (scheme="http://example.org/foo" ) class FooTag { ... }
>>
>> Now I want to add XHTML content to the entry and a single simple
>> extension.
>>
>>   @AtomEntry @Lang("en-US") class Foo {
>>
>>     @AtomID String getId() { ... }
>>
>>     @AtomTitle ( Text.Type.TEXT ) String getTitle() { ... }
>>
>>     @AtomAuthor FooPerson getAuthor() { ... }
>>
>>     @List(AtomCategory.class) List<FooTag> getCategories() { ... }
>>
>>     @AtomContent ( Content.Type.XHTML ) String getContent() { ... }
>>
>>     @Extension (uri="http://example.org/foo",
>>                 localname="foo", prefix="b") String getFoo() { ... }
>>   }
>>
>> Now that I've annotated my classes, I can create an instance and convert
>> those to Atom...
>>
>>     ConversionContext context = new ConversionContext();
>>     DefaultConverter conv = new DefaultConverter();
>>     Foo foo = new Foo();
>>     // set foo properties
>>     Entry entry = conv.convert(test,context);
>>     System.out.println(entry);
>>
>> Now, suppose I want to have more control over how some object is
>> converted.  I can use a CustomConverter annotation, either on the method
>> or the class, e.g.,
>>
>>     @AtomAuthor @CustomConverter(MyConverter.class)
>>     FooPerson getAuthor();
>>
>> Now, FooPerson will be passed to MyConverter rather than the
>> DefaultConverter.
>>
>> The implementation is functional but is far from complete.  I'm also not
>> happy with how complicated the internal implementation details are.  It
>> seems way more complex that it really should be.  However, it does work
>> reasonably well.
>>
>> I can drop this into the contrib module if folks want to play around
>> with it, or I can just upload a zip to my personal site for folks to
>> play around with.
>>
>> - James
>>
> 
> 
> 

Re: Annotations

Posted by Dan Diephouse <da...@envoisolutions.com>.
Very cool James.

One suggestion: conventions! I.e. the default ID should be getId(). The
default Author is getAuthor(). etc etc.

Along these lines I was wondering if it was possible to leverage existing
annotations from JAXB & JPA for those who have using those technologies. We
could automatically create workspaces/collections/entries based on the JPA
relationships. Then we could use JAXB to do the serialization. Not sure yet
if thats a stupid idea... Thoughts?

- Dan

On 8/6/07, James M Snell <ja...@gmail.com> wrote:
>
> Ok, so I have a basic preliminary implementation of an annotation-driven
> Java-to-Atom converter.
>
> Here's how it works:
>
> Let's suppose that I have a java class.
>
>   class Foo {}
>
> I want to serialize this as an Atom Entry document
>
>   @AtomEntry class Foo {}
>
> I want to specify that the xml:lang for the feed is "en-US"
>
>   @AtomEntry @Lang("en-US") class Foo {}
>
> Now I want to add an atom:id, atom:title, atom:updated and atom:author
> to the entry
>
>   @AtomEntry @Lang("en-US") class Foo {
>
>     @AtomID String getId() { ... }
>
>     @AtomTitle ( Text.Type.TEXT ) String getTitle() { ... }
>
>     @AtomUpdated Date getUpdated() { ... }
>
>     @AtomAuthor FooPerson getAuthor() { ... }
>
>   }
>
> I want to have FooPerson automatically converted into an atom:author
> object, so I need to annotate it also.
>
>   @AtomPerson @AtomAuthor class FooPerson {
>
>     @Value String getName() { ... }
>
>     @Email String getEmail() { ... }
>
>   }
>
> Now I want to add a list of categories to Foo:
>
>   @AtomEntry @Lang("en-US") class Foo {
>
>     @AtomID String getId() { ... }
>
>     @AtomTitle ( Text.Type.TEXT ) String getTitle() { ... }
>
>     @AtomAuthor FooPerson getAuthor() { ... }
>
>     @List(AtomCategory.class) List<FooTag> getCategories() { ... }
>   }
>
> Each FooTag needs to be annotated
>
>   @AtomCategory (scheme="http://example.org/foo" ) class FooTag { ... }
>
> Now I want to add XHTML content to the entry and a single simple
> extension.
>
>   @AtomEntry @Lang("en-US") class Foo {
>
>     @AtomID String getId() { ... }
>
>     @AtomTitle ( Text.Type.TEXT ) String getTitle() { ... }
>
>     @AtomAuthor FooPerson getAuthor() { ... }
>
>     @List(AtomCategory.class) List<FooTag> getCategories() { ... }
>
>     @AtomContent ( Content.Type.XHTML ) String getContent() { ... }
>
>     @Extension (uri="http://example.org/foo",
>                 localname="foo", prefix="b") String getFoo() { ... }
>   }
>
> Now that I've annotated my classes, I can create an instance and convert
> those to Atom...
>
>     ConversionContext context = new ConversionContext();
>     DefaultConverter conv = new DefaultConverter();
>     Foo foo = new Foo();
>     // set foo properties
>     Entry entry = conv.convert(test,context);
>     System.out.println(entry);
>
> Now, suppose I want to have more control over how some object is
> converted.  I can use a CustomConverter annotation, either on the method
> or the class, e.g.,
>
>     @AtomAuthor @CustomConverter(MyConverter.class)
>     FooPerson getAuthor();
>
> Now, FooPerson will be passed to MyConverter rather than the
> DefaultConverter.
>
> The implementation is functional but is far from complete.  I'm also not
> happy with how complicated the internal implementation details are.  It
> seems way more complex that it really should be.  However, it does work
> reasonably well.
>
> I can drop this into the contrib module if folks want to play around
> with it, or I can just upload a zip to my personal site for folks to
> play around with.
>
> - James
>



-- 
Dan Diephouse
Envoi Solutions
http://envoisolutions.com | http://netzooid.com/blog