2 * Copyright (c) 2014 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
9 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
11 import com.google.common.base.Charsets;
12 import com.google.common.collect.Lists;
13 import com.google.common.io.ByteSource;
14 import java.io.ByteArrayInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import javax.xml.parsers.DocumentBuilder;
18 import javax.xml.parsers.DocumentBuilderFactory;
19 import javax.xml.parsers.ParserConfigurationException;
20 import org.junit.Before;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
25 import org.w3c.dom.Document;
26 import org.xml.sax.SAXException;
28 public class XmlDocumentUtilsTest {
30 private static final DocumentBuilderFactory BUILDERFACTORY;
33 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
34 factory.setNamespaceAware(true);
35 factory.setCoalescing(true);
36 factory.setIgnoringElementContentWhitespace(true);
37 factory.setIgnoringComments(true);
38 BUILDERFACTORY = factory;
41 public static final String XML_CONTENT = "<input xmlns=\"urn:opendaylight:controller:rpc:test\">\n" +
43 "<ref xmlns:ltha=\"urn:opendaylight:controller:rpc:test\">"+
44 "/ltha:cont/ltha:l[ ltha:id='id/foo/bar' ]"+
48 public static final String RPC_REPLY = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"m-1\">\n" +
52 private SchemaContext schema;
53 private RpcDefinition testRpc;
56 public void setUp() throws Exception {
57 final ByteSource byteSource = new ByteSource() {
59 public InputStream openStream() throws IOException {
60 return XmlDocumentUtilsTest.this.getClass().getResourceAsStream("rpc-test.yang");
63 schema = new YangParserImpl().parseSources(Lists.newArrayList(byteSource));
64 final Module rpcTestModule = schema.getModules().iterator().next();
65 testRpc = rpcTestModule.getRpcs().iterator().next();
68 public static Document readXmlToDocument(final String xmlContent) throws SAXException, IOException {
69 return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(Charsets.UTF_8)));
72 public static Document readXmlToDocument(final InputStream xmlContent) throws SAXException, IOException {
73 final DocumentBuilder dBuilder;
75 dBuilder = BUILDERFACTORY.newDocumentBuilder();
76 } catch (final ParserConfigurationException e) {
77 throw new IllegalStateException("Failed to parse XML document", e);
79 final Document doc = dBuilder.parse(xmlContent);
81 doc.getDocumentElement().normalize();