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;
}
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);
}
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;
}
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());
}
}
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;
}
Aggregations