Merge "Exception for URI /restconf/operations/module_name:rpc ended with slash"
[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 java.io.IOException;
12 import java.io.InputStream;
13 import java.io.InputStreamReader;
14
15 import javax.xml.parsers.ParserConfigurationException;
16
17 import org.opendaylight.controller.netconf.api.NetconfMessage;
18 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
19 import org.w3c.dom.Document;
20 import org.w3c.dom.Element;
21 import org.xml.sax.SAXException;
22
23 import com.google.common.base.Charsets;
24 import com.google.common.base.Preconditions;
25 import com.google.common.io.CharStreams;
26 import com.google.common.io.InputSupplier;
27
28 public class XmlFileLoader {
29
30     public static NetconfMessage xmlFileToNetconfMessage(final String fileName) throws IOException, SAXException,
31             ParserConfigurationException {
32         return new NetconfMessage(xmlFileToDocument(fileName));
33     }
34
35     public static Element xmlFileToElement(final String fileName) throws IOException, SAXException,
36             ParserConfigurationException {
37         return xmlFileToDocument(fileName).getDocumentElement();
38     }
39
40     public static String xmlFileToString(final String fileName) throws IOException, SAXException,
41             ParserConfigurationException {
42         return XmlUtil.toString(xmlFileToDocument(fileName));
43     }
44
45     public static Document xmlFileToDocument(final String fileName) throws IOException, SAXException,
46             ParserConfigurationException {
47         try (InputStream resourceAsStream = XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName)) {
48             Preconditions.checkNotNull(resourceAsStream, fileName);
49             final Document doc = XmlUtil.readXmlToDocument(resourceAsStream);
50             return doc;
51         }
52     }
53
54     public static String fileToString(final String fileName) throws IOException {
55         try (InputStream resourceAsStream = XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName)) {
56             Preconditions.checkNotNull(resourceAsStream);
57
58             InputSupplier<? extends InputStream> supplier = new InputSupplier<InputStream>() {
59                 @Override
60                 public InputStream getInput() throws IOException {
61                     return resourceAsStream;
62                 }
63             };
64
65             InputSupplier<InputStreamReader> readerSupplier = CharStreams.newReaderSupplier(supplier, Charsets.UTF_8);
66
67             return CharStreams.toString(readerSupplier);
68         }
69     }
70
71     public static InputStream getResourceAsStream(final String fileName) {
72         return XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName);
73     }
74 }