com.rexsl.page
Class JaxbBundle

java.lang.Object
  extended by com.rexsl.page.JaxbBundle

public final class JaxbBundle
extends Object

JAXB bundle.

It's a convenient instrument that enables on-fly creation of DOM/XML structures, for example (fluent interface):

 final org.w3c.dom.Element elm = new JaxbBundle("root")
   .add("employee")
     .attr("age", "28")
     .add("dept", "Software")
       .attr("country", "DE")
     .up()
     .add("salary", "> \u20AC 50,000")
     .up()
     .add("rank", "high")
   .up()
   .attr("time", new Date())
   .element();
 

If you convert this elm to XML this is how it will look:

 <?xml version="1.0" ?>
 <root time="Sun Jul 20 16:17:00 EDT 1969">
   <employee age="28">
     <dept country="DE">Software</dept>
     <salary>&gt; € 50,000</salary>
     <rank>high</rank>
   </employee>
 </root>
 

Then, you can add this Element to your JAXB object, and return it from a method annotated with XmlElement, for example:

 @XmlRootElement
 public class Page {
   @XmlElement
   public Object getEmployee() {
     return new JaxbBundle("employee")
       .attr("age", "35")
       .attr("country", "DE")
       .add("salary", "> \u20AC 50,000")
       .up()
       .element();
   }
 }
 

This mechanism, very often, is much more convenient and shorter than a declaration of a new POJO every time you need to return a small piece of XML data.

The class is mutable and thread-safe.

Since:
0.3.7
Version:
$Id: JaxbBundle.java 1758 2012-05-28 14:51:16Z guard $
Author:
Yegor Bugayenko (yegor@rexsl.com)

Constructor Summary
JaxbBundle()
          Default ctor, for JAXB (always throws a runtime exception).
JaxbBundle(String nam)
          Public ctor, with just a name of XML element an no content.
JaxbBundle(String nam, Object text)
          Public ctor, with XML element name and its content.
 
Method Summary
 JaxbBundle add(String nam)
          Add new child XML element.
 JaxbBundle add(String nam, Object txt)
          Add new child with text value.
 JaxbBundle attr(String nam, Object val)
          Add XML attribute to this bundle.
 Element element()
          Convert this bundle into DOM/XML Element.
 JaxbBundle up()
          Return parent bundle.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JaxbBundle

public JaxbBundle()
Default ctor, for JAXB (always throws a runtime exception).


JaxbBundle

public JaxbBundle(@NotNull
                  String nam)
Public ctor, with just a name of XML element an no content.

Parameters:
nam - The name of it

JaxbBundle

public JaxbBundle(@NotNull
                  String nam,
                  Object text)
Public ctor, with XML element name and its content.

Parameters:
nam - The name of XML element
text - Plain text content
Method Detail

add

public JaxbBundle add(@NotNull
                      String nam)
Add new child XML element.

Parameters:
nam - The name of child element
Returns:
The child bundle (use up() on it in order to get back to this object)

add

public JaxbBundle add(@NotNull
                      String nam,
                      @NotNull
                      Object txt)
Add new child with text value.

Parameters:
nam - The name of child
txt - The text
Returns:
The child bundle (use up() on it in order to get back to this object)

attr

public JaxbBundle attr(@NotNull
                       String nam,
                       @NotNull
                       Object val)
Add XML attribute to this bundle.

Parameters:
nam - The name of attribute
val - The plain text value
Returns:
This object

up

public JaxbBundle up()
Return parent bundle.

Returns:
The parent bundle
Suppressed checkstyle violations:
MethodName (3 lines)

element

public Element element()
Convert this bundle into DOM/XML Element.

Returns:
The element


Copyright © 2011-2012 ReXSL.com. All Rights Reserved.