use of lucee.runtime.type.Collection in project Lucee by lucee.
the class ForEachUtil method _toIterator.
private static Iterator _toIterator(Object o) {
if (o instanceof Iteratorable) {
return ((Iteratorable) o).keysAsStringIterator();
}
if (o instanceof Iterator) {
return (Iterator) o;
}
if (o instanceof Enumeration) {
return new EnumAsIt((Enumeration) o);
}
if (o instanceof JavaObject) {
Collection coll = Caster.toCollection(((JavaObject) o).getEmbededObject(null), null);
if (coll != null)
return coll.getIterator();
String[] names = ClassUtil.getFieldNames(((JavaObject) o).getClazz());
return new ArrayIterator(names);
} else if (o instanceof CharSequence) {
return ListUtil.listToArray(o.toString(), ',').getIterator();
}
return null;
}
use of lucee.runtime.type.Collection 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");
}
use of lucee.runtime.type.Collection in project Lucee by lucee.
the class VariableUtilImpl method remove.
/**
* @see lucee.runtime.util.VariableUtil#remove(java.lang.Object, java.lang.String)
*/
@Override
public Object remove(Object coll, String key) throws PageException {
// Collection
if (coll instanceof Collection) {
return ((Collection) coll).remove(KeyImpl.init(key));
} else // Map
if (coll instanceof Map) {
Object obj = ((Map) coll).remove(key);
// 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 XMLCaster method toTextArray.
/**
* casts a value to a XML Text Array
* @param doc XML Document
* @param o Object to cast
* @return XML Text Array
* @throws PageException
*/
public static Text[] toTextArray(Document doc, Object o) throws PageException {
// Node[]
if (o instanceof Node[]) {
Node[] nodes = (Node[]) o;
if (_isAllOfSameType(nodes, Node.TEXT_NODE))
return (Text[]) nodes;
Text[] textes = new Text[nodes.length];
for (int i = 0; i < nodes.length; i++) {
textes[i] = toText(doc, nodes[i]);
}
return textes;
} else // Collection
if (o instanceof Collection) {
Collection coll = (Collection) o;
Iterator<Object> it = coll.valueIterator();
List<Text> textes = new ArrayList<Text>();
while (it.hasNext()) {
textes.add(toText(doc, it.next()));
}
return textes.toArray(new Text[textes.size()]);
}
// Node Map and List
Node[] nodes = _toNodeArray(doc, o);
if (nodes != null)
return toTextArray(doc, nodes);
// Single Text Node
try {
return new Text[] { toText(doc, o) };
} catch (ExpressionException e) {
throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Text Array");
}
}
use of lucee.runtime.type.Collection in project Lucee by lucee.
the class XMLCaster method toElementArray.
/**
* casts a value to a XML Element Array
* @param doc XML Document
* @param o Object to cast
* @return XML Comment Array
* @throws PageException
*/
public static Element[] toElementArray(Document doc, Object o) throws PageException {
// Node[]
if (o instanceof Node[]) {
Node[] nodes = (Node[]) o;
if (_isAllOfSameType(nodes, Node.ELEMENT_NODE))
return (Element[]) nodes;
Element[] elements = new Element[nodes.length];
for (int i = 0; i < nodes.length; i++) {
elements[i] = toElement(doc, nodes[i]);
}
return elements;
} else // Collection
if (o instanceof Collection) {
Collection coll = (Collection) o;
Iterator<Object> it = coll.valueIterator();
List<Element> elements = new ArrayList<Element>();
while (it.hasNext()) {
elements.add(toElement(doc, it.next()));
}
return elements.toArray(new Element[elements.size()]);
}
// Node Map and List
Node[] nodes = _toNodeArray(doc, o);
if (nodes != null)
return toElementArray(doc, nodes);
// Single Text Node
try {
return new Element[] { toElement(doc, o) };
} catch (ExpressionException e) {
throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Element Array");
}
}
Aggregations