Search in sources :

Example 1 with Collection

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

the class XMLCaster method toCommentArray.

/**
 * casts a value to a XML Comment Array
 * @param doc XML Document
 * @param o Object to cast
 * @return XML Comment Array
 * @throws PageException
 */
public static Comment[] toCommentArray(Document doc, Object o) throws PageException {
    // Node[]
    if (o instanceof Node[]) {
        Node[] nodes = (Node[]) o;
        if (_isAllOfSameType(nodes, Node.COMMENT_NODE))
            return (Comment[]) nodes;
        Comment[] comments = new Comment[nodes.length];
        for (int i = 0; i < nodes.length; i++) {
            comments[i] = toComment(doc, nodes[i]);
        }
        return comments;
    } else // Collection
    if (o instanceof Collection) {
        Collection coll = (Collection) o;
        Iterator<Object> it = coll.valueIterator();
        List<Comment> comments = new ArrayList<Comment>();
        while (it.hasNext()) {
            comments.add(toComment(doc, it.next()));
        }
        return comments.toArray(new Comment[comments.size()]);
    }
    // Node Map and List
    Node[] nodes = _toNodeArray(doc, o);
    if (nodes != null)
        return toCommentArray(doc, nodes);
    // Single Text Node
    try {
        return new Comment[] { toComment(doc, o) };
    } catch (ExpressionException e) {
        throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Comment Array");
    }
}
Also used : Comment(org.w3c.dom.Comment) XMLException(lucee.runtime.exp.XMLException) Node(org.w3c.dom.Node) 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)

Example 2 with Collection

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

the class XMLCaster method toAttrArray.

/**
 * casts a value to a XML Attr Array
 * @param doc XML Document
 * @param o Object to cast
 * @return XML Attr Array
 * @throws PageException
 */
public static Attr[] toAttrArray(Document doc, Object o) throws PageException {
    // Node[]
    if (o instanceof Node[]) {
        Node[] nodes = (Node[]) o;
        if (_isAllOfSameType(nodes, Node.ATTRIBUTE_NODE))
            return (Attr[]) nodes;
        Attr[] attres = new Attr[nodes.length];
        for (int i = 0; i < nodes.length; i++) {
            attres[i] = toAttr(doc, nodes[i]);
        }
        return attres;
    } else // Collection
    if (o instanceof Collection) {
        Collection coll = (Collection) o;
        Iterator<Entry<Key, Object>> it = coll.entryIterator();
        Entry<Key, Object> e;
        List<Attr> attres = new ArrayList<Attr>();
        Attr attr;
        Collection.Key k;
        while (it.hasNext()) {
            e = it.next();
            k = e.getKey();
            attr = doc.createAttribute(Decision.isNumber(k.getString()) ? "attribute-" + k.getString() : k.getString());
            attr.setValue(Caster.toString(e.getValue()));
            attres.add(attr);
        }
        return attres.toArray(new Attr[attres.size()]);
    }
    // Node Map and List
    Node[] nodes = _toNodeArray(doc, o);
    if (nodes != null)
        return toAttrArray(doc, nodes);
    // Single Text Node
    try {
        return new Attr[] { toAttr(doc, o) };
    } catch (ExpressionException e) {
        throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Attributes Array");
    }
}
Also used : Node(org.w3c.dom.Node) Attr(org.w3c.dom.Attr) ExpressionException(lucee.runtime.exp.ExpressionException) Entry(java.util.Map.Entry) XMLException(lucee.runtime.exp.XMLException) Iterator(java.util.Iterator) Collection(lucee.runtime.type.Collection) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) Key(lucee.runtime.type.Collection.Key)

Example 3 with Collection

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

the class UDFArgConverter method serialize.

private static String serialize(Object oo, Set<Object> done) {
    if (oo == null)
        return "null";
    Object raw = toRaw(oo);
    if (done.contains(raw))
        return "parent reference";
    done.add(raw);
    Collection c = null;
    Object other = null;
    try {
        if (raw instanceof Object[]) {
            return serializeArray((Object[]) raw, done);
        } else if ((c = Caster.toCollection(raw, null)) != null) {
            if (raw != c) {
                done.add(c);
                other = c;
            }
            return serializeCollection(c, done);
        }
        return raw.toString();
    } finally {
        if (other != null)
            done.remove(other);
        done.remove(raw);
    }
}
Also used : Collection(lucee.runtime.type.Collection)

Example 4 with Collection

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

the class Map method _call.

public static Collection _call(PageContext pc, Object obj, UDF udf, boolean parallel, int maxThreads, Query resQry, short type) throws PageException {
    ExecutorService execute = null;
    List<Future<Data<Object>>> futures = null;
    if (parallel) {
        execute = Executors.newFixedThreadPool(maxThreads);
        futures = new ArrayList<Future<Data<Object>>>();
    }
    Collection coll;
    // Array
    if (type == TYPE_ARRAY) {
        coll = invoke(pc, (Array) obj, udf, execute, futures);
    } else // Query
    if (type == TYPE_QUERY) {
        coll = invoke(pc, (Query) obj, udf, execute, futures, resQry);
    } else // Struct
    if (type == TYPE_STRUCT) {
        coll = invoke(pc, (Struct) obj, udf, execute, futures);
    } else // Array
    if (obj instanceof Array) {
        coll = invoke(pc, (Array) obj, udf, execute, futures);
    } else // Query
    if (obj instanceof Query) {
        coll = invoke(pc, (Query) obj, udf, execute, futures, resQry);
    } else // Struct
    if (obj instanceof Struct) {
        coll = invoke(pc, (Struct) obj, udf, execute, futures);
    } else // other Iteratorable
    if (obj instanceof Iteratorable) {
        coll = invoke(pc, (Iteratorable) obj, udf, execute, futures);
    } else // Map
    if (obj instanceof java.util.Map) {
        coll = invoke(pc, (java.util.Map) obj, udf, execute, futures);
    } else // List
    if (obj instanceof List) {
        coll = invoke(pc, (List) obj, udf, execute, futures);
    } else // Iterator
    if (obj instanceof Iterator) {
        coll = invoke(pc, (Iterator) obj, udf, execute, futures);
    } else // Enumeration
    if (obj instanceof Enumeration) {
        coll = invoke(pc, (Enumeration) obj, udf, execute, futures);
    } else // String List
    if (obj instanceof StringListData) {
        coll = invoke(pc, (StringListData) obj, udf, execute, futures);
    } else
        throw new FunctionException(pc, "Map", 1, "data", "cannot iterate througth this type " + Caster.toTypeName(obj.getClass()));
    if (parallel)
        afterCall(pc, coll, futures, execute);
    return coll;
}
Also used : Enumeration(java.util.Enumeration) Query(lucee.runtime.type.Query) FunctionException(lucee.runtime.exp.FunctionException) Struct(lucee.runtime.type.Struct) Array(lucee.runtime.type.Array) StringListData(lucee.runtime.type.util.StringListData) Iteratorable(lucee.runtime.type.Iteratorable) ExecutorService(java.util.concurrent.ExecutorService) ListIterator(java.util.ListIterator) ForEachQueryIterator(lucee.runtime.type.it.ForEachQueryIterator) Iterator(java.util.Iterator) Future(java.util.concurrent.Future) Collection(lucee.runtime.type.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with Collection

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

the class DeserializeJSON method toQuery.

// {"COLUMNS":["AAA","BBB"],"DATA":[["a","b"],["c","d"]]}
// {"ROWCOUNT":2,"COLUMNS":["AAA","BBB"],"DATA":{"aaa":["a","c"],"bbb":["b","d"]}}
private static Object toQuery(Object obj) throws PageException {
    if (obj instanceof Struct) {
        Struct sct = (Struct) obj;
        Key[] keys = CollectionUtil.keys(sct);
        // Columns
        Key[] columns = null;
        if (contains(keys, KeyConstants._COLUMNS))
            columns = toColumns(sct.get(KeyConstants._COLUMNS, null));
        else if (contains(keys, KeyConstants._COLUMNLIST))
            columns = toColumnlist(sct.get(KeyConstants._COLUMNLIST, null));
        // rowcount
        int rowcount = -1;
        if (contains(keys, ROWCOUNT))
            rowcount = toRowCount(sct.get(ROWCOUNT, null));
        else if (contains(keys, KeyConstants._RECORDCOUNT))
            rowcount = toRowCount(sct.get(KeyConstants._RECORDCOUNT, null));
        if (columns != null) {
            if (keys.length == 2 && contains(keys, KeyConstants._DATA)) {
                Array[] data = toData(sct.get(KeyConstants._DATA, null), columns);
                if (data != null) {
                    return new QueryImpl(columns, data, "query");
                }
            } else if (keys.length == 3 && rowcount != -1 && contains(keys, KeyConstants._DATA)) {
                Array[] data = toData(sct.get(KeyConstants._DATA, null), columns, rowcount);
                if (data != null) {
                    return new QueryImpl(columns, data, "query");
                }
            }
        }
        return toQuery(sct, keys);
    } else /*else if(obj instanceof Query) {
			return toQuery((Query) obj);
		}*/
    if (obj instanceof Collection) {
        Collection coll = (Collection) obj;
        return toQuery(coll, CollectionUtil.keys(coll));
    }
    return obj;
}
Also used : Array(lucee.runtime.type.Array) QueryImpl(lucee.runtime.type.QueryImpl) Collection(lucee.runtime.type.Collection) Key(lucee.runtime.type.Collection.Key) Struct(lucee.runtime.type.Struct)

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