abdera-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James M Snell <jasn...@gmail.com>
Subject Annotations
Date Mon, 06 Aug 2007 04:21:43 GMT
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

Mime
View raw message