Search in sources :

Example 21 with Component

use of lucee.runtime.Component in project Lucee by lucee.

the class Property method doStartTag.

@Override
public int doStartTag() throws PageException {
    if (pageContext.variablesScope() instanceof ComponentScope) {
        Component comp = ((ComponentScope) pageContext.variablesScope()).getComponent();
        comp.setProperty(property);
        property.setOwnerName(comp.getAbsName());
    }
    return SKIP_BODY;
}
Also used : ComponentScope(lucee.runtime.ComponentScope) Component(lucee.runtime.Component)

Example 22 with Component

use of lucee.runtime.Component 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);
    }
}
Also used : PageException(lucee.runtime.exp.PageException) ObjectWrap(lucee.runtime.type.ObjectWrap) Query(lucee.runtime.type.Query) Node(org.w3c.dom.Node) DateTime(lucee.runtime.type.dt.DateTime) Date(java.util.Date) Struct(lucee.runtime.type.Struct) TimeSpan(lucee.runtime.type.dt.TimeSpan) Array(lucee.runtime.type.Array) JavaObject(lucee.runtime.java.JavaObject) UDF(lucee.runtime.type.UDF) JavaObject(lucee.runtime.java.JavaObject) List(java.util.List) Component(lucee.runtime.Component) File(java.io.File) Map(java.util.Map)

Example 23 with Component

use of lucee.runtime.Component 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('}');
}
Also used : Entry(java.util.Map.Entry) ComponentScope(lucee.runtime.ComponentScope) UDF(lucee.runtime.type.UDF) JavaObject(lucee.runtime.java.JavaObject) Component(lucee.runtime.Component) Property(lucee.runtime.component.Property) Key(lucee.runtime.type.Collection.Key)

Example 24 with Component

use of lucee.runtime.Component in project Lucee by lucee.

the class XMLConverter method _deserializeComponent.

/**
 * Desirialize a Component Object
 * @param elComp Component Object as XML Element
 * @return Component Object
 * @throws ConverterException
 * @throws ConverterException
 */
private Object _deserializeComponent(Element elComp) throws ConverterException {
    // String type=elStruct.getAttribute("type");
    String name = elComp.getAttribute("name");
    String md5 = elComp.getAttribute("md5");
    // TLPC
    PageContext pc = ThreadLocalPageContext.get();
    // Load comp
    Component comp = null;
    try {
        comp = pc.loadComponent(name);
        if (!ComponentUtil.md5(comp).equals(md5)) {
            throw new ConverterException("component [" + name + "] in this enviroment has not the same interface as the component to load, it is possible that one off the components has Functions added dynamicly.");
        }
    } catch (ConverterException e) {
        throw e;
    } catch (Exception e) {
        throw new ConverterException(e.getMessage());
    }
    NodeList list = elComp.getChildNodes();
    ComponentScope scope = comp.getComponentScope();
    int len = list.getLength();
    String scopeName;
    Element var, value;
    Collection.Key key;
    for (int i = 0; i < len; i++) {
        Node node = list.item(i);
        if (node instanceof Element) {
            var = (Element) node;
            value = getChildElement((Element) node);
            scopeName = var.getAttribute("scope");
            if (value != null) {
                key = Caster.toKey(var.getAttribute("name"), null);
                if (key == null)
                    continue;
                if ("variables".equalsIgnoreCase(scopeName))
                    scope.setEL(key, _deserialize(value));
                else
                    comp.setEL(key, _deserialize(value));
            }
        }
    }
    return comp;
}
Also used : Key(lucee.runtime.type.Collection.Key) ComponentScope(lucee.runtime.ComponentScope) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Collection(lucee.runtime.type.Collection) ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext) PageContext(lucee.runtime.PageContext) Component(lucee.runtime.Component) PageException(lucee.runtime.exp.PageException) IOException(java.io.IOException)

Example 25 with Component

use of lucee.runtime.Component 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;
}
Also used : PageException(lucee.runtime.exp.PageException) ComponentScope(lucee.runtime.ComponentScope) Component(lucee.runtime.Component) Property(lucee.runtime.component.Property) Key(lucee.runtime.type.Collection.Key)

Aggregations

Component (lucee.runtime.Component)36 PageException (lucee.runtime.exp.PageException)12 Key (lucee.runtime.type.Collection.Key)12 Struct (lucee.runtime.type.Struct)12 PageContextImpl (lucee.runtime.PageContextImpl)9 UDF (lucee.runtime.type.UDF)9 Entry (java.util.Map.Entry)7 ComponentScope (lucee.runtime.ComponentScope)7 PageContext (lucee.runtime.PageContext)7 Array (lucee.runtime.type.Array)7 JavaObject (lucee.runtime.java.JavaObject)6 IOException (java.io.IOException)5 PageSource (lucee.runtime.PageSource)5 ThreadLocalPageContext (lucee.runtime.engine.ThreadLocalPageContext)5 ExpressionException (lucee.runtime.exp.ExpressionException)5 Collection (lucee.runtime.type.Collection)5 ComponentSpecificAccess (lucee.runtime.ComponentSpecificAccess)4 Property (lucee.runtime.component.Property)4 ArrayImpl (lucee.runtime.type.ArrayImpl)4 StructImpl (lucee.runtime.type.StructImpl)4