Example usage for org.springframework.oxm Marshaller marshal

List of usage examples for org.springframework.oxm Marshaller marshal

Introduction

In this page you can find the example usage for org.springframework.oxm Marshaller marshal.

Prototype

void marshal(Object graph, Result result) throws IOException, XmlMappingException;

Source Link

Document

Marshal the object graph with the given root into the provided Result .

Usage

From source file:org.blanco.test.spring.xmltest.Main.java

public static void main(String args[]) throws XmlMappingException, IOException {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("/spring-config.xml");
    Marshaller mar = (Marshaller) ctx.getBean("marshaller");
    Person alex = (Person) ctx.getBean("alex");
    Result result = new StreamResult(System.out);
    mar.marshal(alex, result);

    System.exit(0);/*ww w  . ja  v a  2  s. com*/
}

From source file:de.drv.dsrv.extra.marshaller.impl.ExtraMarschaller.java

@Override
public void marshal(final Object graph, final Result result, final boolean validation)
        throws IOException, XmlMappingException {
    final Marshaller marshaller = getMarschaller(validation);
    marshaller.marshal(graph, result);
}

From source file:org.trpr.platform.integration.impl.xml.XMLTranscoderImpl.java

/**
 * Replacement method that uses JAXB directly instead of via Spring OXM. Provided just as an option
 *//* w w  w.j a  v  a 2  s .c  o  m*/
private String marshalUsingJAXB(Object object) throws XMLDataException {
    try {
        StringWriter stringWriter = new StringWriter();
        javax.xml.bind.JAXBContext context = javax.xml.bind.JAXBContext
                .newInstance(object.getClass().getPackage().getName());
        javax.xml.bind.Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(object, stringWriter);
        return stringWriter.toString();
    } catch (javax.xml.bind.JAXBException e) {
        throw new XMLDataException("Error marshalling Object of type : " + object.getClass().getName(), e);
    }
}

From source file:org.web4thejob.orm.TypeSerailizationTest.java

@Test
public void marshallingTest() throws XmlMappingException, IOException {
    final Marshaller marshaller = ContextUtil.getBean(Marshaller.class);
    Assert.assertNotNull(marshaller);/*from  www  .j a  v a 2 s  .c o m*/

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Result result = new StreamResult(out);
    final Detail detail1 = ContextUtil.getDRS().getOne(Detail.class);
    marshaller.marshal(detail1, result);

    final Unmarshaller unmarshaller = ContextUtil.getBean(Unmarshaller.class);
    final Detail detail2 = (Detail) unmarshaller
            .unmarshal(new StreamSource(new ByteArrayInputStream(out.toByteArray())));

    Assert.assertEquals(detail1, detail2);
}

From source file:org.web4thejob.orm.CriterionImpl.java

private String serializeValue(Object value) {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Marshaller marshaller = ContextUtil.getBean(Marshaller.class);
    final Result result = new StreamResult(out);
    try {/*w  ww  .  j  a  v  a2  s.  com*/
        marshaller.marshal(value, result);
        return out.toString("UTF-8");
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.web4thejob.orm.TypeSerailizationTest.java

@Test
public void simpleMarshallingTest() throws XmlMappingException, IOException {
    final Marshaller marshaller = ContextUtil.getBean(Marshaller.class);
    Assert.assertNotNull(marshaller);/*from www .  j  av a  2s . c  o m*/

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Result result = new StreamResult(out);
    final Master1 master1 = ContextUtil.getDRS().getOne(Master1.class);
    marshaller.marshal(master1, result);

    final Unmarshaller unmarshaller = ContextUtil.getBean(Unmarshaller.class);
    final Master1 master2 = (Master1) unmarshaller
            .unmarshal(new StreamSource(new ByteArrayInputStream(out.toByteArray())));

    Assert.assertEquals(master1, master2);
}

From source file:org.web4thejob.orm.ListSerializationTest.java

@Test
public void datesListTest() throws IOException {
    final Marshaller marshaller = ContextUtil.getBean(Marshaller.class);
    Assert.assertNotNull(marshaller);//from   w  w w  .  j av a2 s  .co  m

    List<Date> dates = new ArrayList<Date>();
    dates.add(new Date());
    dates.add(new Date());
    dates.add(new Date());

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Result result = new StreamResult(out);
    marshaller.marshal(dates, result);
    System.out.println(out.toString("UTF-8"));

}

From source file:org.web4thejob.orm.ListSerializationTest.java

@Test
public void localesListTest() throws IOException {
    final Marshaller marshaller = ContextUtil.getBean(Marshaller.class);
    Assert.assertNotNull(marshaller);/*w w w  . ja  v  a 2 s  . co  m*/

    List<Locale> locales = new ArrayList<Locale>();
    locales.add(Locale.CANADA);
    locales.add(Locale.CHINA);
    locales.add(Locale.ENGLISH);

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Result result = new StreamResult(out);
    marshaller.marshal(locales, result);
    System.out.println(out.toString("UTF-8"));

}

From source file:org.web4thejob.orm.ListSerializationTest.java

@Test
public void entitiesListTest() throws IOException {
    final Marshaller marshaller = ContextUtil.getBean(Marshaller.class);
    Assert.assertNotNull(marshaller);//from  w ww.ja  v a2 s .c o  m

    List<Entity> entities = new ArrayList<Entity>();
    for (Entity entity : ContextUtil.getDRS().getAll(Reference1.class)) {
        entities.add(entity);
    }

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Result result = new StreamResult(out);
    marshaller.marshal(entities, result);
    System.out.println(out.toString("UTF-8"));

}

From source file:org.web4thejob.orm.TypeSerailizationTest.java

@Test
public void marshallingQueryTest() throws XmlMappingException, IOException {
    final Marshaller marshaller = ContextUtil.getBean(Marshaller.class);
    Assert.assertNotNull(marshaller);//from w  w  w  .j a v  a 2  s  .c  om

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Result result = new StreamResult(out);
    final Query query1 = entityFactory.buildQuery(Master1.class);
    query1.setName("123");
    query1.setOwner(ContextUtil.getBean(SecurityService.class).getAdministratorIdentity());
    ContextUtil.getDWS().save(query1);
    marshaller.marshal(query1, result);

    final Unmarshaller unmarshaller = ContextUtil.getBean(Unmarshaller.class);
    final Query query2 = (Query) unmarshaller
            .unmarshal(new StreamSource(new ByteArrayInputStream(out.toByteArray())));

    Assert.assertEquals(query1, query2);
}