Search in sources :

Example 71 with Marshaller

use of javax.xml.bind.Marshaller in project cloudstack by apache.

the class BrocadeVcsApi method convertToString.

protected <T> String convertToString(T object) throws BrocadeVcsApiException {
    final StringWriter stringWriter = new StringWriter();
    try {
        final JAXBContext context = JAXBContext.newInstance(object.getClass());
        final Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(object, stringWriter);
    } catch (final JAXBException e) {
        s_logger.error("Failed to convert object to string : " + e.getMessage());
        throw new BrocadeVcsApiException("Failed to convert object to string : " + e.getMessage());
    }
    final String str = stringWriter.toString();
    s_logger.info(str);
    return str;
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext)

Example 72 with Marshaller

use of javax.xml.bind.Marshaller in project asterixdb by apache.

the class ConfigureConfig method execCommand.

@Override
protected void execCommand() throws Exception {
    configureCluster("local", "local.xml");
    configureCluster("local", "local_chained_declustering_rep.xml");
    configureCluster("local", "local_metadata_only_rep.xml");
    configureCluster("demo", "demo.xml");
    String installerConfPath = InstallerDriver.getManagixHome() + File.separator + InstallerDriver.MANAGIX_CONF_XML;
    JAXBContext ctx = JAXBContext.newInstance(Configuration.class);
    Unmarshaller unmarshaller = ctx.createUnmarshaller();
    Configuration configuration = (Configuration) unmarshaller.unmarshal(new File(installerConfPath));
    configuration.setConfigured(true);
    configuration.getBackup().setBackupDir(InstallerDriver.getManagixHome() + File.separator + "backup");
    configuration.getZookeeper().setHomeDir(InstallerDriver.getManagixHome() + File.separator + InstallerDriver.MANAGIX_INTERNAL_DIR + File.separator + "zookeeper_home");
    configuration.getZookeeper().getServers().setJavaHome(System.getProperty("java.home"));
    Marshaller marshaller = ctx.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(configuration, new FileOutputStream(installerConfPath));
}
Also used : Marshaller(javax.xml.bind.Marshaller) Configuration(org.apache.asterix.installer.schema.conf.Configuration) FileOutputStream(java.io.FileOutputStream) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File)

Example 73 with Marshaller

use of javax.xml.bind.Marshaller 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 74 with Marshaller

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

the class Jaxb2RootElementHttpMessageConverter method writeToResult.

@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws IOException {
    try {
        Class<?> clazz = ClassUtils.getUserClass(o);
        Marshaller marshaller = createMarshaller(clazz);
        setCharset(headers.getContentType(), marshaller);
        marshaller.marshal(o, result);
    } catch (MarshalException ex) {
        throw new HttpMessageNotWritableException("Could not marshal [" + o + "]: " + ex.getMessage(), ex);
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) MarshalException(javax.xml.bind.MarshalException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JAXBException(javax.xml.bind.JAXBException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException)

Example 75 with Marshaller

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

the class Jaxb2XmlEncoder method encode.

@Override
protected Flux<DataBuffer> encode(Object value, DataBufferFactory dataBufferFactory, ResolvableType type, MimeType mimeType, Map<String, Object> hints) {
    try {
        DataBuffer buffer = dataBufferFactory.allocateBuffer(1024);
        OutputStream outputStream = buffer.asOutputStream();
        Class<?> clazz = ClassUtils.getUserClass(value);
        Marshaller marshaller = jaxbContexts.createMarshaller(clazz);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());
        marshaller.marshal(value, outputStream);
        return Flux.just(buffer);
    } catch (JAXBException ex) {
        return Flux.error(ex);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) OutputStream(java.io.OutputStream) JAXBException(javax.xml.bind.JAXBException) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

Marshaller (javax.xml.bind.Marshaller)280 JAXBContext (javax.xml.bind.JAXBContext)162 JAXBException (javax.xml.bind.JAXBException)100 StringWriter (java.io.StringWriter)82 Test (org.junit.Test)33 JAXBElement (javax.xml.bind.JAXBElement)32 ByteArrayOutputStream (java.io.ByteArrayOutputStream)31 File (java.io.File)29 Unmarshaller (javax.xml.bind.Unmarshaller)21 IOException (java.io.IOException)20 FileOutputStream (java.io.FileOutputStream)19 ByteArrayInputStream (java.io.ByteArrayInputStream)15 QName (javax.xml.namespace.QName)14 Element (org.w3c.dom.Element)14 HashMap (java.util.HashMap)12 Writer (java.io.Writer)11 Document (org.w3c.dom.Document)11 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)7 ArrayList (java.util.ArrayList)7