Commit 5c1a2fc1 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Workaround for fixing node type attribute issue

parent fc2d7583
Loading
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
package it.inaf.oats.vospace.datamodel;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import net.ivoa.xml.vospace.v2.Node;

/**
 * JAXB automatically generates the xsi:type attribute, however it doesn't fill
 * it for the root node (it seems that this is by design). Since we need it, we
 * manually added it on the Node class, but this causes a duplication of the
 * attribute in the children nodes. This adapter is applied to children nodes to
 * avoid the duplication by setting the field to null.
 */
public class RemoveDuplicateTypeAdapter extends XmlAdapter<Node, Node> {

    @Override
    public Node unmarshal(Node node) throws Exception {
        return node;
    }

    @Override
    public Node marshal(Node node) throws Exception {
        node.removeType();
        return node;
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
//
package net.ivoa.xml.vospace.v2;

import it.inaf.oats.vospace.datamodel.RemoveDuplicateTypeAdapter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
@@ -14,6 +15,7 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

/**
 *
@@ -66,6 +68,7 @@ public class ContainerNode
    // This should simplify usage and JSON compatibility.
    @XmlElement(name = "node")
    @XmlElementWrapper(name = "nodes", required = true)
    @XmlJavaTypeAdapter(RemoveDuplicateTypeAdapter.class)
    protected List<Node> nodes;

    public List<Node> getNodes() {
+14 −1
Original line number Diff line number Diff line
@@ -59,10 +59,23 @@ public class Node {
    protected PropertyList properties;

    // <edit>
    // Used for generating missing type attribute for root node. For child nodes it is filled automatically.
    @XmlAttribute(name = "type", namespace = "http://www.w3.org/2001/XMLSchema-instance", required = false)
    private String type;

    /* This method exists to fix the issue with type attribute. See RemoveDuplicateTypeAdapter class. */
    public void removeType() {
        this.type = null;
    }
    
    public Node() {
        type = getType();
    }
    
    // Needed for handling inheritance in JSON (in XML, type attribute is automatically generated by JAXB).
    @JsonProperty
    @XmlTransient
    public String getType() {
    public final String getType() {
        return "vos:" + getClass().getSimpleName();
    }
    // </edit>
+2 −2
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ public class NodeTest {

        Document doc = loadDocument(xml);
        assertEquals("vos:node", doc.getDocumentElement().getNodeName());
        //assertEquals("vos:ContainerNode", doc.getDocumentElement().getAttribute("xsi:type"));
        assertEquals("vos:ContainerNode", doc.getDocumentElement().getAttribute("xsi:type"));

        assertTrue(xml.contains("<vos:nodes>"));
        assertTrue(xml.contains("xsi:type=\"vos:DataNode\""));