Initial code drop of netconf protocol implementation
[controller.git] / opendaylight / netconf / netconf-util / src / test / java / org / opendaylight / controller / netconf / util / test / XmlFileLoader.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.util.test;
10
11 import com.google.common.base.Charsets;
12 import com.google.common.base.Preconditions;
13 import com.google.common.io.CharStreams;
14 import com.google.common.io.InputSupplier;
15 import org.opendaylight.controller.netconf.api.NetconfMessage;
16 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
19 import org.xml.sax.SAXException;
20
21 import javax.xml.parsers.ParserConfigurationException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25
26 public class XmlFileLoader {
27
28     public static NetconfMessage xmlFileToNetconfMessage(final String fileName) throws IOException, SAXException,
29             ParserConfigurationException {
30         return new NetconfMessage(xmlFileToDocument(fileName));
31     }
32
33     public static Element xmlFileToElement(final String fileName) throws IOException, SAXException,
34             ParserConfigurationException {
35         return xmlFileToDocument(fileName).getDocumentElement();
36     }
37
38     public static String xmlFileToString(final String fileName) throws IOException, SAXException,
39             ParserConfigurationException {
40         return XmlUtil.toString(xmlFileToDocument(fileName));
41     }
42
43     public static Document xmlFileToDocument(final String fileName) throws IOException, SAXException,
44             ParserConfigurationException {
45         try (InputStream resourceAsStream = XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName)) {
46             Preconditions.checkNotNull(resourceAsStream);
47             final Document doc = XmlUtil.readXmlToDocument(resourceAsStream);
48             return doc;
49         }
50     }
51
52     public static String fileToString(final String fileName) throws IOException {
53         try (InputStream resourceAsStream = XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName)) {
54             Preconditions.checkNotNull(resourceAsStream);
55
56             InputSupplier<? extends InputStream> supplier = new InputSupplier<InputStream>() {
57                 @Override
58                 public InputStream getInput() throws IOException {
59                     return resourceAsStream;
60                 }
61             };
62
63             InputSupplier<InputStreamReader> readerSupplier = CharStreams.newReaderSupplier(supplier, Charsets.UTF_8);
64
65             return CharStreams.toString(readerSupplier);
66         }
67     }
68
69     public static InputStream getResourceAsStream(final String fileName) {
70         return XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName);
71     }
72 }