Search in sources :

Example 26 with Validator

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

the class ValidateV2RC4Identifiers method testPeerReview.

/** Test both types of identifier here
     * 
     * @throws SAXException
     * @throws IOException
     * @throws JAXBException
     * @throws ParserConfigurationException
     */
@Test
public void testPeerReview() throws SAXException, IOException, JAXBException, ParserConfigurationException {
    PeerReview peerReview = unmarshallFromPath("/record_2.0_rc4/samples/peer-review-2.0_rc4.xml", PeerReview.class);
    ExternalID id = peerReview.getExternalIdentifiers().getExternalIdentifier().get(0);
    assertEquals("source-work-id", id.getType());
    assertEquals("work:external-identifier-id", id.getValue());
    assertEquals(new Url("http://orcid.org"), id.getUrl());
    assertEquals(Relationship.SELF, id.getRelationship());
    ExternalID subjectid = peerReview.getSubjectExternalIdentifier();
    assertEquals("doi", subjectid.getType());
    assertEquals("peer-review:subject-external-identifier-id", subjectid.getValue());
    assertEquals(new Url("http://orcid.org"), subjectid.getUrl());
    assertEquals(Relationship.SELF, subjectid.getRelationship());
    Validator validator = getValidator("peer-review");
    validator.validate(marshall(PeerReview.class, peerReview));
    validator.validate(marshallToDOM(PeerReview.class, peerReview));
    //do the full record too
    peerReview = unmarshallFromPath("/record_2.0_rc4/samples/peer-review-full-2.0_rc4.xml", PeerReview.class);
    id = peerReview.getExternalIdentifiers().getExternalIdentifier().get(0);
    assertEquals("source-work-id", id.getType());
    assertEquals("work:external-identifier-id", id.getValue());
    assertEquals(new Url("http://orcid.org"), id.getUrl());
    assertEquals(Relationship.SELF, id.getRelationship());
    subjectid = peerReview.getSubjectExternalIdentifier();
    assertEquals("doi", subjectid.getType());
    assertEquals("peer-review:subject-external-identifier-id", subjectid.getValue());
    assertEquals(new Url("http://orcid.org"), subjectid.getUrl());
    assertEquals(Relationship.SELF, subjectid.getRelationship());
}
Also used : ExternalID(org.orcid.jaxb.model.record_rc4.ExternalID) PeerReview(org.orcid.jaxb.model.record_rc4.PeerReview) Url(org.orcid.jaxb.model.common_rc4.Url) Validator(javax.xml.validation.Validator) MarshallingTest(org.orcid.jaxb.model.notification.custom.MarshallingTest) Test(org.junit.Test)

Example 27 with Validator

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

the class ValidateV2RC4Identifiers method testFunding.

@Test
public void testFunding() throws SAXException, IOException, JAXBException, ParserConfigurationException {
    Funding funding = unmarshallFromPath("/record_2.0_rc4/samples/funding-2.0_rc4.xml", Funding.class);
    assertEquals("funding:organization-defined-type", funding.getOrganizationDefinedType().getContent());
    assertNotNull(funding.getExternalIdentifiers());
    assertNotNull(funding.getExternalIdentifiers().getExternalIdentifier());
    Assert.notEmpty(funding.getExternalIdentifiers().getExternalIdentifier());
    assertEquals("grant_number", funding.getExternalIdentifiers().getExternalIdentifier().get(0).getType());
    assertEquals("funding:external-identifier-value", funding.getExternalIdentifiers().getExternalIdentifier().get(0).getValue());
    assertEquals(new Url("http://tempuri.org"), funding.getExternalIdentifiers().getExternalIdentifier().get(0).getUrl());
    assertEquals(Relationship.SELF, funding.getExternalIdentifiers().getExternalIdentifier().get(0).getRelationship());
    Validator validator = getValidator("funding");
    validator.validate(marshall(Funding.class, funding));
    validator.validate(marshallToDOM(Funding.class, funding));
}
Also used : Funding(org.orcid.jaxb.model.record_rc4.Funding) Url(org.orcid.jaxb.model.common_rc4.Url) Validator(javax.xml.validation.Validator) MarshallingTest(org.orcid.jaxb.model.notification.custom.MarshallingTest) Test(org.junit.Test)

Example 28 with Validator

use of javax.xml.validation.Validator in project OpenClinica by OpenClinica.

the class XmlSchemaValidationHelper method validateAgainstSchema.

public void validateAgainstSchema(File xmlFile, InputStream xsdFile) {
    try {
        // parse an XML document into a DOM tree
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        DocumentBuilder parser = builderFactory.newDocumentBuilder();
        // parser.isNamespaceAware();
        Document document = parser.parse(xmlFile);
        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load a WXS schema, represented by a Schema instance
        Source schemaFile = new StreamSource(xsdFile);
        Schema schema = factory.newSchema(schemaFile);
        // create a Validator instance, which can be used to validate an
        // instance document
        Validator validator = schema.newValidator();
        // validate the DOM tree
        validator.validate(new DOMSource(document));
    } catch (FileNotFoundException ex) {
        throw new OpenClinicaSystemException("File was not found", ex.getCause());
    } catch (IOException ioe) {
        throw new OpenClinicaSystemException("IO Exception", ioe.getCause());
    } catch (SAXParseException spe) {
        //spe.printStackTrace();
        throw new OpenClinicaSystemException("Line : " + spe.getLineNumber() + " - " + spe.getMessage(), spe.getCause());
    } catch (SAXException e) {
        throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
    } catch (ParserConfigurationException pce) {
        throw new OpenClinicaSystemException(pce.getMessage(), pce.getCause());
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.w3c.dom.Document) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Validator(javax.xml.validation.Validator)

Example 29 with Validator

use of javax.xml.validation.Validator in project OpenClinica by OpenClinica.

the class XmlSchemaValidationHelper method validateAgainstSchema.

public void validateAgainstSchema(String xml, File xsdFile) {
    try {
        // parse an XML document into a DOM tree
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        DocumentBuilder parser = builderFactory.newDocumentBuilder();
        Document document = parser.parse(xml);
        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load a WXS schema, represented by a Schema instance
        Source schemaFile = new StreamSource(xsdFile);
        Schema schema = factory.newSchema(schemaFile);
        // create a Validator instance, which can be used to validate an
        // instance document
        Validator validator = schema.newValidator();
        // validate the DOM tree
        validator.validate(new DOMSource(document));
    } catch (FileNotFoundException ex) {
        throw new OpenClinicaSystemException("File was not found", ex.getCause());
    } catch (IOException ioe) {
        throw new OpenClinicaSystemException("IO Exception", ioe.getCause());
    } catch (SAXParseException spe) {
        //spe.printStackTrace();
        throw new OpenClinicaSystemException("Line : " + spe.getLineNumber() + " - " + spe.getMessage(), spe.getCause());
    } catch (SAXException e) {
        throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
    } catch (ParserConfigurationException pce) {
        throw new OpenClinicaSystemException(pce.getMessage(), pce.getCause());
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.w3c.dom.Document) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Validator(javax.xml.validation.Validator)

Example 30 with Validator

use of javax.xml.validation.Validator in project OpenClinica by OpenClinica.

the class XmlSchemaValidationHelper method validateAgainstSchema.

public void validateAgainstSchema(String xml, InputStream xsdFile) {
    try {
        // parse an XML document into a DOM tree
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        DocumentBuilder parser = builderFactory.newDocumentBuilder();
        Document document = parser.parse(new InputSource(new StringReader(xml)));
        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load a WXS schema, represented by a Schema instance
        Source schemaFile = new StreamSource(xsdFile);
        Schema schema = factory.newSchema(schemaFile);
        // create a Validator instance, which can be used to validate an
        // instance document
        Validator validator = schema.newValidator();
        // validate the DOM tree
        validator.validate(new DOMSource(document));
    } catch (FileNotFoundException ex) {
        throw new OpenClinicaSystemException("File was not found", ex.getCause());
    } catch (IOException ioe) {
        // ioe.printStackTrace();
        throw new OpenClinicaSystemException("IO Exception", ioe.getCause());
    } catch (SAXParseException spe) {
        // spe.printStackTrace();
        throw new OpenClinicaSystemException("Line : " + spe.getLineNumber() + " - " + spe.getMessage(), spe.getCause());
    } catch (SAXException e) {
        throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
    } catch (ParserConfigurationException pce) {
        throw new OpenClinicaSystemException(pce.getMessage(), pce.getCause());
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.w3c.dom.Document) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Validator(javax.xml.validation.Validator)

Aggregations

Validator (javax.xml.validation.Validator)80 Schema (javax.xml.validation.Schema)51 SchemaFactory (javax.xml.validation.SchemaFactory)39 StreamSource (javax.xml.transform.stream.StreamSource)38 DOMSource (javax.xml.transform.dom.DOMSource)30 Source (javax.xml.transform.Source)29 Test (org.junit.Test)21 SAXException (org.xml.sax.SAXException)21 IOException (java.io.IOException)17 MarshallingTest (org.orcid.jaxb.model.notification.custom.MarshallingTest)17 Document (org.w3c.dom.Document)13 InputStream (java.io.InputStream)9 URL (java.net.URL)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9 Url (org.orcid.jaxb.model.common_v2.Url)8 InputSource (org.xml.sax.InputSource)8 StringReader (java.io.StringReader)7 JAXBSource (javax.xml.bind.util.JAXBSource)7 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)7 SAXParseException (org.xml.sax.SAXParseException)7