Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / xml / XmlNetconfValidator.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.controller.netconf.util.xml;
10
11 import com.google.common.base.Preconditions;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import javax.xml.transform.Source;
15 import javax.xml.transform.dom.DOMSource;
16 import javax.xml.validation.Schema;
17 import javax.xml.validation.Validator;
18 import org.opendaylight.controller.config.util.xml.XmlUtil;
19 import org.w3c.dom.Document;
20 import org.xml.sax.SAXException;
21
22 public final class XmlNetconfValidator {
23
24     private static final Schema SCHEMA;
25
26     private XmlNetconfValidator() {}
27
28     static {
29         final InputStream xmlSchema = XmlNetconfValidator.class.getResourceAsStream("/xml.xsd");
30         Preconditions.checkNotNull(xmlSchema, "Cannot find xml.xsd");
31
32         final InputStream rfc4714Schema = XmlNetconfValidator.class.getResourceAsStream("/rfc4741.xsd");
33         Preconditions.checkNotNull(rfc4714Schema, "Cannot find rfc4741.xsd");
34         SCHEMA = XmlUtil.loadSchema(xmlSchema, rfc4714Schema);
35     }
36
37     public static void validate(Document inputDocument) throws SAXException, IOException {
38         final Validator validator = SCHEMA.newValidator();
39         final Source source = new DOMSource(inputDocument);
40         validator.validate(source);
41     }
42 }