2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.connector.remoterpc;
10 import org.junit.Test;
11 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
12 import org.opendaylight.yangtools.yang.data.api.Node;
13 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
14 import org.opendaylight.yangtools.yang.data.impl.NodeUtils;
15 import org.opendaylight.yangtools.yang.data.impl.XmlTreeBuilder;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.w3c.dom.Document;
20 import javax.xml.stream.XMLStreamException;
21 import javax.xml.transform.OutputKeys;
22 import javax.xml.transform.Transformer;
23 import javax.xml.transform.TransformerException;
24 import javax.xml.transform.TransformerFactory;
25 import javax.xml.transform.dom.DOMSource;
26 import javax.xml.transform.stream.StreamResult;
27 import java.io.FileNotFoundException;
28 import java.io.InputStream;
29 import java.io.StringWriter;
31 public class SerilizationTest {
33 private static final Logger _logger = LoggerFactory.getLogger(SerilizationTest.class);
35 public void fromXml() {
39 public void toXml() throws FileNotFoundException {
41 InputStream xmlStream = SerilizationTest.class.getResourceAsStream("/FourSimpleChildren.xml");
42 StringWriter writer = new StringWriter();
44 CompositeNode data = loadCompositeNode(xmlStream);
45 Document domTree = NodeUtils.buildShadowDomTree(data);
47 TransformerFactory tf = TransformerFactory.newInstance();
48 Transformer transformer = tf.newTransformer();
49 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
50 //transformer.setOutputProperty(OutputKeys.METHOD, "xml");
51 //transformer.setOutputProperty(OutputKeys.INDENT, "yes");
52 //transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
53 //transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
54 transformer.transform(new DOMSource(domTree), new StreamResult(writer));
55 } catch (TransformerException e) {
56 _logger.error("Error during translation of Document to OutputStream", e);
59 _logger.info("Parsed xml [{}]", writer.toString());
62 //Note to self: Stolen from TestUtils
63 ///Users/alefan/odl/controller4/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java
64 // Figure out how to include TestUtils through pom ...was getting errors
65 private CompositeNode loadCompositeNode(InputStream xmlInputStream) throws FileNotFoundException {
66 if (xmlInputStream == null) {
67 throw new IllegalArgumentException();
71 dataTree = XmlTreeBuilder.buildDataTree(xmlInputStream);
72 } catch (XMLStreamException e) {
73 _logger.error("Error during building data tree from XML", e);
76 if (dataTree == null) {
77 _logger.error("data tree is null");
80 if (dataTree instanceof SimpleNode) {
81 _logger.error("RPC XML was resolved as SimpleNode");
84 return (CompositeNode) dataTree;