use of lucee.runtime.exp.CasterException in project Lucee by lucee.
the class AbstrCFMLExprTransformer method number.
/**
* Transfomiert einen numerische Wert.
* Die Laenge des numerischen Wertes interessiert nicht zu uebersetzungszeit,
* ein "Overflow" fuehrt zu einem Laufzeitfehler.
* Da die zu erstellende CFXD, bzw. dieser Transfomer, keine Vorwegnahme des Laufzeitsystems vornimmt.
* <br />
* EBNF:<br />
* <code>["+"|"-"] digit {digit} {"." digit {digit}};</code>
* @return CFXD Element
* @throws TemplateException
*/
private LitDouble number(ExprData data) throws TemplateException {
// check first character is a number literal representation
if (!(data.srcCode.isCurrentBetween('0', '9') || data.srcCode.isCurrent('.')))
return null;
Position line = data.srcCode.getPosition();
StringBuffer rtn = new StringBuffer();
// get digit on the left site of the dot
if (data.srcCode.isCurrent('.'))
rtn.append('0');
else
rtn.append(digit(data));
// read dot if exist
if (data.srcCode.forwardIfCurrent('.')) {
rtn.append('.');
String rightSite = digit(data);
if (rightSite.length() > 0 && data.srcCode.forwardIfCurrent('e')) {
Boolean expOp = null;
if (data.srcCode.forwardIfCurrent('+'))
expOp = Boolean.TRUE;
else if (data.srcCode.forwardIfCurrent('-'))
expOp = Boolean.FALSE;
if (data.srcCode.isCurrentBetween('0', '9')) {
if (expOp == Boolean.FALSE)
rightSite += "e-";
else if (expOp == Boolean.TRUE)
rightSite += "e+";
else
rightSite += "e";
rightSite += digit(data);
} else {
if (expOp != null)
data.srcCode.previous();
data.srcCode.previous();
}
}
// read right side of the dot
if (rightSite.length() == 0)
// throw new TemplateException(cfml, "Number can't end with [.]"); // DIFF 23
rightSite = "0";
rtn.append(rightSite);
} else // scientific notation
if (data.srcCode.forwardIfCurrent('e')) {
Boolean expOp = null;
if (data.srcCode.forwardIfCurrent('+'))
expOp = Boolean.TRUE;
else if (data.srcCode.forwardIfCurrent('-'))
expOp = Boolean.FALSE;
if (data.srcCode.isCurrentBetween('0', '9')) {
String rightSite = "e";
if (expOp == Boolean.FALSE)
rightSite += "-";
else if (expOp == Boolean.TRUE)
rightSite += "+";
rightSite += digit(data);
rtn.append(rightSite);
} else {
if (expOp != null)
data.srcCode.previous();
data.srcCode.previous();
}
}
comments(data);
try {
return data.factory.createLitDouble(Caster.toDoubleValue(rtn.toString()), line, data.srcCode.getPosition());
} catch (CasterException e) {
throw new TemplateException(data.srcCode, e.getMessage());
}
}
use of lucee.runtime.exp.CasterException in project Lucee by lucee.
the class ArrayUtil method _toDoubleValue.
private static double _toDoubleValue(Array array, int i) throws ExpressionException {
Object obj = array.get(i, null);
if (obj == null)
throw new ExpressionException("there is no element at position [" + i + "] or the element is null");
double tmp = Caster.toDoubleValue(obj, true, Double.NaN);
if (Double.isNaN(tmp))
throw new CasterException(obj, Double.class);
return tmp;
}
use of lucee.runtime.exp.CasterException in project Lucee by lucee.
the class PageContextImpl method subparam.
// used by generated code FUTURE add to interface
public void subparam(String type, String name, final Object value, double min, double max, String strPattern, int maxLength, final boolean isNew) throws PageException {
// check attributes type
if (type == null)
type = "any";
else
type = type.trim().toLowerCase();
// cast and set value
if (!"any".equals(type)) {
// range
if ("range".equals(type)) {
boolean hasMin = Decision.isValid(min);
boolean hasMax = Decision.isValid(max);
double number = Caster.toDoubleValue(value);
if (!hasMin && !hasMax)
throw new ExpressionException("you need to define one of the following attributes [min,max], when type is set to [range]");
if (hasMin && number < min)
throw new ExpressionException("The number [" + Caster.toString(number) + "] is to small, the number must be at least [" + Caster.toString(min) + "]");
if (hasMax && number > max)
throw new ExpressionException("The number [" + Caster.toString(number) + "] is to big, the number cannot be bigger than [" + Caster.toString(max) + "]");
setVariable(name, Caster.toDouble(number));
} else // regex
if ("regex".equals(type) || "regular_expression".equals(type)) {
String str = Caster.toString(value);
if (strPattern == null)
throw new ExpressionException("Missing attribute [pattern]");
if (!Perl5Util.matches(strPattern, str))
throw new ExpressionException("The value [" + str + "] doesn't match the provided pattern [" + strPattern + "]");
setVariable(name, str);
} else if (type.equals("int") || type.equals("integer")) {
if (!Decision.isInteger(value))
throw new ExpressionException("The value [" + value + "] is not a valid integer");
setVariable(name, value);
} else {
if (!Decision.isCastableTo(type, value, true, true, maxLength)) {
if (maxLength > -1 && ("email".equalsIgnoreCase(type) || "url".equalsIgnoreCase(type) || "string".equalsIgnoreCase(type))) {
StringBuilder msg = new StringBuilder(CasterException.createMessage(value, type));
msg.append(" with a maximum length of " + maxLength + " characters");
throw new CasterException(msg.toString());
}
throw new CasterException(value, type);
}
setVariable(name, value);
// REALCAST setVariable(name,Caster.castTo(this,type,value,true));
}
} else if (isNew)
setVariable(name, value);
}
use of lucee.runtime.exp.CasterException in project Lucee by lucee.
the class Image method createImage.
public static Image createImage(PageContext pc, Object obj, boolean check4Var, boolean clone, boolean checkAccess, String format) throws PageException {
try {
if (obj instanceof String || obj instanceof Resource || obj instanceof File) {
try {
Resource res = Caster.toResource(pc, obj, true);
pc.getConfig().getSecurityManager().checkFileLocation(res);
return new Image(res, format);
} catch (ExpressionException ee) {
if (check4Var && Decision.isVariableName(Caster.toString(obj))) {
try {
return createImage(pc, pc.getVariable(Caster.toString(obj)), false, clone, checkAccess, format);
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
throw ee;
}
}
try {
return new Image(Caster.toString(obj), format);
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
throw ee;
}
}
}
if (obj instanceof Image) {
if (clone)
return (Image) ((Image) obj).clone();
return (Image) obj;
}
if (Decision.isBinary(obj))
return new Image(Caster.toBinary(obj), format);
if (obj instanceof BufferedImage)
return new Image(((BufferedImage) obj));
if (obj instanceof java.awt.Image)
return new Image(toBufferedImage((java.awt.Image) obj));
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
throw Caster.toPageException(t);
}
throw new CasterException(obj, "Image");
}
use of lucee.runtime.exp.CasterException in project Lucee by lucee.
the class Caster method toQuery.
/**
* cast a Object to a Query Object
* @param o Object to cast
* @param duplicate duplicate the object or not
* @return casted Query Object
* @throws PageException
*/
public static Query toQuery(Object o, boolean duplicate) throws PageException {
if (o instanceof Query) {
if (duplicate) {
Query src = (Query) o;
Query trg = new QueryImpl(src.getColumnNames(), src.getRowCount(), "query");
Collection.Key[] keys = src.getColumnNames();
QueryColumn[] columnsSrc = new QueryColumn[keys.length];
for (int i = 0; i < columnsSrc.length; i++) {
columnsSrc[i] = src.getColumn(keys[i]);
}
keys = trg.getColumnNames();
QueryColumn[] columnsTrg = new QueryColumn[keys.length];
for (int i = 0; i < columnsTrg.length; i++) {
columnsTrg[i] = trg.getColumn(keys[i]);
}
int i;
for (int row = trg.getRecordcount(); row > 0; row--) {
for (i = 0; i < columnsTrg.length; i++) {
columnsTrg[i].set(row, columnsSrc[i].get(row, null));
}
}
return trg;
}
return (Query) o;
} else if (o instanceof ObjectWrap) {
return toQuery(((ObjectWrap) o).getEmbededObject(), duplicate);
}
throw new CasterException(o, "query");
}
Aggregations