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