Example usage for org.dom4j XPath selectNodes

List of usage examples for org.dom4j XPath selectNodes

Introduction

In this page you can find the example usage for org.dom4j XPath selectNodes.

Prototype

List<Node> selectNodes(Object context);

Source Link

Document

selectNodes performs this XPath expression on the given Node or List of Node s instances appending all the results together into a single list.

Usage

From source file:org.suren.autotest.web.framework.code.DefaultXmlDataSourceGenerator.java

License:Apache License

/**
 * @param document//w w w .jav  a2 s. c o  m
 */
private void parse(Document doc) {
    SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
    simpleNamespaceContext.addNamespace("ns", "http://surenpi.com");

    XPath xpath = new DefaultXPath("/ns:autotest/ns:includePage");
    xpath.setNamespaceContext(simpleNamespaceContext);
    @SuppressWarnings("unchecked")
    List<Element> includePageList = xpath.selectNodes(doc);
    if (includePageList != null && includePageList.size() > 0) {
        for (Element includePage : includePageList) {
            String pageConfig = includePage.attributeValue("pageConfig");

            generate(pageConfig, outputDir);
        }
    }

    xpath = new DefaultXPath("/ns:autotest/ns:pages");
    xpath.setNamespaceContext(simpleNamespaceContext);
    Element pagesEle = (Element) xpath.selectSingleNode(doc);
    String pagePackage = pagesEle.attributeValue("pagePackage", "");
    if (StringUtils.isNotBlank(pagePackage)) {
        pagePackage = pagePackage.trim();
    }

    // pages parse progress
    xpath = new DefaultXPath("/ns:autotest/ns:pages/ns:page");
    xpath.setNamespaceContext(simpleNamespaceContext);
    @SuppressWarnings("unchecked")
    List<Element> pageNodes = xpath.selectNodes(doc);
    if (pageNodes != null) {
        for (Element ele : pageNodes) {
            String pageClsStr = ele.attributeValue("class");
            if (pageClsStr == null) {
                logger.warn("can not found class attribute.");
                continue;
            }

            String dataSrc = ele.attributeValue("dataSource");

            try {
                parse(doc, pagePackage, pageClsStr, dataSrc, ele);
            } catch (NoSuchBeanDefinitionException e) {
                logger.error("Page element [{}] parse error, in document [{}].", "pageClsStr", doc);
                throw e;
            } catch (Exception e) {
                logger.error("Page element parse error.", e);
            }
        }
    }
}

From source file:org.suren.autotest.web.framework.code.DefaultXmlSuiteRunnerGenerator.java

License:Apache License

/**
 * @param document//from   ww w  .  j av  a  2 s  .c  o  m
 */
private void parse(Document doc) {
    SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
    simpleNamespaceContext.addNamespace("ns", "http://surenpi.com");

    XPath xpath = new DefaultXPath("/ns:autotest/ns:includePage");
    xpath.setNamespaceContext(simpleNamespaceContext);
    @SuppressWarnings("unchecked")
    List<Element> includePageList = xpath.selectNodes(doc);
    if (includePageList != null && includePageList.size() > 0) {
        for (Element includePage : includePageList) {
            String pageConfig = includePage.attributeValue("pageConfig");

            generate(pageConfig, outputDir);
        }
    }

    xpath = new DefaultXPath("/ns:autotest/ns:pages");
    xpath.setNamespaceContext(simpleNamespaceContext);
    Element pagesEle = (Element) xpath.selectSingleNode(doc);
    String pagePackage = pagesEle.attributeValue("pagePackage", "");
    if (StringUtils.isNotBlank(pagePackage)) {
        pagePackage = pagePackage.trim();
    }

    // pages parse progress
    xpath = new DefaultXPath("/ns:autotest/ns:pages/ns:page");
    xpath.setNamespaceContext(simpleNamespaceContext);
    @SuppressWarnings("unchecked")
    List<Element> pageNodes = xpath.selectNodes(doc);
    if (pageNodes != null) {
        for (Element ele : pageNodes) {
            String pageClsStr = ele.attributeValue("class");
            if (pageClsStr == null) {
                logger.warn("can not found class attribute.");
                continue;
            }

            try {
                parse(doc, pagePackage, pageClsStr, ele);
            } catch (NoSuchBeanDefinitionException e) {
                logger.error("Page element [{}] parse error, in document [{}].", "pageClsStr", doc);
                throw e;
            } catch (Exception e) {
                logger.error("Page element parse error.", e);
            }
        }
    }
}

From source file:org.suren.autotest.web.framework.settings.Phoenix.java

License:Apache License

/**
 * ??//from   w w w  . ja  v a 2 s .  c  om
 * @param doc xml
 * @throws DocumentException  xml?
 * @throws IOException ??
 * @throws SAXException ?? 
 */
private void parse(Document doc) throws IOException, DocumentException, SAXException {
    SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
    simpleNamespaceContext.addNamespace("ns", "http://surenpi.com");

    SeleniumEngine seleniumEngine = getEngine();
    if (StringUtils.isBlank(seleniumEngine.getDriverStr())) {
        XPath xpath = new DefaultXPath("/ns:autotest/ns:engine");
        xpath.setNamespaceContext(simpleNamespaceContext);
        // engine parse progress
        Element engineEle = (Element) xpath.selectSingleNode(doc);
        if (engineEle == null) {
            throw new RuntimeException("can not found engine config.");
        }

        String driverStr = engineEle.attributeValue("driver", "chrome");
        String remoteStr = engineEle.attributeValue("remote");
        String timeOutStr = engineEle.attributeValue("timeout");
        String fullScreenStr = engineEle.attributeValue("fullScreen", "false");
        String maximizeStr = engineEle.attributeValue("maximize", "true");
        String widthStr = engineEle.attributeValue("width");
        String heightStr = engineEle.attributeValue("height");
        try {
            seleniumEngine.setDriverStr(driverStr);
            seleniumEngine.setRemoteStr(remoteStr);

            try {
                seleniumEngine.setTimeout(Long.parseLong(timeOutStr));
            } catch (NumberFormatException e) {
                logger.warn(String.format("Invalid number string [%s].", timeOutStr));
                seleniumEngine.setTimeout(5);
            }

            try {
                seleniumEngine.setWidth(Integer.parseInt(widthStr));
                seleniumEngine.setHeight(Integer.parseInt(heightStr));
            } catch (NumberFormatException e) {
                logger.warn(String.format("Invalid number width [%s] or height [%s].", widthStr, heightStr));
            }

            seleniumEngine.setFullScreen(Boolean.parseBoolean(fullScreenStr));
            seleniumEngine.setMaximize(Boolean.parseBoolean(maximizeStr));

            seleniumEngine.init();
        } catch (NoSuchBeanDefinitionException e) {
            logger.error("Can not found bean SeleniumEngine.", e);
        }
    }

    XPath xpath = new DefaultXPath("/ns:autotest/ns:includePage");
    xpath.setNamespaceContext(simpleNamespaceContext);
    @SuppressWarnings("unchecked")
    List<Element> includePageList = xpath.selectNodes(doc);
    if (includePageList != null && includePageList.size() > 0) {
        for (Element includePage : includePageList) {
            String pageConfig = includePage.attributeValue("pageConfig");

            readFromClassPath(pageConfig);
        }
    }

    xpath = new DefaultXPath("/ns:autotest/ns:pages");
    xpath.setNamespaceContext(simpleNamespaceContext);
    Element pagesEle = (Element) xpath.selectSingleNode(doc);
    String pagePackage = pagesEle.attributeValue("pagePackage", "");
    if (StringUtils.isNotBlank(pagePackage)) {
        pagePackage = (pagePackage.trim() + ".");
    }

    // pages parse progress
    xpath = new DefaultXPath("/ns:autotest/ns:pages/ns:page");
    xpath.setNamespaceContext(simpleNamespaceContext);
    @SuppressWarnings("unchecked")
    List<Element> pageNodes = xpath.selectNodes(doc);
    if (pageNodes != null) {
        for (Element ele : pageNodes) {
            String pageClsStr = ele.attributeValue("class");
            if (pageClsStr == null) {
                logger.warn("can not found class attribute.");
                continue;
            }

            pageClsStr = (pagePackage + pageClsStr);
            String dataSrcClsStr = ele.attributeValue("dataSource");

            try {
                parse(pageClsStr, dataSrcClsStr, ele);
            } catch (NoSuchBeanDefinitionException e) {
                logger.error("Page element [{}] parse error, in document [{}].", pageClsStr, doc);
                throw e;
            } catch (Exception e) {
                logger.error("Page element parse error.", e);
            }
        }
    }

    //parse datasources
    xpath = new DefaultXPath("/ns:autotest/ns:dataSources/ns:dataSource");
    xpath.setNamespaceContext(simpleNamespaceContext);
    @SuppressWarnings("unchecked")
    List<Element> dataSourceNodes = xpath.selectNodes(doc);
    if (dataSourceNodes != null) {
        for (Element ele : dataSourceNodes) {
            String name = ele.attributeValue("name");
            String type = ele.attributeValue("type");
            String resource = ele.attributeValue("resource");

            dataSourceMap.put(name, new DataSourceInfo(type, resource));
        }
    }
}

From source file:org.suren.autotest.webdriver.downloader.DriverMapping.java

License:Apache License

/**
 * @param browser//from w  ww .  j a va 2  s  .c  o m
 * @param ver
 * @param os ???
 * @param arch CUP3264?
 * @return ?null
 */
public String getUrl(String browser, String ver, String os, String arch) {
    String xpathStr = String.format("//drivers/driver[@type='%s']/supports/browser[@version='%s']", browser,
            ver);
    XPath xpath = new DefaultXPath(xpathStr);

    String path = null;
    @SuppressWarnings("unchecked")
    List<Element> nodes = xpath.selectNodes(document);
    String driverVer = null;
    for (Element ele : nodes) {
        @SuppressWarnings("unchecked")
        List<Element> itemList = ele.getParent().getParent().element("items").elements("item");
        for (Element item : itemList) {
            if (os.equals(item.attributeValue("os")) && arch.equals(item.attributeValue("arch"))) {
                path = item.attributeValue("path");
                break;
            }
        }

        driverVer = ele.getParent().getParent().attributeValue("version");
        break;
    }

    if (driverVer != null && !driverVer.trim().equals("")) {
        String base = document.getRootElement().attributeValue("base");
        String subPath = "";

        for (Browser bro : browserList()) {
            if (browser.equals(bro.getName())) {
                subPath = bro.getPath();
                break;
            }
        }

        if ("win32".equals(os)) {
            arch = "";
        }

        path = base + subPath + "/" + driverVer + "/" + browser + "driver_" + os + arch + ".zip";
    } else {
        path = null;
    }

    return path;
}

From source file:org.suren.autotest.webdriver.downloader.DriverMapping.java

License:Apache License

/**
 * @return ???/*  ww  w.j  a  va2  s  .  c o m*/
 */
public Map<String, Set<String>> supportBrowser() {
    Map<String, Set<String>> browserMap = new HashMap<String, Set<String>>();

    String xpathStr = String.format("//drivers/driver");
    XPath xpath = new DefaultXPath(xpathStr);

    @SuppressWarnings("unchecked")
    List<Element> nodes = xpath.selectNodes(document);
    for (Element ele : nodes) {
        String type = ele.attributeValue("type");

        Set<String> verList = new TreeSet<String>(new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        });
        @SuppressWarnings("unchecked")
        List<Element> verEleList = ele.element("supports").elements("browser");
        for (Element verEle : verEleList) {
            String ver = verEle.attributeValue("version");

            verList.add(ver);
        }

        Set<String> oldVerList = browserMap.get(type);
        if (oldVerList == null) {
            browserMap.put(type, verList);
        } else {
            oldVerList.addAll(verList);
        }
    }

    return browserMap;
}

From source file:org.suren.autotest.webdriver.downloader.DriverMapping.java

License:Apache License

/**
 * @return ?/*from www . j a va  2  s .c  o  m*/
 * @see #browserList()
 * @deprecated remove in 1.2.0
 */
public Map<String, String> driverMap() {
    Map<String, String> driverMap = new HashMap<String, String>();

    String xpathStr = String.format("//mapping/map");
    XPath xpath = new DefaultXPath(xpathStr);

    @SuppressWarnings("unchecked")
    List<Element> nodes = xpath.selectNodes(document);
    for (Element ele : nodes) {
        String type = ele.attributeValue("type");
        String driver = ele.attributeValue("driver");

        driverMap.put(type, driver);
    }

    return driverMap;
}

From source file:org.suren.autotest.webdriver.downloader.DriverMapping.java

License:Apache License

/**
 * @return ??//from  ww  w. ja va  2  s  . co m
 */
public List<Browser> browserList() {
    List<Browser> browserList = new ArrayList<Browser>();

    String xpathStr = String.format("//mapping/map");
    XPath xpath = new DefaultXPath(xpathStr);

    @SuppressWarnings("unchecked")
    List<Element> nodes = xpath.selectNodes(document);
    for (Element ele : nodes) {
        String type = ele.attributeValue("type");
        String driver = ele.attributeValue("driver");
        String alias = ele.attributeValue("alias");
        String path = ele.attributeValue("path");

        Browser browser = new Browser();
        browser.setName(type);
        browser.setDriver(driver);
        browser.setAlias(alias);
        browser.setPath(path);

        browserList.add(browser);
    }

    return browserList;
}

From source file:org.tsp.bws.BWSDocument.java

License:Open Source License

/**
 * Prints the names and ids of all scripts found in the document. 
 *///  www. j  av a  2s. com
public void getScriptNames() {
    XPath xpathSelector = DocumentHelper.createXPath("//script");

    List results = xpathSelector.selectNodes(xmlDocument);

    Element curElement;
    Attribute tempAttribute;
    String tempString = new String();

    for (Iterator elementIterator = results.iterator(); elementIterator.hasNext();) {
        curElement = (Element) elementIterator.next();
        if (debug > 0) {
            System.out.println("Element found");
            System.out.println(" Name: " + curElement.getName());
        }
        try {
            tempAttribute = curElement.attribute("id");
            tempString = tempAttribute.getValue();
        } catch (Exception e) {
            try {
                tempAttribute = curElement.attribute("name");
                tempString = tempAttribute.getValue();
            } catch (Exception e2) {
                System.out.println("[Error] Script without id or name");
            }
        }
        if (debug > 1) {
            System.out.println("  " + tempString);
        }
        scriptNames.addElement((Object) tempString);
    }
}

From source file:org.tsp.bws.BWSDocument.java

License:Open Source License

/** 
 * Get and rewrite all calls to a specific BWS occuring in the document.
 *
 * @param attributeValue the id of the script that is searched for.
 *///  ww  w  . j a v a2 s  . co  m
public void getAttributeElement(String attributeValue) {
    XPath xpathSelector = DocumentHelper.createXPath("//@*");

    List results = xpathSelector.selectNodes(xmlDocument);
    List elementAttributes;

    Attribute curAttribute;

    for (Iterator attributeIterator = results.iterator(); attributeIterator.hasNext();) {
        curAttribute = (Attribute) attributeIterator.next();
        if (debug > 0) {
            System.out.println(attributeValue + "-" + curAttribute.getValue() + "-"
                    + curAttribute.getValue().equals("#:" + attributeValue));
        }
        if ((curAttribute.getValue().startsWith("bws:") || curAttribute.getValue().startsWith("#:"))
                && curAttribute.getValue().indexOf(attributeValue) > 0) {
            //curAttribute.setValue("document.getElementById('BWSApplet').executeScript('" + attributeValue + "',this)");
            //System.out.println("#:"  + curAttribute.getValue().indexOf(":"));
            curAttribute.setValue("document.getElementById('BWSApplet').executeScript('"
                    + curAttribute.getValue().substring(curAttribute.getValue().indexOf(":") + 1) + "',this)");
            //curAttribute.setValue("javascript:bwsexec(" + attributeValue + ")");
        }
    }

}

From source file:org.tsp.bws.BWSDocument.java

License:Open Source License

/** 
 * Inserts the <tt>applet</tt> tag 
 *///  w ww .ja v  a  2  s . co m
public void appendApplet() {
    XPath xpathSelector = DocumentHelper.createXPath("/html/body");
    List results = xpathSelector.selectNodes(xmlDocument);

    // body should only occur once per document!
    if (results.size() > 1) {
        System.err.println("[BWSDocument.appendApplet] Warning! More than one <body> found, using the first!");
    }
    // use the first element returned
    Element bodyElement = (Element) results.get(0);
    bodyElement.addElement("applet").addAttribute("code", "org.tsp.bws.BWSApplet")
            .addAttribute("id", "BWSApplet").addAttribute("width", "0").addAttribute("height", "0")
            .addAttribute("mayscript", "true");

}