Fix checkstyle in yang-model-export
[yangtools.git] / yang / yang-model-export / src / test / java / org / opendaylight / yangtools / yang / model / export / YinExportTestUtils.java
1 /*
2  * Copyright (c) 2017 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 package org.opendaylight.yangtools.yang.model.export;
9
10 import com.google.common.base.Preconditions;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.StringWriter;
14 import java.util.Optional;
15 import javax.xml.transform.OutputKeys;
16 import javax.xml.transform.Transformer;
17 import javax.xml.transform.TransformerException;
18 import javax.xml.transform.TransformerFactory;
19 import javax.xml.transform.TransformerFactoryConfigurationError;
20 import javax.xml.transform.dom.DOMSource;
21 import javax.xml.transform.stream.StreamResult;
22 import org.opendaylight.yangtools.util.xml.UntrustedXML;
23 import org.opendaylight.yangtools.yang.common.Revision;
24 import org.opendaylight.yangtools.yang.common.YangConstants;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Node;
28 import org.xml.sax.SAXException;
29
30 public final class YinExportTestUtils {
31
32     private YinExportTestUtils() {
33         throw new UnsupportedOperationException("Utility class");
34     }
35
36     public static Document loadDocument(final String prefix, final Module module) throws IOException, SAXException {
37         final Optional<Revision> rev = module.getRevision();
38         final String fileName = !rev.isPresent() ? module.getName() : module.getName() + '@' + rev.get().toString();
39         return loadDocument(prefix + '/' + fileName + YangConstants.RFC6020_YIN_FILE_EXTENSION);
40     }
41
42     public static Document loadDocument(final String xmlPath) throws IOException, SAXException {
43         final InputStream resourceAsStream = SchemaContextEmitterTest.class.getResourceAsStream(xmlPath);
44         final Document currentConfigElement = readXmlToDocument(resourceAsStream);
45         Preconditions.checkNotNull(currentConfigElement);
46         return currentConfigElement;
47     }
48
49     static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
50         final Document doc = UntrustedXML.newDocumentBuilder().parse(xmlContent);
51         doc.getDocumentElement().normalize();
52         return doc;
53     }
54
55     public static String toString(final Node xml) {
56         try {
57             final Transformer transformer = TransformerFactory.newInstance().newTransformer();
58             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
59             transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
60
61             final StreamResult result = new StreamResult(new StringWriter());
62             final DOMSource source = new DOMSource(xml);
63             transformer.transform(source, result);
64
65             return result.getWriter().toString();
66         } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) {
67             throw new RuntimeException("Unable to serialize xml element " + xml, e);
68         }
69     }
70 }