Example usage for javax.xml.bind JAXBElement JAXBElement

List of usage examples for javax.xml.bind JAXBElement JAXBElement

Introduction

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

Prototype

public JAXBElement(QName name, Class<T> declaredType, T value) 

Source Link

Document

Construct an xml element instance.

Usage

From source file:org.javelin.sws.ext.bind.jaxb.JaxbTest.java

@Test(expected = IllegalAnnotationsException.class)
public void marshalIllegalProperties() throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1.class);
    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1 mc5 = new org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1();
    mc5.setC(String.class);
    m.marshal(new JAXBElement<org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1>(new QName("a", "a"),
            org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1.class, mc5), System.out);
}

From source file:be.fedict.eid.pkira.xkmsws.util.XMLMarshallingUtil.java

public Document marshalBulkRegisterTypeToDocument(BulkRegisterType bulkRegisterType)
        throws XKMSClientException {
    QName qname = new QName("http://www.w3.org/2002/03/xkms-xbulk", "BulkRegister");
    JAXBElement<BulkRegisterType> jaxbElement = new JAXBElement<BulkRegisterType>(qname, BulkRegisterType.class,
            bulkRegisterType);/*from ww w.  j a  v a  2s  . c  om*/

    try {
        Document doc = documentBuilder.newDocument();
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(jaxbElement, doc);

        return doc;
    } catch (JAXBException e) {
        throw new XKMSClientException("Cannot marshal message.", e);
    }
}

From source file:nz.co.senanque.base.ObjectTest.java

@SuppressWarnings("unused")
@Test//www .j  av  a  2 s.co m
public void test1() throws Exception {
    ValidationSession validationSession = m_validationEngine.createSession();

    // create a customer
    Customer customer = m_customerDAO.createCustomer();
    validationSession.bind(customer);
    Invoice invoice = new Invoice();
    invoice.setDescription("test invoice");
    customer.getInvoices().add(invoice);
    boolean exceptionFound = false;
    try {
        customer.setName("ttt");
    } catch (ValidationException e) {
        exceptionFound = true;
    }
    assertTrue(exceptionFound);

    final ObjectMetadata customerMetadata = validationSession.getMetadata(customer);
    final FieldMetadata customerTypeMetadata = customerMetadata.getFieldMetadata(Customer.CUSTOMERTYPE);

    assertFalse(customerTypeMetadata.isActive());
    assertFalse(customerTypeMetadata.isReadOnly());
    assertFalse(customerTypeMetadata.isRequired());

    customer.setName("aaaab");
    assertTrue(customerTypeMetadata.isActive());
    assertTrue(customerTypeMetadata.isReadOnly());
    assertTrue(customerTypeMetadata.isRequired());
    exceptionFound = false;
    try {
        customer.setCustomerType("B");
    } catch (Exception e) {
        exceptionFound = true;
    }
    assertTrue(exceptionFound);
    exceptionFound = false;
    try {
        customer.setCustomerType("XXX");
    } catch (Exception e) {
        exceptionFound = true;
    }
    assertTrue(exceptionFound);
    customer.setBusiness(IndustryType.AG);
    customer.setAmount(new Double(500.99));
    final long id = m_customerDAO.save(customer);
    log.info(id);

    // fetch customer back
    customer = m_customerDAO.getCustomer(id);
    final int invoiceCount = customer.getInvoices().size();
    validationSession.bind(customer);
    invoice = new Invoice();
    invoice.setDescription("test invoice2");
    customer.getInvoices().add(invoice);
    m_customerDAO.save(customer);

    // fetch customer again
    customer = m_customerDAO.getCustomer(id);
    customer.toString();
    validationSession.bind(customer);
    final Invoice inv = customer.getInvoices().get(0);
    customer.getInvoices().remove(inv);

    //ObjectMetadata metadata = validationSession.getMetadata(customer);
    ObjectMetadata metadata = customer.getMetadata();
    org.junit.Assert.assertEquals("this is a description",
            metadata.getFieldMetadata(Customer.NAME).getDescription());
    List<ChoiceBase> choices = metadata.getFieldMetadata(Customer.BUSINESS).getChoiceList();
    assertEquals(1, choices.size());
    List<ChoiceBase> choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList();
    assertEquals(2, choices2.size());
    choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList();
    assertEquals(2, choices2.size());

    customer.setName("aab");
    choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList();
    assertEquals(6, choices2.size());

    // Convert customer to XML
    QName qname = new QName("http://www.example.org/sandbox", "Session");
    JAXBElement<Session> sessionJAXB = new JAXBElement<Session>(qname, Session.class, new Session());
    sessionJAXB.getValue().getCustomers().add(customer); //??This fails to actually add
    StringWriter marshallWriter = new StringWriter();
    Result marshallResult = new StreamResult(marshallWriter);
    m_marshaller.marshal(sessionJAXB, marshallResult);
    marshallWriter.flush();
    String result = marshallWriter.getBuffer().toString().trim();
    String xml = result.replaceAll("\\Qhttp://www.example.org/sandbox\\E", "http://www.example.org/sandbox");
    log.info(xml);

    // Convert customer back to objects
    SAXBuilder builder = new SAXBuilder();
    org.jdom.Document resultDOM = builder.build(new StringReader(xml));
    @SuppressWarnings("unchecked")
    JAXBElement<Session> request = (JAXBElement<Session>) m_unmarshaller.unmarshal(new JDOMSource(resultDOM));
    validationSession = m_validationEngine.createSession();
    validationSession.bind(request.getValue());
    assertEquals(3, validationSession.getProxyCount());
    List<Customer> customers = request.getValue().getCustomers();
    assertEquals(1, customers.size());
    customers.clear();
    assertEquals(1, validationSession.getProxyCount());
    request.toString();
    validationSession.close();
}

From source file:gov.nih.nci.caxchange.transcend.CaXchangeRequestService.java

private String getCaXchangeRequestxml(final Message parameter) {
    String requestXML = null;//from   w w  w  .  j  av a2  s.  co m

    final QName qname = new QName("http://caXchange.nci.nih.gov/caxchangerequest", "caxchangerequest");
    final JAXBElement<Message> message = new JAXBElement<Message>(qname, Message.class, parameter);
    final StringWriter sw = new StringWriter();
    try {
        getMarshaller().marshal(message, sw);
        requestXML = sw.toString();
    } catch (JAXBException e) {
        LOG.error("Error marshalling CaXchangeRequest!", e);
    }
    return requestXML;
}

From source file:com.onespatial.jrc.tns.oml_to_rif.fixture.DomBasedUnitTest.java

/**
 * @param rifDocument/*from  w w  w  .j a  v  a2s. co  m*/
 *            {@link org.w3._2007.rif.Document}
 * @return {@link Document}
 * @throws JAXBException
 *             if any errors occurred trying to marshall the RIF content to
 *             an DOM document
 * @throws ParserConfigurationException
 */
protected org.w3c.dom.Document getDomFromRif(org.w3._2007.rif.Document rifDocument) throws JAXBException {
    org.w3c.dom.Document domDocument = builder.newDocument();
    JAXBContext jc = JAXBContext.newInstance("org.w3._2007.rif", getClass().getClassLoader()); //$NON-NLS-1$
    jc.createMarshaller()
            .marshal(new JAXBElement<org.w3._2007.rif.Document>(new QName("http://www.w3.org/2007/rif#", //$NON-NLS-1$
                    "Document", "rif"), org.w3._2007.rif.Document.class, rifDocument), //$NON-NLS-1$ //$NON-NLS-2$
                    domDocument);
    return domDocument;
}

From source file:org.javelin.sws.ext.bind.jaxb.JaxbTest.java

@Test
public void marshalIllegalName() throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(org.javelin.sws.ext.bind.jaxb.context1.MyClassJ1.class);
    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    // and I thought it's illegal...
    m.marshal(new JAXBElement<String>(new QName("a", "a a"), String.class, "hello"), System.out);
}

From source file:eu.trentorise.smartcampus.permissionprovider.controller.CASController.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private static JAXBElement<Object> createElement(String key, String value) {
    return new JAXBElement(new QName("http://www.yale.edu/tp/cas", key, "cas"), String.class,
            value == null ? "" : value);
}

From source file:org.javelin.sws.ext.bind.internal.model.ClassHierarchyTest.java

@Test
public void handleXmlAccessTypePublicMember() throws Exception {
    // f1, f3, f4, p1, p3, p4 - public fields, public properties, annotated fields, annotated properties
    System.out.println("\nC4");
    JAXBContext.newInstance(C4.class).createMarshaller()
            .marshal(new JAXBElement<C4>(new QName("", "r"), C4.class, new C4()), System.out);
    JAXBContext ctx = SweJaxbContextFactory.createContext(new Class[] { C4.class }, null);
    Map<Class<?>, TypedPattern<?>> patterns = (Map<Class<?>, TypedPattern<?>>) ReflectionTestUtils.getField(ctx,
            "patterns");
    ComplexTypePattern<C4> pattern = (ComplexTypePattern<C4>) patterns.get(C4.class);
    Map<QName, PropertyMetadata<C4, ?>> elements = (Map<QName, PropertyMetadata<C4, ?>>) ReflectionTestUtils
            .getField(pattern, "elements");
    assertThat(elements.size(), equalTo(6));
    assertTrue(elements.containsKey(new QName("", "f1")));
    assertTrue(elements.containsKey(new QName("", "f3")));
    assertTrue(elements.containsKey(new QName("", "f4")));
    assertTrue(elements.containsKey(new QName("", "p1")));
    assertTrue(elements.containsKey(new QName("", "p3")));
    assertTrue(elements.containsKey(new QName("", "p4")));
}

From source file:de.extra.extraClientLight.helper.BuildExtraTransport.java

/**
 * Baut die Query zusammen/*w w w  .  java  2s  .  c o m*/
 * 
 * @param requestBean
 * @return DataRequestType
 */

private static DataRequestType buildQuery(RequestExtraBean requestBean) {

    DataRequestType dataRequest = new DataRequestType();
    DataRequestQueryType dataQuery = new DataRequestQueryType();
    DataRequestArgumentType requestIdArgument = new DataRequestArgumentType();
    DataRequestArgumentType procedureArgument = new DataRequestArgumentType();
    DataRequestArgumentType dataTypeArgument = new DataRequestArgumentType();

    requestIdArgument.setProperty(ClientConstants.QUERY_REQUESTID);

    OperandType operand = new OperandType();
    operand.setValue("1234");

    final JAXBElement<OperandType> jaxbOperand = new JAXBElement<OperandType>(
            new QName("http://www.extra-standard.de/namespace/message/1", "GT"), OperandType.class, operand);
    jaxbOperand.setValue(operand);

    requestIdArgument.getContent().add(jaxbOperand);
    dataQuery.getArgument().add(requestIdArgument);

    procedureArgument.setProperty(ClientConstants.QUERY_PROCEDURE);

    dataQuery.getArgument().add(procedureArgument);

    dataTypeArgument.setProperty(ClientConstants.QUERY_DATATYPE);
    dataQuery.getArgument().add(dataTypeArgument);

    dataRequest.setQuery(dataQuery);

    return dataRequest;
}

From source file:nz.co.senanque.sandbox.ObjectTest.java

@Test
public void test1() throws Exception {
    @SuppressWarnings("unused")
    Object en = IndustryType.fromValue("Ag");
    ValidationSession validationSession = m_validationEngine.createSession();

    // create a customer
    Customer customer = m_customerDAO.createCustomer();
    validationSession.bind(customer);/* ww  w  . ja  v a2  s  .  c  o  m*/
    Invoice invoice = new Invoice();
    invoice.setDescription("test invoice");
    invoice.setTestBoolean(true);
    customer.getInvoices().add(invoice);
    boolean exceptionFound = false;
    try {
        customer.setName("ttt");
    } catch (ValidationException e) {
        exceptionFound = true;
    }
    assertTrue(exceptionFound);

    final ObjectMetadata customerMetadata = validationSession.getMetadata(customer);
    @SuppressWarnings("unused")
    final FieldMetadata customerTypeMetadata = customerMetadata.getFieldMetadata(Customer.CUSTOMERTYPE);

    //        assertFalse(customerTypeMetadata.isActive());
    //        assertFalse(customerTypeMetadata.isReadOnly());
    //        assertFalse(customerTypeMetadata.isRequired());

    customer.setName("aaaab");
    exceptionFound = false;
    try {
        customer.setCustomerType("B");
    } catch (Exception e) {
        exceptionFound = true;
    }
    //        assertTrue(exceptionFound);
    exceptionFound = false;
    try {
        customer.setCustomerType("XXX");
    } catch (Exception e) {
        exceptionFound = true;
    }
    assertTrue(exceptionFound);
    customer.setBusiness(IndustryType.AG);
    customer.setAmount(new Double(500.99));
    final long id = m_customerDAO.save(customer);
    log.info("{}", id);

    // fetch customer back
    customer = m_customerDAO.getCustomer(id);
    @SuppressWarnings("unused")
    final int invoiceCount = customer.getInvoices().size();
    validationSession.bind(customer);
    invoice = new Invoice();
    ValidationUtils.setDefaults(invoice);
    invoice.setDescription("test invoice2");
    invoice.setTestBoolean(true);
    customer.getInvoices().add(invoice);
    assertEquals("xyz", invoice.getTestDefault());
    assertEquals("Ag", invoice.getTestEnumDefault().value());
    m_customerDAO.save(customer);

    // fetch customer again
    customer = m_customerDAO.getCustomer(id);
    customer.toString();
    final Invoice inv0 = customer.getInvoices().get(0);
    assertTrue(inv0.isTestBoolean());

    validationSession.bind(customer);
    final Invoice inv = customer.getInvoices().get(0);
    assertTrue(inv.isTestBoolean());
    customer.getInvoices().remove(inv);

    //ObjectMetadata metadata = validationSession.getMetadata(customer);
    ObjectMetadata metadata = customer.getMetadata();
    assertEquals("this is a description", metadata.getFieldMetadata(Customer.NAME).getDescription());
    assertEquals("ABC", metadata.getFieldMetadata(Customer.NAME).getPermission());
    @SuppressWarnings("unused")
    List<ChoiceBase> choices = metadata.getFieldMetadata(Customer.BUSINESS).getChoiceList();
    //        assertEquals(1,choices.size());
    List<ChoiceBase> choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList();
    //        assertEquals(1,choices2.size());
    choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList();
    //        assertEquals(1,choices2.size());

    for (ChoiceBase choice : choices2) {
        System.out.println(choice.getDescription());
    }

    customer.setName("aab");
    choices2 = metadata.getFieldMetadata(Customer.CUSTOMERTYPE).getChoiceList();
    //        assertEquals(6,choices2.size());

    // Convert customer to XML
    QName qname = new QName("http://www.example.org/sandbox", "Session");
    JAXBElement<Session> sessionJAXB = new JAXBElement<Session>(qname, Session.class, new Session());
    sessionJAXB.getValue().getCustomers().add(customer); //??This fails to actually add
    StringWriter marshallWriter = new StringWriter();
    Result marshallResult = new StreamResult(marshallWriter);
    m_marshaller.marshal(sessionJAXB, marshallResult);
    marshallWriter.flush();
    String result = marshallWriter.getBuffer().toString().trim();
    String xml = result.replaceAll("\\Qhttp://www.example.org/sandbox\\E", "http://www.example.org/sandbox");
    log.info("{}", xml);

    // Convert customer back to objects
    SAXBuilder builder = new SAXBuilder();
    org.jdom.Document resultDOM = builder.build(new StringReader(xml));
    @SuppressWarnings("unchecked")
    JAXBElement<Session> request = (JAXBElement<Session>) m_unmarshaller.unmarshal(new JDOMSource(resultDOM));
    validationSession = m_validationEngine.createSession();
    validationSession.bind(request.getValue());
    assertEquals(3, validationSession.getProxyCount());
    List<Customer> customers = request.getValue().getCustomers();
    assertEquals(1, customers.size());
    assertTrue(customers.get(0).getInvoices().get(0).isTestBoolean());
    customers.clear();
    assertEquals(1, validationSession.getProxyCount());
    request.toString();
    validationSession.close();
}