Search in sources :

Example 6 with JAXBElement

use of jakarta.xml.bind.JAXBElement in project xades4j by luisgoncalves.

the class DefaultQualifyingPropertiesUnmarshaller method unmarshalProperties.

@Override
public void unmarshalProperties(Element qualifyingProps, QualifyingPropertiesDataCollector propertyDataCollector) throws UnmarshalException {
    XmlQualifyingPropertiesType xmlQualifyingProps = null;
    try {
        // Create the JAXB unmarshaller and unmarshalProperties the root JAXB element
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        JAXBElement<XmlQualifyingPropertiesType> qualifPropsElem = (JAXBElement<XmlQualifyingPropertiesType>) unmarshaller.unmarshal(qualifyingProps);
        xmlQualifyingProps = qualifPropsElem.getValue();
    } catch (jakarta.xml.bind.UnmarshalException ex) {
        throw new UnmarshalException("Cannot bind XML elements to Java classes", ex);
    } catch (JAXBException ex) {
        throw new UnmarshalException("Cannot unmarshall properties. Error on JAXB unmarshalling.", ex);
    }
    // Iterate the modules to convert the different types of properties.
    for (UnmarshallerModule module : modules) {
        module.convertProperties(xmlQualifyingProps, qualifyingProps, propertyDataCollector);
    }
}
Also used : XmlQualifyingPropertiesType(xades4j.xml.bind.xades.XmlQualifyingPropertiesType) JAXBException(jakarta.xml.bind.JAXBException) JAXBElement(jakarta.xml.bind.JAXBElement) Unmarshaller(jakarta.xml.bind.Unmarshaller)

Example 7 with JAXBElement

use of jakarta.xml.bind.JAXBElement in project spring-framework by spring-projects.

the class Jaxb2UnmarshallerTests method unmarshalPartialStaxSourceXmlStreamReader.

@Test
@Override
@SuppressWarnings("unchecked")
public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
    // skip to flights
    streamReader.nextTag();
    // skip to flight
    streamReader.nextTag();
    Source source = StaxUtils.createStaxSource(streamReader);
    JAXBElement<FlightType> element = (JAXBElement<FlightType>) unmarshaller.unmarshal(source);
    FlightType flight = element.getValue();
    testFlight(flight);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) FlightType(org.springframework.oxm.jaxb.test.FlightType) JAXBElement(jakarta.xml.bind.JAXBElement) XMLInputFactory(javax.xml.stream.XMLInputFactory) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) FileDataSource(jakarta.activation.FileDataSource) Test(org.junit.jupiter.api.Test)

Example 8 with JAXBElement

use of jakarta.xml.bind.JAXBElement in project spring-framework by spring-projects.

the class Jaxb2UnmarshallerTests method unmarshalAnXmlReferingToAWrappedXmlElementDecl.

@Test
@SuppressWarnings("unchecked")
public void unmarshalAnXmlReferingToAWrappedXmlElementDecl() throws Exception {
    // SPR-10714
    unmarshaller = new Jaxb2Marshaller();
    unmarshaller.setPackagesToScan(new String[] { "org.springframework.oxm.jaxb" });
    unmarshaller.afterPropertiesSet();
    Source source = new StreamSource(new StringReader("<brand-airplane><name>test</name></brand-airplane>"));
    JAXBElement<Airplane> airplane = (JAXBElement<Airplane>) unmarshaller.unmarshal(source);
    assertThat(airplane.getValue().getName()).as("Unmarshalling via explicit @XmlRegistry tag should return correct type").isEqualTo("test");
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) JAXBElement(jakarta.xml.bind.JAXBElement) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) FileDataSource(jakarta.activation.FileDataSource) Test(org.junit.jupiter.api.Test)

Example 9 with JAXBElement

use of jakarta.xml.bind.JAXBElement in project spring-framework by spring-projects.

the class StandardClasses method standardClassDataHandler.

public JAXBElement<DataHandler> standardClassDataHandler() {
    DataSource dataSource = new URLDataSource(getClass().getResource("spring-ws.png"));
    DataHandler dataHandler = new DataHandler(dataSource);
    return new JAXBElement<>(NAME, DataHandler.class, dataHandler);
}
Also used : URLDataSource(jakarta.activation.URLDataSource) DataHandler(jakarta.activation.DataHandler) JAXBElement(jakarta.xml.bind.JAXBElement) URLDataSource(jakarta.activation.URLDataSource) DataSource(jakarta.activation.DataSource)

Example 10 with JAXBElement

use of jakarta.xml.bind.JAXBElement in project jaxrs-api by eclipse-ee4j.

the class JaxbLinkTest method testSerializationOfJaxbLink.

@Test
public void testSerializationOfJaxbLink() throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(Link.JaxbLink.class);
    final Marshaller marshaller = jaxbContext.createMarshaller();
    final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    Map<QName, Object> expectedParams = new HashMap<QName, Object>();
    final QName qName = new QName("http://example.ns", "foo");
    expectedParams.put(qName, "test");
    final URI expectedUri = URI.create("/foo/bar");
    Link.JaxbLink expected = new Link.JaxbLink(expectedUri, expectedParams);
    final StringWriter writer = new StringWriter();
    JAXBElement<Link.JaxbLink> jaxbLinkJAXBElement = new JAXBElement<Link.JaxbLink>(new QName("", "link"), Link.JaxbLink.class, expected);
    marshaller.marshal(jaxbLinkJAXBElement, writer);
    final Link.JaxbLink actual = unmarshaller.unmarshal(new StreamSource(new StringReader(writer.toString())), Link.JaxbLink.class).getValue();
    assertEquals(expected, actual, "Unmarshalled JaxbLink instance not equal to the marshalled one.");
    assertEquals(expectedUri, actual.getUri(), "Unmarshalled JaxbLink instance URI not equal to original.");
    assertEquals(expectedParams, actual.getParams(), "Unmarshalled JaxbLink instance params not equal to original.");
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) JAXBContext(jakarta.xml.bind.JAXBContext) JAXBElement(jakarta.xml.bind.JAXBElement) URI(java.net.URI) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) Unmarshaller(jakarta.xml.bind.Unmarshaller) Test(org.junit.jupiter.api.Test)

Aggregations

JAXBElement (jakarta.xml.bind.JAXBElement)16 QName (javax.xml.namespace.QName)11 Test (org.junit.jupiter.api.Test)6 GET (jakarta.ws.rs.GET)3 Path (jakarta.ws.rs.Path)3 StringReader (java.io.StringReader)3 StreamSource (javax.xml.transform.stream.StreamSource)3 Tag (org.junit.jupiter.api.Tag)3 FileDataSource (jakarta.activation.FileDataSource)2 Produces (jakarta.ws.rs.Produces)2 GenericEntity (jakarta.ws.rs.core.GenericEntity)2 SseEventSink (jakarta.ws.rs.sse.SseEventSink)2 JAXBContext (jakarta.xml.bind.JAXBContext)2 JAXBException (jakarta.xml.bind.JAXBException)2 Unmarshaller (jakarta.xml.bind.Unmarshaller)2 Source (javax.xml.transform.Source)2 FlightType (org.springframework.oxm.jaxb.test.FlightType)2 XmlAnyType (xades4j.xml.bind.xades.XmlAnyType)2 XmlSigPolicyQualifiersListType (xades4j.xml.bind.xades.XmlSigPolicyQualifiersListType)2 DataHandler (jakarta.activation.DataHandler)1