2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.data.codec.xml;
10 import static java.util.Objects.requireNonNull;
12 import java.io.ByteArrayInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.StringWriter;
16 import java.nio.charset.StandardCharsets;
17 import javax.xml.transform.OutputKeys;
18 import javax.xml.transform.Transformer;
19 import javax.xml.transform.TransformerException;
20 import javax.xml.transform.TransformerFactory;
21 import javax.xml.transform.TransformerFactoryConfigurationError;
22 import javax.xml.transform.dom.DOMSource;
23 import javax.xml.transform.stream.StreamResult;
24 import org.junit.AfterClass;
25 import org.junit.BeforeClass;
26 import org.opendaylight.yangtools.util.xml.UntrustedXML;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Node;
33 import org.xml.sax.SAXException;
35 public abstract class AbstractAnydataTest {
36 static final QName FOO_QNAME = QName.create("test-anydata", "foo");
37 static final NodeIdentifier FOO_NODEID = NodeIdentifier.create(FOO_QNAME);
39 static SchemaContext SCHEMA_CONTEXT;
42 public static void beforeClass() throws Exception {
43 SCHEMA_CONTEXT = YangParserTestUtils.parseYangResource("/test-anydata.yang");
47 public static void afterClass() {
48 SCHEMA_CONTEXT = null;
51 static DOMSourceAnydata toDOMSource(final String str) throws IOException, SAXException {
52 return new DOMSourceAnydata(new DOMSource(
53 // DOMSource must have a single document element, which we are ignoring
54 readXmlToDocument(toInputStream("<IGNORED>" + str + "</IGNORED>")).getDocumentElement()));
57 static InputStream toInputStream(final String str) {
58 return new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
61 private static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
62 final Document doc = UntrustedXML.newDocumentBuilder().parse(xmlContent);
63 doc.getDocumentElement().normalize();
68 * Load external XML resources.
69 * @param xmlPath {@link String} path to source
70 * @return {@link Document}
71 * @throws IOException Exception in Loading file
72 * @throws SAXException XML parse exception
74 static Document loadXmlDocument(final String xmlPath) throws IOException, SAXException {
75 return requireNonNull(readXmlToDocument(loadResourcesAsInputStream(xmlPath)));
79 * Load external resources as {@link InputStream}.
80 * @param xmlPath {@link String} Path to file
81 * @return {@link InputStream}
83 static InputStream loadResourcesAsInputStream(final String xmlPath) {
84 return SchemalessXMLStreamNormalizedNodeStreamWriterTest.class
85 .getResourceAsStream(xmlPath);
89 * Transform Node object to formatted XML string.
90 * @param xml {@link Node}
91 * @return {@link String}
93 static String toString(final Node xml) {
95 final Transformer transformer = TransformerFactory.newInstance().newTransformer();
96 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
97 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
99 final StreamResult result = new StreamResult(new StringWriter());
100 final DOMSource source = new DOMSource(xml);
101 transformer.transform(source, result);
103 return result.getWriter().toString();
104 } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) {
105 throw new RuntimeException("Unable to serialize xml element " + xml, e);
110 * Transform DomSource object to formatted XML string.
111 * @param node {@link DOMSource}
112 * @return {@link String}
113 * @throws TransformerException Internal {@link Transformer} exception
115 static String getXmlFromDOMSource(final DOMSource node) throws TransformerException {
116 StringWriter writer = new StringWriter();
117 Transformer transformer = TransformerFactory.newInstance().newTransformer();
118 transformer.transform(node, new StreamResult(writer));
119 return writer.toString();