use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.
the class Caster method _castTo.
private static Object _castTo(PageContext pc, String strType, Object o) throws PageException {
if (o instanceof Component) {
Component comp = ((Component) o);
if (comp.instanceOf(strType))
return o;
throw new ExpressionException("can't cast Component of Type [" + comp.getAbsName() + "] to [" + strType + "]");
}
if (o instanceof Pojo) {
Component cfc = AxisCaster.toComponent(pc, ((Pojo) o), strType, null);
if (cfc != null)
return cfc;
throw new ExpressionException("can't cast Pojo of Type [" + o.getClass().getName() + "] to [" + strType + "]");
}
if (strType.endsWith("[]") && Decision.isArray(o)) {
String _strType = strType.substring(0, strType.length() - 2);
short _type = CFTypes.toShort(_strType, false, (short) -1);
Array arr = Caster.toArray(o, null);
if (arr != null) {
// convert the values
Iterator<Entry<Key, Object>> it = arr.entryIterator();
Array _arr = new ArrayImpl();
Entry<Key, Object> e;
Object src, trg;
boolean hasChanged = false;
while (it.hasNext()) {
e = it.next();
src = e.getValue();
trg = castTo(pc, _type, _strType, src);
_arr.setEL(e.getKey(), trg);
if (src != trg)
hasChanged = true;
}
if (!hasChanged)
return arr;
return _arr;
}
}
throw new CasterException(o, strType);
}
use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.
the class DataSourceServiceImpl method getDrivers.
@Override
public Struct getDrivers() throws ServiceException, SecurityException {
checkReadAccess();
Struct rtn = new StructImpl();
Struct driver;
try {
Resource luceeContext = ResourceUtil.toResourceExisting(pc(), "/lucee/admin/dbdriver/");
Resource[] children = luceeContext.listResources(new ExtensionResourceFilter(Constants.getComponentExtensions()));
String name;
for (int i = 0; i < children.length; i++) {
driver = new StructImpl();
name = ListFirst.call(pc(), children[i].getName(), ".");
driver.setEL(KeyConstants._name, name);
driver.setEL("handler", children[i].getName());
rtn.setEL(name, driver);
}
} catch (ExpressionException e) {
throw new ServiceException(e.getMessage());
}
return rtn;
}
use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.
the class BIF method callWithNamedValues.
@Override
public Object callWithNamedValues(PageContext pageContext, Struct values, boolean doIncludePath) throws PageException {
ArrayList<FunctionLibFunctionArg> flfas = flf.getArg();
Iterator<FunctionLibFunctionArg> it = flfas.iterator();
FunctionLibFunctionArg arg;
Object val;
List<Ref> refs = new ArrayList<Ref>();
while (it.hasNext()) {
arg = it.next();
// match by name
val = values.get(arg.getName(), null);
// match by alias
if (val == null) {
String alias = arg.getAlias();
if (!StringUtil.isEmpty(alias, true)) {
String[] aliases = lucee.runtime.type.util.ListUtil.trimItems(lucee.runtime.type.util.ListUtil.listToStringArray(alias, ','));
for (int x = 0; x < aliases.length; x++) {
val = values.get(aliases[x], null);
if (val != null)
break;
}
}
}
if (val == null) {
if (arg.getRequired()) {
String[] names = flf.getMemberNames();
String n = ArrayUtil.isEmpty(names) ? "" : names[0];
throw new ExpressionException("missing required argument [" + arg.getName() + "] for build in function call [" + n + "]");
}
} else {
refs.add(new Casting(arg.getTypeAsString(), arg.getType(), new LFunctionValue(new LString(arg.getName()), val)));
}
}
BIFCall call = new BIFCall(flf, refs.toArray(new Ref[refs.size()]));
return call.getValue(pageContext);
}
use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.
the class UDFGSProperty method validate.
static final void validate(String validate, Struct validateParams, Object obj) throws PageException {
if (StringUtil.isEmpty(validate, true))
return;
validate = validate.trim().toLowerCase();
if (!validate.equals("regex") && !Decision.isValid(validate, obj))
throw new ExpressionException(createMessage(validate, obj));
// range
if (validateParams == null)
return;
if (validate.equals("integer") || validate.equals("numeric") || validate.equals("number")) {
double min = Caster.toDoubleValue(validateParams.get(KeyConstants._min, null), false, Double.NaN);
double max = Caster.toDoubleValue(validateParams.get(KeyConstants._max, null), false, Double.NaN);
double d = Caster.toDoubleValue(obj);
if (!Double.isNaN(min) && d < min)
throw new ExpressionException(validate + " [" + Caster.toString(d) + "] is out of range, value must be more than or equal to [" + min + "]");
if (!Double.isNaN(max) && d > max)
throw new ExpressionException(validate + " [" + Caster.toString(d) + "] is out of range, value must be less than or equal to [" + max + "]");
} else if (validate.equals("string")) {
double min = Caster.toDoubleValue(validateParams.get(MIN_LENGTH, null), false, Double.NaN);
double max = Caster.toDoubleValue(validateParams.get(MAX_LENGTH, null), false, Double.NaN);
String str = Caster.toString(obj);
int l = str.length();
if (!Double.isNaN(min) && l < ((int) min))
throw new ExpressionException("string [" + str + "] is to short [" + l + "], the string must be at least [" + min + "] characters");
if (!Double.isNaN(max) && l > ((int) max))
throw new ExpressionException("string [" + str + "] is to long [" + l + "], the string can have a maximum length of [" + max + "] characters");
} else if (validate.equals("regex")) {
String pattern = Caster.toString(validateParams.get(KeyConstants._pattern, null), null);
String value = Caster.toString(obj);
if (!StringUtil.isEmpty(pattern, true) && !IsValid.regex(value, pattern))
throw new ExpressionException("the string [" + value + "] does not match the regular expression pattern [" + pattern + "]");
}
}
use of lucee.runtime.exp.ExpressionException in project Lucee by lucee.
the class UDFRemoveProperty method callWithNamedValues.
@Override
public Object callWithNamedValues(PageContext pageContext, Struct values, boolean doIncludePath) throws PageException {
UDFUtil.argumentCollection(values, getFunctionArguments());
Key key = arguments[0].getName();
Object value = values.get(key, null);
if (value == null) {
Key[] keys = CollectionUtil.keys(values);
if (keys.length == 1) {
value = values.get(keys[0]);
} else
throw new ExpressionException("The parameter " + key + " to function " + getFunctionName() + " is required but was not passed in.");
}
return remove(pageContext, value);
}
Aggregations