jimetevenard / Intrinsic-JAXP-validator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Intrinsic JAXP Validator

An implementation of the Java XML validation API SchemaFactory to validate XML documents against their own declared grammars.

How to use it

Just as a Maven Dependency

This Maven artifact can be added as a dependency for any JAXP-compliant(2) validator, such as the XML Maven Plugin.

You can find a example here : https://github.com/labo-jim/intrinsic-validator-test-project

In your Java application

import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
final String schemaLanguage = "http://componentcorp.com/xml/ns/xml-model/1.0";
SchemaFactory intrinsicSchemaFactory = SchemaFactory.newInstance(schemaLanguage);

Schema schema = intrinsicSchemaFactory.newSchema();	
Validator validator = schema.newValidator();

Source yourXML = Your XML...
validator.validate(yourXML);

The instanciation of a SAX ValidatorHandler is not supported for several reasons(1) (cf. below).

What's under the hood

Simple and Stupid

The Intrinsic JAXP Validator is designed under the KISS principle.
It is simple and stupid.

The Intrinsic JAXP Validator does not handle himself any validation process.
It will simply instanciate a SchemaFactory for the declared schemma language.

This meens that you, of course, need to have a matching JAXP-compliant(2) implementation of each schema type you use in your classpath.(3)

Notes

  1. Why not supporting the SAX validation (i.e. with a ValidatorHandler)
    • Because the goal of this helper is to validate XML documents against various Schema type, including the unusual suspects, i.e. Schematron and NVDL, for witch no SAX implementation is available.
    • Since who can do more can do less, validators that implements the ValidatorHandler also implements the Validator
  2. JAXP stands for JAva XML Processing API.
    cf. <https://docs.oracle.com/javase/7/docs/api/javax/xml/validation/package-summary.html
  3. An implementation for W3C XML-Schema is provided with the JVM.
    For RelaxNG, see these wrappers for Jing :

About


Languages

Language:Java 100.0%