Search in sources :

Example 11 with CasterException

use of lucee.runtime.exp.CasterException in project Lucee by lucee.

the class Caster method toString.

/**
 * cast a Object to a String
 * @param o Object to cast
 * @return casted String
 * @throws PageException
 */
public static String toString(Object o) throws PageException {
    if (o instanceof String)
        return (String) o;
    else if (o instanceof Number)
        return toString(((Number) o));
    else if (o instanceof Boolean)
        return toString(((Boolean) o).booleanValue());
    else if (o instanceof Castable)
        return ((Castable) o).castToString();
    else if (o instanceof Date) {
        if (o instanceof DateTime)
            return ((DateTime) o).castToString();
        return new DateTimeImpl((Date) o).castToString();
    } else if (o instanceof Clob)
        return toString((Clob) o);
    else if (o instanceof Locale)
        return toString((Locale) o);
    else if (o instanceof TimeZone)
        return toString((TimeZone) o);
    else if (o instanceof Node)
        return XMLCaster.toString((Node) o);
    else if (o instanceof Reader) {
        Reader r = null;
        try {
            return IOUtil.toString(r = (Reader) o);
        } catch (IOException e) {
            throw Caster.toPageException(e);
        } finally {
            IOUtil.closeEL(r);
        }
    } else if (o instanceof InputStream) {
        PageContextImpl pc = (PageContextImpl) ThreadLocalPageContext.get();
        InputStream r = null;
        try {
            return IOUtil.toString(r = (InputStream) o, pc.getWebCharset());
        } catch (IOException e) {
            throw Caster.toPageException(e);
        } finally {
            IOUtil.closeEL(r);
        }
    } else if (o instanceof byte[]) {
        PageContextImpl pc = (PageContextImpl) ThreadLocalPageContext.get();
        try {
            return new String((byte[]) o, pc.getWebCharset());
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            return new String((byte[]) o);
        }
    } else if (o instanceof char[])
        return new String((char[]) o);
    else if (o instanceof ObjectWrap)
        return toString(((ObjectWrap) o).getEmbededObject());
    else if (o instanceof Calendar)
        return toString(((Calendar) o).getTime());
    else if (o == null)
        return "";
    // INFO Collection is new of type Castable
    if (o instanceof Map || o instanceof List || o instanceof Function)
        throw new CasterException(o, "string");
    return o.toString();
}
Also used : Locale(java.util.Locale) CasterException(lucee.runtime.exp.CasterException) ObjectWrap(lucee.runtime.type.ObjectWrap) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) Calendar(java.util.Calendar) Reader(java.io.Reader) IOException(java.io.IOException) PageContextImpl(lucee.runtime.PageContextImpl) Date(java.util.Date) DateTime(lucee.runtime.type.dt.DateTime) Function(lucee.runtime.ext.function.Function) TimeZone(java.util.TimeZone) DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl) ArrayList(java.util.ArrayList) ArrayAsList(lucee.runtime.type.wrap.ArrayAsList) List(java.util.List) NodeList(org.w3c.dom.NodeList) Clob(java.sql.Clob) Map(java.util.Map)

Example 12 with CasterException

use of lucee.runtime.exp.CasterException in project Lucee by lucee.

the class Caster method toDoubleValue.

public static double toDoubleValue(String str, boolean alsoFromDate) throws CasterException {
    // throw new CasterException("can't cast empty string to a number value");
    if (str == null)
        return 0;
    str = str.trim();
    double rtn_ = 0;
    double _rtn = 0;
    int eCount = 0;
    double deep = 1;
    int pos = 0;
    int len = str.length();
    if (len == 0)
        throw new CasterException("can't cast empty string to a number value");
    char curr = str.charAt(pos);
    boolean isMinus = false;
    if (curr == '+') {
        if (len == ++pos)
            throw new CasterException("can't cast [+] string to a number value");
    }
    if (curr == '-') {
        if (len == ++pos)
            throw new CasterException("can't cast [-] string to a number value");
        isMinus = true;
    }
    boolean hasDot = false;
    // boolean hasExp=false;
    do {
        curr = str.charAt(pos);
        if (curr < '0') {
            if (curr == '.') {
                if (hasDot) {
                    if (!alsoFromDate)
                        throw new CasterException("cannot cast [" + str + "] string to a number value");
                    return toDoubleValueViaDate(str);
                }
                hasDot = true;
            } else {
                if (pos == 0 && Decision.isBoolean(str))
                    return toBooleanValue(str, false) ? 1.0D : 0.0D;
                if (!alsoFromDate)
                    throw new CasterException("cannot cast [" + str + "] string to a number value");
                return toDoubleValueViaDate(str);
            // throw new CasterException("can't cast ["+str+"] string to a number value");
            }
        } else if (curr > '9') {
            if (curr == 'e' || curr == 'E') {
                try {
                    return Double.parseDouble(str);
                } catch (NumberFormatException e) {
                    if (!alsoFromDate)
                        throw new CasterException("cannot cast [" + str + "] string to a number value");
                    return toDoubleValueViaDate(str);
                // throw new CasterException("can't cast ["+str+"] string to a number value");
                }
            }
            // else {
            if (pos == 0 && Decision.isBoolean(str))
                return toBooleanValue(str, false) ? 1.0D : 0.0D;
            if (!alsoFromDate)
                throw new CasterException("cannot cast [" + str + "] string to a number value");
            return toDoubleValueViaDate(str);
        // throw new CasterException("can't cast ["+str+"] string to a number value");
        // }
        } else if (!hasDot) {
            rtn_ *= 10;
            rtn_ += toDigit(curr);
        } else /*else if(hasExp) {
                eCount*=10;
                eCount+=toDigit(curr);
            }*/
        {
            deep *= 10;
            _rtn *= 10;
            _rtn += toDigit(curr);
        // rtn_+=(toDigit(curr)/deep);
        // deep*=10;
        }
    } while (++pos < len);
    if (deep > 1) {
        rtn_ += (_rtn /= deep);
    }
    if (isMinus)
        rtn_ = -rtn_;
    if (eCount > 0)
        for (int i = 0; i < eCount; i++) rtn_ *= 10;
    return rtn_;
}
Also used : CasterException(lucee.runtime.exp.CasterException)

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