OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.xml;

import org.openconcerto.utils.StringUtils;
import org.openconcerto.utils.cache.CacheResult;
import org.openconcerto.utils.cache.ICache;
import org.openconcerto.utils.cc.ExnTransformer;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Stack;

/**
 * To decode XML in {@link XMLEncoder} format.
 * 
 * @author Sylvain CUAZ
 * @param <S> type of source
 * @param <E> type of exception
 */
public abstract class AbstractXMLDecoder<S, E extends Exception> {

    protected static interface Children<C> {
        C getNextChild();
    }

    private static final ICache<Object, Method, Object> cache = new ICache<Object, Method, Object>(60, -1, "methods for " + XMLCodecUtils.class);
    private static final ICache<Object, Constructor<?>, Object> cacheCtor = new ICache<Object, Constructor<?>, Object>(60, -1, "constructors for " + XMLCodecUtils.class);

    protected abstract String getLocalName(S elem);

    protected abstract String getAttributeValue(S elem, final String attrName);

    protected abstract String getElementText(S elem) throws E;

    protected abstract S getFirstChild(S elem);

    protected abstract String toString(S elem);

    protected abstract Children<S> createChildren(S elem) throws E;

    public final <K, V> Map<K, V> decodeFromArray(final S elem, final Class<K> keyClass, final Class<V> valueClass) {
        return XMLCodecUtils.decodeFromArray((Object[]) this.decode1(elem), keyClass, valueClass);
    }

    /**
     * Tries to decode an xml element parsed from a string obtained from XMLEncoder. This doesn't
     * use {@link XMLDecoder} as it requires outputting it first to string which is inefficient.
     * NOTE: this decoder supports only a subset of XMLDecoder.
     * 
     * @param javaElem a "java" element.
     * @return the decoded object.
     */
    public final Object decode1(S javaElem) {
        final S elem = getFirstChild(javaElem);
        try {
            return eval(elem, new Stack<Object>(), new HashMap<String, Object>());
        } catch (Exception e) {
            throw new IllegalStateException("error decoding " + toString(javaElem), e);
        }
    }

    private final Object eval(S elem, Stack<Object> context, final Map<String, Object> ids)
            throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, E {
        preEval(elem);
        final Object res = this._eval(elem, context, ids);
        postEval(elem, res);
        return res;
    }

    protected void preEval(S elem) throws E {
    }

    protected void nullDecoded(S elem) throws E {
    }

    protected void idRefDecoded(S elem) throws E {
    }

    protected void postEval(S elem, final Object res) throws E {
    }

    private final Object _eval(S elem, Stack<Object> context, final Map<String, Object> ids)
            throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, E {
        final String n = getLocalName(elem);
        // Ordered from real world scenario
        // string : 80k
        // void : 53k
        // int : 18k
        // object : 9k
        // null : 6k
        switch (n) {
        case "string":
            return getElementText(elem);
        case "void":
        case "object":
            final String idref = getAttributeValue(elem, "idref");
            if (idref != null) {
                if (!ids.containsKey(idref))
                    throw new IllegalStateException("id '" + idref + "' wasn't defined");
                idRefDecoded(elem);
                return ids.get(idref);
            }
            final String id = getAttributeValue(elem, "id");
            final String targetClass = getAttributeValue(elem, "class");
            final Object target = targetClass == null ? context.peek() : Class.forName(targetClass);
            final String propAttr = getAttributeValue(elem, "property");
            final String indexAttr = getAttributeValue(elem, "index");
            final String methodAttr = getAttributeValue(elem, "method");

            // statement or expression
            final Object res = evalContainer(elem, context, ids, new ExnTransformer<List<Object>, Object, Exception>() {
                @Override
                public Object transformChecked(List<Object> args) throws Exception {
                    // call the statement
                    final Object res;

                    if (propAttr != null) {
                        final String methodName = (args.size() == 0 ? "get" : "set") + StringUtils.firstUp(propAttr);
                        res = invoke(target, methodName, args);
                    } else if (indexAttr != null) {
                        final String methodName;
                        if (target instanceof List) {
                            methodName = args.size() == 0 ? "get" : "set";
                            // get(index) or set(index, value)
                            args.add(0, Integer.valueOf(indexAttr));
                            res = invoke(target, methodName, args);
                        } else if (target.getClass().isArray()) {
                            final Class<?> componentType = target.getClass().getComponentType();
                            // in Array there's set(array, int index, Object value) or
                            // setPrimitive(array, int index, primitive value)
                            methodName = (args.size() == 0 ? "get" : "set") + (componentType.isPrimitive() ? StringUtils.firstUp(componentType.getSimpleName()) : "");
                            args.add(0, target);
                            args.add(1, Integer.valueOf(indexAttr));
                            res = invoke(Array.class, methodName, args);
                        } else
                            throw new IllegalStateException("use index with neither List nor array: " + target);
                    } else if (methodAttr != null) {
                        res = invoke(target, methodAttr, args);
                    } else
                        res = getCtor((Class<?>) target, args).newInstance(args.toArray());
                    return res;
                }
            });
            // not very functional but it works
            if (id != null)
                ids.put(id, res);
            return res;
        case "int":
            return Integer.valueOf(getElementText(elem));
        case "null":
            nullDecoded(elem);
            return null;
        case "boolean":
            return Boolean.valueOf(getElementText(elem));
        case "byte":
            return Byte.valueOf(getElementText(elem));
        case "char":
            return Character.valueOf(getElementText(elem).charAt(0));
        case "short":
            return Short.valueOf(getElementText(elem));
        case "long":
            return Long.valueOf(getElementText(elem));
        case "float":
            return Float.valueOf(getElementText(elem));
        case "double":
            return Double.valueOf(getElementText(elem));
        case "array":
            final String classAttr = getAttributeValue(elem, "class");
            final String lengthAttr = getAttributeValue(elem, "length");

            final Class<?> componentClass = parseClassName(classAttr);
            if (lengthAttr != null) {
                context.push(Array.newInstance(componentClass, Integer.parseInt(lengthAttr)));
                final Children<S> children = createChildren(elem);
                S child;
                while ((child = children.getNextChild()) != null) {
                    eval(child, context, ids);
                }
                return context.pop();
            } else {
                final List<Object> items = new LinkedList<>();
                final Children<S> children = createChildren(elem);
                S child;
                while ((child = children.getNextChild()) != null) {
                    items.add(eval(child, context, ids));
                }
                final Object resArray = Array.newInstance(componentClass, items.size());
                int i = 0;
                for (final Object item : items)
                    Array.set(resArray, i++, item);
                return resArray;
            }
        case "class":
            return Class.forName(getElementText(elem));
        default:
            throw new UnsupportedOperationException("doesn't yet support " + n);
        }
    }

    private final Object evalContainer(final S parent, Stack<Object> context, final Map<String, Object> ids, final ExnTransformer<List<Object>, Object, ? extends Exception> transf)
            throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, E {
        final List<Object> args = new ArrayList<Object>();
        final Children<S> children = createChildren(parent);
        boolean noVoid = true;
        S child = null;
        while (noVoid && (child = children.getNextChild()) != null) {
            if (getLocalName(child).equals("void"))
                noVoid = false;
            else {
                args.add(eval(child, context, ids));
            }
        }

        // call the statement
        final Object res = transf.transformCheckedWithExn(args, false, InvocationTargetException.class, InstantiationException.class, IllegalAccessException.class);

        context.push(res);

        // now call the voids
        if (child != null) {
            do {
                eval(child, context, ids);
            } while ((child = children.getNextChild()) != null);
        }
        return context.pop();
    }

    private static final Object invoke(final Object target, String methodName, final List<Object> args) throws IllegalAccessException, InvocationTargetException {
        // <object class="Cell" method="createEmpty" >
        // for static methods the target is already a class
        final Class clazz = target instanceof Class ? (Class) target : target.getClass();
        final Method m = getMethod(clazz, methodName, args);
        return m.invoke(target, args.toArray());
    }

    private static final Method getMethod(Class<?> clazz, String name, List<Object> actualArgs) {
        final List<Class<?>> actualClasses = objectsToClasses(actualArgs);
        final List<Object> key = new ArrayList<Object>(3);
        key.add(clazz);
        key.add(name);
        key.add(actualClasses);

        final CacheResult<Method> cacheRes = cache.check(key);
        if (cacheRes.getState() == CacheResult.State.VALID)
            return cacheRes.getRes();

        final Method res = findMethod(clazz, name, actualClasses);
        if (res == null)
            throw new IllegalStateException("No matching method " + name + " found in " + clazz);
        cache.put(key, res);
        return res;
    }

    private static final Constructor getCtor(Class<?> clazz, List<Object> actualArgs) {
        final List<Class<?>> actualClasses = objectsToClasses(actualArgs);
        final List<Object> key = new ArrayList<Object>(3);
        key.add(clazz);
        key.add(actualClasses);

        final CacheResult<Constructor<?>> cacheRes = cacheCtor.check(key);
        if (cacheRes.getState() == CacheResult.State.VALID)
            return cacheRes.getRes();

        final Constructor res = findCtor(clazz, actualClasses);
        cacheCtor.put(key, res);
        return res;
    }

    private static final List<Class<?>> objectsToClasses(List<Object> actualArgs) {
        final List<Class<?>> actualClasses = new ArrayList<Class<?>>(actualArgs.size());
        for (final Object actualArg : actualArgs)
            actualClasses.add(actualArg == null ? null : actualArg.getClass());
        return actualClasses;
    }

    // TODO return the most specific matching method instead of the first one
    // (handle both Sub/Superclass and primitive/object type)
    private static final Method findMethod(Class<?> clazz, String name, List<Class<?>> actualArgs) {
        for (final Method m : clazz.getMethods()) {
            if (m.getName().equals(name) && callableWith(m.getParameterTypes(), actualArgs)) {
                return m;
            }
        }
        return null;
    }

    // TODO see findMethod()
    private static final Constructor findCtor(Class<?> clazz, List<Class<?>> actualArgs) {
        for (final Constructor m : clazz.getConstructors()) {
            if (callableWith(m.getParameterTypes(), actualArgs)) {
                return m;
            }
        }
        return null;
    }

    private static final boolean callableWith(Class<?>[] formalArgs, List<Class<?>> actualArgs) {
        if (formalArgs.length != actualArgs.size())
            return false;
        int i = 0;
        for (final Class<?> argClass : formalArgs) {
            final Class<?> actualArg = actualArgs.get(i);
            // null match everything
            if (actualArg != null && !argClass.isAssignableFrom(actualArg) && argClass != getPrimitive(actualArg))
                return false;
            i++;
        }

        return true;
    }

    private static Class<?> getPrimitive(Class<?> argClass) {
        if (argClass == Boolean.class)
            return Boolean.TYPE;
        else if (argClass == Character.class)
            return Character.TYPE;
        else if (argClass == Byte.class)
            return Byte.TYPE;
        else if (argClass == Short.class)
            return Short.TYPE;
        else if (argClass == Integer.class)
            return Integer.TYPE;
        else if (argClass == Long.class)
            return Long.TYPE;
        else if (argClass == Float.class)
            return Float.TYPE;
        else if (argClass == Double.class)
            return Double.TYPE;
        else
            return null;
    }

    private static final Map<String, Class> primitiveNames = new HashMap<String, Class>();

    static {
        primitiveNames.put("boolean", boolean.class);
        primitiveNames.put("byte", byte.class);
        primitiveNames.put("char", char.class);
        primitiveNames.put("short", short.class);
        primitiveNames.put("int", int.class);
        primitiveNames.put("long", long.class);
        primitiveNames.put("float", float.class);
        primitiveNames.put("double", double.class);
    }

    /**
     * Parse class names (including primitive).
     * 
     * @param className a class name, eg "java.lang.String" or "int".
     * @return the matching class, eg java.lang.String.class or Integer.TYPE.
     * @throws ClassNotFoundException if the passed name doesn't exist.
     */
    private static Class<?> parseClassName(String className) throws ClassNotFoundException {
        final Class<?> primitive = primitiveNames.get(className);
        if (primitive != null)
            return primitive;
        else
            return Class.forName(className);
    }
}