You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@abdera.apache.org by Neale <ne...@nealeupstone.com> on 2007/10/30 02:46:21 UTC

Adding more elements within an extension

Hi,

I'm making use of extensions to put some of our own data into entries 
and feeds.

What I'd ideally want to be able to do is something like:

    MyListElement list = new MyListElement( "listName" );  // extends 
Element
    list.addValue("one");
    list.addValue("two");
    entry.addExtension( list );

For this to generate:

    <myns:mylistelement name="listName">
      <value>one</value>
      <value>two</value>
    </mylistelement>

Looking at the code for FOMEntry, the above looks like a rather onerous 
task.

Instead, I can use the existing code to do:

    Element e = entry.addExtension(MYLIST_QNAME);
    e.setAttributeValue("name", "listName");
   
    And what I now want is:
    Element value1 = e.createElement(VALUE_QNAME);
    value1.setText("one");

This doesn't seem possible.. what should I do?

Cheers,

Neale


Re: Adding more elements within an extension

Posted by Neale <ne...@nealeupstone.com>.
Thanks James.

I see where I was missing stuff now.  The <T extends Element> return 
from addExtension() was what threw me.  It might be worth changing some 
of the examples to show ExtensibleElement being used.

And... thanks for the wrapped approach to doing it.  That's what I like 
to see.... something that will allow people looking at code to not have 
to worry about it being XML.

All the best,

Neale

PS. I'll let you know about the project this is for, when we launch... 
for now it's a bit hush.




James M Snell wrote:
> You can do the following:
>
>   ExtensibleElement e = entry.addExtension(MYLIST_QNAME);
>   e.setAttributeValue("name", "listName");
>   e.addSimpleExtension(VALUE_QNAME,"one");
>   e.addSimpleExtension(VALUE_QNAME,"two");
>
>
> Or you can implement a static extension.  Implement a class that extends
> the ExtensibleElementWrapper class.  Implement an ExtensionFactory for
> it. Register the ExtensionFactory with Abdera. Code below.
>
> foo/MyListElement.java
> ==================
>   package foo;
>   import javax.xml.namespace.QName;
>
>   import org.apache.abdera.factory.Factory;
>   import org.apache.abdera.model.Element;
>   import org.apache.abdera.model.ExtensibleElementWrapper;
>
>   public class MyListElement
>     extends ExtensibleElementWrapper {
>     public static final String NS = "http://example.org";
>     public static final QName MYLIST =
>       new QName("http://example.org","mylist");
>     public static final QName VALUE =
>       new QName("http://example.org","value");
>     public MyListElement(Element internal) {
>       super(internal);
>     }
>     public MyListElement(Factory factory, QName qname) {
>       super(factory, qname);
>     }
>     public void setName(String name) {
>       setAttributeValue("listName",name);
>     }
>     public void addValue(String value) {
>       addSimpleExtension(VALUE, value);
>     }
>   }
>
> foo/MyListExtensionFactory.java
> ===========================
>   package foo;
>   import org.apache.abdera.util.AbstractExtensionFactory;
>
>   public class MyListExtensionFactory
>     extends AbstractExtensionFactory {
>     public MyListExtensionFactory() {
>       super(MyListElement.NS);
>       addImpl(MyListElement.MYLIST,MyListElement.class);
>     }
>   }
>
> META-INF/services/org.apache.abdera.factory.ExtensionFactory
> =============================================================
>   foo.MyListExtensionFactory
>
>
> To use it:
> ==========
>   MyListElement list = entry.addExtension(MyListElement.MYLIST);
>   list.setName("listName");
>   list.addValue("one");
>   list.addValue("two");
>
> Enjoy!
>
> - James
>
> Neale wrote:
>   
>> Hi,
>>
>> I'm making use of extensions to put some of our own data into entries
>> and feeds.
>>
>> What I'd ideally want to be able to do is something like:
>>
>>    MyListElement list = new MyListElement( "listName" );  // extends
>> Element
>>    list.addValue("one");
>>    list.addValue("two");
>>    entry.addExtension( list );
>>
>> For this to generate:
>>
>>    <myns:mylistelement name="listName">
>>      <value>one</value>
>>      <value>two</value>
>>    </mylistelement>
>>
>> Looking at the code for FOMEntry, the above looks like a rather onerous
>> task.
>>
>> Instead, I can use the existing code to do:
>>
>>    Element e = entry.addExtension(MYLIST_QNAME);
>>    e.setAttributeValue("name", "listName");
>>      And what I now want is:
>>    Element value1 = e.createElement(VALUE_QNAME);
>>    value1.setText("one");
>>
>> This doesn't seem possible.. what should I do?
>>
>> Cheers,
>>
>> Neale
>>
>>
>>     

Re: Adding more elements within an extension

Posted by James M Snell <ja...@gmail.com>.
You can do the following:

  ExtensibleElement e = entry.addExtension(MYLIST_QNAME);
  e.setAttributeValue("name", "listName");
  e.addSimpleExtension(VALUE_QNAME,"one");
  e.addSimpleExtension(VALUE_QNAME,"two");


Or you can implement a static extension.  Implement a class that extends
the ExtensibleElementWrapper class.  Implement an ExtensionFactory for
it. Register the ExtensionFactory with Abdera. Code below.

foo/MyListElement.java
==================
  package foo;
  import javax.xml.namespace.QName;

  import org.apache.abdera.factory.Factory;
  import org.apache.abdera.model.Element;
  import org.apache.abdera.model.ExtensibleElementWrapper;

  public class MyListElement
    extends ExtensibleElementWrapper {
    public static final String NS = "http://example.org";
    public static final QName MYLIST =
      new QName("http://example.org","mylist");
    public static final QName VALUE =
      new QName("http://example.org","value");
    public MyListElement(Element internal) {
      super(internal);
    }
    public MyListElement(Factory factory, QName qname) {
      super(factory, qname);
    }
    public void setName(String name) {
      setAttributeValue("listName",name);
    }
    public void addValue(String value) {
      addSimpleExtension(VALUE, value);
    }
  }

foo/MyListExtensionFactory.java
===========================
  package foo;
  import org.apache.abdera.util.AbstractExtensionFactory;

  public class MyListExtensionFactory
    extends AbstractExtensionFactory {
    public MyListExtensionFactory() {
      super(MyListElement.NS);
      addImpl(MyListElement.MYLIST,MyListElement.class);
    }
  }

META-INF/services/org.apache.abdera.factory.ExtensionFactory
=============================================================
  foo.MyListExtensionFactory


To use it:
==========
  MyListElement list = entry.addExtension(MyListElement.MYLIST);
  list.setName("listName");
  list.addValue("one");
  list.addValue("two");

Enjoy!

- James

Neale wrote:
> Hi,
> 
> I'm making use of extensions to put some of our own data into entries
> and feeds.
> 
> What I'd ideally want to be able to do is something like:
> 
>    MyListElement list = new MyListElement( "listName" );  // extends
> Element
>    list.addValue("one");
>    list.addValue("two");
>    entry.addExtension( list );
> 
> For this to generate:
> 
>    <myns:mylistelement name="listName">
>      <value>one</value>
>      <value>two</value>
>    </mylistelement>
> 
> Looking at the code for FOMEntry, the above looks like a rather onerous
> task.
> 
> Instead, I can use the existing code to do:
> 
>    Element e = entry.addExtension(MYLIST_QNAME);
>    e.setAttributeValue("name", "listName");
>      And what I now want is:
>    Element value1 = e.createElement(VALUE_QNAME);
>    value1.setText("one");
> 
> This doesn't seem possible.. what should I do?
> 
> Cheers,
> 
> Neale
> 
>