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();
}
Aggregations