Loading src/main/java/it/inaf/oats/vospace/datamodel/RemoveDuplicateTypeAdapter.java 0 → 100644 +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; } } src/main/java/net/ivoa/xml/vospace/v2/ContainerNode.java +3 −0 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; /** * Loading Loading @@ -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() { Loading src/main/java/net/ivoa/xml/vospace/v2/Node.java +14 −1 Original line number Diff line number Diff line Loading @@ -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> Loading src/test/java/net/ivoa/xml/vospace/v2/NodeTest.java +2 −2 Original line number Diff line number Diff line Loading @@ -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\"")); Loading Loading
src/main/java/it/inaf/oats/vospace/datamodel/RemoveDuplicateTypeAdapter.java 0 → 100644 +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; } }
src/main/java/net/ivoa/xml/vospace/v2/ContainerNode.java +3 −0 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; /** * Loading Loading @@ -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() { Loading
src/main/java/net/ivoa/xml/vospace/v2/Node.java +14 −1 Original line number Diff line number Diff line Loading @@ -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> Loading
src/test/java/net/ivoa/xml/vospace/v2/NodeTest.java +2 −2 Original line number Diff line number Diff line Loading @@ -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\"")); Loading