Search in sources :

Example 36 with JAXBContext

use of javax.xml.bind.JAXBContext in project openhab1-addons by openhab.

the class DenonConnector method postDocument.

private <T, S> T postDocument(String uri, Class<T> response, S request) {
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(request.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        StringWriter sw = new StringWriter();
        jaxbMarshaller.marshal(request, sw);
        String result = doHttpRequest("POST", uri, sw.toString());
        if (StringUtils.isNotBlank(result)) {
            JAXBContext jcResponse = JAXBContext.newInstance(response);
            @SuppressWarnings("unchecked") T obj = (T) jcResponse.createUnmarshaller().unmarshal(IOUtils.toInputStream(result));
            return obj;
        }
    } catch (JAXBException e) {
        logger.debug("Encoding error in post", e);
    }
    return null;
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext)

Example 37 with JAXBContext

use of javax.xml.bind.JAXBContext in project platformlayer by platformlayer.

the class MarshallerContextResolver method getContext.

@Override
public Marshaller getContext(Class<?> clazz) {
    if (clazz.equals(ManagedItemCollection.class)) {
    // OK
    } else if (ItemBase.class.isAssignableFrom(clazz)) {
    // OK
    } else {
        return null;
    }
    JAXBContext jaxbContext = jaxbContextHelper.getJaxbContext(clazz);
    try {
        Marshaller m = jaxbContext.createMarshaller();
        // m.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl());
        m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl());
        return m;
    } catch (JAXBException e) {
        throw new IllegalStateException("Error creating XML marshaller", e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) ItemBase(org.platformlayer.core.model.ItemBase) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext)

Example 38 with JAXBContext

use of javax.xml.bind.JAXBContext in project platformlayer by platformlayer.

the class ServiceResource method getSchema.

@GET
@Path("schema")
@Produces({ XML })
public String getSchema() throws IOException, JAXBException {
    ServiceProvider serviceProvider = getServiceProvider();
    String namespace = null;
    List<Class<?>> javaClasses = Lists.newArrayList();
    for (ModelClass<?> modelClass : serviceProvider.getModels().all()) {
        javaClasses.add(modelClass.getJavaClass());
        String modelNamespace = modelClass.getPrimaryNamespace();
        if (namespace == null) {
            namespace = modelNamespace;
        } else if (!namespace.equals(modelNamespace)) {
            throw new IllegalStateException();
        }
    }
    JAXBContext jaxbContext = JAXBContext.newInstance(javaClasses.toArray(new Class<?>[javaClasses.size()]));
    MemorySchemaOutputResolver schemaOutputResolver = new MemorySchemaOutputResolver();
    jaxbContext.generateSchema(schemaOutputResolver);
    Map<String, StringWriter> writers = schemaOutputResolver.getWriters();
    StringWriter writer = writers.get(namespace);
    if (writer == null) {
        throw new IllegalArgumentException();
    }
    return writer.getBuffer().toString();
}
Also used : MemorySchemaOutputResolver(org.platformlayer.xml.MemorySchemaOutputResolver) StringWriter(java.io.StringWriter) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) ModelClass(org.platformlayer.xaas.services.ModelClass) JAXBContext(javax.xml.bind.JAXBContext) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 39 with JAXBContext

use of javax.xml.bind.JAXBContext in project spring-framework by spring-projects.

the class AbstractJaxb2HttpMessageConverter method createMarshaller.

/**
	 * Create a new {@link Marshaller} for the given class.
	 * @param clazz the class to create the marshaller for
	 * @return the {@code Marshaller}
	 * @throws HttpMessageConversionException in case of JAXB errors
	 */
protected final Marshaller createMarshaller(Class<?> clazz) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        Marshaller marshaller = jaxbContext.createMarshaller();
        customizeMarshaller(marshaller);
        return marshaller;
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not create Marshaller for class [" + clazz + "]: " + ex.getMessage(), ex);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) JAXBContext(javax.xml.bind.JAXBContext)

Example 40 with JAXBContext

use of javax.xml.bind.JAXBContext in project camel by apache.

the class CamelContextFactoryBeanTest method contextAsString.

private String contextAsString(CamelContextFactoryBean context) throws Exception {
    StringWriter stringOut = new StringWriter();
    JAXBContext jaxb = JAXBContext.newInstance(CamelContextFactoryBean.class);
    jaxb.createMarshaller().marshal(context, stringOut);
    return stringOut.toString();
}
Also used : StringWriter(java.io.StringWriter) JAXBContext(javax.xml.bind.JAXBContext)

Aggregations

JAXBContext (javax.xml.bind.JAXBContext)442 Unmarshaller (javax.xml.bind.Unmarshaller)240 Marshaller (javax.xml.bind.Marshaller)129 JAXBException (javax.xml.bind.JAXBException)128 Test (org.junit.Test)92 InputStream (java.io.InputStream)84 File (java.io.File)50 StringWriter (java.io.StringWriter)47 BaseTest (org.orcid.core.BaseTest)39 V2Convertible (org.orcid.core.version.V2Convertible)39 StringReader (java.io.StringReader)27 IOException (java.io.IOException)26 InputSource (org.xml.sax.InputSource)21 ArrayList (java.util.ArrayList)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 JAXBElement (javax.xml.bind.JAXBElement)19 FileOutputStream (java.io.FileOutputStream)18 Schema (javax.xml.validation.Schema)18 SchemaFactory (javax.xml.validation.SchemaFactory)17 SAXSource (javax.xml.transform.sax.SAXSource)14