Example usage for com.fasterxml.jackson.dataformat.xml XmlFactory configure

List of usage examples for com.fasterxml.jackson.dataformat.xml XmlFactory configure

Introduction

In this page you can find the example usage for com.fasterxml.jackson.dataformat.xml XmlFactory configure.

Prototype

public final XmlFactory configure(ToXmlGenerator.Feature f, boolean state) 

Source Link

Document

Method for enabling or disabling specified XML generator feature.

Usage

From source file:org.restlet.ext.jackson.JacksonRepresentation.java

/**
 * Creates a Jackson object mapper based on a media type. It supports JSON,
 * JSON Smile, XML, YAML and CSV./*from w  w  w . j  a  v a2  s.c  o m*/
 * 
 * @return The Jackson object mapper.
 */
protected ObjectMapper createObjectMapper() {
    ObjectMapper result = null;

    if (MediaType.APPLICATION_JSON.isCompatible(getMediaType())) {
        JsonFactory jsonFactory = new JsonFactory();
        jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
        result = new ObjectMapper(jsonFactory);
    } else if (MediaType.APPLICATION_JSON_SMILE.isCompatible(getMediaType())) {
        SmileFactory smileFactory = new SmileFactory();
        smileFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
        result = new ObjectMapper(smileFactory);
        // [ifndef android]
    } else if (MediaType.APPLICATION_XML.isCompatible(getMediaType())
            || MediaType.TEXT_XML.isCompatible(getMediaType())) {
        javax.xml.stream.XMLInputFactory xif = XmlFactoryProvider.newInputFactory();
        xif.setProperty(javax.xml.stream.XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
                isExpandingEntityRefs());
        xif.setProperty(javax.xml.stream.XMLInputFactory.SUPPORT_DTD, isExpandingEntityRefs());
        xif.setProperty(javax.xml.stream.XMLInputFactory.IS_VALIDATING, isValidatingDtd());
        javax.xml.stream.XMLOutputFactory xof = XmlFactoryProvider.newOutputFactory();
        XmlFactory xmlFactory = new XmlFactory(xif, xof);
        xmlFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
        result = new XmlMapper(xmlFactory);
        // [enddef]
    } else if (MediaType.APPLICATION_YAML.isCompatible(getMediaType())
            || MediaType.TEXT_YAML.isCompatible(getMediaType())) {
        YAMLFactory yamlFactory = new YAMLFactory();
        yamlFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
        result = new ObjectMapper(yamlFactory);
    } else if (MediaType.TEXT_CSV.isCompatible(getMediaType())) {
        CsvFactory csvFactory = new CsvFactory();
        csvFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
        result = new CsvMapper(csvFactory);
    } else {
        JsonFactory jsonFactory = new JsonFactory();
        jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
        result = new ObjectMapper(jsonFactory);
    }

    return result;
}