Example usage for javax.xml.bind Unmarshaller setAdapter

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

Introduction

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

Prototype

public void setAdapter(XmlAdapter adapter);

Source Link

Document

Associates a configured instance of XmlAdapter with this unmarshaller.

Usage

From source file:com.rapid.server.RapidHttpServlet.java

public static Unmarshaller getUnmarshaller() throws JAXBException, IOException {

    // initialise the unmarshaller
    Unmarshaller unmarshaller = _jaxbContext.createUnmarshaller();
    // add the encrypted xml adapter
    unmarshaller.setAdapter(_encryptedXmlAdapter);

    // add a validation listener (this makes for better error messages)
    unmarshaller.setEventHandler(new ValidationEventHandler() {
        @Override/*from  www . j  a v  a 2s  .  c o  m*/
        public boolean handleEvent(ValidationEvent event) {

            // get the location
            ValidationEventLocator location = event.getLocator();

            // log
            _logger.debug("JAXB validation event - " + event.getMessage()
                    + (location == null ? ""
                            : " at line " + location.getLineNumber() + ", column " + location.getColumnNumber()
                                    + ", node " + location.getNode()));

            // messages with "unrecognized type name" are very useful they're not sever themselves must almost always followed by a severe with a less meaningful message 
            if (event.getMessage().contains("unrecognized type name")
                    || event.getSeverity() == ValidationEvent.FATAL_ERROR) {
                return false;
            } else {
                return true;
            }

        }
    });

    // return
    return unmarshaller;
}

From source file:org.codice.ddf.parser.xml.XmlParser.java

private <T> T unmarshal(ParserConfigurator configurator, Function<Unmarshaller, T> func)
        throws ParserException {
    JAXBContext jaxbContext = getContext(configurator.getContextPath(), configurator.getClassLoader());

    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    try {/*from   www .j a v a2s  . co m*/
        Thread.currentThread().setContextClassLoader(configurator.getClassLoader());

        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        if (configurator.getAdapter() != null) {
            unmarshaller.setAdapter(configurator.getAdapter());
        }
        if (configurator.getHandler() != null) {
            unmarshaller.setEventHandler(configurator.getHandler());
        }
        for (Map.Entry<String, Object> propRow : configurator.getProperties().entrySet()) {
            unmarshaller.setProperty(propRow.getKey(), propRow.getValue());
        }

        return func.apply(unmarshaller);
    } catch (RuntimeException e) {
        LOGGER.error("Error unmarshalling ", e);
        throw new ParserException("Error unmarshalling", e);
    } catch (JAXBException e) {
        LOGGER.error("Error unmarshalling ", e);
        throw new ParserException("Error unmarshalling", e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }
}

From source file:org.fuin.units4j.Units4JUtils.java

/**
 * Unmarshals the given data using a given context. A <code>null</code> XML
 * data argument returns <code>null</code>.
 * /* w  w  w.j  av  a 2  s.  c  o  m*/
 * @param ctx
 *            Context to use.
 * @param xmlData
 *            XML data or <code>null</code>.
 * @param adapters
 *            Adapters to associate with the unmarshaller or
 *            <code>null</code>.
 * 
 * @return Data or <code>null</code>.
 * 
 * @param <T>
 *            Type of the expected data.
 */
@SuppressWarnings("unchecked")
public static <T> T unmarshal(@NotNull final JAXBContext ctx, final String xmlData,
        final XmlAdapter<?, ?>[] adapters) {
    if (xmlData == null) {
        return null;
    }
    try {
        final Unmarshaller unmarshaller = ctx.createUnmarshaller();
        if (adapters != null) {
            for (XmlAdapter<?, ?> adapter : adapters) {
                unmarshaller.setAdapter(adapter);
            }
        }
        unmarshaller.setEventHandler(new ValidationEventHandler() {
            @Override
            public boolean handleEvent(final ValidationEvent event) {
                if (event.getSeverity() > 0) {
                    if (event.getLinkedException() == null) {
                        throw new RuntimeException("Error unmarshalling the data: " + event.getMessage());
                    }
                    throw new RuntimeException("Error unmarshalling the data", event.getLinkedException());
                }
                return true;
            }
        });
        return (T) unmarshaller.unmarshal(new StringReader(xmlData));
    } catch (final JAXBException ex) {
        throw new RuntimeException("Error unmarshalling test data", ex);
    }
}

From source file:org.gitools.resource.AbstractXmlFormat.java

public void beforeRead(InputStream in, IResourceLocator resourceLocator, Unmarshaller unmarshaller,
        IProgressMonitor progressMonitor) throws PersistenceException {
    dependencies = new ArrayList<>();
    unmarshaller.setAdapter(new ResourceReferenceXmlAdapter(dependencies, resourceLocator));
}

From source file:org.springframework.oxm.jaxb.Jaxb2Marshaller.java

/**
 * Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.
 * Gets called after creation of JAXB {@code Marshaller}, and after the respective properties have been set.
 * <p>The default implementation sets the {@link #setUnmarshallerProperties(Map) defined properties}, the {@link
 * #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[])
 * schemas}, {@link #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener) listener}, and
 * {@link #setAdapters(XmlAdapter[]) adapters}.
 *//*w ww . j  a  v a  2  s  .  co m*/
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
    if (this.unmarshallerProperties != null) {
        for (String name : this.unmarshallerProperties.keySet()) {
            unmarshaller.setProperty(name, this.unmarshallerProperties.get(name));
        }
    }
    if (this.unmarshallerListener != null) {
        unmarshaller.setListener(this.unmarshallerListener);
    }
    if (this.validationEventHandler != null) {
        unmarshaller.setEventHandler(this.validationEventHandler);
    }
    if (this.adapters != null) {
        for (XmlAdapter<?, ?> adapter : this.adapters) {
            unmarshaller.setAdapter(adapter);
        }
    }
    if (this.schema != null) {
        unmarshaller.setSchema(this.schema);
    }
}