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

pl.szczepanik.silencio.processors.visitors.AbstractJacksonVisitor Maven / Gradle / Ivy

Go to download

Silencio is a Java library for transforming and converting XML, JSON, Properties, YAML and other formats. It is applicable for most operations such as obfuscation, encryption, minimisation (minifying), anonymous. Library is fully customizable and extensible.

The newest version!
package pl.szczepanik.silencio.processors.visitors;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import pl.szczepanik.silencio.core.Key;
import pl.szczepanik.silencio.core.ProcessorException;

/**
 * Collects common methods and attributes used by visitors that implements Jackson's library.
 *
 * @author Damian Szczepanik (damianszczepanik@github)
 */
public abstract class AbstractJacksonVisitor extends AbstractVisitor {

    /**
     * Process map or array/list objects.
     *
     * @param key   key
     * @param value value that should be converted
     */
    protected void processComplex(String key, Object value) {
        if (isMap(value)) {
            processMap((Map) value);
        } else if (isArray(value)) {
            processArray(key, (List) value);
        } else {
            throw new ProcessorException("Unknown type of the key: " + value.getClass().getName());
        }
    }

    /**
     * Process map object.
     *
     * @param map value that should be converted
     */
    protected void processMap(Map map) {
        for (Map.Entry keyMap : map.entrySet()) {
            String key = keyMap.getKey();
            Object value = keyMap.getValue();

            if (isBasicType(value)) {
                map.put(key, processValue(new Key(key), value).getValue());
            } else {
                processComplex(key, value);
            }
        }
    }

    /**
     * Process array/list object.
     *
     * @param list value that should be converted
     */
    private void processArray(String key, List list) {
        for (int i = 0; i < list.size(); i++) {
            Object value = list.get(i);
            if (isBasicType(value)) {
                list.set(i, processValue(new Key(key), value).getValue());
            } else {
                processComplex(key, value);
            }
        }
    }

    private boolean isMap(Object value) {
        return value instanceof Map;
    }

    private boolean isArray(Object value) {
        return value instanceof List;
    }

    private boolean isBasicType(Object value) {
        // list of available types http://wiki.fasterxml.com/JacksonInFiveMinutes
        return value instanceof String
                || value instanceof Integer || value instanceof Long || value instanceof BigInteger
                || value instanceof Double || value instanceof BigDecimal
                || value instanceof Boolean
                || value == null;
    }
}