2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.export.test;
10 import com.google.common.base.Preconditions;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.StringWriter;
14 import javax.xml.transform.OutputKeys;
15 import javax.xml.transform.Transformer;
16 import javax.xml.transform.TransformerException;
17 import javax.xml.transform.TransformerFactory;
18 import javax.xml.transform.TransformerFactoryConfigurationError;
19 import javax.xml.transform.dom.DOMSource;
20 import javax.xml.transform.stream.StreamResult;
21 import org.opendaylight.yangtools.util.xml.UntrustedXML;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Node;
24 import org.xml.sax.SAXException;
26 public class YinExportTestUtils {
28 private YinExportTestUtils() {
29 throw new UnsupportedOperationException("Utility class");
32 public static Document loadDocument(final String xmlPath) throws IOException, SAXException {
33 final InputStream resourceAsStream = SchemaContextEmitterTest.class.getResourceAsStream(xmlPath);
34 final Document currentConfigElement = readXmlToDocument(resourceAsStream);
35 Preconditions.checkNotNull(currentConfigElement);
36 return currentConfigElement;
39 static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
40 final Document doc = UntrustedXML.newDocumentBuilder().parse(xmlContent);
41 doc.getDocumentElement().normalize();
45 public static String toString(final Node xml) {
47 final Transformer transformer = TransformerFactory.newInstance().newTransformer();
48 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
49 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
51 final StreamResult result = new StreamResult(new StringWriter());
52 final DOMSource source = new DOMSource(xml);
53 transformer.transform(source, result);
55 return result.getWriter().toString();
56 } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) {
57 throw new RuntimeException("Unable to serialize xml element " + xml, e);