Search in sources :

Example 6 with CasterException

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());
    }
}
Also used : CasterException(lucee.runtime.exp.CasterException) Position(lucee.transformer.Position) TemplateException(lucee.runtime.exp.TemplateException) LitString(lucee.transformer.expression.literal.LitString) ExprString(lucee.transformer.expression.ExprString) ExprBoolean(lucee.transformer.expression.ExprBoolean)

Example 7 with CasterException

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;
}
Also used : CasterException(lucee.runtime.exp.CasterException) ExpressionException(lucee.runtime.exp.ExpressionException)

Example 8 with CasterException

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);
}
Also used : CasterException(lucee.runtime.exp.CasterException) ExpressionException(lucee.runtime.exp.ExpressionException)

Example 9 with CasterException

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");
}
Also used : CasterException(lucee.runtime.exp.CasterException) Resource(lucee.commons.io.res.Resource) AttributedString(java.text.AttributedString) IIOImage(javax.imageio.IIOImage) BufferedImage(java.awt.image.BufferedImage) File(java.io.File) ExpressionException(lucee.runtime.exp.ExpressionException) BufferedImage(java.awt.image.BufferedImage)

Example 10 with CasterException

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");
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) CasterException(lucee.runtime.exp.CasterException) ObjectWrap(lucee.runtime.type.ObjectWrap) Query(lucee.runtime.type.Query) QueryColumn(lucee.runtime.type.QueryColumn) Key(lucee.runtime.type.Collection.Key)

Aggregations

CasterException (lucee.runtime.exp.CasterException)12 ExpressionException (lucee.runtime.exp.ExpressionException)6 Key (lucee.runtime.type.Collection.Key)5 Entry (java.util.Map.Entry)4 ObjectWrap (lucee.runtime.type.ObjectWrap)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 JavaObject (lucee.runtime.java.JavaObject)3 XMLMultiElementArray (lucee.runtime.text.xml.struct.XMLMultiElementArray)3 Array (lucee.runtime.type.Array)3 ArrayAsList (lucee.runtime.type.wrap.ArrayAsList)3 ListAsArray (lucee.runtime.type.wrap.ListAsArray)3 NodeList (org.w3c.dom.NodeList)3 Locale (java.util.Locale)2 Component (lucee.runtime.Component)2 XMLMultiElementStruct (lucee.runtime.text.xml.struct.XMLMultiElementStruct)2 XMLStruct (lucee.runtime.text.xml.struct.XMLStruct)2 ArrayImpl (lucee.runtime.type.ArrayImpl)2 CollectionStruct (lucee.runtime.type.CollectionStruct)2 Struct (lucee.runtime.type.Struct)2