use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.
the class XMLMultiElementStruct method set.
public Object set(int index, Object value) throws PageException {
Element element = XMLCaster.toElement(getOwnerDocument(), value);
Object obj = array.get(index, null);
if (obj instanceof Element) {
Element el = ((Element) obj);
el.getParentNode().replaceChild(XMLCaster.toRawNode(element), XMLCaster.toRawNode(el));
} else if (array.size() + 1 == index) {
getParentNode().appendChild(XMLCaster.toRawNode(element));
} else {
throw new ExpressionException("the index for child node is out of range", "valid range is from 1 to " + (array.size() + 1));
}
return array.setE(index, element);
}
use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.
the class Reflector method getGetter.
/**
* to get a Getter Method of a Object
* @param clazz Class to invoke method from
* @param prop Name of the Method without get
* @return return Value of the getter Method
* @throws NoSuchMethodException
* @throws PageException
*/
public static MethodInstance getGetter(Class clazz, String prop) throws PageException, NoSuchMethodException {
String getterName = "get" + StringUtil.ucFirst(prop);
MethodInstance mi = getMethodInstanceEL(null, clazz, KeyImpl.getInstance(getterName), ArrayUtil.OBJECT_EMPTY);
if (mi == null) {
String isName = "is" + StringUtil.ucFirst(prop);
mi = getMethodInstanceEL(null, clazz, KeyImpl.getInstance(isName), ArrayUtil.OBJECT_EMPTY);
if (mi != null) {
Method m = mi.getMethod();
Class rtn = m.getReturnType();
if (rtn != Boolean.class && rtn != boolean.class)
mi = null;
}
}
if (mi == null)
throw new ExpressionException("No matching property [" + prop + "] found in [" + Caster.toTypeName(clazz) + "]");
Method m = mi.getMethod();
if (m.getReturnType() == void.class)
throw new NoSuchMethodException("invalid return Type, method [" + m.getName() + "] for Property [" + getterName + "] must have return type not void");
return mi;
}
use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.
the class Caster method toString.
private static String toString(Clob clob) throws ExpressionException {
try {
Reader in = clob.getCharacterStream();
StringBuffer buf = new StringBuffer();
for (int c = in.read(); c != -1; c = in.read()) {
buf.append((char) c);
}
return buf.toString();
} catch (Exception e) {
throw ExpressionException.newInstance(e);
}
}
use of lucee.runtime.exp.ExpressionException 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.exp.ExpressionException 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");
}
Aggregations