Search in sources :

Example 1 with Token

use of com.taobao.weex.el.parse.Token in project incubator-weex by apache.

the class FailedCaseTest method testVElseIf0.

public void testVElseIf0() {
    JSONObject data = new JSONObject();
    JSONObject item = new JSONObject();
    item.put("number", 0.0);
    data.put("item", item);
    ArrayStack stack = new ArrayStack();
    stack.push(data);
    Token token = Parser.parse("!(item.number%3 === 0) && (item.number%3 === 1)");
    System.out.println(token.toString() + "  " + token.execute(stack));
    Token if2 = Parser.parse("!(!(item.number%3 === 0) && (item.number%3 === 1))");
    System.out.println(if2 + "   " + if2.execute(stack));
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) Token(com.taobao.weex.el.parse.Token) ArrayStack(com.taobao.weex.el.parse.ArrayStack)

Example 2 with Token

use of com.taobao.weex.el.parse.Token in project incubator-weex by apache.

the class IfStatementTest method isIfTrue.

private boolean isIfTrue(String code, Object context) {
    Token block = Parser.parse(code);
    System.out.println(code + " ==> " + block);
    return Operators.isTrue(block.execute(context));
}
Also used : Token(com.taobao.weex.el.parse.Token)

Example 3 with Token

use of com.taobao.weex.el.parse.Token in project incubator-weex by apache.

the class ParserTest method show.

private void show(String code) {
    Token block = Parser.parse(code);
    System.out.println(code + " ==> " + block);
}
Also used : Token(com.taobao.weex.el.parse.Token)

Example 4 with Token

use of com.taobao.weex.el.parse.Token in project incubator-weex by apache.

the class Statements method doRenderComponent.

/**
 *  @param component component with v-for statement, v-if statement and bind attrs
 *  @param context   execute context
 *  render component in context, the function do the following  work.
 *  execute component's v-for statement, v-if statement in context,
 *  and rebuild component's tree with the statement, v-for reuse component execute by pre render.
 *  if executed, component will be removed, don't remove, just mark it waste;
 *  may be next render it can be used.
 *  after statement has executed, render component's binding attrs in context and bind it to component.
 */
private static final int doRenderComponent(WXComponent component, CellRenderContext context, List<WXComponent> updates) {
    WXVContainer parent = component.getParent();
    WXDomObject domObject = (WXDomObject) component.getDomObject();
    WXAttr attrs = domObject.getAttrs();
    WXStatement statement = attrs.getStatement();
    if (statement != null) {
        WXDomObject parentDomObject = (WXDomObject) parent.getDomObject();
        Token vif = null;
        JSONObject vfor = null;
        if (statement.get(WXStatement.WX_IF) instanceof Token) {
            vif = (Token) statement.get(WXStatement.WX_IF);
        }
        if (statement.get(WXStatement.WX_FOR) instanceof JSONObject) {
            vfor = (JSONObject) statement.get(WXStatement.WX_FOR);
        }
        // execute v-for content
        if (vfor != null) {
            int renderIndex = parent.indexOf(component);
            if (vfor.get(WXStatement.WX_FOR_LIST) instanceof Token) {
                Token listBlock = (Token) vfor.get(WXStatement.WX_FOR_LIST);
                String indexKey = vfor.getString(WXStatement.WX_FOR_INDEX);
                String itemKey = vfor.getString(WXStatement.WX_FOR_ITEM);
                Object data = null;
                if (listBlock != null) {
                    data = listBlock.execute(context.stack);
                }
                if ((data instanceof List || data instanceof Map)) {
                    Collection collection = null;
                    Map map = null;
                    if (data instanceof List) {
                        collection = (List) data;
                    } else {
                        map = (Map) data;
                        collection = map.keySet();
                    }
                    Map<String, Object> loop = new HashMap<>();
                    int index = 0;
                    for (Object item : collection) {
                        Object key = null;
                        Object value = item;
                        if (map == null) {
                            key = index;
                            value = item;
                        } else {
                            key = item;
                            value = map.get(item);
                        }
                        if (indexKey != null) {
                            loop.put(indexKey, key);
                        }
                        if (itemKey != null) {
                            loop.put(itemKey, value);
                        } else {
                            context.stack.push(value);
                        }
                        if (loop.size() > 0) {
                            context.stack.push(loop);
                        }
                        if (vif != null) {
                            if (!Operators.isTrue(vif.execute(context.stack))) {
                                continue;
                            }
                        }
                        // find resuable renderNode
                        WXComponent renderNode = null;
                        if (renderIndex < parent.getChildCount()) {
                            renderNode = parent.getChild(renderIndex);
                            // check is same statment, if true, it is usabled.
                            if (!isCreateFromNodeStatement(renderNode, component)) {
                                renderNode = null;
                            }
                            if (renderNode != null) {
                                if (renderNode.isWaste()) {
                                    renderNode.setWaste(false);
                                }
                            }
                        }
                        // none resuable render node, create node, add to parent, but clear node's statement
                        if (renderNode == null) {
                            long start = System.currentTimeMillis();
                            renderNode = copyComponentTree(component, parent);
                            renderNode.setWaste(false);
                            WXDomObject renderNodeDomObject = (WXDomObject) renderNode.getDomObject();
                            if (renderNodeDomObject.getAttrs().getStatement() != null) {
                                renderNodeDomObject.getAttrs().getStatement().remove(WXStatement.WX_FOR);
                                // clear node's statement
                                renderNodeDomObject.getAttrs().getStatement().remove(WXStatement.WX_IF);
                            }
                            parentDomObject.add(renderNodeDomObject, renderIndex);
                            parent.addChild(renderNode, renderIndex);
                            updates.add(renderNode);
                            if (WXEnvironment.isApkDebugable()) {
                                WXLogUtils.d(WXRecyclerTemplateList.TAG, Thread.currentThread().getName() + renderNode.getRef() + renderNode.getDomObject().getType() + "statements copy component tree used " + (System.currentTimeMillis() - start));
                            }
                        }
                        doBindingAttrsEventAndRenderChildNode(renderNode, (WXDomObject) renderNode.getDomObject(), context, updates);
                        renderIndex++;
                        if (loop.size() > 0) {
                            context.stack.push(loop);
                        }
                        if (itemKey == null) {
                            context.stack.pop();
                        }
                    }
                }
            } else {
                WXLogUtils.e(WXRecyclerTemplateList.TAG, vfor.toJSONString() + " not call vfor block, for pre compile");
            }
            // after v-for execute, remove component created pre v-for.
            for (; renderIndex < parent.getChildCount(); renderIndex++) {
                WXComponent wasteNode = parent.getChild(renderIndex);
                if (!isCreateFromNodeStatement(wasteNode, component)) {
                    break;
                }
                wasteNode.setWaste(true);
            }
            return renderIndex - parent.indexOf(component);
        }
        // execute v-if context
        if (vif != null) {
            if (!Operators.isTrue(vif.execute(context.stack))) {
                component.setWaste(true);
                return 1;
            } else {
                component.setWaste(false);
            }
        }
    }
    doBindingAttrsEventAndRenderChildNode(component, domObject, context, updates);
    return 1;
}
Also used : WXComponent(com.taobao.weex.ui.component.WXComponent) HashMap(java.util.HashMap) Token(com.taobao.weex.el.parse.Token) WXStatement(com.taobao.weex.dom.binding.WXStatement) WXVContainer(com.taobao.weex.ui.component.WXVContainer) WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) Collection(java.util.Collection) WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) ArrayList(java.util.ArrayList) WXRecyclerTemplateList(com.taobao.weex.ui.component.list.template.WXRecyclerTemplateList) List(java.util.List) HashMap(java.util.HashMap) ArrayMap(android.support.v4.util.ArrayMap) Map(java.util.Map) WXAttr(com.taobao.weex.dom.WXAttr)

Example 5 with Token

use of com.taobao.weex.el.parse.Token in project incubator-weex by apache.

the class Statements method renderBindingAttrs.

public static Map<String, Object> renderBindingAttrs(ArrayMap bindAttrs, ArrayStack stack) {
    Set<Map.Entry<String, Object>> entrySet = bindAttrs.entrySet();
    Map<String, Object> dynamic = dynamicLocal.get();
    if (dynamic == null) {
        dynamic = new HashMap<>();
        dynamicLocal.set(dynamic);
    }
    if (dynamic.size() > 0) {
        dynamic.clear();
    }
    for (Map.Entry<String, Object> entry : entrySet) {
        Object value = entry.getValue();
        String key = entry.getKey();
        if (value instanceof JSONObject && (((JSONObject) value).get(ELUtils.BINDING) instanceof Token)) {
            JSONObject binding = (JSONObject) value;
            Token block = (Token) (binding.get(ELUtils.BINDING));
            Object blockValue = block.execute(stack);
            dynamic.put(key, blockValue);
        } else if (value instanceof JSONArray) {
            JSONArray array = (JSONArray) value;
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < array.size(); i++) {
                Object element = array.get(i);
                if (element instanceof CharSequence) {
                    builder.append(element);
                    continue;
                }
                if (element instanceof JSONObject && (((JSONObject) element).get(ELUtils.BINDING) instanceof Token)) {
                    JSONObject binding = (JSONObject) element;
                    Token block = (Token) (binding.get(ELUtils.BINDING));
                    Object blockValue = block.execute(stack);
                    if (blockValue == null) {
                        blockValue = "";
                    }
                    builder.append(blockValue);
                }
            }
            String builderString = builder.toString();
            if (builderString.length() > 256) {
                if (WXEnvironment.isApkDebugable()) {
                    WXLogUtils.w(WXRecyclerTemplateList.TAG, " warn too big string " + builderString);
                }
            }
            dynamic.put(key, builderString);
        }
    }
    return dynamic;
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray) Token(com.taobao.weex.el.parse.Token) JSONObject(com.alibaba.fastjson.JSONObject) WXDomObject(com.taobao.weex.dom.WXDomObject) JSONObject(com.alibaba.fastjson.JSONObject) HashMap(java.util.HashMap) ArrayMap(android.support.v4.util.ArrayMap) Map(java.util.Map)

Aggregations

Token (com.taobao.weex.el.parse.Token)10 JSONObject (com.alibaba.fastjson.JSONObject)7 WXDomObject (com.taobao.weex.dom.WXDomObject)4 ArrayMap (android.support.v4.util.ArrayMap)3 JSONArray (com.alibaba.fastjson.JSONArray)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ArrayList (java.util.ArrayList)2 WXAttr (com.taobao.weex.dom.WXAttr)1 WXStatement (com.taobao.weex.dom.binding.WXStatement)1 ArrayStack (com.taobao.weex.el.parse.ArrayStack)1 Parser (com.taobao.weex.el.parse.Parser)1 WXComponent (com.taobao.weex.ui.component.WXComponent)1 WXVContainer (com.taobao.weex.ui.component.WXVContainer)1 WXRecyclerTemplateList (com.taobao.weex.ui.component.list.template.WXRecyclerTemplateList)1 Collection (java.util.Collection)1 List (java.util.List)1