@Loggable(value=1) public final class JaxbBundle extends Object
It's a convenient instrument that enables on-fly creation of DOM/XML structures, for example (fluent interface):
Element element = 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 element 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>> € 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.
Since version 0.4.10 it's possible to add links, groups, and other bundles. For example:
final Element element = new JaxbBundle("garage")
.link(new Link("add", "/add-car"))
.add("cars").add(
new JaxbBundle.Group<String>(cars) {
@Override
public JaxbBundle bundle(Car car) {
return new JaxbBundle("car").add("make", car.make()).up();
}
}
).up()
.add(new JaxbBundle("owner").add("email", "...").up())
.element();
The class is mutable and thread-safe.
| Modifier and Type | Class and Description |
|---|---|
static class |
JaxbBundle.Group<T>
Group.
|
| Constructor and Description |
|---|
JaxbBundle()
Default ctor, for JAXB (always throws a runtime exception).
|
JaxbBundle(String nam)
Public ctor, with just a name of XML element and no text content.
|
JaxbBundle(String nam,
String text)
Public ctor, with XML element name and its content.
|
| Modifier and Type | Method and Description |
|---|---|
JaxbBundle |
add(Element element)
Add new child XML element.
|
JaxbBundle |
add(JaxbBundle.Group<?> group)
Add new group.
|
JaxbBundle |
add(JaxbBundle bundle)
Add new bundle.
|
JaxbBundle |
add(String nam)
Add new child XML element.
|
JaxbBundle |
add(String nam,
String txt)
Add new child with text value.
|
JaxbBundle |
attr(String nam,
String val)
Add XML attribute to this bundle.
|
Element |
element()
Convert this bundle into DOM/XML
Element. |
JaxbBundle |
link(Link link)
Add new link into
<links> section. |
JaxbBundle |
up()
Return parent bundle.
|
public JaxbBundle()
You're not supposed to use this constructor. Instead, use either
JaxbBundle(String) or JaxbBundle(String,String).
public JaxbBundle(@NotNull
String nam)
nam - The name of XML element@NotNull public JaxbBundle add(@NotNull String nam)
nam - The name of child elementup() on it in order to get back to
the parent bundle)@NotNull public JaxbBundle add(@NotNull Element element)
element - The DOM element to add@NotNull public JaxbBundle add(@NotNull String nam, @NotNull String txt)
nam - The name of childtxt - The textup() on it in order to get back to
the parent bundle)@NotNull public JaxbBundle attr(@NotNull String nam, @NotNull String val)
nam - The name of attributeval - The plain text value@NotNull public JaxbBundle link(@NotNull Link link)
<links> section.link - The link to attach@NotNull public JaxbBundle add(@NotNull JaxbBundle bundle)
bundle - The bundle to add@NotNull public JaxbBundle add(@NotNull JaxbBundle.Group<?> group)
group - The group@NotNull public JaxbBundle up()
Copyright © 2011–2014 ReXSL.com. All rights reserved.