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:net.ageto.gyrex.impex.persistence.cassandra.storage.CassandraRepositoryImpl.java

/**
 * Persist process configuration./*from w  w w  .  j  av a2 s.c o m*/
 * 
 * @param process
 */
public void insertOrUpdateProcess(IProcessConfig process) {

    JAXBContext context;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    try {
        context = JAXBContext.newInstance(ProcessConfig.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        // process / process step configuration xml formatted
        m.marshal(process, outputStream);

    } catch (JAXBException e) {
        throw new IllegalArgumentException(e.getMessage());
    }

    Mutator<String> mutator = HFactory.createMutator(getImpexKeyspace(), StringSerializer.get());
    // insert process configuration
    mutator.insert(process.getId(), columnFamilyProcesses,
            HFactory.createStringColumn(columnNameProcess, outputStream.toString()));
}

From source file:eu.impress.repository.service.BedAvailabilityServiceImpl.java

@Override
public String getBedAvailablityHAVE(String hospitalname) {

    String hospitalstatushave;/*from   w w  w .  ja v a2  s.  co m*/
    BedStats bedStats = bedService.getHospitalAvailableBeds(hospitalname);
    HospitalStatus hospitalStatus = beansTransformation.BedStatstoHAVE(bedStats);

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(HospitalStatus.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        //jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter sw = new StringWriter();
        //marshal the envelope 
        jaxbMarshaller.marshal(hospitalStatus, sw);
        hospitalstatushave = sw.toString();

    } catch (JAXBException e) {
        e.printStackTrace();
        return "Error Marshalling XML Object";
    }

    return hospitalstatushave;

}

From source file:com.manydesigns.portofino.persistence.Persistence.java

public synchronized void saveXmlModel() throws IOException, JAXBException {
    //TODO gestire conflitti con modifiche esterne?
    File tempFile = File.createTempFile(appModelFile.getName(), "");

    JAXBContext jc = JAXBContext.newInstance(Model.JAXB_MODEL_PACKAGES);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(model, tempFile);

    ElementsFileUtils.moveFileSafely(tempFile, appModelFile.getAbsolutePath());
    lastLiquibaseRunTime = new Date(0);
    logger.info("Saved xml model to file: {}", appModelFile);
}

From source file:eu.impress.repository.service.BedAvailabilityServiceImpl.java

@Override
public String createBedAvailabilityDE() throws DatatypeConfigurationException {

    String DEmessageenvelope = "";
    String DEmessage = "";

    EDXLDistribution ed = EDXLlib.createEDXLEnvelope();

    try {// w  ww  . j av  a2 s  . c o  m
        JAXBContext jaxbContext = JAXBContext.newInstance(EDXLDistribution.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        //jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter sw = new StringWriter();
        //marshal the envelope 
        jaxbMarshaller.marshal(ed, sw);
        DEmessageenvelope = sw.toString();

        //could not unescape characters no matter what!
        //encapsulate the edxl have message into DE by avoiding jaxb 
        //DEmessage = EDXLlib.DEEncapsulation(DEmessageenvelope, edxlhave);
        DEmessage = DEmessageenvelope;

    } catch (JAXBException e) {
        e.printStackTrace();
        return "Error Marshalling XML Object";
    }

    return DEmessage;

}

From source file:org.opennms.netmgt.ncs.rest.AbstractSpringJerseyRestTestCase.java

protected void putXmlObject(JAXBContext context, String url, int expectedStatus, Object object)
        throws Exception {

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Marshaller marshaller = context.createMarshaller();
    marshaller.marshal(object, out);
    byte[] content = out.toByteArray();

    MockHttpServletRequest request = createRequest(PUT, url);
    request.setContentType(MediaType.APPLICATION_XML);
    request.setContent(content);//from   w  ww  . j  ava2 s .  com
    MockHttpServletResponse response = createResponse();
    dispatch(request, response);
    assertEquals(expectedStatus, response.getStatus());

}

From source file:de.thorstenberger.examServer.dao.xml.AbstractJAXBDao.java

/**
 * Serializes the object into xml. Will wrap any exception into a {@link RuntimeException}.
 *
 * @param obj//from w  w w.j  a v  a 2  s .  c  o  m
 *            the object to save.
 * @throws RuntimeException
 *             wrapping {@link JAXBException} of {@link IOException}
 */
synchronized protected void save(final Object obj) {
    log.debug(String.format("Trying to save xml package to file '%s'", workingPath + "/" + xmlFileName));
    final String txId = startTransaction();
    Marshaller marshaller = null;
    try {

        marshaller = JAXBUtils.getJAXBMarshaller(jc);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
        final BufferedOutputStream bos = new BufferedOutputStream(
                getFRM().writeResource(txId, this.xmlFileName));
        marshaller.marshal(obj, bos);

        bos.close();
        commitTransaction(txId);
    } catch (final JAXBException e) {
        rollback(txId, e);
        throw new RuntimeException(e);
    } catch (final IOException e) {
        rollback(txId, e);
        throw new RuntimeException(e);
    } catch (final ResourceManagerException e) {
        rollback(txId, e);
        throw new RuntimeException(e);
    } catch (RuntimeException e) {
        rollback(txId, e);
        throw e;
    } finally {
        if (marshaller != null)
            JAXBUtils.releaseJAXBMarshaller(jc, marshaller);
    }
}

From source file:com.tangfan.test.UserServiceTest.java

/**
 * add jaxws-ri???//from  w w  w. j a  va2 s  .  c o  m
 */
@Test
public void add() {
    try {
        //1.?xml
        JAXBContext jaxb = JAXBContext.newInstance(LicenseInfo.class);
        User lu = new User();
        lu.setId(0);
        lu.setNickname("");
        lu.setUsername("admin");
        lu.setPassword("123");
        LicenseInfo info = new LicenseInfo();
        info.setLoginUser(lu);

        QName qName = new QName(ns, "licenseInfo");
        JAXBElement<LicenseInfo> jele = new JAXBElement<LicenseInfo>(qName, LicenseInfo.class, info);

        Marshaller mars = jaxb.createMarshaller();
        mars.setProperty(Marshaller.JAXB_FRAGMENT, true);//xml
        mars.setProperty(Marshaller.JAXB_ENCODING, "utf-8");//?

        //2.?dom
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        mars.marshal(jele, doc);

        //3.Headers.create?header
        WSBindingProvider wsb = (WSBindingProvider) port;
        wsb.setOutboundHeaders(Headers.create(doc.getDocumentElement()));

        User u = new User();
        u.setId(2);
        u.setUsername("master");
        u.setNickname("jboss?");
        u.setPassword("123456");
        port.add(u);
    } catch (UserException_Exception e) {
        System.out.println(e.getMessage());
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}

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

@Test
public void unmarshalSameQNames() throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(org.javelin.sws.ext.bind.jaxb.context1.MyClassJ1.class,
            org.javelin.sws.ext.bind.jaxb.context2.MyClassJ2.class);
    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    log.info("context1");
    org.javelin.sws.ext.bind.jaxb.context1.MyClassJ1 mc1 = new org.javelin.sws.ext.bind.jaxb.context1.MyClassJ1();
    mc1.setP(new MyProperty1());
    m.marshal(new JAXBElement<org.javelin.sws.ext.bind.jaxb.context1.MyClassJ1>(new QName("a", "a"),
            org.javelin.sws.ext.bind.jaxb.context1.MyClassJ1.class, mc1), System.out);
    log.info("context2");
    org.javelin.sws.ext.bind.jaxb.context2.MyClassJ2 mc2 = new org.javelin.sws.ext.bind.jaxb.context2.MyClassJ2();
    mc2.setP(new MyProperty2());
    m.marshal(new JAXBElement<org.javelin.sws.ext.bind.jaxb.context2.MyClassJ2>(new QName("a", "a"),
            org.javelin.sws.ext.bind.jaxb.context2.MyClassJ2.class, mc2), System.out);

    Object object = ctx.createUnmarshaller().unmarshal(
            new StreamSource(
                    new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n"
                            + "<ns2:a xmlns=\"urn:x\" xmlns:ns2=\"a\">\r\n" + "    <p/>\r\n" + "</ns2:a>")),
            org.javelin.sws.ext.bind.jaxb.context2.MyClassJ2.class);
    log.info("Class: {}", ((JAXBElement<?>) object).getValue().getClass());
}

From source file:com.u2apple.tool.dao.DeviceXmlDaoJaxbImpl.java

private void flushVids() throws JAXBException, PropertyException {
    String vidFileFormat = Configuration.getProperty(Constants.VID_FILE_FORMAT);
    for (String vid : changedVids) {
        File file = new File(String.format(vidFileFormat, vid));
        JAXBContext jaxbContext = JAXBContext.newInstance(VID.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        VID v = getVid(getStaticMapFile().getVids(), vid);
        jaxbMarshaller.marshal(v, file);
    }//from   ww w  .  j  av a  2s  .  c  o m
    changedVids.clear();
}

From source file:net.ageto.gyrex.impex.console.internal.ImpexConsoleCommands.java

/**
 *
 * List process configurations./*  www.  j ava2 s  .  c o  m*/
 *
 * @param interpreter
 * @return
 */
public Object _impexListProcesses(final CommandInterpreter interpreter) {
    final String instanceShortcut = interpreter.nextArgument();
    IRuntimeContext context = getContext(instanceShortcut);
    if (context == null) {
        interpreter.println(INSTANCE_MISSING);
        return null;
    }

    // determine process from database
    final IProcessConfigManager processManager = context.get(IProcessConfigManager.class);
    ArrayList<IProcessConfig> processes = (ArrayList<IProcessConfig>) processManager.findAllProcesses();

    JAXBContext jaxbContext;

    try {
        for (IProcessConfig process : processes) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            jaxbContext = JAXBContext.newInstance(ProcessConfig.class);
            Marshaller m = jaxbContext.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(process, outputStream);

            final StringBuilder sb = new StringBuilder();
            sb.append("=========== " + process.getId() + " ===========\n");
            sb.append("\n");
            sb.append(outputStream.toString() + "\n");
            sb.append("\n");
            sb.append("=========== " + process.getId() + " ===========\n");
            interpreter.print(sb);

        }
    } catch (JAXBException e) {
        interpreter.printStackTrace(e);
    }

    return null;
}