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");
}
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) + "]");
}
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;
}
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;
}
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);
}
}
}
Aggregations