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:com.mijao.poc.jaxb.PooledMarshaller.java

public void marshal(Object o, Writer writer) {

    Marshaller marshaller = null;
    try {//from  w  ww . j av  a  2s .com
        marshaller = (Marshaller) (pool.borrowObject());
        marshaller.marshal(o, writer);
    } catch (Exception e) {
        logger.error("Could not marshal code ", e);
    } finally {
        try {
            if (null != marshaller) {
                pool.returnObject(marshaller);
            }
        } catch (Exception e) {
            // ignored
        }
    }
}

From source file:com.mijao.poc.jaxb.PooledMarshaller.java

public void marshal(Object o, ContentHandler contentHandler) {

    Marshaller marshaller = null;
    try {// ww w  .j av  a 2  s . com
        marshaller = (Marshaller) (pool.borrowObject());
        marshaller.marshal(o, contentHandler);
    } catch (Exception e) {
        logger.error("Could not marshal code ", e);
    } finally {
        try {
            if (null != marshaller) {
                pool.returnObject(marshaller);
            }
        } catch (Exception e) {
            // ignored
        }
    }
}

From source file:com.mijao.poc.jaxb.PooledMarshaller.java

public void marshal(Object o, Node node) {

    Marshaller marshaller = null;
    try {//from   ww  w  .  ja  v  a 2  s  .c  om
        marshaller = (Marshaller) (pool.borrowObject());
        marshaller.marshal(o, node);
    } catch (Exception e) {
        logger.error("Could not marshal code ", e);
    } finally {
        try {
            if (null != marshaller) {
                pool.returnObject(marshaller);
            }
        } catch (Exception e) {
            // ignored
        }
    }
}

From source file:com.mijao.poc.jaxb.PooledMarshaller.java

public void marshal(Object o, XMLStreamWriter xmlStreamWriter) {

    Marshaller marshaller = null;
    try {/*ww  w .  j  a  v  a2  s.c  o m*/
        marshaller = (Marshaller) (pool.borrowObject());
        marshaller.marshal(o, xmlStreamWriter);
    } catch (Exception e) {
        logger.error("Could not marshal code ", e);
    } finally {
        try {
            if (null != marshaller) {
                pool.returnObject(marshaller);
            }
        } catch (Exception e) {
            // ignored
        }
    }
}

From source file:com.mijao.poc.jaxb.PooledMarshaller.java

public void marshal(Object o, XMLEventWriter xmlEventWriter) {

    Marshaller marshaller = null;
    try {// w  w w  . j  a v  a2  s  .c o  m
        marshaller = (Marshaller) (pool.borrowObject());
        marshaller.marshal(o, xmlEventWriter);
    } catch (Exception e) {
        logger.error("Could not marshal code ", e);
    } finally {
        try {
            if (null != marshaller) {
                pool.returnObject(marshaller);
            }
        } catch (Exception e) {
            // ignored
        }
    }
}

From source file:edu.duke.cabig.c3pr.webservice.integration.SubjectRegistryWebServicePerformanceTest.java

private void executeBulkQuerySubjectRegistryTest() throws SQLException, Exception {
    SubjectRegistry service = getService();

    // successful creation
    final QueryStudySubjectRegistryRequest request = new QueryStudySubjectRegistryRequest();
    DSETAdvanceSearchCriterionParameter dsetAdvanceSearchCriterionParameter = new DSETAdvanceSearchCriterionParameter();
    dsetAdvanceSearchCriterionParameter.getItem().add(createAdvanceSearchParam("like", "%%%"));
    request.setSearchParameter(dsetAdvanceSearchCriterionParameter);

    JAXBContext context = JAXBContext.newInstance("edu.duke.cabig.c3pr.webservice.subjectregistry");
    Marshaller marshaller = context.createMarshaller();
    marshaller.marshal(request, System.out);
    System.out.flush();/*from   w ww.ja v a2s . co  m*/
    startProfiling();
    List<StudySubject> studySubjects = service.querySubjectRegistry(request).getStudySubjects().getItem();
    stopProfiling();
    assertEquals(200, studySubjects.size());
    //      startProfiling();
    //      request.setReduceGraph(iso.BL(true));
    //      service.querySubjectRegistry(request).getStudySubjects();
    //      stopProfiling();
    //      assertEquals(200, studySubjects.size());
}

From source file:eu.modaclouds.sla.service.rest.AbstractSLARest.java

protected String printError(int code, String text) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {/*w  ww .ja  va  2s.c o m*/
        ErrorResponse errorResponse = new ErrorResponse();

        errorResponse.setCode(code);
        errorResponse.setMessage(text);

        JAXBContext jaxbContext;
        jaxbContext = JAXBContext.newInstance(eu.atos.sla.parser.data.ErrorResponse.class);
        Marshaller marshaller = jaxbContext.createMarshaller();

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        marshaller.marshal(errorResponse, out);
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return out.toString();

}

From source file:eu.modaclouds.sla.service.rest.AbstractSLARest.java

protected String printMessage(int code, String text) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {/*from ww w.ja  v  a  2s  . c om*/

        MessageResponse messageResponse = new MessageResponse();

        messageResponse.setCode(code);
        messageResponse.setMessage(text);

        JAXBContext jaxbContext = JAXBContext.newInstance(eu.atos.sla.parser.data.MessageResponse.class);
        Marshaller marshaller = jaxbContext.createMarshaller();

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        marshaller.marshal(messageResponse, out);
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return out.toString();

}

From source file:org.remus.marketplace.servlets.PopularListing.java

@Override
public void handleRequest(HttpServletRequest arg0, HttpServletResponse arg1)
        throws ServletException, IOException {
    arg1.setContentType("text/xml");
    try {/*from   www .  j  a v  a 2  s. co  m*/
        JAXBContext newInstance = JAXBContext.newInstance(Marketplace.class,
                org.remus.marketplace.xml.Market.class);

        Marketplace marketplace = new Marketplace();

        List<Object> findByCategoriesId = nodeDao.query(new AdvancedCriteria().setMaxResults(10)
                .setProjection(Projections.projectionList().add(Projections.groupProperty(Node.ID), "dl_node")
                        .add(Projections.alias(Projections.rowCount(), "downloadCount")))
                .addSubCriteria(new AdvancedCriteria().setAssosication(Node.DOWNLOADS)

                        .addOrder(Order.desc("downloadCount"))));
        boolean favoriteMode = arg0.getParameter("favorites") != null
                && Boolean.parseBoolean(arg0.getParameter("favorites"));
        Favorite favorite = null;
        Popular popular = null;
        if (favoriteMode) {
            favorite = new Favorite();
            favorite.setCount(findByCategoriesId.size());
        } else {
            popular = new Popular();
            popular.setCount(findByCategoriesId.size());
        }
        for (Object findById : findByCategoriesId) {
            Object[] rs = (Object[]) findById;
            int parseInt = Integer.parseInt(rs[0].toString());
            Node findById2 = nodeDao.findById(parseInt);
            org.remus.marketplace.xml.Node node = XMLBuilder.buildNode(serverPrefix, findById2);
            if (favoriteMode) {
                favorite.getNode().add(node);
            } else {
                popular.getNode().add(node);
            }
        }
        if (favoriteMode) {
            marketplace.setFavorites(favorite);
        } else {
            marketplace.setPopular(popular);
        }
        Marshaller createMarshaller = newInstance.createMarshaller();
        XMLSerializer xmlSerializer = XMLBuilder.getXMLSerializer(arg1.getOutputStream());
        createMarshaller.marshal(marketplace, xmlSerializer.asContentHandler());
    } catch (JAXBException e) {
        throw new ServletException(e);
    }

}

From source file:actions.AddStudent.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    ActionForward findForward = mapping.findForward("main");

    StudentForm formStudent = (StudentForm) request.getAttribute("studentForm");

    Student newStudent = new Student();
    BeanUtils.copyProperties(newStudent, formStudent);

    System.out.println("newStudent.firstName=" + newStudent.getFirstName());
    System.out.println("newStudent.lastName=" + newStudent.getLastName());
    System.out.println("newStudent.studentId=" + newStudent.getStudentId());
    System.out.println("newStudent.dob=" + newStudent.getDob());

    //Student.fileWrite(null, newStudent.fileOutputString());
    boolean successAdd = newStudent.saveStudent();
    Student.getStudents().put(newStudent.getStudentId(), newStudent);

    ActionMessages messages = new ActionMessages();
    if (successAdd) {
        messages.add("message1", (new ActionMessage("label.student.added.successfully")));
    } else {/*from w  w  w .  j a v  a2s.c o  m*/
        messages.add("error", (new ActionMessage("label.student.added.error")));
    }
    saveMessages(request, messages);

    //**********************************************************************
    //**********************************************************************
    //**********************************************************************
    //http://www.vogella.com/tutorials/JAXB/article.html
    // create JAXB context and instantiate marshaller
    JAXBContext context = JAXBContext.newInstance(Student.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    // Write to System.out
    StringWriter sw = new StringWriter();
    m.marshal(newStudent, sw);
    String xmlStudent = sw.toString();
    System.out.println("xmlEncodedStudent=" + xmlStudent);
    //**********************************************************************
    //**********************************************************************

    //Unmarshall back for testing
    Unmarshaller um = context.createUnmarshaller();
    Student backStudent = (Student) um.unmarshal(new StringReader(xmlStudent));
    System.out.println("Student back from xml:" + backStudent.toString());

    //**********************************************************************
    //**********************************************************************
    //**********************************************************************
    return findForward;

}