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