Example usage for org.dom4j XPath setNamespaceContext

List of usage examples for org.dom4j XPath setNamespaceContext

Introduction

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

Prototype

void setNamespaceContext(NamespaceContext namespaceContext);

Source Link

Document

Sets the namespace context to be used when evaluating XPath expressions

Usage

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

License:Apache License

/**
 * ??//from  w w w. j  a va 2  s . c  o 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));
        }
    }
}