Search in sources :

Example 66 with Schema

use of javax.xml.validation.Schema in project ORCID-Source by ORCID.

the class ValidateV2RC3Identifiers method getValidator.

public Validator getValidator(String name) throws SAXException {
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Schema schema = factory.newSchema(getClass().getResource("/record_2.0_rc3/" + name + "-2.0_rc3.xsd"));
    Validator validator = schema.newValidator();
    return validator;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) Validator(javax.xml.validation.Validator)

Example 67 with Schema

use of javax.xml.validation.Schema in project ORCID-Source by ORCID.

the class ValidateV2_1SamplesTest method marshall.

private void marshall(Object object, String path) throws JAXBException, SAXException, URISyntaxException {
    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = context.createMarshaller();
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File(getClass().getResource(path).toURI()));
    marshaller.setSchema(schema);
    marshaller.marshal(object, System.out);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Marshaller(javax.xml.bind.Marshaller) Schema(javax.xml.validation.Schema) JAXBContext(javax.xml.bind.JAXBContext) File(java.io.File)

Example 68 with Schema

use of javax.xml.validation.Schema in project ORCID-Source by ORCID.

the class ValidateV2_1IdentifiersTest method getValidator.

public Validator getValidator(String name) throws SAXException {
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Schema schema = factory.newSchema(getClass().getResource("/record_2.1/" + name + "-2.1.xsd"));
    Validator validator = schema.newValidator();
    return validator;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) Validator(javax.xml.validation.Validator)

Example 69 with Schema

use of javax.xml.validation.Schema in project ORCID-Source by ORCID.

the class OrcidValidationJaxbContextResolver method validate.

public void validate(Object toValidate) {
    String apiVersion = getApiVersion();
    String schemaFilenamePrefix = getSchemaFilenamePrefix(toValidate.getClass(), apiVersion);
    try {
        Schema schema = getSchema(schemaFilenamePrefix, apiVersion);
        JAXBContext context = JAXBContext.newInstance(toValidate.getClass());
        Source source = new JAXBSource(context, toValidate);
        Validator validator = schema.newValidator();
        validator.validate(source);
    } catch (SAXException | JAXBException | IOException e) {
        //Validation exceptions should return BAD_REQUEST status
        throw new WebApplicationException(e, Status.BAD_REQUEST.getStatusCode());
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) Schema(javax.xml.validation.Schema) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBSource(javax.xml.bind.util.JAXBSource) JAXBSource(javax.xml.bind.util.JAXBSource) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException)

Example 70 with Schema

use of javax.xml.validation.Schema in project ORCID-Source by ORCID.

the class OrcidValidationJaxbContextResolver method getSchema.

private Schema getSchema(String schemaFilenamePrefix, String apiVersion) throws SAXException {
    apiVersion = (apiVersion == null ? "" : apiVersion);
    String schemaPath = "/" + schemaFilenamePrefix + apiVersion + ".xsd";
    Schema schema = schemaByPath.get(schemaPath);
    if (schema != null) {
        return schema;
    }
    Source source = new StreamSource(OrcidValidationJaxbContextResolver.class.getResourceAsStream(schemaPath));
    schema = createSchemaFactory().newSchema(source);
    schemaByPath.put(schemaPath, schema);
    return schema;
}
Also used : Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBSource(javax.xml.bind.util.JAXBSource)

Aggregations

Schema (javax.xml.validation.Schema)102 SchemaFactory (javax.xml.validation.SchemaFactory)72 Validator (javax.xml.validation.Validator)51 StreamSource (javax.xml.transform.stream.StreamSource)45 SAXException (org.xml.sax.SAXException)35 Source (javax.xml.transform.Source)29 DOMSource (javax.xml.transform.dom.DOMSource)25 IOException (java.io.IOException)22 JAXBContext (javax.xml.bind.JAXBContext)18 Document (org.w3c.dom.Document)18 InputStream (java.io.InputStream)17 File (java.io.File)15 URL (java.net.URL)15 DocumentBuilder (javax.xml.parsers.DocumentBuilder)14 JAXBException (javax.xml.bind.JAXBException)13 Unmarshaller (javax.xml.bind.Unmarshaller)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)13 InputSource (org.xml.sax.InputSource)13 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)12 SAXParseException (org.xml.sax.SAXParseException)9