Example usage for javax.xml.bind JAXBContext createMarshaller

List of usage examples for javax.xml.bind JAXBContext createMarshaller

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext createMarshaller.

Prototype

public abstract Marshaller createMarshaller() throws JAXBException;

Source Link

Document

Create a Marshaller object that can be used to convert a java content tree into XML data.

Usage

From source file:eu.modaclouds.sla.mediator.Utils.java

public static <E> void print(E e, OutputStream os, Class<?>... classesToBeBound) throws JAXBException {
    JAXBContext jaxbContext;
    Marshaller jaxbMarshaller;/*from  w w w.  ja  v  a2s.c o m*/
    jaxbContext = JAXBContext.newInstance(classesToBeBound);
    jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    /*
     * http://stackoverflow.com/a/22756191
     */
    jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
    jaxbMarshaller.marshal(e, os);
}

From source file:net.firejack.platform.core.utils.FileUtils.java

public static <T> void writeJAXB(T obj, OutputStream stream, Class... classes)
        throws IOException, JAXBException {

    OutputFormat outputFormat = new OutputFormat();
    outputFormat.setCDataElements(new String[] { CDATA_DESCRIPTION });
    outputFormat.setIndenting(true);/*from   w  w  w  .  j av  a 2 s.c  o  m*/

    XMLSerializer serializer = new XMLSerializer(outputFormat);
    serializer.setOutputByteStream(stream);

    if (classes.length == 0) {
        classes = (Class[]) ArrayUtils.add(classes, obj.getClass());
    }

    JAXBContext jaxbContext = JAXBContext.newInstance(classes);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(obj, serializer.asContentHandler());
}

From source file:cool.pandora.modeller.DocManifestBuilder.java

/**
 * marshal./* ww  w .j av  a2 s .c om*/
 *
 * @param hocr File
 * @return hocr
 * @throws JAXBException Exception
 */
private static ByteArrayOutputStream marshal(final File hocr) throws JAXBException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final JAXBContext jaxbContext = JAXBContext.newInstance(File.class);
    final Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(hocr, out);
    return out;
}

From source file:eu.seaclouds.platform.planner.core.application.decorators.SeaCloudsManagementPolicyDecorator.java

private static String encodeBase64MonitoringRules(MonitoringInfo monitoringInfo) {
    StringWriter sw = new StringWriter();
    String encodeMonitoringRules;
    JAXBContext jaxbContext;
    String marshalledMonitoringRules = null;
    try {// w  w  w  . ja  v a2 s.  co m
        jaxbContext = JAXBContext.newInstance(MonitoringRules.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        jaxbMarshaller.marshal(monitoringInfo.getApplicationMonitoringRules(), sw);
        marshalledMonitoringRules = sw.toString();
    } catch (JAXBException e) {
        log.error("Monitoring rules {} can not be marshalled by addSeaCloudsPolicy in " + "DamGenerator",
                monitoringInfo.getApplicationMonitoringRules());
    }
    encodeMonitoringRules = Base64.encodeBase64String(marshalledMonitoringRules.getBytes());
    return encodeMonitoringRules;
}

From source file:com.wavemaker.runtime.ws.HTTPBindingSupport.java

public static String convertToXMLString(Object o) {
    try {/*ww w. j a  v  a2 s . c o m*/
        JAXBContext context = JAXBContext.newInstance(o.getClass());
        Marshaller marshaller = context.createMarshaller();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        marshaller.marshal(o, os);
        return new String(os.toByteArray());
    } catch (JAXBException e) {
        throw new WebServiceInvocationException(e);
    }
}

From source file:br.ufpb.dicomflow.integrationAPI.tools.ReadService.java

private static void saveMessages(List<MessageIF> messages, File destDir) throws JAXBException, IOException {
    Iterator<MessageIF> it = messages.iterator();
    while (it.hasNext()) {
        MessageIF messageIF = (MessageIF) it.next();
        ServiceIF service = messageIF.getService();
        Logger.v(rb.getString("loaded-service") + service.getName() + " - " + service.getAction() + " - "
                + service.getMessageID());

        JAXBContext jaxbContext = JAXBContext.newInstance(messageIF.getService().getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        FileOutputStream fos = new FileOutputStream(destDir.getAbsolutePath() + File.pathSeparator
                + service.getName() + "_" + service.getAction() + "_" + service.getMessageID());
        jaxbMarshaller.marshal(messageIF.getService(), fos);

        if (messageIF.getAttach() != null && messageIF.getAttach().length > 0) {
            fos = new FileOutputStream(destDir.getAbsolutePath() + File.pathSeparator + service.getName() + "_"
                    + service.getAction() + "_" + service.getMessageID() + "_attach");
            fos.write(messageIF.getAttach());
            fos.close();/*from  w w w.  jav a  2s .  com*/
        }

    }
}

From source file:com.sarm.lonelyplanet.process.DestinationProcessorTest.java

/**
 * This method is not a JUnit test method. This is used to create an XML
 * file of 30000 destinations and to regressively test the UnMarshalling
 * technique used in the LPUnMarshaller. This is called by the test method :
 * testProcessDestinationByStAXfor30000 to test the 30000 destinations.
 *
 * @param destinationFileName//w  ww.  j  av  a 2  s  .  c o m
 * @throws JAXBException
 * @throws FileNotFoundException
 * @throws XMLStreamException
 * @throws UnsupportedEncodingException
 */
public static void testMarshall(String destinationFileName)
        throws JAXBException, FileNotFoundException, XMLStreamException, UnsupportedEncodingException {
    DestinationProcessor destinationProcessor = new DestinationProcessor();
    List<Destination> destinations = destinationProcessor.processDestinationByStAX(destinationFileName);
    List<Destination> tempDestinations = new ArrayList<>();
    for (int i = 0; i < 1500; i++) {
        for (Destination destination : destinations) {
            Destination temp = new Destination();
            temp = destination.clone();
            temp.setTitle(temp.getTitle() + i);
            tempDestinations.add(temp);
        }
    }
    Destinations destinationList = new Destinations();
    destinationList.setDestinations(tempDestinations);
    logger.info(" destinationList " + destinationList.getDestinations().size());

    JAXBContext jc = JAXBContext.newInstance(Destinations.class);
    logger.debug(jc.getClass());
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(destinationList, new File(regressDestinationfile));
    logger.info("Successfully created regressDestinationfile " + regressDestinationfile);

}

From source file:com.evolveum.midpoint.testing.model.client.sample.RunScript.java

private static String marshalObject(Object object) throws JAXBException, FileNotFoundException {
    JAXBContext jc = getJaxbContext();
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter sw = new StringWriter();
    marshaller.marshal(object, sw);/*from  w  w w .  j a v  a 2s . c  o m*/
    return sw.toString();
}

From source file:edu.harvard.lib.lcloud.SolrItem.java

protected static void marshalingExample(Item item) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Item.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(item, System.out);

}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static void marshal(Object object, Writer w) throws JAXBException {
    Class clazz = object.getClass();
    JAXBContext context = s_contexts.get(clazz);
    if (context == null) {
        context = JAXBContext.newInstance(clazz);
        s_contexts.put(clazz, context);/*from  w  w  w .ja  v a2  s .  co  m*/
    }

    ValidationEventCollector valEventHndlr = new ValidationEventCollector();
    Marshaller marshaller = context.createMarshaller();
    marshaller.setSchema(null);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setEventHandler(valEventHndlr);

    try {
        marshaller.marshal(object, w);
    } catch (Exception e) {
        if (e instanceof JAXBException) {
            throw (JAXBException) e;
        } else {
            throw new MarshalException(e.getMessage(), e);
        }
    }
    if (valEventHndlr.hasEvents()) {
        for (ValidationEvent valEvent : valEventHndlr.getEvents()) {
            if (valEvent.getSeverity() != ValidationEvent.WARNING) {
                // throw a new Marshall Exception if there is a parsing error
                throw new MarshalException(valEvent.getMessage(), valEvent.getLinkedException());
            }
        }
    }
}