Example usage for javax.xml.bind JAXBContext createMarshaller

List of usage examples for javax.xml.bind JAXBContext createMarshaller

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext createMarshaller.

Prototype

public abstract Marshaller createMarshaller() throws JAXBException;

Source Link

Document

Create a Marshaller object that can be used to convert a java content tree into XML data.

Usage

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

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

From source file:com.vns.pdf.impl.PdfDocument.java

private void createTextAreaFile() {
    String pageXmlFileName = textAreaFilePath.toAbsolutePath().toString();
    try {/*from  w ww .j av  a  2  s  . co m*/
        JAXBContext context = JAXBContext.newInstance(Doc.class);
        Marshaller ms = context.createMarshaller();
        ms.setProperty(JAXB_FORMATTED_OUTPUT, true);
        ms.setProperty(CharacterEscapeHandler.class.getName(),
                (CharacterEscapeHandler) (ch, start, length, isAttVal, out) -> {
                    String escape = StringEscapeUtils.escapeXml11(new String(ch));
                    if (!escape.contains("&#"))
                        out.write(escape.toCharArray(), 0, escape.toCharArray().length);
                });
        ms.marshal(doc, new File(pageXmlFileName));
    } catch (JAXBException ex) {
        LOGGER.error(ex.getMessage(), ex);
    }
}

From source file:cz.lbenda.dataman.db.DbConfig.java

/** Save session conf into File
 * @param writer writer to which is configuration saved */
public void save(Writer writer) throws IOException {
    cz.lbenda.dataman.schema.dataman.ObjectFactory of = new cz.lbenda.dataman.schema.dataman.ObjectFactory();
    SessionType st = storeToSessionType(null, null);
    try {/*  ww w .  j  a  v a  2 s. c om*/
        JAXBContext jc = JAXBContext.newInstance(cz.lbenda.dataman.schema.dataman.ObjectFactory.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(of.createSession(st), writer);
    } catch (JAXBException e) {
        LOG.error("Problem with write configuration: " + e.toString(), e);
        throw new RuntimeException("Problem with write configuration: " + e.toString(), e);
    }
}

From source file:gov.nih.nci.integration.caaers.CaAERSAdverseEventServiceClientIntegrationTest.java

private Marshaller getMarshaller() throws JAXBException {
    final JAXBContext jc = JAXBContext.newInstance(AdverseEventsInputMessage.class);
    return jc.createMarshaller();
}

From source file:com.castlemock.web.basis.model.RepositoryImpl.java

/**
 * The method provides the functionality to export an entity and convert it to a String
 * @param id The id of the entityy that will be converted and exported
 * @return The entity with the provided id as a String
 *//*from w ww.j a v  a  2  s .  c om*/
@Override
public String exportOne(final I id) {
    try {
        final T type = collection.get(id);
        final JAXBContext context = JAXBContext.newInstance(entityClass);
        final Marshaller marshaller = context.createMarshaller();
        final StringWriter writer = new StringWriter();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(type, writer);
        return writer.toString();
    } catch (JAXBException e) {
        throw new IllegalStateException("Unable to export type", e);
    }
}

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);/*from w  ww .j a va  2s .c  om*/

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

From source file:org.apache.cxf.systest.jaxrs.JAXRSAtomBookTest.java

private Entry createBookEntry(int id, String name) throws Exception {

    Book b = new Book();
    b.setId(id);//  ww w.j  a v  a2  s . c  om
    b.setName(name);

    Factory factory = Abdera.getNewFactory();
    JAXBContext jc = JAXBContext.newInstance(Book.class);

    Entry e = factory.getAbdera().newEntry();
    e.setTitle(b.getName());
    e.setId(Long.toString(b.getId()));

    StringWriter writer = new StringWriter();
    jc.createMarshaller().marshal(b, writer);

    Content ct = factory.newContent(Content.Type.XML);
    ct.setValue(writer.toString());
    e.setContentElement(ct);
    return e;
}

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  ww  w  .  j a  v  a  2 s  . co m*/

    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:edu.umd.ks.cm.util.spring.CmToSisExportAdvice.java

@Transactional(readOnly = false, noRollbackFor = { DoesNotExistException.class }, rollbackFor = {
        Throwable.class })
private void doWorkflowDocument(CourseInfo courseInfo, ContextInfo contextInfo) {
    try {// w ww .  j a  va  2  s  .c o m
        String principalId = SecurityUtils.getCurrentPrincipalId();

        // Creating xml element with all info
        CourseModifyAuditInfo course = getCourseModifyAuditInfo(courseInfo, principalId, contextInfo);
        JAXBContext context = JAXBContext.newInstance(CourseModifyAuditInfo.class);
        Marshaller marshaller = context.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.marshal(course, writer);
        String appContent = writer.toString();

        // Creating a document and sending to Workflow to "Final" state
        // 2.0 upgrade - based on code in KualiStudentPostProcessorBase line 119
        WorkflowDocument document = WorkflowDocumentFactory.createDocument(principalId,
                "kuali.admin.type.course.modify");
        document.setApplicationContent(appContent);
        document.saveDocument("");
        document.approve("Audit course change to approve");
    } catch (Exception e) {
        LOG.error("Error doWorkflowDocument with courseId: " + courseInfo.getId(), e);
    }
}

From source file:org.kemri.wellcome.dhisreport.api.impl.DHIS2ReportingServiceImpl.java

@Override
public void marshallReportTemplates(OutputStream os, ReportTemplates rt) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(ReportTemplates.class);
    Marshaller marshaller = jaxbContext.createMarshaller();

    marshaller.marshal(rt, os);//  w  w w .j a  v  a  2 s .  c  o m
}