All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.squeakysand.commons.xml.SchemaHelper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.squeakysand.commons.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * @author Craig S. Dickson
 */
public final class SchemaHelper {

    private static final Logger LOG = LoggerFactory.getLogger(SchemaHelper.class);

    public static Schema createSchema(String schemaResourcePath) throws XmlProcessingException {
        return createSchema(schemaResourcePath, XMLConstants.W3C_XML_SCHEMA_NS_URI);
    }

    public static Schema createSchema(String schemaResourcePath, String schemaType) throws XmlProcessingException {
        InputStream xsdInput = SchemaHelper.class.getResourceAsStream(schemaResourcePath);
        return createSchema(xsdInput, schemaType);
    }

    public static Schema createSchema(InputStream schemaInputStream) throws XmlProcessingException {
        return createSchema(schemaInputStream, XMLConstants.W3C_XML_SCHEMA_NS_URI);
    }

    public static Schema createSchema(InputStream schemaInputStream, String schemaType) throws XmlProcessingException {
        SchemaFactory factory = SchemaFactory.newInstance(schemaType);
        InputSource inputSource = new InputSource(schemaInputStream);
        Source xsdSource = new SAXSource(inputSource);
        Schema schema = null;
        try {
            schema = factory.newSchema(xsdSource);
        } catch (SAXException e) {
            throw new XmlProcessingException(e);
        }
        return schema;
    }

    public static boolean validate(File schemaFile, File xmlFile) throws XmlProcessingException {
        InputStream schemaInput = null;
        InputStream xmlInput = null;
        try {
            schemaInput = new FileInputStream(schemaFile);
            xmlInput = new FileInputStream(xmlFile);
            return validate(schemaInput, xmlInput);
        } catch (FileNotFoundException e) {
            throw new XmlProcessingException(e);
        } finally {
            if (schemaInput != null) {
                try {
                    schemaInput.close();
                } catch (IOException e) {
                    LOG.warn(e.getMessage(), e);
                }
            }
            if (xmlInput != null) {
                try {
                    xmlInput.close();
                } catch (IOException e) {
                    LOG.warn(e.getMessage(), e);
                }
            }

        }
    }

    public static boolean validate(InputStream schemaInputStream, InputStream xmlInputStream) throws XmlProcessingException {
        Schema schema = createSchema(schemaInputStream);
        return validate(schema, xmlInputStream);
    }

    public static boolean validate(Schema schema, InputStream xmlInputStream) throws XmlProcessingException {
        StreamSource xmlSource = new StreamSource(xmlInputStream);
        Validator validator = schema.newValidator();
        try {
            validator.validate(xmlSource);
        } catch (SAXException e) {
            throw new XmlProcessingException(e);
        } catch (IOException e) {
            throw new XmlProcessingException(e);
        }
        return true;
    }

    private SchemaHelper() {
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy