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.netconf.util.xml;
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;
22 public final class XmlNetconfValidator {
24 private static final Schema SCHEMA;
26 private XmlNetconfValidator() {}
29 final InputStream xmlSchema = XmlNetconfValidator.class.getResourceAsStream("/xml.xsd");
30 Preconditions.checkNotNull(xmlSchema, "Cannot find xml.xsd");
32 final InputStream rfc4714Schema = XmlNetconfValidator.class.getResourceAsStream("/rfc4741.xsd");
33 Preconditions.checkNotNull(rfc4714Schema, "Cannot find rfc4741.xsd");
34 SCHEMA = XmlUtil.loadSchema(xmlSchema, rfc4714Schema);
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);