2 * Copyright (c) 2013 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.controller.netconf.util.xml;
11 import java.io.IOException;
12 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;
19 import org.w3c.dom.Document;
20 import org.xml.sax.SAXException;
22 import com.google.common.base.Preconditions;
24 public class XmlNetconfValidator {
25 static final Schema schema;
28 final InputStream xmlSchema = XmlNetconfValidator.class.getResourceAsStream("/xml.xsd");
29 Preconditions.checkNotNull(xmlSchema, "Cannot find xml.xsd");
31 final InputStream rfc4714Schema = XmlNetconfValidator.class.getResourceAsStream("/rfc4741.xsd");
32 Preconditions.checkNotNull(rfc4714Schema, "Cannot find rfc4741.xsd");
33 schema = XmlUtil.loadSchema(xmlSchema, rfc4714Schema);
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);