Example usage for javax.xml.bind Unmarshaller unmarshal

List of usage examples for javax.xml.bind Unmarshaller unmarshal

Introduction

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

Prototype

public <T> JAXBElement<T> unmarshal(javax.xml.stream.XMLEventReader reader, Class<T> declaredType)
        throws JAXBException;

Source Link

Document

Unmarshal root element to JAXB mapped declaredType and return the resulting content tree.

Usage

From source file:ee.ria.xroad.common.message.SoapParserImpl.java

@SuppressWarnings("unchecked")
static <T> T unmarshalHeader(Class<?> clazz, SOAPHeader soapHeader, boolean checkRequiredFields)
        throws Exception {
    Unmarshaller unmarshaller = JaxbUtils.createUnmarshaller(clazz);

    if (checkRequiredFields) {
        unmarshaller.setListener(new RequiredHeaderFieldsChecker(clazz));
    }//from   w  w  w .j a  v  a 2  s . c o  m

    unmarshaller.setEventHandler(event -> {
        switch (event.getSeverity()) {
        case ValidationEvent.WARNING:
            return true;
        case ValidationEvent.ERROR:
            Throwable t = event.getLinkedException();

            return !(t instanceof AccessorException && t.getCause() instanceof CodedException);
        case ValidationEvent.FATAL_ERROR:
            return false;
        default:
            return true;
        }
    });

    JAXBElement<T> jaxbElement = (JAXBElement<T>) unmarshaller.unmarshal(soapHeader, clazz);
    return jaxbElement.getValue();
}

From source file:se.inera.intyg.intygstjanst.web.service.converter.SendMessageToCareConverterTest.java

private SendMessageToCareType getSendMessageToCareTypeFromFile(String fileName) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(SendMessageToCareType.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    return unmarshaller.unmarshal(new StreamSource(new ClassPathResource(fileName).getInputStream()),
            SendMessageToCareType.class).getValue();
}

From source file:se.inera.intyg.intygstjanst.web.service.impl.SendMessageToCareServiceImplTest.java

private SendMessageToCare loadFromFile(String fileName) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(SendMessageToCareType.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    SendMessageToCareType sendMessageToCareType = unmarshaller
            .unmarshal(new StreamSource(new ClassPathResource(fileName).getInputStream()),
                    SendMessageToCareType.class)
            .getValue();//from w ww  .  jav a 2  s  .  c  o  m
    return new SendMessageToCareConverter().convertSendMessageToCare(sendMessageToCareType);
}

From source file:se.inera.intyg.intygstjanst.web.integration.stub.RevokeMedicalCertificateResponderStubTest.java

@Test
public void testName() throws Exception {
    // read request from file
    JAXBContext jaxbContext = JAXBContext.newInstance(RevokeMedicalCertificateRequestType.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    RevokeMedicalCertificateRequestType request = unmarshaller.unmarshal(new StreamSource(
            new ClassPathResource("revoke-medical-certificate/revoke-medical-certificate-request.xml")
                    .getInputStream()),/* w  w  w  . j  a v a 2 s  .  c  om*/
            RevokeMedicalCertificateRequestType.class).getValue();

    stub.revokeMedicalCertificate(null, request);

    verify(store).makulera(UTLATANDE_ID, REVOKE_MESSAGE);
}

From source file:edu.wisc.http.converter.xml.JaxbMarshallingHttpMessageConverter.java

@Override
protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException {
    try {//from   w w w  .java2  s.com
        final Unmarshaller unmarshaller = this.jaxbContext.createUnmarshaller();

        Object result = unmarshaller.unmarshal(source, clazz);
        if (result instanceof JAXBElement<?>) {
            result = ((JAXBElement<?>) result).getValue();
        }

        if (!clazz.isInstance(result)) {
            throw new TypeMismatchException(result, clazz);
        }
        return result;
    } catch (JAXBException e) {
        throw new HttpMessageNotReadableException("Could not read [" + clazz + "]", e);
    }
}

From source file:io.mapzone.controller.catalog.csw.CswResponse.java

protected <T> T readObject(InputStream in, Class<T> type) throws JAXBException {
    Unmarshaller unmarshaller = jaxbContext.get().createUnmarshaller();
    JAXBElement<T> elm = unmarshaller.unmarshal(new StreamSource(in), type);
    return elm.getValue();
}

From source file:org.sonarqube.shell.commands.ExportCommands.java

@VisibleForTesting
Profile getProfile(String profile) {
    File path = new File(profile);
    Profile result = new Profile();
    if (!(path.isFile() && path.canRead())) {
        consoleOutAndThrowsException(/*from w w w .  j  a v  a  2  s. c o m*/
                String.format("Invalid path: '%s'. Please provide a valid path and try again.", path));
    }

    try {

        Unmarshaller unmarshaller = context.getUnmarshaller();
        result = unmarshaller.unmarshal(new StreamSource(path), Profile.class).getValue();
    } catch (JAXBException e) {
        // do nothing because the profile will don't pass the validation
    }

    if (!result.isValid()) {
        consoleOutAndThrowsException(String.format(
                "Failed to parse the profile file: '%s'. Please provide a valid profile and try again.", path));
    }
    consoleOut("Profile has been successfully loaded.\n" + result);
    return result;
}

From source file:es.caib.sgtsic.xml.XmlManager.java

private T unmarshal(InputStream is) throws JAXBException {

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    return (T) jaxbUnmarshaller.unmarshal(new StreamSource(is), clazz);

}

From source file:io.mapzone.controller.catalog.csw.TransactionResponse.java

protected void handleInsert(InsertType op, UnitOfWork uow) throws JAXBException {
    for (Element any : op.getAny()) {
        Unmarshaller unmarshaller = jaxbContext.get().createUnmarshaller();
        SummaryRecordType record = unmarshaller.unmarshal(any, SummaryRecordType.class).getValue();

        CatalogEntry entry = uow.createEntity(CatalogEntry.class, null, CatalogEntry.defaults);
        updateEntry(entry, record);//from   w  w w .ja  v a  2 s.co  m
    }
}

From source file:io.mapzone.controller.catalog.csw.TransactionResponse.java

protected void handleUpdate(UpdateType op, UnitOfWork uow) throws Exception {
    Unmarshaller unmarshaller = jaxbContext.get().createUnmarshaller();
    SummaryRecordType record = unmarshaller.unmarshal(op.getAny(), SummaryRecordType.class).getValue();

    BooleanExpression expr = new FilterParser(op.getConstraint(), null).parse();
    ResultSet<CatalogEntry> rs = uow.query(CatalogEntry.class).where(expr).maxResults(2).execute();
    if (rs.size() == 0) {
        log.warn("No entry found for: " + expr);
    } else if (rs.size() > 1) {
        log.warn("Multiple entries found for: " + expr);
    } else {/*from w  ww.  j  av  a 2 s  . com*/
        updateEntry(rs.stream().findAny().get(), record);
    }
}