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