Search in sources :

Example 1 with XMLPrettyPrinter

use of org.nextprot.api.commons.utils.XMLPrettyPrinter in project nextprot-api by calipho-sib.

the class XSDValidationTest method shouldValidateXMLFilewithXSD.

@Test
public void shouldValidateXMLFilewithXSD() {
    Schema schema;
    try {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        schema = factory.newSchema(new StreamSource(new File("src/main/webapp/nextprot-export-v2.xsd")));
        File f = new File("tmp.xml");
        StreamSource xmlFile = new StreamSource(f);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        streamEntryService.streamEntries(Collections.singletonList("NX_Q15858"), NextprotMediaType.XML, "entry", baos, "");
        XMLPrettyPrinter prettyPrinter = new XMLPrettyPrinter();
        String prettyXml = prettyPrinter.prettify(baos.toString());
        PrintWriter out = new PrintWriter(f);
        out.print(prettyXml);
        out.close();
        // instance document
        Validator validator = schema.newValidator();
        // validate the DOM tree
        validator.validate(xmlFile);
        f.delete();
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLPrettyPrinter(org.nextprot.api.commons.utils.XMLPrettyPrinter) File(java.io.File) Validator(javax.xml.validation.Validator) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) WebIntegrationBaseTest(org.nextprot.api.web.dbunit.base.mvc.WebIntegrationBaseTest) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Schema (javax.xml.validation.Schema)1 SchemaFactory (javax.xml.validation.SchemaFactory)1 Validator (javax.xml.validation.Validator)1 Test (org.junit.Test)1 XMLPrettyPrinter (org.nextprot.api.commons.utils.XMLPrettyPrinter)1 WebIntegrationBaseTest (org.nextprot.api.web.dbunit.base.mvc.WebIntegrationBaseTest)1