Search in sources :

Example 1 with AnyXmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode in project controller by opendaylight.

the class NormalizedNodePrunerTest method testAnyXMLNodeNotPrunedWhenHasParent.

@Test
public void testAnyXMLNodeNotPrunedWhenHasParent() throws IOException {
    NormalizedNodePruner pruner = prunerFullSchema(TestModel.TEST_PATH);
    AnyXmlNode child = Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME)).withValue(mock(DOMSource.class)).build();
    NormalizedNode<?, ?> input = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).withChild(child).build();
    NormalizedNodeWriter.forStreamWriter(pruner).write(input);
    NormalizedNode<?, ?> actual = pruner.normalizedNode();
    assertEquals("normalizedNode", input, actual);
}
Also used : NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) AnyXmlNode(org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode) Test(org.junit.Test)

Example 2 with AnyXmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode in project controller by opendaylight.

the class NormalizedNodePruner method anyxmlNode.

@SuppressWarnings("unchecked")
@Override
public void anyxmlNode(YangInstanceIdentifier.NodeIdentifier nodeIdentifier, Object value) throws IOException, IllegalArgumentException {
    checkNotSealed();
    NormalizedNodeBuilderWrapper parent = stack.peek();
    AnyXmlNode anyXmlNode = Builders.anyXmlBuilder().withNodeIdentifier(nodeIdentifier).withValue((DOMSource) value).build();
    if (parent != null) {
        if (hasValidSchema(nodeIdentifier.getNodeType(), parent)) {
            parent.builder().addChild(anyXmlNode);
        }
    } else {
        // If there's no parent node then this is a stand alone AnyXmlNode.
        if (nodePathSchemaNode != null) {
            this.normalizedNode = anyXmlNode;
        }
        sealed = true;
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) AnyXmlNode(org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode)

Example 3 with AnyXmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode in project controller by opendaylight.

the class NormalizedNodeStreamReaderWriterTest method testAnyXmlStreaming.

@Test
public void testAnyXmlStreaming() throws Exception {
    String xml = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    Node xmlNode = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml))).getDocumentElement();
    assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
    NormalizedNode<?, ?> anyXmlContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME)).withValue(new DOMSource(xmlNode)).build()).build();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
    nnout.writeNormalizedNode(anyXmlContainer);
    NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bos.toByteArray()));
    ContainerNode deserialized = (ContainerNode) nnin.readNormalizedNode();
    Optional<DataContainerChild<? extends PathArgument, ?>> child = deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
    assertEquals("AnyXml child present", true, child.isPresent());
    StreamResult xmlOutput = new StreamResult(new StringWriter());
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(((AnyXmlNode) child.get()).getValue(), xmlOutput);
    assertEquals("XML", xml, xmlOutput.getWriter().toString());
    assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode) child.get()).getValue().getNode().getNamespaceURI());
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) LeafSetEntryNode(org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode) AnyXmlNode(org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode) Node(org.w3c.dom.Node) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) StringWriter(java.io.StringWriter) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) StringReader(java.io.StringReader) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) Test(org.junit.Test)

Example 4 with AnyXmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode in project controller by opendaylight.

the class SerializationUtilsTest method testSerializeDeserializeAnyXmlNode.

@Test
public void testSerializeDeserializeAnyXmlNode() throws Exception {
    final ByteArrayInputStream is = new ByteArrayInputStream("<xml><data/></xml>".getBytes(Charset.defaultCharset()));
    final Document parse = UntrustedXML.newDocumentBuilder().parse(is);
    final AnyXmlNode anyXmlNode = Builders.anyXmlBuilder().withNodeIdentifier(id("anyXmlNode")).withValue(new DOMSource(parse)).build();
    final byte[] bytes = SerializationUtils.serializeNormalizedNode(anyXmlNode);
    final NormalizedNode<?, ?> deserialized = SerializationUtils.deserializeNormalizedNode(bytes);
    final DOMSource value = (DOMSource) deserialized.getValue();
    final Diff diff = XMLUnit.compareXML((Document) anyXmlNode.getValue().getNode(), value.getNode().getOwnerDocument());
    Assert.assertTrue(diff.toString(), diff.similar());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) ByteArrayInputStream(java.io.ByteArrayInputStream) Diff(org.custommonkey.xmlunit.Diff) AnyXmlNode(org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

AnyXmlNode (org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode)4 DOMSource (javax.xml.transform.dom.DOMSource)3 Test (org.junit.Test)3 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Transformer (javax.xml.transform.Transformer)1 StreamResult (javax.xml.transform.stream.StreamResult)1 Diff (org.custommonkey.xmlunit.Diff)1 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)1 PathArgument (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument)1 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)1 DataContainerChild (org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild)1 LeafSetEntryNode (org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode)1 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1