Example usage for com.fasterxml.jackson.dataformat.xml XmlMapper XmlMapper

List of usage examples for com.fasterxml.jackson.dataformat.xml XmlMapper XmlMapper

Introduction

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

Prototype

public XmlMapper(JacksonXmlModule module) 

Source Link

Usage

From source file:ninja.utils.XmlMapperProvider.java

@Override
public XmlMapper get() {

    JacksonXmlModule module = new JacksonXmlModule();
    // Check out: https://github.com/FasterXML/jackson-dataformat-xml
    // setDefaultUseWrapper produces more similar output to
    // the Json output. You can change that with annotations in your
    // models./*w  ww . ja  v  a 2s  .c om*/
    module.setDefaultUseWrapper(false);

    XmlMapper xmlMapper = new XmlMapper(module);
    xmlMapper.registerModule(new AfterburnerModule());

    return xmlMapper;

}

From source file:com.spectralogic.ds3autogen.Ds3SpecParserImpl.java

public Ds3SpecParserImpl() {
    module = new JacksonXmlModule();
    module.setDefaultUseWrapper(false);/* w  w  w. j av a 2 s .co  m*/
    mapper = new XmlMapper(module);
    mapper.registerModule(new GuavaModule());
    final SimpleFilterProvider filterProvider = new SimpleFilterProvider().setFailOnUnknownId(false);
    mapper.setFilters(filterProvider);
}

From source file:org.doctester.testbrowser.Response.java

public Response(Map<String, String> headers, int httpStatus, String payload) {

    // configure the JacksonXml mapper in a cleaner way...
    JacksonXmlModule module = new JacksonXmlModule();
    // Check out: https://github.com/FasterXML/jackson-dataformat-xml
    // setDefaultUseWrapper produces more similar output to
    // the Json output. You can change that with annotations in your
    // models./*from w  ww . ja  v a 2 s .  com*/
    module.setDefaultUseWrapper(false);
    this.xmlMapper = new XmlMapper(module);

    this.objectMapper = new ObjectMapper();

    this.headers = headers;
    this.httpStatus = httpStatus;
    this.payload = payload;

}

From source file:com.kurtraschke.ctatt.gtfsrealtime.services.TrainTrackerDataService.java

@PostConstruct
public void start() {
    _connectionManager = new BasicHttpClientConnectionManager();
    JacksonXmlModule m = new JacksonXmlModule();
    m.setDefaultUseWrapper(false);/*w ww  . ja  v  a2  s . c  o m*/
    _xmlMapper = new XmlMapper(m);
}

From source file:org.jberet.support.io.XmlItemReaderWriterBase.java

/**
 * Initializes {@link #xmlFactory} field, which may be instantiated or obtained from other part of the application.
 *//*from  w  ww  . ja  v a2 s  .c  o m*/
protected void initXmlFactory() throws Exception {
    if (xmlFactoryLookup != null) {
        xmlFactory = InitialContext.doLookup(xmlFactoryLookup);
        xmlMapper = (XmlMapper) xmlFactory.getCodec();
    } else {
        initXmlModule();
        xmlFactory = new XmlFactory();
        xmlMapper = xmlModule == null ? new XmlMapper(xmlFactory) : new XmlMapper(xmlFactory, xmlModule);
        xmlFactory.setCodec(xmlMapper);
    }
    MappingJsonFactoryObjectFactory.configureCustomSerializersAndDeserializers(xmlMapper, null, null,
            customDataTypeModules, getClass().getClassLoader());
}

From source file:org.resthub.web.converter.MappingJackson2XmlHttpMessageConverter.java

/**
 * Construct a new {@code BindingJacksonHttpMessageConverter}.
 *//*  w  w  w  .j  a  v  a  2  s .  c o m*/
public MappingJackson2XmlHttpMessageConverter() {
    super(new MediaType("application", "xml", DEFAULT_CHARSET));
    JacksonXmlModule xmlModule = new JacksonXmlModule();
    xmlModule.setDefaultUseWrapper(false);
    objectMapper = new XmlMapper(xmlModule);
    SimpleModule module = new SimpleModule();
    module.addAbstractTypeMapping(Page.class, PageResponse.class);
    objectMapper.registerModule(module);
    AnnotationIntrospector introspector = new JacksonAnnotationIntrospector();
    objectMapper.setAnnotationIntrospector(introspector);
}

From source file:org.jberet.support.io.XmlFactoryObjectFactory.java

/**
 * Gets an instance of {@code com.fasterxml.jackson.dataformat.xml.XmlFactory} based on the resource configuration in the
 * application server. The parameter {@code environment} contains XmlFactory configuration properties, and accepts
 * the following properties:/*  w ww  . ja v a2  s  . com*/
 * <ul>
 * <li>inputDecorator: fully-qualified name of a class that extends {@code com.fasterxml.jackson.core.io.InputDecorator}
 * <li>outputDecorator: fully-qualified name of a class that extends {@code com.fasterxml.jackson.core.io.OutputDecorator}
 * <li>xmlTextElementName: 
 * <li>defaultUseWrapper:
 * </ul>
 *
 * @param obj         the JNDI name of {@code com.fasterxml.jackson.dataformat.xml.XmlFactory} resource
 * @param name        always null
 * @param nameCtx     always null
 * @param environment a {@code Hashtable} of configuration properties
 * @return an instance of {@code com.fasterxml.jackson.dataformat.xml.XmlFactory}
 * @throws Exception any exception occurred
 */
@Override
public Object getObjectInstance(final Object obj, final Name name, final Context nameCtx,
        final Hashtable<?, ?> environment) throws Exception {
    XmlFactory xmlFactory = xmlFactoryCached;
    if (xmlFactory == null) {
        synchronized (this) {
            xmlFactory = xmlFactoryCached;
            if (xmlFactory == null) {
                xmlFactoryCached = xmlFactory = new XmlFactory();
            }

            JacksonXmlModule xmlModule = null;
            NoMappingJsonFactoryObjectFactory.configureInputDecoratorAndOutputDecorator(xmlFactory,
                    environment);

            final Object xmlTextElementName = environment.get("xmlTextElementName");
            if (xmlTextElementName != null) {
                xmlModule = new JacksonXmlModule();
                xmlModule.setXMLTextElementName((String) xmlTextElementName);
            }

            final Object defaultUseWrapper = environment.get("defaultUseWrapper");
            if (defaultUseWrapper != null) {
                if (defaultUseWrapper.equals("false")) {
                    if (xmlModule == null) {
                        xmlModule = new JacksonXmlModule();
                    }
                    xmlModule.setDefaultUseWrapper(false);
                } else if (defaultUseWrapper.equals("true")) {
                    //default value is already true, so nothing to do
                } else {
                    throw SupportMessages.MESSAGES.invalidReaderWriterProperty(null, (String) defaultUseWrapper,
                            "defaultUseWrapper");
                }
            }

            final XmlMapper xmlMapper = xmlModule == null ? new XmlMapper(xmlFactory)
                    : new XmlMapper(xmlFactory, xmlModule);
            xmlFactory.setCodec(xmlMapper);
        }
    }
    return xmlFactory;
}

From source file:com.opentable.extension.BodyTransformer.java

@Override
public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
        FileSource fileSource, Parameters parameters) {
    Map object = null;/*from ww w . ja  va 2  s . c o  m*/
    String requestBody = request.getBodyAsString();

    try {
        object = jsonMapper.readValue(requestBody, Map.class);
    } catch (IOException e) {
        try {
            JacksonXmlModule configuration = new JacksonXmlModule();
            //Set the default value name for xml elements like <user type="String">Dmytro</user>
            configuration.setXMLTextElementName("value");
            xmlMapper = new XmlMapper(configuration);
            object = xmlMapper.readValue(requestBody, Map.class);
        } catch (IOException ex) {
            //Validate is a body has the 'name=value' parameters
            if (StringUtils.isNotEmpty(requestBody)
                    && (requestBody.contains("&") || requestBody.contains("="))) {
                object = new HashMap();
                String[] pairedValues = requestBody.split("&");
                for (String pair : pairedValues) {
                    String[] values = pair.split("=");
                    object.put(values[0], values.length > 1 ? decodeUTF8Value(values[1]) : "");
                }
            } else {
                System.err.println(
                        "[Body parse error] The body doesn't match any of 3 possible formats (JSON, XML, key=value).");
            }
        }
    }

    if (hasEmptyBody(responseDefinition)) {
        return responseDefinition;
    }

    String body = getBody(responseDefinition, fileSource);

    return ResponseDefinitionBuilder.like(responseDefinition).but().withBodyFile(null)
            .withBody(transformResponse(object, body)).build();
}

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./* w  w w  .j av  a2s  . 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;
}