Package cn.wanghaomiao.xpath.model

Examples of cn.wanghaomiao.xpath.model.Node


    public List<Object> evaluate(String xpath,Elements root) throws NoSuchAxisException, NoSuchFunctionException {
        List<Object> res = new LinkedList<Object>();
        Elements context = root;
        List<Node> xpathNodes=getXpathNodeTree(xpath);
        for (int i=0;i<xpathNodes.size();i++){
            Node n = xpathNodes.get(i);
            LinkedList<Element> contextTmp = new LinkedList<Element>();
            if (n.getScopeEm()== ScopeEm.RECURSIVE||n.getScopeEm()==ScopeEm.CURREC){
                if (n.getTagName().startsWith("@")){
                    for (Element e:context){
                        //处理上下文自身节点
                        String key = n.getTagName().substring(1);
                        if (key.equals("*")){
                            res.add(e.attributes().toString());
                        }else {
                            String value = e.attr(key);
                            if (StringUtils.isNotBlank(value)){
                                res.add(value);
                            }
                        }
                        //处理上下文子代节点
                        for (Element dep:e.getAllElements()){
                            if (key.equals("*")){
                                res.add(dep.attributes().toString());
                            }else {
                                String value = dep.attr(key);
                                if (StringUtils.isNotBlank(value)){
                                    res.add(value);
                                }
                            }
                        }
                    }
                }else if (n.getTagName().endsWith("()")){
                    //递归执行方法默认只支持text()
                    res.add(context.text());
                }else {
                    Elements searchRes = context.select(n.getTagName());
                    for (Element e:searchRes){
                        Element filterR = filter(e,n);
                        if (filterR!=null){
                            contextTmp.add(filterR);
                        }
                    }
                    context = new Elements(contextTmp);
                }

            }else {
                if (n.getTagName().startsWith("@")){
                     for (Element e:context){
                         String key = n.getTagName().substring(1);
                         if (key.equals("*")){
                             res.add(e.attributes().toString());
                         }else {
                             String value = e.attr(key);
                             if (StringUtils.isNotBlank(value)){
                                 res.add(value);
                             }
                         }
                     }
                }else if (n.getTagName().endsWith("()")){
                    res = (List<Object>) callFunc(n.getTagName().substring(0,n.getTagName().length()-2),context);
                }else {
                    for (Element e:context){
                        Elements filterScope = e.children();
                        if (StringUtils.isNotBlank(n.getAxis())){
                            filterScope = getAxisScopeEls(n.getAxis(),e);
                        }
                        for (Element chi:filterScope){
                            Element fchi=filter(chi,n);
                            if (fchi!=null){
                                contextTmp.add(fchi);
View Full Code Here


    public List<Object> evaluate(String xpath,Elements root) throws NoSuchAxisException, NoSuchFunctionException {
        List<Object> res = new LinkedList<Object>();
        Elements context = root;
        List<Node> xpathNodes=getXpathNodeTree(xpath);
        for (int i=0;i<xpathNodes.size();i++){
            Node n = xpathNodes.get(i);
            LinkedList<Element> contextTmp = new LinkedList<Element>();
            if (n.getScopeEm()== ScopeEm.RECURSIVE||n.getScopeEm()==ScopeEm.CURREC){
                if (n.getTagName().startsWith("@")){
                    for (Element e:context){
                        //处理上下文自身节点
                        String key = n.getTagName().substring(1);
                        if (key.equals("*")){
                            res.add(e.attributes().toString());
                        }else {
                            String value = e.attr(key);
                            if (StringUtils.isNotBlank(value)){
                                res.add(value);
                            }
                        }
                        //处理上下文子代节点
                        for (Element dep:e.getAllElements()){
                            if (key.equals("*")){
                                res.add(dep.attributes().toString());
                            }else {
                                String value = dep.attr(key);
                                if (StringUtils.isNotBlank(value)){
                                    res.add(value);
                                }
                            }
                        }
                    }
                }else if (n.getTagName().endsWith("()")){
                    //递归执行方法默认只支持text()
                    res.add(context.text());
                }else {
                    Elements searchRes = context.select(n.getTagName());
                    for (Element e:searchRes){
                        Element filterR = filter(e,n);
                        if (filterR!=null){
                            contextTmp.add(filterR);
                        }
                    }
                    context = new Elements(contextTmp);
                }

            }else {
                if (n.getTagName().startsWith("@")){
                     for (Element e:context){
                         String key = n.getTagName().substring(1);
                         if (key.equals("*")){
                             res.add(e.attributes().toString());
                         }else {
                             String value = e.attr(key);
                             if (StringUtils.isNotBlank(value)){
                                 res.add(value);
                             }
                         }
                     }
                }else if (n.getTagName().endsWith("()")){
                    res = (List<Object>) callFunc(n.getTagName().substring(0,n.getTagName().length()-2),context);
                }else {
                    for (Element e:context){
                        Elements filterScope = e.children();
                        if (StringUtils.isNotBlank(n.getAxis())){
                            filterScope = getAxisScopeEls(n.getAxis(),e);
                        }
                        for (Element chi:filterScope){
                            Element fchi=filter(chi,n);
                            if (fchi!=null){
                                contextTmp.add(fchi);
View Full Code Here

TOP

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

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.