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.impress.repository.service.BedAvailabilityServiceImpl.java

@Override
public String getBedTypeAllAvailablityHAVE() {
    String hospitalstatushave;//from  ww  w  .j a v a 2 s  . co  m
    List<eu.impress.repository.model.BedStats> bedStatsList = bedService.getHospitalAllAvailableBedTypes();
    HospitalStatus hospitalStatus = beansTransformation.BedTypesStatstoHAVE(bedStatsList);

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

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

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

    return hospitalstatushave;

}

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

@Override
public String getBedTypeAvailablityHAVE(String hospitalname) {
    String hospitalstatushave;//from  w w w .j av a2s.co  m
    List<eu.impress.repository.model.BedStats> bedStatsList = bedService
            .getHospitalAvailableBedTypes(hospitalname);
    HospitalStatus hospitalStatus = beansTransformation.BedTypesStatstoHAVE(bedStatsList);

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

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

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

    return hospitalstatushave;

}

From source file:com.quangphuong.crawler.util.XMLUtil.java

public <T> void marshallUtil(String xmlPath, T entityClass) {
    try {//from  w ww  . j  a  v a2  s .co  m
        JAXBContext cxt = JAXBContext.newInstance(entityClass.getClass());
        Marshaller marshaller = cxt.createMarshaller();
        marshaller.setProperty(marshaller.JAXB_FORMATTED_OUTPUT, true);
        File file = new File(rootPath, xmlPath);
        System.out.println("Path: " + file.getAbsolutePath());
        marshaller.marshal(entityClass, file);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ca.ualberta.physics.cssdp.catalogue.resource.ProjectResourceTest.java

@Test
public void testCreateGetAndDeleteWithJSONAndXML() throws Exception {

    Project project = new Project();
    project.setExternalKey(Mnemonic.of("TEST"));
    project.setHost("localhost");
    project.setName("Test Project");

    Observatory o = new Observatory();
    o.setExternalKey(Mnemonic.of("TEST-OBSERV"));
    o.setDescription("Test Observatory");
    o.setProject(project);/*from  w w w. j a va  2  s  . c  o m*/
    o.setLocation(10d, 10d);
    project.getObservatories().add(o);

    InstrumentType i = new InstrumentType();
    i.setExternalKey(Mnemonic.of("TEST-INST"));
    i.setDescription("Test Instrument Type");
    i.setProject(project);
    project.getInstrumentTypes().add(i);

    DataProduct dp = new DataProduct();
    dp.setExternalKey(Mnemonic.of("TEST-DP"));
    dp.setDescription("Test Data Product");
    dp.setInstrumentTypes(project.getInstrumentTypes());
    dp.addObservatories(project.getObservatories());
    dp.setProject(project);
    project.getDataProducts().add(dp);

    String projectJSON = mapper.writeValueAsString(project);
    System.out.println(projectJSON);
    Response res = given().body(projectJSON).and().contentType("application/json").expect().statusCode(201)
            .when().post(ResourceUrls.PROJECT);

    Project createdProject = get(res.getHeader("location")).as(Project.class);

    Assert.assertNotNull(createdProject.getId());
    for (Observatory obs : createdProject.getObservatories()) {
        Assert.assertNotNull(obs.getId());
        Assert.assertTrue(obs.getId().toString(), obs.getId() > 0);
    }
    for (InstrumentType it : createdProject.getInstrumentTypes()) {
        Assert.assertNotNull(it.getId());
        Assert.assertTrue(it.getId().toString(), it.getId() > 0);
    }
    for (DataProduct dataProduct : createdProject.getDataProducts()) {
        Assert.assertNotNull(dataProduct.getId());
        Assert.assertTrue(dataProduct.getId().toString(), dataProduct.getId() > 0);
        for (Observatory o2 : dataProduct.getObservatories()) {
            Assert.assertNotNull(o2.getId());
            Assert.assertTrue(o2.getId().toString(), o2.getId() > 0);
        }
        for (InstrumentType it2 : dataProduct.getInstrumentTypes()) {
            Assert.assertNotNull(it2.getId());
            Assert.assertTrue(it2.getId().toString(), it2.getId() > 0);
        }

    }

    expect().statusCode(200).when().delete(res.getHeader("location"));
    expect().statusCode(404).when().get(res.getHeader("location"));

    // same test with XML now
    project.setExternalKey(Mnemonic.of("TEST2"));

    StringWriter writer = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(Project.class);
    Marshaller m = context.createMarshaller();
    m.marshal(project, writer);

    String xml = writer.toString();
    System.out.println(xml);
    res = given().content(xml).and().contentType("application/xml").expect().statusCode(201).when()
            .contentType(ContentType.XML).post(ResourceUrls.PROJECT);

    createdProject = get(res.getHeader("location")).as(Project.class);

    Assert.assertNotNull(createdProject.getId());
    for (Observatory obs : createdProject.getObservatories()) {
        Assert.assertNotNull(obs.getId());
        Assert.assertTrue(obs.getId().toString(), obs.getId() > 0);
    }
    for (InstrumentType it : createdProject.getInstrumentTypes()) {
        Assert.assertNotNull(it.getId());
        Assert.assertTrue(it.getId().toString(), it.getId() > 0);
    }
    for (DataProduct dataProduct : createdProject.getDataProducts()) {
        Assert.assertNotNull(dataProduct.getId());
        Assert.assertTrue(dataProduct.getId().toString(), dataProduct.getId() > 0);
        for (Observatory o2 : dataProduct.getObservatories()) {
            Assert.assertNotNull(o2.getId());
            Assert.assertTrue(o2.getId().toString(), o2.getId() > 0);
        }
        for (InstrumentType it2 : dataProduct.getInstrumentTypes()) {
            Assert.assertNotNull(it2.getId());
            Assert.assertTrue(it2.getId().toString(), it2.getId() > 0);
        }

    }

    expect().statusCode(200).when().delete(res.getHeader("location"));
    expect().statusCode(404).when().get(res.getHeader("location"));

}

From source file:fr.fastconnect.factory.tibco.bw.maven.compile.ArchiveBuilder.java

public void save(File f) {
    try {/*from w w w  . java  2 s.c o m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
        Marshaller m = jaxbContext.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(this.repository, f);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:com.quangphuong.crawler.util.XMLUtil.java

public <T> String marshallWithoutFile(T entityClass) {
    try {//from w w  w  .  ja va  2  s.  c o  m
        JAXBContext cxt = JAXBContext.newInstance(entityClass.getClass());

        Marshaller marshaller = cxt.createMarshaller();
        marshaller.setProperty(marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(marshaller.JAXB_ENCODING, "UTF-8");
        StringWriter sw = new StringWriter();
        marshaller.marshal(entityClass, sw);
        return sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:eu.learnpad.core.impl.cw.XwikiBridgeInterfaceRestResource.java

@Override
public void notifyScoreUpdate(ScoreRecord record) throws LpRestException {
    String contentType = "application/xml";
    HttpClient httpClient = this.getClient();
    String uri = String.format("%s/learnpad/cw/bridge/scores", DefaultRestResource.REST_URI);
    PostMethod postMethod = new PostMethod(uri);
    postMethod.addRequestHeader(HttpHeaders.CONTENT_TYPE, contentType);

    try {//from w  w w .jav  a2 s  .c o  m
        JAXBContext jc = JAXBContext.newInstance(ScoreRecord.class);
        Writer marshalledRecord = new StringWriter();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(record, marshalledRecord);

        postMethod.setRequestEntity(new StringRequestEntity(marshalledRecord.toString(), contentType, "UTF-8"));

        httpClient.executeMethod(postMethod);
    } catch (JAXBException e1) {
        e1.printStackTrace();
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.jaspersoft.jasperserver.jaxrs.client.apiadapters.jobs.BatchJobsOperationsAdapter.java

private String buildXml(ReportJobModel reportJobModel) {
    try {/*from ww w  .j  a v  a2 s  .com*/
        StringWriter writer = new StringWriter();
        JAXBContext jaxbContext = JAXBContext.newInstance(ReportJobModel.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(reportJobModel, writer);
        return writer.toString();
    } catch (JAXBException e) {
        log.warn("Can't marshal report job model.");
        throw new RuntimeException("Failed inFolder build report job model xml.", e);
    }
}

From source file:eu.learnpad.core.impl.cw.XwikiBridgeInterfaceRestResource.java

@Override
public void notifyRecommendations(String modelSetId, String simulationid, String userId, Recommendations rec)
        throws LpRestException {
    String contentType = "application/xml";

    HttpClient httpClient = this.getClient();
    String uri = String.format("%s/learnpad/cw/bridge/notify/%s", DefaultRestResource.REST_URI, modelSetId);

    PutMethod putMethod = new PutMethod(uri);
    putMethod.addRequestHeader("Accept", contentType);

    NameValuePair[] queryString = new NameValuePair[2];
    queryString[0] = new NameValuePair("simulationid", simulationid);
    queryString[1] = new NameValuePair("userid", userId);
    putMethod.setQueryString(queryString);

    try {// w ww .j a v  a2 s .  c  o m
        JAXBContext jc = JAXBContext.newInstance(Recommendations.class);
        Writer recWriter = new StringWriter();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(rec, recWriter);

        RequestEntity requestEntity = new StringRequestEntity(recWriter.toString(), contentType, "UTF-8");
        putMethod.setRequestEntity(requestEntity);

        httpClient.executeMethod(putMethod);
    } catch (JAXBException | IOException e) {
        throw new LpRestExceptionXWikiImpl(e.getMessage(), e.getCause());
    }
}

From source file:io.onedecision.engine.decisions.converter.DecisionModelConverter.java

private void writeAsXml(Object o, Writer writer) throws IOException {
    JAXBContext context;/*from  w  w w  .  j a  v a  2  s. co m*/
    try {
        context = JAXBContext.newInstance(o.getClass());
        Marshaller m = context.createMarshaller();
        m.marshal(o, writer);
    } catch (JAXBException e) {
        throw new IOException(e.getMessage(), e);
    }
}