use of lucee.runtime.component.Property in project Lucee by lucee.
the class JSONConverter method _serializeStruct.
/**
* serialize a Struct
* @param struct Struct to serialize
* @param sb
* @param serializeQueryByColumns
* @param addUDFs
* @param done
* @throws ConverterException
*/
public void _serializeStruct(PageContext pc, Set test, Struct struct, StringBuilder sb, boolean serializeQueryByColumns, boolean addUDFs, Set<Object> done) throws ConverterException {
// Component
if (struct instanceof Component) {
String res = castToJson(pc, (Component) struct, NULL_STRING);
if (res != NULL_STRING) {
sb.append(res);
return;
}
}
sb.append(goIn());
sb.append("{");
// Key[] keys = struct.keys();
// Key key;
Iterator<Entry<Key, Object>> it = struct.entryIterator();
Entry<Key, Object> e;
Object value;
boolean doIt = false;
while (it.hasNext()) {
e = it.next();
// key=keys[i];
value = e.getValue();
if (!addUDFs && (value instanceof UDF || value == null))
continue;
if (doIt)
sb.append(',');
doIt = true;
sb.append(StringUtil.escapeJS(e.getKey().getString(), '"', charsetEncoder));
sb.append(':');
_serialize(pc, test, value, sb, serializeQueryByColumns, done);
}
if (struct instanceof Component) {
Boolean remotingFetch;
Component comp = (Component) struct;
boolean isPeristent = false;
isPeristent = comp.isPersistent();
Property[] props = comp.getProperties(false);
ComponentScope scope = comp.getComponentScope();
for (int i = 0; i < props.length; i++) {
if (!ignoreRemotingFetch) {
remotingFetch = Caster.toBoolean(props[i].getDynamicAttributes().get(REMOTING_FETCH, null), null);
if (remotingFetch == null) {
if (isPeristent && ORMUtil.isRelated(props[i]))
continue;
} else if (!remotingFetch.booleanValue())
continue;
}
Key key = KeyImpl.getInstance(props[i].getName());
value = scope.get(key, null);
if (!addUDFs && (value instanceof UDF || value == null))
continue;
if (doIt)
sb.append(',');
doIt = true;
sb.append(StringUtil.escapeJS(key.getString(), '"', charsetEncoder));
sb.append(':');
_serialize(pc, test, value, sb, serializeQueryByColumns, done);
}
}
sb.append('}');
}
use of lucee.runtime.component.Property in project Lucee by lucee.
the class AxisCaster method toComponent.
public static Component toComponent(PageContext pc, Pojo pojo, String compPath, Component defaultValue) {
try {
Component cfc = pc.loadComponent(compPath);
Property[] props = cfc.getProperties(false, true, false, false);
PojoIterator it = new PojoIterator(pojo);
// only when the same amount of properties
if (props.length == it.size()) {
Map<Collection.Key, Property> propMap = toMap(props);
Property p;
Pair<Collection.Key, Object> pair;
ComponentScope scope = cfc.getComponentScope();
while (it.hasNext()) {
pair = it.next();
p = propMap.get(pair.getName());
if (p == null)
return defaultValue;
Object val = null;
try {
val = Caster.castTo(pc, p.getType(), pair.getValue(), false);
} catch (PageException e) {
}
// store in variables and this scope
scope.setEL(pair.getName(), val);
cfc.setEL(pair.getName(), val);
}
return cfc;
}
} catch (PageException e) {
}
return defaultValue;
}
use of lucee.runtime.component.Property in project Lucee by lucee.
the class AxisCaster method _initPojo.
private static void _initPojo(PageContext pc, TypeEntry typeEntry, QName type, Pojo pojo, Property[] props, Struct sct, Component comp, TypeMapping tm, Set<Object> done) throws PageException {
Property p;
Object v;
Collection.Key k;
CFMLExpressionInterpreter interpreter = new CFMLExpressionInterpreter(false);
for (int i = 0; i < props.length; i++) {
p = props[i];
k = Caster.toKey(p.getName());
// value
v = sct.get(k, null);
if (v == null && comp != null)
v = comp.get(k, null);
if (v != null)
v = Caster.castTo(pc, p.getType(), v, false);
else {
if (!StringUtil.isEmpty(p.getDefault())) {
try {
v = Caster.castTo(pc, p.getType(), p.getDefault(), false);
} catch (PageException pe) {
try {
v = interpreter.interpret(pc, p.getDefault());
v = Caster.castTo(pc, p.getType(), v, false);
} catch (PageException pe2) {
throw new ExpressionException("can not use default value [" + p.getDefault() + "] for property [" + p.getName() + "] with type [" + p.getType() + "]");
}
}
}
}
// set or throw
if (v == null) {
if (p.isRequired())
throw new ExpressionException("required property [" + p.getName() + "] is not defined");
} else {
TypeEntry childTE = null;
QName childT = null;
if (typeEntry != null) {
childTE = AxisUtil.getContainedElement(typeEntry, p.getName(), null);
if (childTE != null)
childT = childTE.getQName();
}
Reflector.callSetter(pojo, p.getName().toLowerCase(), _toAxisType(tm, null, childTE, childT, null, v, done));
}
}
}
Aggregations