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:com.mondora.chargify.controller.ChargifyAdapter.java

protected String toXml(Object o) {
    try {/*from ww w .  j a  va2 s.com*/
        StringWriter sw = new StringWriter();
        JAXBContext context = JAXBContext.newInstance(o.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, charset);
        marshaller.setSchema(null);
        marshaller.marshal(o, sw);
        return sw.toString();
    } catch (JAXBException e) {
        if (logger.isTraceEnabled())
            logger.trace(e.getMessage(), e);
        if (logger.isInfoEnabled())
            logger.info(e.getMessage());
    }
    return null;
}

From source file:com.mycompany.dao.report.XMLReport.java

@Override
public void generate() throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Books.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    Books books = (Books) jaxbUnmarshaller.unmarshal(new File(fileName));

    for (Iterator<Book> iterator = books.getBooks().iterator(); iterator.hasNext();) {
        Book book = iterator.next();
        if (book.getQuantity() != 0) {
            // Remove the current element from the iterator and the list.
            iterator.remove();/*from  ww w  . j  a  va  2s  . c o  m*/
        }
    }

    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(books, new File(fileName2));

}

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

@Override
public void handleRequest(HttpServletRequest arg0, HttpServletResponse arg1)
        throws ServletException, IOException {
    int contentId = Integer.parseInt(arg0.getParameter("nodeId"));
    arg1.setContentType("text/xml");
    try {//from   w w w.  j av  a  2s.c  om
        JAXBContext newInstance = JAXBContext.newInstance(Marketplace.class,
                org.remus.marketplace.xml.Node.class, Platforms.class, Platform.class, IUs.class, IU.class);

        Marketplace marketplace = new Marketplace();
        Node findById = nodeDao.findById(contentId);
        if (findById != null) {
            org.remus.marketplace.xml.Node node = XMLBuilder.buildNode(serverPrefix, findById);
            marketplace.setNode(node);
            Marshaller createMarshaller = newInstance.createMarshaller();
            XMLSerializer xmlSerializer = XMLBuilder.getXMLSerializer(arg1.getOutputStream());
            createMarshaller.marshal(marketplace, xmlSerializer.asContentHandler());
        } else {
            throw new ServletException("Node not found");
        }
    } catch (JAXBException e) {
        throw new ServletException(e);
    }

}

From source file:be.fedict.eid.applet.service.signer.facets.XAdESSignatureFacet.java

/**
 * Main constructor.//from   www. j  a v  a2 s . co m
 * 
 * @param clock
 *            the clock to be used for determining the xades:SigningTime
 * @param digestAlgorithm
 *            the digest algorithm to be used for all required XAdES digest
 *            operations. Possible values: "SHA-1", "SHA-256", or "SHA-512".
 * @param signaturePolicyService
 *            the optional signature policy service used for XAdES-EPES.
 */
public XAdESSignatureFacet(Clock clock, DigestAlgo digestAlgorithm,
        SignaturePolicyService signaturePolicyService) {
    this.clock = clock;
    this.digestAlgorithm = digestAlgorithm;
    this.signaturePolicyService = signaturePolicyService;

    try {
        this.datatypeFactory = DatatypeFactory.newInstance();
    } catch (DatatypeConfigurationException e) {
        throw new RuntimeException("datatype config error: " + e.getMessage(), e);
    }
    this.xadesObjectFactory = new ObjectFactory();
    this.xmldsigObjectFactory = new be.fedict.eid.applet.service.signer.jaxb.xmldsig.ObjectFactory();
    this.xadesNamespacePrefixMapper = new XAdESNamespacePrefixMapper();
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
        this.marshaller = jaxbContext.createMarshaller();
        this.marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", this.xadesNamespacePrefixMapper);
    } catch (JAXBException e) {
        throw new RuntimeException("JAXB error: " + e.getMessage(), e);
    }
    this.dataObjectFormatMimeTypes = new HashMap<String, String>();
}

From source file:hydrograph.ui.propertywindow.widgets.customwidgets.schema.GridRowLoader.java

private void exportFile(List<GridRow> schemaGridRowList, Schema schema)
        throws JAXBException, PropertyException {
    JAXBContext jaxbContext;
    jaxbContext = JAXBContext.newInstance(Schema.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    for (GridRow gridRow : schemaGridRowList) {
        Field field = ExternalSchemaUtil.INSTANCE.convertGridRowToJaxbSchemaField(gridRow);
        fields.getField().add(field);/*  www. j a  v  a2 s.c o  m*/
    }
    schema.setFields(fields);
    jaxbMarshaller.marshal(schema, schemaFile);
}

From source file:org.openmrs.module.dhisreport.api.model.ReportTemplatesTest.java

@Test
public void marshallReportTemplates() throws Exception {
    ClassPathResource resource = new ClassPathResource("templates_ethiopia.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(ReportTemplates.class);

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    ReportTemplates reportTemplates = (ReportTemplates) jaxbUnmarshaller.unmarshal(resource.getInputStream());
    Collection<DataValueTemplate> dvts = reportTemplates.getReportDefinitions().get(1).getDataValueTemplates();
    for (DataValueTemplate dvt : dvts) {
        dvt.setQuery("select count(*) from something & something_else");
    }//from w w  w . j a v a 2s .c  o  m
    Marshaller jaxbmarshaller = jaxbContext.createMarshaller();
    jaxbmarshaller.marshal(reportTemplates, System.out);
}

From source file:ddf.security.pdp.realm.xacml.processor.BalanaClientTest.java

@Test
public void testEvaluateroleuseractionquerycitizenshipCA() throws Exception {
    LOGGER.debug("\n\n\n##### testEvaluate_role_user_action_query_citizenship_CA");

    final String country = "CA";

    testSetup();/* w  w w.  ja va  2  s  .com*/

    RequestType xacmlRequestType = new RequestType();
    xacmlRequestType.setCombinedDecision(false);
    xacmlRequestType.setReturnPolicyIdList(false);

    AttributesType actionAttributes = new AttributesType();
    actionAttributes.setCategory(ACTION_CATEGORY);
    AttributeType actionAttribute = new AttributeType();
    actionAttribute.setAttributeId(ACTION_ID);
    actionAttribute.setIncludeInResult(false);
    AttributeValueType actionValue = new AttributeValueType();
    actionValue.setDataType(STRING_DATA_TYPE);
    actionValue.getContent().add(QUERY_ACTION);
    actionAttribute.getAttributeValue().add(actionValue);
    actionAttributes.getAttribute().add(actionAttribute);

    AttributesType subjectAttributes = new AttributesType();
    subjectAttributes.setCategory(SUBJECT_CATEGORY);
    AttributeType subjectAttribute = new AttributeType();
    subjectAttribute.setAttributeId(SUBJECT_ID);
    subjectAttribute.setIncludeInResult(false);
    AttributeValueType subjectValue = new AttributeValueType();
    subjectValue.setDataType(STRING_DATA_TYPE);
    subjectValue.getContent().add(TEST_USER_2);
    subjectAttribute.getAttributeValue().add(subjectValue);
    subjectAttributes.getAttribute().add(subjectAttribute);

    AttributeType roleAttribute = new AttributeType();
    roleAttribute.setAttributeId(ROLE_CLAIM);
    roleAttribute.setIncludeInResult(false);
    AttributeValueType roleValue = new AttributeValueType();
    roleValue.setDataType(STRING_DATA_TYPE);
    roleValue.getContent().add(ROLE);
    roleAttribute.getAttributeValue().add(roleValue);
    subjectAttributes.getAttribute().add(roleAttribute);

    AttributesType categoryAttributes = new AttributesType();
    categoryAttributes.setCategory(PERMISSIONS_CATEGORY);
    AttributeType citizenshipAttribute = new AttributeType();
    citizenshipAttribute.setAttributeId(CITIZENSHIP_ATTRIBUTE);
    citizenshipAttribute.setIncludeInResult(false);
    AttributeValueType citizenshipValue = new AttributeValueType();
    citizenshipValue.setDataType(STRING_DATA_TYPE);
    citizenshipValue.getContent().add(country);
    citizenshipAttribute.getAttributeValue().add(citizenshipValue);
    categoryAttributes.getAttribute().add(citizenshipAttribute);

    xacmlRequestType.getAttributes().add(actionAttributes);
    xacmlRequestType.getAttributes().add(subjectAttributes);
    xacmlRequestType.getAttributes().add(categoryAttributes);

    BalanaClient pdp = new BalanaClient(tempDir.getCanonicalPath(), new XmlParser());

    // Perform Test
    ResponseType xacmlResponse = pdp.evaluate(xacmlRequestType);

    // Verify
    JAXBContext jaxbContext = JAXBContext.newInstance(ResponseType.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    ObjectFactory objectFactory = new ObjectFactory();
    Writer writer = new StringWriter();
    marshaller.marshal(objectFactory.createResponse(xacmlResponse), writer);
    LOGGER.debug("\nXACML 3.0 Response:\n{}", writer.toString());
    assertEquals(xacmlResponse.getResult().get(0).getDecision(), DecisionType.DENY);

}

From source file:ddf.security.pdp.realm.xacml.processor.XacmlClientTest.java

@Test
public void testEvaluateroleuseractionquerycitizenshipCA() throws Exception {
    LOGGER.debug("\n\n\n##### testEvaluate_role_user_action_query_citizenship_CA");

    final String country = "CA";

    testSetup();//  www  .j a v  a2  s  . c o  m

    RequestType xacmlRequestType = new RequestType();
    xacmlRequestType.setCombinedDecision(false);
    xacmlRequestType.setReturnPolicyIdList(false);

    AttributesType actionAttributes = new AttributesType();
    actionAttributes.setCategory(ACTION_CATEGORY);
    AttributeType actionAttribute = new AttributeType();
    actionAttribute.setAttributeId(ACTION_ID);
    actionAttribute.setIncludeInResult(false);
    AttributeValueType actionValue = new AttributeValueType();
    actionValue.setDataType(STRING_DATA_TYPE);
    actionValue.getContent().add(QUERY_ACTION);
    actionAttribute.getAttributeValue().add(actionValue);
    actionAttributes.getAttribute().add(actionAttribute);

    AttributesType subjectAttributes = new AttributesType();
    subjectAttributes.setCategory(SUBJECT_CATEGORY);
    AttributeType subjectAttribute = new AttributeType();
    subjectAttribute.setAttributeId(SUBJECT_ID);
    subjectAttribute.setIncludeInResult(false);
    AttributeValueType subjectValue = new AttributeValueType();
    subjectValue.setDataType(STRING_DATA_TYPE);
    subjectValue.getContent().add(TEST_USER_2);
    subjectAttribute.getAttributeValue().add(subjectValue);
    subjectAttributes.getAttribute().add(subjectAttribute);

    AttributeType roleAttribute = new AttributeType();
    roleAttribute.setAttributeId(ROLE_CLAIM);
    roleAttribute.setIncludeInResult(false);
    AttributeValueType roleValue = new AttributeValueType();
    roleValue.setDataType(STRING_DATA_TYPE);
    roleValue.getContent().add(ROLE);
    roleAttribute.getAttributeValue().add(roleValue);
    subjectAttributes.getAttribute().add(roleAttribute);

    AttributesType categoryAttributes = new AttributesType();
    categoryAttributes.setCategory(PERMISSIONS_CATEGORY);
    AttributeType citizenshipAttribute = new AttributeType();
    citizenshipAttribute.setAttributeId(CITIZENSHIP_ATTRIBUTE);
    citizenshipAttribute.setIncludeInResult(false);
    AttributeValueType citizenshipValue = new AttributeValueType();
    citizenshipValue.setDataType(STRING_DATA_TYPE);
    citizenshipValue.getContent().add(country);
    citizenshipAttribute.getAttributeValue().add(citizenshipValue);
    categoryAttributes.getAttribute().add(citizenshipAttribute);

    xacmlRequestType.getAttributes().add(actionAttributes);
    xacmlRequestType.getAttributes().add(subjectAttributes);
    xacmlRequestType.getAttributes().add(categoryAttributes);

    XacmlClient pdp = new XacmlClient(tempDir.getCanonicalPath(), new XmlParser());

    // Perform Test
    ResponseType xacmlResponse = pdp.evaluate(xacmlRequestType);

    // Verify
    JAXBContext jaxbContext = JAXBContext.newInstance(ResponseType.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    ObjectFactory objectFactory = new ObjectFactory();
    Writer writer = new StringWriter();
    marshaller.marshal(objectFactory.createResponse(xacmlResponse), writer);
    LOGGER.debug("\nXACML 3.0 Response:\n{}", writer.toString());
    assertEquals(xacmlResponse.getResult().get(0).getDecision(), DecisionType.DENY);

}

From source file:org.opennms.netmgt.ncs.rest.AbstractSpringJerseyRestTestCase.java

protected void putXmlObject(JAXBContext context, String url, int expectedStatus, Object object)
        throws Exception {

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Marshaller marshaller = context.createMarshaller();
    marshaller.marshal(object, out);/*from   w w w . j  ava2 s  . co  m*/
    byte[] content = out.toByteArray();

    MockHttpServletRequest request = createRequest(PUT, url);
    request.setContentType(MediaType.APPLICATION_XML);
    request.setContent(content);
    MockHttpServletResponse response = createResponse();
    dispatch(request, response);
    assertEquals(expectedStatus, response.getStatus());

}

From source file:ddf.security.pdp.realm.xacml.processor.BalanaClientTest.java

@Test
public void testBalanaWrapperpoliciesdirectorypolicyadded() throws Exception {
    LOGGER.debug("\n\n\n##### testBalanaWrapper_policies_directory_policy_added");

    File policyDir = folder.newFolder("tempDir");

    BalanaClient.defaultPollingIntervalInSeconds = 1;
    // Perform Test
    BalanaClient pdp = new BalanaClient(policyDir.getCanonicalPath(), new XmlParser());

    File srcFile = new File(
            projectHome + File.separator + RELATIVE_POLICIES_DIR + File.separator + POLICY_FILE);
    FileUtils.copyFileToDirectory(srcFile, policyDir);

    Thread.sleep(2000);/*  w w w. j a va  2s.c o  m*/

    RequestType xacmlRequestType = new RequestType();
    xacmlRequestType.setCombinedDecision(false);
    xacmlRequestType.setReturnPolicyIdList(false);

    AttributesType actionAttributes = new AttributesType();
    actionAttributes.setCategory(ACTION_CATEGORY);
    AttributeType actionAttribute = new AttributeType();
    actionAttribute.setAttributeId(ACTION_ID);
    actionAttribute.setIncludeInResult(false);
    AttributeValueType actionValue = new AttributeValueType();
    actionValue.setDataType(STRING_DATA_TYPE);
    actionValue.getContent().add(QUERY_ACTION);
    actionAttribute.getAttributeValue().add(actionValue);
    actionAttributes.getAttribute().add(actionAttribute);

    AttributesType subjectAttributes = new AttributesType();
    subjectAttributes.setCategory(SUBJECT_CATEGORY);
    AttributeType subjectAttribute = new AttributeType();
    subjectAttribute.setAttributeId(SUBJECT_ID);
    subjectAttribute.setIncludeInResult(false);
    AttributeValueType subjectValue = new AttributeValueType();
    subjectValue.setDataType(STRING_DATA_TYPE);
    subjectValue.getContent().add(TEST_USER_1);
    subjectAttribute.getAttributeValue().add(subjectValue);
    subjectAttributes.getAttribute().add(subjectAttribute);

    AttributeType roleAttribute = new AttributeType();
    roleAttribute.setAttributeId(ROLE_CLAIM);
    roleAttribute.setIncludeInResult(false);
    AttributeValueType roleValue = new AttributeValueType();
    roleValue.setDataType(STRING_DATA_TYPE);
    roleValue.getContent().add(ROLE);
    roleAttribute.getAttributeValue().add(roleValue);
    subjectAttributes.getAttribute().add(roleAttribute);

    AttributesType categoryAttributes = new AttributesType();
    categoryAttributes.setCategory(PERMISSIONS_CATEGORY);
    AttributeType citizenshipAttribute = new AttributeType();
    citizenshipAttribute.setAttributeId(CITIZENSHIP_ATTRIBUTE);
    citizenshipAttribute.setIncludeInResult(false);
    AttributeValueType citizenshipValue = new AttributeValueType();
    citizenshipValue.setDataType(STRING_DATA_TYPE);
    citizenshipValue.getContent().add(US_COUNTRY);
    citizenshipAttribute.getAttributeValue().add(citizenshipValue);
    categoryAttributes.getAttribute().add(citizenshipAttribute);

    xacmlRequestType.getAttributes().add(actionAttributes);
    xacmlRequestType.getAttributes().add(subjectAttributes);
    xacmlRequestType.getAttributes().add(categoryAttributes);

    // Perform Test
    ResponseType xacmlResponse = pdp.evaluate(xacmlRequestType);

    // Verify - The policy was loaded to allow a permit decision
    JAXBContext jaxbContext = JAXBContext.newInstance(ResponseType.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    ObjectFactory objectFactory = new ObjectFactory();
    Writer writer = new StringWriter();
    marshaller.marshal(objectFactory.createResponse(xacmlResponse), writer);
    LOGGER.debug("\nXACML 3.0 Response:\n{}", writer.toString());
    assertEquals(xacmlResponse.getResult().get(0).getDecision(), DecisionType.PERMIT);

    FileUtils.deleteDirectory(policyDir);
}