/* * Copyright (c) 2017 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.yangtools.yang.model.export.test; import com.google.common.base.Preconditions; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.opendaylight.yangtools.util.xml.UntrustedXML; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.SAXException; public class YinExportTestUtils { private YinExportTestUtils() { throw new UnsupportedOperationException("Utility class"); } public static Document loadDocument(final String xmlPath) throws IOException, SAXException { final InputStream resourceAsStream = SchemaContextEmitterTest.class.getResourceAsStream(xmlPath); final Document currentConfigElement = readXmlToDocument(resourceAsStream); Preconditions.checkNotNull(currentConfigElement); return currentConfigElement; } static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException { final Document doc = UntrustedXML.newDocumentBuilder().parse(xmlContent); doc.getDocumentElement().normalize(); return doc; } public static String toString(final Node xml) { try { final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); final StreamResult result = new StreamResult(new StringWriter()); final DOMSource source = new DOMSource(xml); transformer.transform(source, result); return result.getWriter().toString(); } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) { throw new RuntimeException("Unable to serialize xml element " + xml, e); } } }