Search in sources :

Example 1 with Function

use of lucee.runtime.ext.function.Function 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)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 Clob (java.sql.Clob)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 TimeZone (java.util.TimeZone)1 PageContextImpl (lucee.runtime.PageContextImpl)1 CasterException (lucee.runtime.exp.CasterException)1 Function (lucee.runtime.ext.function.Function)1 ObjectWrap (lucee.runtime.type.ObjectWrap)1 DateTime (lucee.runtime.type.dt.DateTime)1 DateTimeImpl (lucee.runtime.type.dt.DateTimeImpl)1 ArrayAsList (lucee.runtime.type.wrap.ArrayAsList)1