Search in sources :

Example 11 with Collection

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;
}
Also used : Enumeration(java.util.Enumeration) JavaObject(lucee.runtime.java.JavaObject) ForEachIteratorable(lucee.runtime.type.ForEachIteratorable) Iteratorable(lucee.runtime.type.Iteratorable) Iterator(java.util.Iterator) Collection(lucee.runtime.type.Collection) EnumAsIt(lucee.runtime.type.it.EnumAsIt)

Example 12 with Collection

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");
}
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 13 with Collection

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) + "]");
}
Also used : Collection(lucee.runtime.type.Collection) List(java.util.List) Map(java.util.Map) ExpressionException(lucee.runtime.exp.ExpressionException)

Example 14 with Collection

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");
    }
}
Also used : XMLException(lucee.runtime.exp.XMLException) Node(org.w3c.dom.Node) Iterator(java.util.Iterator) Collection(lucee.runtime.type.Collection) Text(org.w3c.dom.Text) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) ExpressionException(lucee.runtime.exp.ExpressionException)

Example 15 with Collection

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");
    }
}
Also used : XMLException(lucee.runtime.exp.XMLException) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Iterator(java.util.Iterator) Collection(lucee.runtime.type.Collection) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) ExpressionException(lucee.runtime.exp.ExpressionException)

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