Search in sources :

Example 6 with Collection

use of lucee.runtime.type.Collection 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 7 with Collection

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

the class VariableUtilImpl method remove.

public Object remove(Object coll, Collection.Key key) throws PageException {
    // Collection
    if (coll instanceof Collection) {
        return ((Collection) coll).remove(key);
    } else // Map
    if (coll instanceof Map) {
        Object obj = ((Map) coll).remove(key.getString());
        // if(obj==null)obj=((Map)coll).remove(MapAsStruct.getCaseSensitiveKey((Map)coll, key));
        if (obj == null)
            throw new ExpressionException("can't remove key [" + key + "] from map");
        return obj;
    } else // List
    if (coll instanceof List) {
        int i = Caster.toIntValue(key);
        Object obj = ((List) coll).remove(i);
        if (obj == null)
            throw new ExpressionException("can't remove index [" + key + "] from list");
        return obj;
    }
    // TODO Support for Node
    throw new ExpressionException("can't remove key [" + key + "] from Object of type [" + Caster.toTypeName(coll) + "]");
}
Also used : Collection(lucee.runtime.type.Collection) List(java.util.List) Map(java.util.Map) ExpressionException(lucee.runtime.exp.ExpressionException)

Example 8 with Collection

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

the class StructCopy method call.

public static Object call(PageContext pc, Struct src) throws PageException {
    Collection trg = (Collection) Duplicator.duplicate(src, false);
    Collection.Key[] keys = CollectionUtil.keys(trg);
    Collection.Key key;
    Object o;
    for (int i = 0; i < keys.length; i++) {
        key = keys[i];
        o = src.get(key, null);
        if (o instanceof Array)
            trg.set(key, Duplicator.duplicate(o, false));
    }
    return trg;
}
Also used : Array(lucee.runtime.type.Array) Collection(lucee.runtime.type.Collection)

Example 9 with Collection

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

the class StructFindKey method getValues.

/**
 * @param coll
 * @param value
 * @param all
 * @param buffer
 * @return
 * @throws PageException
 */
private static boolean getValues(Array array, Collection coll, String value, boolean all, String path) throws PageException {
    // Collection.Key[] keys=coll.keys();
    Iterator<Entry<Key, Object>> it = coll.entryIterator();
    Entry<Key, Object> e;
    boolean abort = false;
    Collection.Key key;
    while (it.hasNext()) {
        e = it.next();
        if (abort)
            break;
        key = e.getKey();
        Object o = e.getValue();
        // matching value  (this function search first for base)
        if (key.getString().equalsIgnoreCase(value)) {
            Struct sct = new StructImpl();
            sct.setEL(KeyConstants._value, o);
            sct.setEL(KeyConstants._path, createKey(coll, path, key));
            sct.setEL(KeyConstants._owner, coll);
            array.append(sct);
            if (!all)
                abort = true;
        }
        // Collection
        if (!abort) {
            if (o instanceof Collection) {
                abort = getValues(array, ((Collection) o), value, all, createKey(coll, path, key));
            } else if (o instanceof List) {
                abort = getValues(array, ListAsArray.toArray((List<?>) o), value, all, createKey(coll, path, key));
            } else if (o instanceof Map) {
                abort = getValues(array, MapAsStruct.toStruct((Map<?, ?>) o), value, all, createKey(coll, path, key));
            }
        }
    }
    return abort;
}
Also used : Key(lucee.runtime.type.Collection.Key) Entry(java.util.Map.Entry) StructImpl(lucee.runtime.type.StructImpl) Collection(lucee.runtime.type.Collection) List(java.util.List) Map(java.util.Map) Key(lucee.runtime.type.Collection.Key) Struct(lucee.runtime.type.Struct) MapAsStruct(lucee.runtime.type.wrap.MapAsStruct)

Example 10 with Collection

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

the class UDFUtil method argumentCollection.

public static void argumentCollection(Struct values, FunctionArgument[] funcArgs) {
    Object value = values.removeEL(KeyConstants._argumentCollection);
    if (value != null) {
        value = Caster.unwrap(value, value);
        if (value instanceof Argument) {
            Argument argColl = (Argument) value;
            Iterator<Key> it = argColl.keyIterator();
            Key k;
            int i = -1;
            while (it.hasNext()) {
                i++;
                k = it.next();
                if (funcArgs.length > i && k instanceof ArgumentIntKey) {
                    if (!values.containsKey(funcArgs[i].getName()))
                        values.setEL(funcArgs[i].getName(), argColl.get(k, Argument.NULL));
                    else
                        values.setEL(k, argColl.get(k, Argument.NULL));
                } else if (!values.containsKey(k)) {
                    values.setEL(k, argColl.get(k, Argument.NULL));
                }
            }
        } else if (value instanceof Collection) {
            Collection argColl = (Collection) value;
            // Collection.Key[] keys = argColl.keys();
            Iterator<Key> it = argColl.keyIterator();
            Key k;
            while (it.hasNext()) {
                k = it.next();
                if (!values.containsKey(k)) {
                    values.setEL(k, argColl.get(k, Argument.NULL));
                }
            }
        } else if (value instanceof Map) {
            Map map = (Map) value;
            Iterator it = map.entrySet().iterator();
            Map.Entry entry;
            Key key;
            while (it.hasNext()) {
                entry = (Entry) it.next();
                key = Caster.toKey(entry.getKey(), null);
                if (!values.containsKey(key)) {
                    values.setEL(key, entry.getValue());
                }
            }
        } else if (value instanceof java.util.List) {
            java.util.List list = (java.util.List) value;
            Iterator it = list.iterator();
            Object v;
            int index = 0;
            Key k;
            while (it.hasNext()) {
                v = it.next();
                k = ArgumentIntKey.init(++index);
                if (!values.containsKey(k)) {
                    values.setEL(k, v);
                }
            }
        } else {
            values.setEL(KeyConstants._argumentCollection, value);
        }
    }
}
Also used : Entry(java.util.Map.Entry) Argument(lucee.runtime.type.scope.Argument) FunctionArgument(lucee.runtime.type.FunctionArgument) Iterator(java.util.Iterator) Collection(lucee.runtime.type.Collection) ArgumentIntKey(lucee.runtime.type.scope.ArgumentIntKey) ArrayList(java.util.ArrayList) Map(java.util.Map) Key(lucee.runtime.type.Collection.Key) ArgumentIntKey(lucee.runtime.type.scope.ArgumentIntKey)

Aggregations

Collection (lucee.runtime.type.Collection)22 List (java.util.List)12 Iterator (java.util.Iterator)8 ExpressionException (lucee.runtime.exp.ExpressionException)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 Key (lucee.runtime.type.Collection.Key)6 Struct (lucee.runtime.type.Struct)6 Node (org.w3c.dom.Node)6 Entry (java.util.Map.Entry)4 XMLException (lucee.runtime.exp.XMLException)4 Array (lucee.runtime.type.Array)4 NodeList (org.w3c.dom.NodeList)4 Enumeration (java.util.Enumeration)3 FunctionException (lucee.runtime.exp.FunctionException)3 Iteratorable (lucee.runtime.type.Iteratorable)3 Query (lucee.runtime.type.Query)3 StructImpl (lucee.runtime.type.StructImpl)3 ListIterator (java.util.ListIterator)2 ExecutorService (java.util.concurrent.ExecutorService)2