use of lucee.runtime.type.dt.DateTime in project Lucee by lucee.
the class TagLibTagAttr method getValues.
public Object[] getValues() {
if (valueList == null)
return null;
if (values != null)
return values;
String[] res = ListUtil.trimItems(ListUtil.listToStringArray(valueList, delimiter));
short type = CFTypes.toShort(getType(), false, CFTypes.TYPE_ANY);
// String
if (type == CFTypes.TYPE_STRING || type == CFTypes.TYPE_ANY) {
values = res;
} else // Numeric
if (type == CFTypes.TYPE_NUMERIC) {
List<Double> list = new ArrayList<Double>();
Double d;
for (int i = 0; i < res.length; i++) {
d = Caster.toDouble(res[i], null);
if (d != null)
list.add(d);
}
values = list.toArray(new Double[list.size()]);
} else // Boolean
if (type == CFTypes.TYPE_BOOLEAN) {
List<Boolean> list = new ArrayList<Boolean>();
Boolean b;
for (int i = 0; i < res.length; i++) {
b = Caster.toBoolean(res[i], null);
if (b != null)
list.add(b);
}
values = list.toArray(new Boolean[list.size()]);
} else // DateTime
if (type == CFTypes.TYPE_DATETIME) {
List<DateTime> list = new ArrayList<DateTime>();
DateTime dt;
for (int i = 0; i < res.length; i++) {
dt = Caster.toDate(res[i], true, null, null);
if (dt != null)
list.add(dt);
}
values = list.toArray(new DateTime[list.size()]);
} else // Timespan
if (type == CFTypes.TYPE_TIMESPAN) {
List<TimeSpan> list = new ArrayList<TimeSpan>();
TimeSpan ts;
for (int i = 0; i < res.length; i++) {
ts = Caster.toTimespan(res[i], null);
if (ts != null)
list.add(ts);
}
values = list.toArray(new TimeSpan[list.size()]);
} else // TODO add support for other types ?
{
valueList = null;
}
return values;
}
use of lucee.runtime.type.dt.DateTime in project Lucee by lucee.
the class JSONConverter method _serialize.
/**
* serialize a Object to his xml Format represenation
* @param object Object to serialize
* @param sb StringBuilder to write data
* @param serializeQueryByColumns
* @param done
* @throws ConverterException
*/
private void _serialize(PageContext pc, Set test, Object object, StringBuilder sb, boolean serializeQueryByColumns, Set done) throws ConverterException {
// NULL
if (object == null || object == NULL) {
sb.append(goIn());
sb.append("null");
return;
}
// String
if (object instanceof String || object instanceof StringBuilder) {
sb.append(goIn());
sb.append(StringUtil.escapeJS(object.toString(), '"', charsetEncoder));
return;
}
// Character
if (object instanceof Character) {
sb.append(goIn());
sb.append(StringUtil.escapeJS(String.valueOf(((Character) object).charValue()), '"', charsetEncoder));
return;
}
// Number
if (object instanceof Number) {
sb.append(goIn());
sb.append(Caster.toString(((Number) object)));
return;
}
// Boolean
if (object instanceof Boolean) {
sb.append(goIn());
sb.append(Caster.toString(((Boolean) object).booleanValue()));
return;
}
// DateTime
if (object instanceof DateTime) {
_serializeDateTime((DateTime) object, sb);
return;
}
// Date
if (object instanceof Date) {
_serializeDate((Date) object, sb);
return;
}
// XML
if (object instanceof Node) {
_serializeXML((Node) object, sb);
return;
}
// Timespan
if (object instanceof TimeSpan) {
_serializeTimeSpan((TimeSpan) object, sb);
return;
}
// File
if (object instanceof File) {
_serialize(pc, test, ((File) object).getAbsolutePath(), sb, serializeQueryByColumns, done);
return;
}
// String Converter
if (object instanceof ScriptConvertable) {
sb.append(((ScriptConvertable) object).serialize());
return;
}
// byte[]
if (object instanceof byte[]) {
sb.append("\"" + Base64Coder.encode((byte[]) object) + "\"");
return;
}
Object raw = LazyConverter.toRaw(object);
if (done.contains(raw)) {
sb.append(goIn());
sb.append("null");
return;
}
done.add(raw);
try {
// Component
if (object instanceof Component) {
_serializeComponent(pc, test, (Component) object, sb, serializeQueryByColumns, done);
return;
}
// UDF
if (object instanceof UDF) {
_serializeUDF(pc, test, (UDF) object, sb, serializeQueryByColumns, done);
return;
}
// Struct
if (object instanceof Struct) {
_serializeStruct(pc, test, (Struct) object, sb, serializeQueryByColumns, true, done);
return;
}
// Map
if (object instanceof Map) {
_serializeMap(pc, test, (Map) object, sb, serializeQueryByColumns, done);
return;
}
// Array
if (object instanceof Array) {
_serializeArray(pc, test, (Array) object, sb, serializeQueryByColumns, done);
return;
}
// List
if (object instanceof List) {
_serializeList(pc, test, (List) object, sb, serializeQueryByColumns, done);
return;
}
// Query
if (object instanceof Query) {
_serializeQuery(pc, test, (Query) object, sb, serializeQueryByColumns, done);
return;
}
// Native Array
if (Decision.isNativeArray(object)) {
if (object instanceof char[])
_serialize(pc, test, new String((char[]) object), sb, serializeQueryByColumns, done);
else {
_serializeArray(pc, test, ArrayUtil.toReferenceType(object, ArrayUtil.OBJECT_EMPTY), sb, serializeQueryByColumns, done);
}
return;
}
// ObjectWrap
if (object instanceof ObjectWrap) {
try {
_serialize(pc, test, ((ObjectWrap) object).getEmbededObject(), sb, serializeQueryByColumns, done);
} catch (PageException e) {
if (object instanceof JavaObject) {
_serializeClass(pc, test, ((JavaObject) object).getClazz(), null, sb, serializeQueryByColumns, done);
} else
throw new ConverterException("can't serialize Object of type [ " + Caster.toClassName(object) + " ]");
}
return;
}
_serializeClass(pc, test, object.getClass(), object, sb, serializeQueryByColumns, done);
} finally {
done.remove(raw);
}
}
use of lucee.runtime.type.dt.DateTime in project Lucee by lucee.
the class HTTPServletRequestWrap method getDateHeader.
@Override
public long getDateHeader(String name) {
if (!disconnected)
return req.getDateHeader(name);
String h = getHeader(name);
if (h == null)
return -1;
DateTime dt = DateCaster.toDateAdvanced(h, null, null);
if (dt == null)
throw new IllegalArgumentException("cannot convert [" + getHeader(name) + "] to date time value");
return dt.getTime();
}
use of lucee.runtime.type.dt.DateTime 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.type.dt.DateTime in project Lucee by lucee.
the class DateCaster method _toDateSimpleTS.
private static DateTime _toDateSimpleTS(DateString ds, TimeZone timeZone, DateTime defaultValue) {
// "^\\{d '([0-9]{2,4})-([0-9]{1,2})-([0-9]{1,2})'\\}$"
if (ds.fwIfNext('d')) {
if (!(ds.fwIfNext(' ') && ds.fwIfNext('\'')))
return defaultValue;
ds.next();
// year
int year = ds.readDigits();
if (year == -1)
return defaultValue;
if (!ds.fwIfCurrent('-'))
return defaultValue;
// month
int month = ds.readDigits();
if (month == -1)
return defaultValue;
if (!ds.fwIfCurrent('-'))
return defaultValue;
// day
int day = ds.readDigits();
if (day == -1)
return defaultValue;
if (!(ds.fwIfCurrent('\'') && ds.fwIfCurrent('}')))
return defaultValue;
// new DateTimeImpl(year,month,day);
if (ds.isAfterLast())
return util.toDateTime(timeZone, year, month, day, 0, 0, 0, 0, defaultValue);
return defaultValue;
} else // "^\\{ts '([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{2}):([0-9]{1,2}):([0-9]{2})'\\}$"
if (ds.fwIfNext('t')) {
if (!(ds.fwIfNext('s') && ds.fwIfNext(' ') && ds.fwIfNext('\''))) {
// Time
if (!(ds.fwIfNext(' ') && ds.fwIfNext('\'')))
return defaultValue;
ds.next();
// hour
int hour = ds.readDigits();
if (hour == -1)
return defaultValue;
if (!ds.fwIfCurrent(':'))
return defaultValue;
// minute
int minute = ds.readDigits();
if (minute == -1)
return defaultValue;
if (!ds.fwIfCurrent(':'))
return defaultValue;
// second
int second = ds.readDigits();
if (second == -1)
return defaultValue;
// Milli Second
int millis = 0;
if (ds.fwIfCurrent('.')) {
millis = ds.readDigits();
}
int before = ds.getPos();
DateTime tmp = parseOffset(ds, timeZone, new int[] { 1899, 12, 30 }, hour, minute, second, millis, false, defaultValue);
if (tmp == null && before != ds.getPos())
return defaultValue;
if (!(ds.fwIfCurrent('\'') && ds.fwIfCurrent('}')))
return defaultValue;
if (ds.isAfterLast()) {
if (tmp != null) {
return new TimeImpl(tmp.getTime(), false);
}
long time = util.toTime(timeZone, 1899, 12, 30, hour, minute, second, millis, DEFAULT_VALUE);
if (time == DEFAULT_VALUE)
return defaultValue;
return new TimeImpl(time, false);
}
return defaultValue;
}
ds.next();
// year
int year = ds.readDigits();
if (year == -1)
return defaultValue;
if (!ds.fwIfCurrent('-'))
return defaultValue;
// month
int month = ds.readDigits();
if (month == -1)
return defaultValue;
if (!ds.fwIfCurrent('-'))
return defaultValue;
// day
int day = ds.readDigits();
if (day == -1)
return defaultValue;
if (!ds.fwIfCurrent(' '))
return defaultValue;
// hour
int hour = ds.readDigits();
if (hour == -1)
return defaultValue;
if (!ds.fwIfCurrent(':'))
return defaultValue;
// minute
int minute = ds.readDigits();
if (minute == -1)
return defaultValue;
if (!ds.fwIfCurrent(':'))
return defaultValue;
// second
int second = ds.readDigits();
if (second == -1)
return defaultValue;
// Milli Second
int millis = 0;
if (ds.fwIfCurrent('.')) {
millis = ds.readDigits();
}
int before = ds.getPos();
DateTime tmp = parseOffset(ds, timeZone, new int[] { year, month, day }, hour, minute, second, millis, false, defaultValue);
if (tmp == null && before != ds.getPos())
return defaultValue;
if (!(ds.fwIfCurrent('\'') && ds.fwIfCurrent('}')))
return defaultValue;
if (ds.isAfterLast()) {
if (tmp != null)
return tmp;
return util.toDateTime(timeZone, year, month, day, hour, minute, second, millis, defaultValue);
}
return defaultValue;
} else
return defaultValue;
}
Aggregations