it.bz.opendatahub.alpinebits.xml.XmlValidationSchemaProvider Maven / Gradle / Ivy
// SPDX-FileCopyrightText: NOI Techpark
//
// SPDX-License-Identifier: MPL-2.0
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package it.bz.opendatahub.alpinebits.xml;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.net.URL;
/**
* This class provides builder methods to create {@link Schema}
* instances for XML validation.
*/
public final class XmlValidationSchemaProvider {
private XmlValidationSchemaProvider() {
// Empty
}
/**
* Build and return a RNG schema for the specified AlpineBits version.
*
* This method assumes, that the RNG schema file is loadable from the
* classpath and that the filename corresponds to the following pattern:
*
* alpinebits-VERSION.rng
*
* where VERSION
denotes a valid AlpineBits version (e.g. 2017-10).
*
* If no valid RNG file could be found a {@link InvalidSchemaException}
* will be thrown.
*
* @param version build RNG schema for this AlpineBits version
* @return a {@link Schema} that can be used for XML validation
* @throws InvalidSchemaException if the file could not be found
* or it wasn't a valid RNG file.
*/
public static Schema buildRngSchemaForAlpineBitsVersion(String version) {
return XmlValidationSchemaProvider.buildRngSchema("alpinebits-" + version + ".rng");
}
/**
* Build and return a RNG schema from the file specified by filename
.
*
* This method assumes, that the RNG schema file is loadable from the
* classpath.
*
* If no valid RNG file could be found a {@link InvalidSchemaException}
* will be thrown.
*
* @param filename name of the RNG file in the classpath
* @return a {@link Schema} that can be used for XML validation
* @throws InvalidSchemaException if the file could not be found
* or it wasn't a valid RNG file.
*/
@SuppressWarnings("checkstyle:illegalcatch")
public static Schema buildRngSchema(String filename) {
System.setProperty(
SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI,
"com.thaiopensource.relaxng.jaxp.XMLSyntaxSchemaFactory"
);
try {
return XmlValidationSchemaProvider.buildKnownSchema(filename, XMLConstants.RELAXNG_NS_URI);
} catch (Exception e) {
throw XmlValidationSchemaProvider.buildValidationException(filename, "RNG", e);
}
}
/**
* Build and return a XSD schema for the specified AlpineBits version.
*
* This method assumes, that the XSD schema file is loadable from the
* classpath and that the filename corresponds to the following pattern:
*
* alpinebits-VERSION.xsd
*
* where VERSION
denotes a valid AlpineBits version (e.g. 2017-10).
*
* If no valid XSD file could be found a {@link InvalidSchemaException}
* will be thrown.
*
* @param version build XSD schema for this AlpineBits version
* @return a {@link Schema} that can be used for XML validation
* @throws InvalidSchemaException if the file could not be found
* or it wasn't a valid XSD file.
*/
public static Schema buildXsdSchemaForAlpineBitsVersion(String version) {
return XmlValidationSchemaProvider.buildXsdSchema("alpinebits-" + version + ".xsd");
}
/**
* Build and return a XSD schema from the file specified by filename
.
*
* This method assumes, that the XSD schema file is loadable from the
* classpath.
*
* If no valid XSD file could be found a {@link InvalidSchemaException}
* will be thrown.
*
* @param filename name of the XSD file in the classpath
* @return a {@link Schema} that can be used for XML validation
* @throws InvalidSchemaException if the file could not be found
* or it wasn't a valid XSD file.
*/
@SuppressWarnings("checkstyle:illegalcatch")
public static Schema buildXsdSchema(String filename) {
try {
return XmlValidationSchemaProvider.buildKnownSchema(filename, XMLConstants.W3C_XML_SCHEMA_NS_URI);
} catch (Exception e) {
throw XmlValidationSchemaProvider.buildValidationException(filename, "XSD", e);
}
}
private static Schema buildKnownSchema(String filename, String schemaLanguage) throws SAXException {
URL xsdUrl = XmlValidationSchemaProvider.class.getClassLoader().getResource(filename);
SchemaFactory sf = SchemaFactory.newInstance(schemaLanguage);
return sf.newSchema(xsdUrl);
}
private static InvalidSchemaException buildValidationException(String filename, String validationType, Throwable e) {
return new InvalidSchemaException("Error while building " + validationType + " schema from file " + filename, e);
}
}