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:cz.muni.fi.editor.typemanager.TypeServiceImpl.java

public TypeServiceImpl() throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(Type.class);
    this.unmarshaller = context.createUnmarshaller();
    this.marshaller = context.createMarshaller();
    this.marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
}

From source file:gov.nih.nci.cacis.ip.mirthconnect.CanonicalModelProcessor.java

/**
 * Method accepts canonical data for processing
 * //w  ww.  j a  va2 s  .c  om
 * @param request CaCISRequest
 * @return response CaCISResponse
 * @throws AcceptCanonicalFault Fault
 * @throws
 */
@WebResult(name = "caCISResponse", targetNamespace = CACIS_NS, partName = "parameter")
@WebMethod
public gov.nih.nci.cacis.CaCISResponse acceptCanonical(
        @WebParam(partName = "parameter", name = "caCISRequest", targetNamespace = CACIS_NS) CaCISRequest request)
        throws AcceptCanonicalFault {

    final CaCISResponse response = new CaCISResponse();

    final StringWriter sw = new StringWriter();
    try {
        final JAXBContext jc = JAXBContext.newInstance(CaCISRequest.class);
        final Marshaller m = jc.createMarshaller();

        final PrintWriter pw = new PrintWriter(sw);
        m.marshal(request, pw);
        response.setStatus(ResponseStatusType.SUCCESS);
    } catch (JAXBException jaxE) {
        throw new AcceptCanonicalFault("Error Marshalling object", jaxE);
    }

    try {
        String mcResponse = webServiceMessageReceiver.processData(sw.toString());

        if (mcResponse != null && (mcResponse.indexOf("Error") > -1 || mcResponse.indexOf("Exception") > -1
                || mcResponse.indexOf("ERROR") > -1 || mcResponse.indexOf("error") > -1)) {
            mcResponse = StringUtils.remove(mcResponse, "SUCCESS:");
            String channelUid = StringUtils.substringBetween(mcResponse, "(", ")");
            if (channelUid != null) {
                mcResponse = StringUtils.remove(mcResponse, "(" + channelUid + ")");
            }
            throw new AcceptCanonicalFault(
                    StringUtils.substring(mcResponse, StringUtils.lastIndexOf(mcResponse, ':')));
        }
        response.setStatus(ResponseStatusType.SUCCESS);
        return response;
        // CHECKSTYLE:OFF
    } catch (Exception e) {
        // CHECKSTYLE:ON
        //throw new AcceptCanonicalFault("Error processing message!" + e.getMessage(), e);
        throw new AcceptCanonicalFault(e.getMessage(), e);
    }
}

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

public <T> String marshallWithoutFile(T entityClass) {
    try {//from w w w  .j a  v  a 2 s .  c om
        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:com.cognifide.aet.rest.XUnitServlet.java

private Marshaller prepareJaxbMarshaller() throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Testsuites.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    return jaxbMarshaller;
}

From source file:ee.ria.xroad.confproxy.commandline.ConfProxyUtilGenerateAnchor.java

/**
 * Generates an achor xml file based on the provided proxy configuration
 * properties and writes it to the provided output stream.
 * @param conf configuration proxy properties instance
 * @param instanceIdentifier instance identifier of the resulting anchor
 * @param out the output stream for writing the generated xml
 * @throws Exception if xml generation fails
 *///ww w  .  j av a  2 s. c  o m
private void generateAnchorXml(final ConfProxyProperties conf, final String instanceIdentifier,
        final OutputStream out) throws Exception {
    JAXBContext jaxbCtx = JAXBContext.newInstance(ObjectFactory.class);
    Marshaller marshaller = jaxbCtx.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    ObjectFactory factory = new ObjectFactory();
    ConfigurationSourceType sourceType = factory.createConfigurationSourceType();
    sourceType.setDownloadURL(conf.getConfigurationProxyURL() + "/" + OutputBuilder.SIGNED_DIRECTORY_NAME);
    for (byte[] cert : conf.getVerificationCerts()) {
        sourceType.getVerificationCert().add(cert);
    }
    ConfigurationAnchorType anchorType = factory.createConfigurationAnchorType();
    anchorType.setInstanceIdentifier(instanceIdentifier);
    GregorianCalendar gcal = new GregorianCalendar();
    gcal.setTimeZone(TimeZone.getTimeZone("UTC"));
    XMLGregorianCalendar xgcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
    anchorType.setGeneratedAt(xgcal);
    anchorType.getSource().add(sourceType);
    JAXBElement<ConfigurationAnchorType> root = factory.createConfigurationAnchor(anchorType);

    marshaller.marshal(root, out);
}

From source file:org.jasig.portlet.courses.dao.xml.Jaxb2CourseSummaryHttpMessageConverter.java

/**
 * Creates a new {@link Marshaller} for the given class.
 *
 * @param clazz the class to create the marshaller for
 * @return the {@code Marshaller}//from w w  w  .j  a v a  2  s .c o  m
 * @throws HttpMessageConversionException in case of JAXB errors
 */
protected final Marshaller createWrapperMarshaller(Class clazz) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        return jaxbContext.createMarshaller();
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException(
                "Could not create Marshaller for class [" + clazz + "]: " + ex.getMessage(), ex);
    }
}

From source file:org.mashupmedia.service.MapperManagerImpl.java

protected Marshaller getMarshaller() throws JAXBException {
    if (marshaller != null) {
        return marshaller;
    }//from ww w  . java  2  s .  com

    JAXBContext jaxbContext = JAXBContext.newInstance(Song.class);
    marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FRAGMENT, true);
    return marshaller;
}

From source file:org.camelcookbook.rest.binding.BindingModeSpringTest.java

@Test
public void testSetOneXml() throws Exception {
    final Item item = getItemService().getItem(0);

    // change name to something different
    item.setName(item.getName() + "Foo");

    JAXBContext jaxbContext = JAXBContext.newInstance(Item.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    StringWriter sw = new StringWriter();

    jaxbMarshaller.marshal(item, sw);//from   w  ww  .ja v a 2 s  .  c  o m

    String xmlItem = sw.toString();

    String out = fluentTemplate().to("undertow:http://localhost:" + port1 + "/items/0")
            .withHeader(Exchange.HTTP_METHOD, "PUT").withBody(xmlItem).request(String.class);

    assertEquals(item, getItemService().getItem(0));
}

From source file:org.javelin.sws.ext.bind.SweJaxbContextFactoryTest.java

@Test
public void scanExceptionClassForJaxbRi() throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(IllegalArgumentException.class);
    System.out.println(ctx.toString());
    ctx.createMarshaller().marshal(new JAXBElement<IllegalArgumentException>(new QName("urn:test", "e"),
            IllegalArgumentException.class, new IllegalArgumentException("exception")), System.out);
}

From source file:fr.fastconnect.factory.tibco.bw.codereview.ConvertRulesToSonarMojo.java

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