Merge "Fix checkstyle warnings in netty-threadgroup-config."
[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.w3c.dom.Document;
19 import org.xml.sax.SAXException;
20
21 public final class XmlNetconfValidator {
22
23     private static final Schema SCHEMA;
24
25     private XmlNetconfValidator() {}
26
27     static {
28         final InputStream xmlSchema = XmlNetconfValidator.class.getResourceAsStream("/xml.xsd");
29         Preconditions.checkNotNull(xmlSchema, "Cannot find xml.xsd");
30
31         final InputStream rfc4714Schema = XmlNetconfValidator.class.getResourceAsStream("/rfc4741.xsd");
32         Preconditions.checkNotNull(rfc4714Schema, "Cannot find rfc4741.xsd");
33         SCHEMA = XmlUtil.loadSchema(xmlSchema, rfc4714Schema);
34     }
35
36     public static void validate(Document inputDocument) throws SAXException, IOException {
37         final Validator validator = SCHEMA.newValidator();
38         final Source source = new DOMSource(inputDocument);
39         validator.validate(source);
40     }
41 }