Package cn.wanghaomiao.xpath.model

Examples of cn.wanghaomiao.xpath.model.Predicate


     * @return
     */
    public Element filter(Element e,Node node) throws NoSuchFunctionException, NoSuchAxisException {
        if (node.getTagName().equals("*")||node.getTagName().equals(e.nodeName())){
            if (node.getPredicate()!=null){
                Predicate p = node.getPredicate();
                if (p.getOpEm()==null){
                    if (p.getValue().matches("\\d+")&&getElIndex(e)==Integer.parseInt(p.getValue())){
                        return e;
                    }else if (p.getValue().endsWith("()")&&(Boolean)callFilterFunc(p.getValue().substring(0,p.getValue().length()-2),e)){
                        return e;
                    }
                    //todo p.value ~= contains(./@href,'renren.com')
                }else {
                    if (p.getLeft().matches("[^/]+\\(\\)")){
                        Object filterRes=p.getOpEm().excute(callFilterFunc(p.getLeft().substring(0,p.getLeft().length()-2),e).toString(),p.getRight());
                        if (filterRes instanceof Boolean && (Boolean) filterRes){
                            return e;
                        }else if(filterRes instanceof Integer && e.siblingIndex()==Integer.parseInt(filterRes.toString())){
                            return e;
                        }
                    }else if (p.getLeft().startsWith("@")){
                        String lValue = e.attr(p.getLeft().substring(1));
                        Object filterRes = p.getOpEm().excute(lValue,p.getRight());
                        if ((Boolean) filterRes){
                            return e;
                        }
                    }else {
                        // 操作符左边不是函数、属性默认就是xpath表达式了
                        List<Element> eltmp = new LinkedList<Element>();
                        eltmp.add(e);
                        List<Object> rstmp=evaluate(p.getLeft(),new Elements(eltmp));
                        if ((Boolean) p.getOpEm().excute(StringUtils.join(rstmp,""),p.getRight())){
                            return e;
                        }
                    }
                }
            }else {
View Full Code Here


     * @return
     */
    public Element filter(Element e,Node node) throws NoSuchFunctionException, NoSuchAxisException {
        if (node.getTagName().equals("*")||node.getTagName().equals(e.nodeName())){
            if (node.getPredicate()!=null){
                Predicate p = node.getPredicate();
                if (p.getOpEm()==null){
                    if (p.getValue().matches("\\d+")&&getElIndex(e)==Integer.parseInt(p.getValue())){
                        return e;
                    }else if (p.getValue().endsWith("()")&&(Boolean)callFilterFunc(p.getValue().substring(0,p.getValue().length()-2),e)){
                        return e;
                    }
                    //todo p.value ~= contains(./@href,'renren.com')
                }else {
                    if (p.getLeft().matches("[^/]+\\(\\)")){
                        Object filterRes=p.getOpEm().excute(callFilterFunc(p.getLeft().substring(0,p.getLeft().length()-2),e).toString(),p.getRight());
                        if (filterRes instanceof Boolean && (Boolean) filterRes){
                            return e;
                        }else if(filterRes instanceof Integer && e.siblingIndex()==Integer.parseInt(filterRes.toString())){
                            return e;
                        }
                    }else if (p.getLeft().startsWith("@")){
                        String lValue = e.attr(p.getLeft().substring(1));
                        Object filterRes = p.getOpEm().excute(lValue,p.getRight());
                        if ((Boolean) filterRes){
                            return e;
                        }
                    }else {
                        // 操作符左边不是函数、属性默认就是xpath表达式了
                        List<Element> eltmp = new LinkedList<Element>();
                        eltmp.add(e);
                        List<Object> rstmp=evaluate(p.getLeft(),new Elements(eltmp));
                        if ((Boolean) p.getOpEm().excute(StringUtils.join(rstmp,""),p.getRight())){
                            return e;
                        }
                    }
                }
            }else {
View Full Code Here

     */
    public Predicate genPredicate(String pre){
        StringBuilder op = new StringBuilder();
        StringBuilder left = new StringBuilder();
        StringBuilder right = new StringBuilder();
        Predicate predicate = new Predicate();
        char[] preArray = pre.toCharArray();
        int index = preArray.length-1;
        int argDeep = 0;
        int opFlag = 0;
        if (pre.matches(".+(\\+|=|-|>|<|>=|<=|^=|\\*=|$=|~=|!=)'.+'")){
            while (index>=0){
                char tmp = preArray[index];
                if (tmp=='\''){
                    argDeep+=1;
                }
                if (argDeep==1&&tmp!='\''){
                    right.insert(0,tmp);
                }else if (argDeep==2&&EmMap.getInstance().commOpChar.contains(tmp)){
                    op.insert(0,tmp);
                    opFlag=1;
                }else if (argDeep>=2&&opFlag>0){
                    argDeep++;//取完操作符后剩下的都属于left
                    left.insert(0,tmp);
                }
                index-=1;
            }
        }else if (pre.matches(".+(\\+|=|-|>|<|>=|<=|^=|\\*=|$=|~=|!=)[^']+")){
            while (index>=0){
                char tmp = preArray[index];
                if (opFlag==0&&EmMap.getInstance().commOpChar.contains(tmp)){
                    op.insert(0,tmp);
                }else {
                    if (op.length()>0){
                        left.insert(0,tmp);
                        opFlag=1;
                    }else {
                        right.insert(0,tmp);
                    }
                }
                index-=1;
            }
        }

        predicate.setOpEm(EmMap.getInstance().opEmMap.get(op.toString()));
        predicate.setLeft(left.toString());
        predicate.setRight(right.toString());
        predicate.setValue(pre);
        return predicate;
    }
View Full Code Here

TOP

Related Classes of cn.wanghaomiao.xpath.model.Predicate

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.