Example usage for org.dom4j XPath selectSingleNode

List of usage examples for org.dom4j XPath selectSingleNode

Introduction

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

Prototype

Node selectSingleNode(Object context);

Source Link

Document

selectSingleNode evaluates this XPath expression on the given Node or List of Node s and returns the result as a single Node instance.

Usage

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

License:Apache License

/**
 * @param ele/*from w  w  w . j  ava  2  s.co  m*/
 */
private void updateXmlDataSourceByEle(Element ele, String pagePackage, String pageClsStr) {
    SimpleDateFormat dateFormat = new SimpleDateFormat();
    suiteRunnerDoc.addComment("Auto created by AutoTest, " + dateFormat.format(new Date()));

    SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
    simpleNamespaceContext.addNamespace("ns", "http://suite.surenpi.com");

    XPath xpath = new DefaultXPath("/ns:suite");
    xpath.setNamespaceContext(simpleNamespaceContext);

    //?
    Element dataSourcesEle = suiteRunnerDoc.getRootElement();//(Element) xpath.selectSingleNode(suiteRunnerDoc);
    if (dataSourcesEle == null) {
        String prefix = "suren";
        dataSourcesEle = suiteRunnerDoc.addElement(prefix + ":suite");

        dataSourcesEle.addNamespace(prefix, "http://suite.surenpi.com");
        dataSourcesEle.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        dataSourcesEle.addAttribute("xsi:schemaLocation", "http://suite.surenpi.com "
                + "http://surenpi.com/schema/suite/autotest.web.framework.suite.xsd ");
        dataSourcesEle.addAttribute("pageConfig", this.srcCoding);
        dataSourcesEle.addAttribute("pagePackage", pagePackage);
    }

    xpath = new DefaultXPath("/ns:suite/ns:page[@class='" + pageClsStr + "']");
    xpath.setNamespaceContext(simpleNamespaceContext);
    Element dataSourceEle = (Element) xpath.selectSingleNode(suiteRunnerDoc);
    if (dataSourceEle == null) {
        String prefix = dataSourcesEle.getNamespacePrefix();
        if (StringUtils.isBlank("")) {
            String parentName = dataSourcesEle.getName();
            if (parentName.contains(":")) {
                prefix = parentName.split(":")[0];
            }
        }

        if (StringUtils.isNotBlank(prefix)) {
            prefix = prefix + ":";
        }

        dataSourceEle = dataSourcesEle.addElement(prefix + "page");

        dataSourceEle.addAttribute("class", pageClsStr);
    }

    //??

    xpath = new DefaultXPath("/ns:suite/ns:page[@class='" + pageClsStr + "']/ns:actions[1]");
    xpath.setNamespaceContext(simpleNamespaceContext);
    Element pageEle = (Element) xpath.selectSingleNode(dataSourceEle);
    if (pageEle == null) {
        String prefix = dataSourceEle.getNamespacePrefix();
        if (StringUtils.isNotBlank(prefix)) {
            prefix = prefix + ":";
        }

        pageEle = dataSourceEle.addElement(prefix + "actions");
    }

    ele.accept(new PageFieldVisitor(pageEle));
}

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

License:Apache License

/**
 * ??//from  w  ww .  j  a  v a2 s  .co  m
 * @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:se.kb.xml.XPathWrapper.java

License:Apache License

/**
 * Selects a single node that matches the given XPath.
 * /*from   w ww  . ja v a  2 s  . c  om*/
 * @param xpathExpression the xpath
 * @return a single <code>Node</code> or <code>null</code> if no match
 */
public Node selectSingleNode(String xpathExpression) {
    XPath xpath = createXPath(xpathExpression);
    return xpath.selectSingleNode(this.node);
}