Example usage for javax.xml.bind Marshaller marshal

List of usage examples for javax.xml.bind Marshaller marshal

Introduction

In this page you can find the example usage for javax.xml.bind Marshaller marshal.

Prototype

public void marshal(Object jaxbElement, javax.xml.stream.XMLEventWriter writer) throws JAXBException;

Source Link

Document

Marshal the content tree rooted at jaxbElement into a javax.xml.stream.XMLEventWriter .

Usage

From source file:eu.crisis_economics.abm.dashboard.cluster.script.BashScheduler.java

/** {@inheritDoc} 
 * @throws SchedulerException /*from   w w  w  . j  a v a2 s. co  m*/
 */
@Override
public String runParameterSweep(final Model paramSweepConfig, final String timeLimit, final File workDir)
        throws SchedulerException {

    File file = null;
    try {
        //         file = File.createTempFile("paramsweep-", ".xml");
        file = new File(workDir, "paramsweep-config.xml");
        Marshaller marshaller = JAXBContext.newInstance(Model.class).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(paramSweepConfig, file);
        //      } catch (IOException e) {
        //         throw new SchedulerException("Could not create temporary parameter-sweep configuration xml.", e);
    } catch (JAXBException e) {
        throw new SchedulerException(
                "Could not write temporary parameter-sweep configuration xml: " + file.toString(), e);
    }
    CommandLine cmd = new CommandLine(cmdFile);
    Map<String, Object> substitutions = new HashMap<String, Object>();
    substitutions.put(CMD_SUBSTITUTION_NAME_FILE, file);
    cmd.setSubstitutionMap(substitutions);

    if (timeLimit != null && !timeLimit.isEmpty()) {
        cmd.addArgument("-t", false);
        cmd.addArgument(timeLimit, false);
    }

    // add server port argument
    cmd.addArgument("-p", false);
    cmd.addArgument(String.valueOf(serverPort), false);

    cmd.addArgument("${" + CMD_SUBSTITUTION_NAME_FILE + "}", false);

    DefaultExecutor executor = new DefaultExecutor();
    executor.setWorkingDirectory(workDir);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(byteArrayOutputStream);

    executor.setStreamHandler(streamHandler);

    try {
        executor.execute(cmd);
    } catch (ExecuteException e) {
        throw new SchedulerException(
                paramSweepCmd + " exited with " + e.getExitValue() + ". Output:\n" + byteArrayOutputStream, e);
    } catch (IOException e) {
        throw new SchedulerException(
                "Execution of " + paramSweepCmd + " failed. Output:\n" + byteArrayOutputStream, e);
    }

    // the standard output of the script is the job id
    final String jobId = byteArrayOutputStream.toString();

    return jobId;
}

From source file:esg.node.components.registry.ShardsListGleaner.java

public synchronized boolean saveShardsList(Shards shardlist) {
    boolean success = false;
    if (shardlist == null) {
        log.error("shardlist is null ? [" + shardlist + "]");
        return success;
    }//from  w w w.  ja  va 2 s.c  om
    log.trace("Saving SHARDS list information to " + shardsListPath + shardsListFile);
    try {
        JAXBContext jc = JAXBContext.newInstance(Shards.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(shardlist, new FileOutputStream(shardsListPath + shardsListFile));
        success = true;
    } catch (Exception e) {
        log.error(e);
    }

    return success;
}

From source file:com.mgmtp.perfload.loadprofiles.ui.ctrl.ConfigController.java

public void saveActiveSettings() {
    checkState(activeSettingsFile != null, "No active settings file set.");

    Writer wr = null;//from   w  w  w. j  a  v  a2 s  . c o m
    File file = new File(settingsDir, activeSettingsFile);
    file.getParentFile().mkdir();

    try {
        wr = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        JAXBContext context = JAXBContext.newInstance(Settings.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(getActiveSettings(), wr);
    } catch (JAXBException ex) {
        String msg = "Error marshalling contents to file: " + file;
        throw new LoadProfileException(msg, ex);
    } catch (IOException ex) {
        throw new LoadProfileException(ex.getMessage(), ex);
    } finally {
        Closeables.closeQuietly(wr);
    }
}

From source file:dk.dma.epd.common.prototype.communication.webservice.ShoreHttp.java

public void setXmlMarshalContent(String contextPath, Object obj)
        throws JAXBException, UnsupportedEncodingException {
    JAXBContext jc = JAXBContext.newInstance(contextPath);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_ENCODING, ENCODING);
    StringWriter sw = new StringWriter();
    m.marshal(obj, sw);
    String req = sw.toString();//from   w ww.j a v  a 2 s  .  co m
    LOG.debug("XML request: " + req);
    setRequestBody(sw.toString().getBytes(ENCODING), ENCODING);
}

From source file:esg.node.components.registry.AtsWhitelistGleaner.java

public synchronized boolean saveAtsWhitelist(AtsWhitelist atss) {
    boolean success = false;
    if (atss == null) {
        log.error("atss (whitelist) is null ? [" + atss + "]");
        return success;
    }/*from www  .  j  av a  2s.com*/
    log.trace("Saving ATS Whitelist information to " + atsWhitelistPath + atsWhitelistFile);
    try {
        JAXBContext jc = JAXBContext.newInstance(AtsWhitelist.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(atss, new FileOutputStream(atsWhitelistPath + atsWhitelistFile));
        success = true;
    } catch (Exception e) {
        log.error(e);
    }

    return success;
}

From source file:com.azaptree.services.command.http.handler.WebXmlRequestCommandServiceHandler.java

private void generateWADL(final Request baseRequest, final HttpServletResponse response) {
    // TODO: implement HTTP caching
    response.setStatus(HttpStatus.OK_200);
    response.setContentType("application/vnd.sun.wadl+xml");

    final Application app = new Application();
    app.setGrammars(createWadlGrammars());
    for (String catalogName : commandService.getCommandCatalogNames()) {
        app.getResources().add(createWadlResources(catalogName));
    }//from   w  ww  . ja  v a2s . c om

    try {
        final JAXBContext wadlJaxbContext = JAXBContext.newInstance(Application.class.getPackage().getName());
        final Marshaller marshaller = wadlJaxbContext.createMarshaller();
        marshaller.marshal(app, response.getOutputStream());
    } catch (JAXBException | IOException e) {
        throw new IllegalStateException("Failed to marshall WADL to HTTP response output stream", e);
    }

    baseRequest.setHandled(true);
}

From source file:com.floreantpos.model.PosPrinters.java

public void save() {
    try {/*  www  .  ja va 2  s .c o  m*/
        getDefaultKitchenPrinter();

        populatePrinterMaps();

        File file = new File("config", "printers.xml"); //$NON-NLS-1$ //$NON-NLS-2$

        JAXBContext jaxbContext = JAXBContext.newInstance(PosPrinters.class);
        Marshaller m = jaxbContext.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        StringWriter writer = new StringWriter();
        m.marshal(this, writer);

        FileUtils.write(file, writer.toString());

    } catch (Exception e) {
        PosLog.error(getClass(), e);
    }
}

From source file:hydrograph.ui.graph.debugconverter.DebugConverter.java

public void marshall(Debug debug, IFile outPutFile) throws JAXBException, IOException, CoreException {

    JAXBContext jaxbContext = JAXBContext.newInstance(Debug.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    marshaller.marshal(debug, out);

    if (outPutFile.exists())
        outPutFile.setContents(new ByteArrayInputStream(out.toByteArray()), true, false, null);
    else/*from  w w w  .  j a  va 2  s  . com*/
        outPutFile.create(new ByteArrayInputStream(out.toByteArray()), true, null);
    out.close();
}

From source file:edu.htwm.vsp.phoebook.rest.client.RestClient.java

/**
 * Example code for serializing a PhoneUser via JAXB.
 *///  ww  w. ja  v a 2 s.  co  m
public void marshalPhoneUserToXML(PhoneUser user, Writer out) throws JAXBException {

    JAXBContext jc = JAXBContext.newInstance(PhoneUser.class);
    assertNotNull(jc);
    Marshaller marshaller = jc.createMarshaller();

    assertNotNull(marshaller);

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
    marshaller.marshal(user, out);
}

From source file:org.javelin.sws.ext.bind.jaxb.JaxbTest.java

@Test(expected = IllegalAnnotationsException.class)
public void marshalMixed() throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(org.javelin.sws.ext.bind.jaxb.context4.MyClassJ4.class);
    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    org.javelin.sws.ext.bind.jaxb.context4.MyClassJ4 mc4 = new org.javelin.sws.ext.bind.jaxb.context4.MyClassJ4();
    m.marshal(new JAXBElement<org.javelin.sws.ext.bind.jaxb.context4.MyClassJ4>(new QName("a", "a"),
            org.javelin.sws.ext.bind.jaxb.context4.MyClassJ4.class, mc4), System.out);
}