All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bremersee.sms.ExtensionUtils Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
/*
 * Copyright 2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.bremersee.sms;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import org.apache.commons.lang3.Validate;
import org.w3c.dom.Node;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 

* Utility methods to transform an extension that was serialized by a * {@link JAXBContext} or an {@link ObjectMapper} into a POJO. *

* * @author Christian Bremer */ public abstract class ExtensionUtils { private static final ObjectMapper DEFAULT_OBJECT_MAPPER = new ObjectMapper(); private static final Map, JAXBContext> JAXB_CONTEXTS = new ConcurrentHashMap<>(); private ExtensionUtils() { } private static JAXBContext getJaxbContext(Class valueType) throws JAXBException { JAXBContext jaxbContext = JAXB_CONTEXTS.get(valueType); if (jaxbContext == null) { jaxbContext = JAXBContext.newInstance(valueType); JAXB_CONTEXTS.put(valueType, jaxbContext); } return jaxbContext; } /** * Transforms a XML node or a JSON map into an object. * * @param xmlNodeOrJsonMap * the XML node or JSON map * @param valueType * the class of the target object * @param jaxbContextOrObjectMapper * a {@link JAXBContext} or a {@link ObjectMapper} (can be null) * @return the target object * @throws Exception * if transformation fails */ @SuppressWarnings("unchecked") public static T transform(Object xmlNodeOrJsonMap, Class valueType, Object jaxbContextOrObjectMapper) throws Exception { if (xmlNodeOrJsonMap == null) { return null; } Validate.notNull(valueType, "valueType must not be null"); if (valueType.isAssignableFrom(xmlNodeOrJsonMap.getClass())) { return valueType.cast(xmlNodeOrJsonMap); } if (xmlNodeOrJsonMap instanceof Node) { JAXBContext jaxbContext; if (jaxbContextOrObjectMapper != null && jaxbContextOrObjectMapper instanceof JAXBContext) { jaxbContext = (JAXBContext) jaxbContextOrObjectMapper; } else { jaxbContext = null; } return xmlNodeToObject((Node) xmlNodeOrJsonMap, valueType, jaxbContext); } if (xmlNodeOrJsonMap instanceof Map) { ObjectMapper objectMapper; if (jaxbContextOrObjectMapper != null && jaxbContextOrObjectMapper instanceof ObjectMapper) { objectMapper = (ObjectMapper) jaxbContextOrObjectMapper; } else { objectMapper = null; } return jsonMapToObject((Map) xmlNodeOrJsonMap, valueType, objectMapper); } throw new IllegalArgumentException( "xmlNodeOrJsonMap must be of type " + Node.class.getName() + " or of type " + Map.class.getName()); } /** * Transforms a XML node into an object. * * @param node * the XML node * @param valueType * the class of the target object * @param jaxbContext * the {@link JAXBContext} (can be null) * @return the target object * @throws JAXBException * if transformation fails */ public static T xmlNodeToObject(Node node, Class valueType, JAXBContext jaxbContext) throws JAXBException { if (node == null) { return null; } Validate.notNull(valueType, "valueType must not be null"); if (jaxbContext == null) { jaxbContext = getJaxbContext(valueType); } return valueType.cast(jaxbContext.createUnmarshaller().unmarshal(node)); } /** * Transforms a JSON map into an object. * * @param map * the JSON map * @param valueType * the class of the target object * @param objectMapper * the JSON object mapper (can be null) * @return the target object * @throws IOException * if transformation fails */ public static T jsonMapToObject(Map map, Class valueType, ObjectMapper objectMapper) throws IOException { if (map == null) { return null; } Validate.notNull(valueType, "valueType must not be null"); if (objectMapper == null) { objectMapper = DEFAULT_OBJECT_MAPPER; } return objectMapper.readValue(objectMapper.writeValueAsBytes(map), valueType); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy