I'd say your assumption is correct.
The reduced code doesn't indicate why you want to use an adapter in the
first place. Most likely there is some other way of achieving your goal.
-W
On Wed, Jul 29, 2009 at 3:58 AM, Pinch, Marnee <Marnee.Pinch_at_pearson.com>wrote:
> I have a class that I am trying to serialize using JAXB with
> annotations. (We are not generating classes by compiling the schema.)
>
>
>
> The class contains the @XmlRootElement and @XmlJavaTypeAdapter
> annotations. In a unit test for my real application, I attempt to serialize
> an instance of the class. The adapter’s marshal method is not being
> invoked.
>
>
>
> I wrote up sample code which I have copied below.
>
>
>
> The API for @XmlJavaTypeAdapter does not list @XmlRootElement as an
> annotation that can be used with @XmlJavaTypeAdapter. I am assuming,
> although I don’t know for sure, that this is the issue. If it is, can
> anyone tell me how to use an adapter with the root element?
>
>
>
> Note that we are running with java 6.
>
> ================================
>
> package somepackage;
>
>
>
> import java.io.ByteArrayOutputStream;
>
>
>
> import javax.xml.bind.JAXBContext;
>
> import javax.xml.bind.JAXBException;
>
> import javax.xml.bind.Marshaller;
>
> import javax.xml.bind.annotation.XmlRootElement;
>
> import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
>
>
>
>
>
> @XmlRootElement(name="node")
>
> @XmlJavaTypeAdapter(TestContentNodeAdapter.class)
>
> public final class TestContentNode {
>
>
>
> private final String
> name; // node name -- may be null
>
>
>
>
>
> private TestContentNode() {
>
> name = null;
>
> }
>
>
>
> public TestContentNode(String name) {
>
> this.name = name;
>
> }
>
> public String getName() {
>
> return name;
>
> }
>
>
>
> public static void main(String[] args) throws JAXBException {
>
> final JAXBContext context =
> JAXBContext.newInstance(TestContentNode.class);
>
> final Marshaller marshaller =
> context.createMarshaller();
>
>
>
> final TestContentNodeAdapter adapter = new
> TestContentNodeAdapter();
>
> marshaller.setAdapter(adapter);
>
> final ByteArrayOutputStream output = new
> ByteArrayOutputStream();
>
>
>
> marshaller.marshal(new TestContentNode("Some
> name"), output);
>
>
>
> if (adapter.getInvoked() == 0) {
>
> throw new RuntimeException("the
> adapter's marshal method was never invoked");
>
> }
>
> }
>
> }
>
>
>
>
>
> package somepackage;
>
>
>
> import javax.xml.bind.annotation.adapters.XmlAdapter;
>
>
>
> final class TestContentNodeAdapter extends
> XmlAdapter<TestContentNodeWrapper, TestContentNode> {
>
>
>
> private int invoked;
>
>
>
> public int getInvoked() {
>
> return invoked;
>
> }
>
>
>
>
>
> @Override
>
>
>
> public TestContentNodeWrapper marshal(TestContentNode node)
> throws Exception {
>
> invoked++;
>
>
>
> if (node == null) {
>
> throw new NullPointerException("See
> what I mean? It's null.");
>
> }
>
>
>
> return new TestContentNodeWrapper(node);
>
> }
>
>
>
>
>
> @Override
>
>
>
> public TestContentNode unmarshal(TestContentNodeWrapper
> nodeWrapper) throws Exception {
>
> return new TestContentNode(nodeWrapper.getName());
>
> }
>
> }
>
>
>
>
>
> package somepackage;
>
>
>
> import javax.xml.bind.annotation.XmlAttribute;
>
>
>
> final class TestContentNodeWrapper {
>
>
>
> @XmlAttribute(name="name")
>
> private String name;
>
>
>
>
>
> TestContentNodeWrapper(TestContentNode node) {
>
> name = node.getName();
>
> }
>
>
>
>
>
> public String getName() {
>
> return name;
>
> }
>
> }
>