org.suren.autotest.webdriver.downloader.DriverMapping.java Source code

Java tutorial

Introduction

Here is the source code for org.suren.autotest.webdriver.downloader.DriverMapping.java

Source

/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.suren.autotest.webdriver.downloader;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import org.dom4j.xpath.DefaultXPath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.surenpi.autotest.utils.StringUtils;

/**
 * webdriver?
 * @author <a href="http://surenpi.com">suren</a>
 */
public class DriverMapping {
    private static final Logger logger = LoggerFactory.getLogger(DriverMapping.class);

    private Document document;
    private Properties osMap = new Properties();

    public void init() {
        try (InputStream input = this.getClass().getClassLoader().getResourceAsStream("driver.mapping.xml");
                InputStream enginePro = this.getClass().getClassLoader()
                        .getResourceAsStream("os.mapping.properties")) {
            SAXReader reader = new SAXReader();

            document = reader.read(input);

            if (enginePro != null) {
                osMap.load(enginePro);
            }
        } catch (IOException e) {
            logger.error("", e);
        } catch (DocumentException e) {
            logger.error("", e);
        }
    }

    /**
     * ????
     * @see #getUrl(String, String, String, String)
     * @param browser ?
     * @param ver ?
     * @return ?
     */
    public String getUrl(String browser, String ver) {
        //?
        final String os = System.getProperty("os.name");
        final String arch = System.getProperty("os.arch");

        String commonOs = osMap.getProperty("os.map.name." + os);
        String commonArch = osMap.getProperty("os.map.arch." + arch);

        if (StringUtils.isAnyBlank(commonOs, commonArch)) {
            throw new RuntimeException(String.format("unknow os [%s] and arch [%s].", os, arch));
        }

        return getUrl(browser, ver, commonOs, commonArch);
    }

    /**
     * @param browser
     * @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;
    }

    /**
     * @return ???
     */
    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;
    }

    /**
     * @return ?
     * @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;
    }

    /**
     * @return ??
     */
    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;
    }
}