use of lucee.runtime.type.ObjectWrap in project Lucee by lucee.
the class Caster method toArray.
/**
* cast a Object to a Array Object
* @param o Object to cast
* @return casted Array
* @throws PageException
*/
public static Array toArray(Object o) throws PageException {
if (o instanceof Array)
return (Array) o;
else if (o instanceof Object[]) {
return new ArrayImpl((Object[]) o);
} else if (o instanceof List) {
// new ArrayImpl(((List) o).toArray());
return ListAsArray.toArray((List) o);
} else if (o instanceof Set) {
// new ArrayImpl(((List) o).toArray());
return toArray(((Set) o).toArray());
} else if (o instanceof XMLStruct) {
XMLMultiElementStruct xmes;
if (o instanceof XMLMultiElementStruct) {
xmes = (XMLMultiElementStruct) o;
} else {
XMLStruct sct = (XMLStruct) o;
Array a = new ArrayImpl();
a.append(o);
xmes = new XMLMultiElementStruct(a, sct.getCaseSensitive());
}
return new XMLMultiElementArray(xmes);
} else if (o instanceof ObjectWrap) {
return toArray(((ObjectWrap) o).getEmbededObject());
} else if (o instanceof Struct) {
// function _toArray
if (o instanceof Component) {
Component c = (Component) o;
PageContext pc = ThreadLocalPageContext.get();
if (pc != null) {
Member member = c.getMember(Component.ACCESS_PRIVATE, KeyConstants.__toArray, false, false);
// Object o = get(pc,"_toString",null);
if (member instanceof UDFPlus) {
UDFPlus udf = (UDFPlus) member;
if (udf.getReturnType() == CFTypes.TYPE_ARRAY && udf.getFunctionArguments().length == 0) {
return Caster.toArray(c.call(pc, KeyConstants.__toArray, new Object[0]));
}
}
}
}
Struct sct = (Struct) o;
Array arr = new ArrayImpl();
Iterator<Entry<Key, Object>> it = sct.entryIterator();
Entry<Key, Object> e = null;
try {
while (it.hasNext()) {
e = it.next();
arr.setE(toIntValue(e.getKey().getString()), e.getValue());
}
} catch (ExpressionException ee) {
throw new ExpressionException("can't cast struct to a array, key [" + e.getKey().getString() + "] is not a number");
}
return arr;
} else if (o instanceof boolean[])
return new ArrayImpl(ArrayUtil.toReferenceType((boolean[]) o));
else if (o instanceof byte[])
return new ArrayImpl(ArrayUtil.toReferenceType((byte[]) o));
else if (o instanceof char[])
return new ArrayImpl(ArrayUtil.toReferenceType((char[]) o));
else if (o instanceof short[])
return new ArrayImpl(ArrayUtil.toReferenceType((short[]) o));
else if (o instanceof int[])
return new ArrayImpl(ArrayUtil.toReferenceType((int[]) o));
else if (o instanceof long[])
return new ArrayImpl(ArrayUtil.toReferenceType((long[]) o));
else if (o instanceof float[])
return new ArrayImpl(ArrayUtil.toReferenceType((float[]) o));
else if (o instanceof double[])
return new ArrayImpl(ArrayUtil.toReferenceType((double[]) o));
throw new CasterException(o, "Array");
}
use of lucee.runtime.type.ObjectWrap in project Lucee by lucee.
the class Caster method toList.
/**
* cast a Object to a Array Object
* @param o Object to cast
* @param duplicate
* @return casted Array
* @throws PageException
*/
public static List toList(Object o, boolean duplicate) throws PageException {
if (o instanceof List) {
if (duplicate) {
List src = (List) o;
int size = src.size();
ArrayList trg = new ArrayList();
for (int i = 0; i < size; i++) {
trg.add(i, src.get(i));
}
return trg;
}
return (List) o;
} else if (o instanceof Object[]) {
ArrayList list = new ArrayList();
Object[] arr = (Object[]) o;
for (int i = 0; i < arr.length; i++) list.add(i, arr[i]);
return list;
} else if (o instanceof Array) {
if (!duplicate)
return ArrayAsList.toList((Array) o);
ArrayList list = new ArrayList();
Array arr = (Array) o;
for (int i = 0; i < arr.size(); i++) list.add(i, arr.get(i + 1, null));
return list;
} else if (o instanceof Iterator) {
Iterator it = (Iterator) o;
ArrayList list = new ArrayList();
while (it.hasNext()) {
list.add(it.next());
}
return list;
} else if (o instanceof XMLStruct) {
XMLStruct sct = ((XMLStruct) o);
if (sct instanceof XMLMultiElementStruct)
return toList(new XMLMultiElementArray((XMLMultiElementStruct) o));
ArrayList list = new ArrayList();
list.add(sct);
return list;
} else if (o instanceof ObjectWrap) {
return toList(((ObjectWrap) o).getEmbededObject());
} else if (o instanceof Struct) {
Struct sct = (Struct) o;
ArrayList arr = new ArrayList();
Iterator<Entry<Key, Object>> it = sct.entryIterator();
Entry<Key, Object> e = null;
try {
while (it.hasNext()) {
e = it.next();
arr.add(toIntValue(e.getKey().getString()), e.getValue());
}
} catch (ExpressionException ee) {
throw new ExpressionException("can't cast struct to a array, key [" + (e != null ? e.getKey() : "") + "] is not a number");
}
return arr;
} else if (o instanceof boolean[])
return toList(ArrayUtil.toReferenceType((boolean[]) o));
else if (o instanceof byte[])
return toList(ArrayUtil.toReferenceType((byte[]) o));
else if (o instanceof char[])
return toList(ArrayUtil.toReferenceType((char[]) o));
else if (o instanceof short[])
return toList(ArrayUtil.toReferenceType((short[]) o));
else if (o instanceof int[])
return toList(ArrayUtil.toReferenceType((int[]) o));
else if (o instanceof long[])
return toList(ArrayUtil.toReferenceType((long[]) o));
else if (o instanceof float[])
return toList(ArrayUtil.toReferenceType((float[]) o));
else if (o instanceof double[])
return toList(ArrayUtil.toReferenceType((double[]) o));
throw new CasterException(o, "List");
}
use of lucee.runtime.type.ObjectWrap in project Lucee by lucee.
the class BundleInfo method call.
public static Struct call(PageContext pc, Object obj) throws PageException {
if (obj == null)
throw new FunctionException(pc, "bundleInfo", 1, "object", "value is null");
Class<?> clazz;
if (obj instanceof JavaObject)
clazz = ((JavaObject) obj).getClazz();
else if (obj instanceof ObjectWrap)
clazz = ((ObjectWrap) obj).getEmbededObject().getClass();
else
clazz = obj.getClass();
ClassLoader cl = clazz.getClassLoader();
if (cl instanceof BundleClassLoader) {
BundleClassLoader bcl = (BundleClassLoader) cl;
Bundle b = bcl.getBundle();
Struct sct = new StructImpl();
sct.setEL(KeyConstants._id, b.getBundleId());
sct.setEL(KeyConstants._name, b.getSymbolicName());
sct.setEL("location", b.getLocation());
sct.setEL(KeyConstants._version, b.getVersion().toString());
sct.setEL(KeyConstants._state, OSGiUtil.toState(b.getState(), null));
try {
sct.setEL("requiredBundles", toArray1(OSGiUtil.getRequiredBundles(b)));
sct.setEL("requiredPackages", toArray2(OSGiUtil.getRequiredPackages(b)));
} catch (BundleException be) {
throw Caster.toPageException(be);
}
return sct;
}
throw new ApplicationException(obj + "given object is not from a OSGi bundle");
}
use of lucee.runtime.type.ObjectWrap in project Lucee by lucee.
the class Throw method toPageException.
public static PageException toPageException(Object object, PageException defaultValue) throws PageException {
if ((object instanceof ObjectWrap))
return toPageException(((ObjectWrap) object).getEmbededObject(), defaultValue);
if (object instanceof CatchBlock) {
CatchBlock cb = (CatchBlock) object;
return cb.getPageException();
}
if (object instanceof PageException)
return (PageException) object;
if (object instanceof Throwable) {
Throwable t = (Throwable) object;
return new CustomTypeException(t.getMessage(), "", "", t.getClass().getName(), "");
}
if (object instanceof Struct) {
Struct sct = (Struct) object;
String type = Caster.toString(sct.get(KeyConstants._type, ""), "").trim();
String msg = Caster.toString(sct.get(KeyConstants._message, null), null);
if (!StringUtil.isEmpty(msg, true)) {
String detail = Caster.toString(sct.get(KeyConstants._detail, null), null);
String errCode = Caster.toString(sct.get("ErrorCode", null), null);
String extInfo = Caster.toString(sct.get("ExtendedInfo", null), null);
PageException pe = null;
if ("application".equalsIgnoreCase(type))
pe = new ApplicationException(msg, detail);
else if ("expression".equalsIgnoreCase(type))
pe = new ExpressionException(msg, detail);
else
pe = new CustomTypeException(msg, detail, errCode, type, extInfo);
// Extended Info
if (!StringUtil.isEmpty(extInfo, true))
pe.setExtendedInfo(extInfo);
// Error Code
if (!StringUtil.isEmpty(errCode, true))
pe.setErrorCode(errCode);
// Additional
if (pe instanceof PageExceptionImpl) {
PageExceptionImpl pei = (PageExceptionImpl) pe;
sct = Caster.toStruct(sct.get("additional", null), null);
if (sct != null) {
Iterator<Entry<Key, Object>> it = sct.entryIterator();
Entry<Key, Object> e;
while (it.hasNext()) {
e = it.next();
pei.setAdditional(e.getKey(), e.getValue());
}
}
}
return pe;
}
}
return defaultValue;
}
use of lucee.runtime.type.ObjectWrap in project Lucee by lucee.
the class JSONConverter method _serialize.
/**
* serialize a Object to his xml Format represenation
* @param object Object to serialize
* @param sb StringBuilder to write data
* @param serializeQueryByColumns
* @param done
* @throws ConverterException
*/
private void _serialize(PageContext pc, Set test, Object object, StringBuilder sb, boolean serializeQueryByColumns, Set done) throws ConverterException {
// NULL
if (object == null || object == NULL) {
sb.append(goIn());
sb.append("null");
return;
}
// String
if (object instanceof String || object instanceof StringBuilder) {
sb.append(goIn());
sb.append(StringUtil.escapeJS(object.toString(), '"', charsetEncoder));
return;
}
// Character
if (object instanceof Character) {
sb.append(goIn());
sb.append(StringUtil.escapeJS(String.valueOf(((Character) object).charValue()), '"', charsetEncoder));
return;
}
// Number
if (object instanceof Number) {
sb.append(goIn());
sb.append(Caster.toString(((Number) object)));
return;
}
// Boolean
if (object instanceof Boolean) {
sb.append(goIn());
sb.append(Caster.toString(((Boolean) object).booleanValue()));
return;
}
// DateTime
if (object instanceof DateTime) {
_serializeDateTime((DateTime) object, sb);
return;
}
// Date
if (object instanceof Date) {
_serializeDate((Date) object, sb);
return;
}
// XML
if (object instanceof Node) {
_serializeXML((Node) object, sb);
return;
}
// Timespan
if (object instanceof TimeSpan) {
_serializeTimeSpan((TimeSpan) object, sb);
return;
}
// File
if (object instanceof File) {
_serialize(pc, test, ((File) object).getAbsolutePath(), sb, serializeQueryByColumns, done);
return;
}
// String Converter
if (object instanceof ScriptConvertable) {
sb.append(((ScriptConvertable) object).serialize());
return;
}
// byte[]
if (object instanceof byte[]) {
sb.append("\"" + Base64Coder.encode((byte[]) object) + "\"");
return;
}
Object raw = LazyConverter.toRaw(object);
if (done.contains(raw)) {
sb.append(goIn());
sb.append("null");
return;
}
done.add(raw);
try {
// Component
if (object instanceof Component) {
_serializeComponent(pc, test, (Component) object, sb, serializeQueryByColumns, done);
return;
}
// UDF
if (object instanceof UDF) {
_serializeUDF(pc, test, (UDF) object, sb, serializeQueryByColumns, done);
return;
}
// Struct
if (object instanceof Struct) {
_serializeStruct(pc, test, (Struct) object, sb, serializeQueryByColumns, true, done);
return;
}
// Map
if (object instanceof Map) {
_serializeMap(pc, test, (Map) object, sb, serializeQueryByColumns, done);
return;
}
// Array
if (object instanceof Array) {
_serializeArray(pc, test, (Array) object, sb, serializeQueryByColumns, done);
return;
}
// List
if (object instanceof List) {
_serializeList(pc, test, (List) object, sb, serializeQueryByColumns, done);
return;
}
// Query
if (object instanceof Query) {
_serializeQuery(pc, test, (Query) object, sb, serializeQueryByColumns, done);
return;
}
// Native Array
if (Decision.isNativeArray(object)) {
if (object instanceof char[])
_serialize(pc, test, new String((char[]) object), sb, serializeQueryByColumns, done);
else {
_serializeArray(pc, test, ArrayUtil.toReferenceType(object, ArrayUtil.OBJECT_EMPTY), sb, serializeQueryByColumns, done);
}
return;
}
// ObjectWrap
if (object instanceof ObjectWrap) {
try {
_serialize(pc, test, ((ObjectWrap) object).getEmbededObject(), sb, serializeQueryByColumns, done);
} catch (PageException e) {
if (object instanceof JavaObject) {
_serializeClass(pc, test, ((JavaObject) object).getClazz(), null, sb, serializeQueryByColumns, done);
} else
throw new ConverterException("can't serialize Object of type [ " + Caster.toClassName(object) + " ]");
}
return;
}
_serializeClass(pc, test, object.getClass(), object, sb, serializeQueryByColumns, done);
} finally {
done.remove(raw);
}
}
Aggregations