Search in sources :

Example 51 with JAXBContext

use of javax.xml.bind.JAXBContext in project ORCID-Source by ORCID.

the class OrcidMessage method marshall.

private static String marshall(Object obj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    StringWriter writer = new StringWriter();
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(obj, writer);
    return writer.toString();
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBContext(javax.xml.bind.JAXBContext)

Example 52 with JAXBContext

use of javax.xml.bind.JAXBContext in project ORCID-Source by ORCID.

the class ValidateV2_1SamplesTest method unmarshall.

private Object unmarshall(Reader reader, Class<?> type, String schemaPath) throws SAXException, URISyntaxException {
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        if (schemaPath != null) {
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(new File(getClass().getResource(schemaPath).toURI()));
            unmarshaller.setSchema(schema);
        }
        return unmarshaller.unmarshal(reader);
    } catch (JAXBException e) {
        throw new RuntimeException("Unable to unmarshall orcid message" + e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File)

Example 53 with JAXBContext

use of javax.xml.bind.JAXBContext in project ORCID-Source by ORCID.

the class ValidateV2SamplesTest 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 54 with JAXBContext

use of javax.xml.bind.JAXBContext in project ORCID-Source by ORCID.

the class ValidateV2SamplesTest method unmarshall.

private Object unmarshall(Reader reader, Class<?> type, String schemaPath) throws SAXException, URISyntaxException {
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        if (schemaPath != null) {
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(new File(getClass().getResource(schemaPath).toURI()));
            unmarshaller.setSchema(schema);
        }
        return unmarshaller.unmarshal(reader);
    } catch (JAXBException e) {
        throw new RuntimeException("Unable to unmarshall orcid message" + e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File)

Example 55 with JAXBContext

use of javax.xml.bind.JAXBContext in project opennms by OpenNMS.

the class JdbcDataCollectionConfigTest method generateSchema.

@Test
public void generateSchema() throws Exception {
    File schemaFile = fa.expecting("jdbc-datacollection-config.xsd");
    JAXBContext c = JAXBContext.newInstance(JdbcDataCollectionConfig.class);
    c.generateSchema(new TestOutputResolver(schemaFile));
    if (fa.isInitialized()) {
        fa.deleteExpected();
    }
}
Also used : JAXBContext(javax.xml.bind.JAXBContext) File(java.io.File) Test(org.junit.Test)

Aggregations

JAXBContext (javax.xml.bind.JAXBContext)1150 Unmarshaller (javax.xml.bind.Unmarshaller)558 Marshaller (javax.xml.bind.Marshaller)371 JAXBException (javax.xml.bind.JAXBException)330 Test (org.junit.Test)226 File (java.io.File)161 InputStream (java.io.InputStream)150 StringWriter (java.io.StringWriter)138 IOException (java.io.IOException)105 ByteArrayOutputStream (java.io.ByteArrayOutputStream)76 StringReader (java.io.StringReader)74 StreamSource (javax.xml.transform.stream.StreamSource)73 JAXBElement (javax.xml.bind.JAXBElement)72 ArrayList (java.util.ArrayList)58 URL (java.net.URL)55 ByteArrayInputStream (java.io.ByteArrayInputStream)50 Schema (javax.xml.validation.Schema)48 SchemaFactory (javax.xml.validation.SchemaFactory)48 OutputStream (java.io.OutputStream)42 BaseTest (org.orcid.core.BaseTest)39