Search in sources :

Example 1 with Objects

use of lucee.runtime.type.Objects in project Lucee by lucee.

the class VariableUtilImpl method get.

public Object get(PageContext pc, Object coll, Collection.Key key) throws PageException {
    // Objects
    if (coll instanceof Objects) {
        return ((Objects) coll).get(pc, key);
    } else // Collection
    if (coll instanceof Collection) {
        return ((Collection) coll).get(key);
    } else // Map
    if (coll instanceof Map) {
        Object rtn = null;
        try {
            rtn = ((Map) coll).get(key.getString());
            if (rtn == null && coll.getClass().getName().startsWith("org.hibernate."))
                rtn = ((Map) coll).get(MapAsStruct.getCaseSensitiveKey((Map) coll, key.getString()));
            if (rtn != null)
                return rtn;
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
        }
        rtn = Reflector.getProperty(coll, key.getString(), null);
        if (rtn != null)
            return rtn;
        String realKey = MapAsStruct.getCaseSensitiveKey((Map) coll, key.getString());
        String detail = null;
        if (realKey != null) {
            detail = "The keys for this Map are case-sensitive, use bracked notation like this \"map['" + realKey + "']\" instead of dot notation like this  \"map." + realKey + "\" to address the Map";
        }
        throw new ExpressionException("Key [" + key.getString() + "] doesn't exist in Map (" + ((Map) coll).getClass().getName() + ")", detail);
    } else // List
    if (coll instanceof List) {
        try {
            Object rtn = ((List) coll).get(Caster.toIntValue(key.getString()) - 1);
            if (rtn == null)
                throw new ExpressionException("Key [" + key.getString() + "] doesn't exist in List");
            return rtn;
        } catch (IndexOutOfBoundsException e) {
            throw new ExpressionException("Key [" + key.getString() + "] doesn't exist in List");
        }
    } else // Native Array
    if (Decision.isNativeArray(coll)) {
        Object rtn = ArrayUtil.get(coll, Caster.toIntValue(key.getString()) - 1, null);
        if (rtn == null)
            throw new ExpressionException("Key [" + key.getString() + "] doesn't exist in Native Array");
        return rtn;
    } else // Node
    if (coll instanceof Node) {
        // print.out("get:"+key);
        return XMLStructFactory.newInstance((Node) coll, false).get(key);
    } else if (coll instanceof String) {
        if (Decision.isInteger(key.getString())) {
            // i do the decision call and the caster call, because in most cases the if will be false
            String str = (String) coll;
            int index = Caster.toIntValue(key.getString(), -1);
            if (index > 0 && index <= str.length()) {
                return str.substring(index - 1, index);
            }
        }
    }
    // Direct Object Access
    if (coll != null && pc.getConfig().getSecurityManager().getAccess(SecurityManager.TYPE_DIRECT_JAVA_ACCESS) == SecurityManager.VALUE_YES) {
        return Reflector.getProperty(coll, key.getString());
    }
    throw new ExpressionException("No matching property [" + key.getString() + "] found");
}
Also used : Node(org.w3c.dom.Node) Objects(lucee.runtime.type.Objects) Collection(lucee.runtime.type.Collection) List(java.util.List) Map(java.util.Map) ExpressionException(lucee.runtime.exp.ExpressionException)

Example 2 with Objects

use of lucee.runtime.type.Objects in project Lucee by lucee.

the class VariableUtilImpl method get.

@Override
public Object get(PageContext pc, Object coll, String key) throws PageException {
    // Objects
    if (coll instanceof Objects) {
        return ((Objects) coll).get(pc, KeyImpl.init(key));
    } else // Collection
    if (coll instanceof Collection) {
        return ((Collection) coll).get(KeyImpl.init(key));
    } else // Map
    if (coll instanceof Map) {
        Object rtn = null;
        try {
            rtn = ((Map) coll).get(key);
            // if(rtn==null)rtn=((Map)coll).get(MapAsStruct.getCaseSensitiveKey((Map)coll, key));
            if (rtn != null)
                return rtn;
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
        }
        rtn = Reflector.getProperty(coll, key, null);
        if (rtn != null)
            return rtn;
        throw new ExpressionException("Key [" + key + "] doesn't exist in Map (" + Caster.toClassName(coll) + ")", "keys are [" + keyList(((Map) coll)) + "]");
    } else // List
    if (coll instanceof List) {
        try {
            Object rtn = ((List) coll).get(Caster.toIntValue(key) - 1);
            if (rtn == null)
                throw new ExpressionException("Key [" + key + "] doesn't exist in List");
            return rtn;
        } catch (IndexOutOfBoundsException e) {
            throw new ExpressionException("Key [" + key + "] doesn't exist in List");
        }
    } else // Native Array
    if (Decision.isNativeArray(coll)) {
        Object rtn = ArrayUtil.get(coll, Caster.toIntValue(key) - 1, null);
        if (rtn == null)
            throw new ExpressionException("Key [" + key + "] doesn't exist in Native Array");
        return rtn;
    } else // Node
    if (coll instanceof Node) {
        return XMLStructFactory.newInstance((Node) coll, false).get(key);
    }
    // Direct Object Access
    if (pc.getConfig().getSecurityManager().getAccess(SecurityManager.TYPE_DIRECT_JAVA_ACCESS) == SecurityManager.VALUE_YES) {
        return Reflector.getProperty(coll, key);
    }
    throw new ExpressionException("No matching property [" + key + "] found");
}
Also used : Node(org.w3c.dom.Node) Objects(lucee.runtime.type.Objects) Collection(lucee.runtime.type.Collection) List(java.util.List) Map(java.util.Map) ExpressionException(lucee.runtime.exp.ExpressionException)

Aggregations

List (java.util.List)2 Map (java.util.Map)2 ExpressionException (lucee.runtime.exp.ExpressionException)2 Collection (lucee.runtime.type.Collection)2 Objects (lucee.runtime.type.Objects)2 Node (org.w3c.dom.Node)2