X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Ftest%2FXmlFileLoader.java;fp=opendaylight%2Fnetconf%2Fnetconf-util%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Futil%2Ftest%2FXmlFileLoader.java;h=8b60719ebe2df0028166deab0d3a38011bb59d4b;hp=0000000000000000000000000000000000000000;hb=a92d9d6a21a0f6ca8d2153795721f500eaf29ee9;hpb=fbc3092ca33990f0fc4a47f008786a416c484488 diff --git a/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/test/XmlFileLoader.java b/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/test/XmlFileLoader.java new file mode 100644 index 0000000000..8b60719ebe --- /dev/null +++ b/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/test/XmlFileLoader.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.util.test; + +import com.google.common.base.Charsets; +import com.google.common.base.Preconditions; +import com.google.common.io.CharStreams; +import com.google.common.io.InputSupplier; +import org.opendaylight.controller.netconf.api.NetconfMessage; +import org.opendaylight.controller.netconf.util.xml.XmlUtil; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class XmlFileLoader { + + public static NetconfMessage xmlFileToNetconfMessage(final String fileName) throws IOException, SAXException, + ParserConfigurationException { + return new NetconfMessage(xmlFileToDocument(fileName)); + } + + public static Element xmlFileToElement(final String fileName) throws IOException, SAXException, + ParserConfigurationException { + return xmlFileToDocument(fileName).getDocumentElement(); + } + + public static String xmlFileToString(final String fileName) throws IOException, SAXException, + ParserConfigurationException { + return XmlUtil.toString(xmlFileToDocument(fileName)); + } + + public static Document xmlFileToDocument(final String fileName) throws IOException, SAXException, + ParserConfigurationException { + try (InputStream resourceAsStream = XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName)) { + Preconditions.checkNotNull(resourceAsStream); + final Document doc = XmlUtil.readXmlToDocument(resourceAsStream); + return doc; + } + } + + public static String fileToString(final String fileName) throws IOException { + try (InputStream resourceAsStream = XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName)) { + Preconditions.checkNotNull(resourceAsStream); + + InputSupplier supplier = new InputSupplier() { + @Override + public InputStream getInput() throws IOException { + return resourceAsStream; + } + }; + + InputSupplier readerSupplier = CharStreams.newReaderSupplier(supplier, Charsets.UTF_8); + + return CharStreams.toString(readerSupplier); + } + } + + public static InputStream getResourceAsStream(final String fileName) { + return XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName); + } +}