<commit_before>package com.amazon.aiv.sulfur.factories;

import com.amazon.aiv.sulfur.Page;
import com.amazon.aiv.sulfur.factories.exceptions.*;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Set;

/**
 * @author <NAME> <<EMAIL>>
 *
 * #factory
 * #singleton
 *
 * TODO
 */
public class PageFactory {
    private static final Logger LOG = Logger.getLogger(PageFactory.class);

    private final Config mConfig;
    private final PageConfigFactory mPageConfigFactory;

    private static PageFactory singleton = null;

    private PageFactory() {
        // Read configuration file location
        String configFilePath = System.getProperty(Consts.SYSPROP_CONFIG_FILE_PATH);
        if (null == configFilePath) {
            throw new ConfigNotProvidedException();
        }

        // Parse configuration file
        Gson gson = new Gson();
        try {
            FileReader configFileReader = new FileReader(configFilePath);
            mConfig = gson.fromJson(configFileReader, Config.class);

            // Logging
            LOG.debug("FOUND Sulfur Config file: " + configFilePath);
            mConfig.logDebug(LOG);
        } catch (FileNotFoundException fnfe) {
            LOG.error("INVALID Config (not found)");
            throw new InvalidConfigException(configFilePath);
        } catch (JsonSyntaxException jse) {
            LOG.error("INVALID Config (malformed)");
            throw jse;
        }

        // Fetch a PageConfigFactory
        mPageConfigFactory = PageConfigFactory.getInstance();

        LOG.debug("Available Pages: " + getAvailablePageConfigs());
    }

    /**
     * Factory Method
     *
     * @return The PageFactory
     */
    public synchronized static PageFactory getInstance() {
        if (null == singleton) {
            singleton = new PageFactory();
        }

        return singleton;
    }

    /**
     * Creates a Page.
     * It validates the input parameters, checking if the requested driver exists, if the page exists and the
     * mandatory path and query parameters are all provided.
     *
     * NOTE: The returned Page hasn't loaded yet, so the User can still operate on it before the initial HTTP GET.
     *
     * @param driverName Possible values are listed in @see Consts interface
     * @param pageName Name of the Page we want to load. It must be part of the given PageConfig(s)
     * @param pathParams Map of parameters that will be set in the Page URL Path (@see PageConfig)
     * @param queryParams Map of parameters that will be set in the Page URL Query (@see PageConfig)
     * @return A "ready to load" Page object
     */
    public Page createPage(String driverName,
                           String pageName,
                           Map<String, String> pathParams,
                           Map<String, String> queryParams) {

        // Validate Driver Name
        if (!getConfig().getDrivers().contains(driverName)) {
            throw new UnavailableDriverException(driverName);
        }
        // Validate Page Name
        if (!getAvailablePageConfigs().contains(pageName)) {
            throw new UnavailablePageException(pageName);
        }

        try {
            // Fetch required PageConfig
            PageConfig pageConfig = mPageConfigFactory.getPageConfig(pageName);

            // Compose URL Path & Query to the Page
            String urlPath = pageConfig.composeUrlPath(pathParams);
            String urlQuery = pageConfig.composeUrlQuery(queryParams);

            // Create the requested driver
            WebDriver driver = WebDriverFactory.createDriver(driverName);

            // Create the destination URL
            String url;
            try {
                url = new URL(mConfig.getProtocol(), mConfig.getHost(), mConfig.getPort(), urlPath + "?" + urlQuery).toString();
            } catch (MalformedURLException mue) {
                LOG.fatal(String.format("FAILED to compose the URL to the Page '%s'", pageName), mue);
                throw mue;
            }

            return new Page(driver, url);
        } catch (Exception e) {
            throw new FailedToCreatePageException(e);
        }
    }

    public Set<String> getAvailablePageConfigs() {
        return mPageConfigFactory.getPageConfigs().keySet();
    }

    /**
     * @return The Sulfur Configuration currently used by the PageFactory
     */
    public Config getConfig() {
        return mConfig;
    }
}
<commit_msg>It's better if we don't wrap Exceptions emitted by ".createPage()" into a single Exception.<commit_after>package com.amazon.aiv.sulfur.factories;

import com.amazon.aiv.sulfur.Page;
import com.amazon.aiv.sulfur.factories.exceptions.*;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Set;

/**
 * @author <NAME> <<EMAIL>>
 *
 * #factory
 * #singleton
 *
 * TODO
 */
public class PageFactory {
    private static final Logger LOG = Logger.getLogger(PageFactory.class);

    private final Config mConfig;
    private final PageConfigFactory mPageConfigFactory;

    private static PageFactory singleton = null;

    private PageFactory() {
        // Read configuration file location
        String configFilePath = System.getProperty(Consts.SYSPROP_CONFIG_FILE_PATH);
        if (null == configFilePath) {
            throw new ConfigNotProvidedException();
        }

        // Parse configuration file
        Gson gson = new Gson();
        try {
            FileReader configFileReader = new FileReader(configFilePath);
            mConfig = gson.fromJson(configFileReader, Config.class);

            // Logging
            LOG.debug("FOUND Sulfur Config file: " + configFilePath);
            mConfig.logDebug(LOG);
        } catch (FileNotFoundException fnfe) {
            LOG.error("INVALID Config (not found)");
            throw new InvalidConfigException(configFilePath);
        } catch (JsonSyntaxException jse) {
            LOG.error("INVALID Config (malformed)");
            throw jse;
        }

        // Fetch a PageConfigFactory
        mPageConfigFactory = PageConfigFactory.getInstance();

        LOG.debug("Available Pages: " + getAvailablePageConfigs());
    }

    /**
     * Factory Method
     *
     * @return The PageFactory
     */
    public synchronized static PageFactory getInstance() {
        if (null == singleton) {
            singleton = new PageFactory();
        }

        return singleton;
    }

    /**
     * Creates a Page.
     * It validates the input parameters, checking if the requested driver exists, if the page exists and the
     * mandatory path and query parameters are all provided.
     *
     * NOTE: The returned Page hasn't loaded yet, so the User can still operate on it before the initial HTTP GET.
     *
     * @param driverName Possible values are listed in @see Consts interface
     * @param pageName Name of the Page we want to load. It must be part of the given PageConfig(s)
     * @param pathParams Map of parameters that will be set in the Page URL Path (@see PageConfig)
     * @param queryParams Map of parameters that will be set in the Page URL Query (@see PageConfig)
     * @return A "ready to load" Page object
     */
    public Page createPage(String driverName,
                           String pageName,
                           Map<String, String> pathParams,
                           Map<String, String> queryParams) {

        // Validate Driver Name
        if (!getConfig().getDrivers().contains(driverName)) {
            throw new UnavailableDriverException(driverName);
        }
        // Validate Page Name
        if (!getAvailablePageConfigs().contains(pageName)) {
            throw new UnavailablePageException(pageName);
        }

        // Fetch required PageConfig
        PageConfig pageConfig = mPageConfigFactory.getPageConfig(pageName);

        // Compose URL Path & Query to the Page
        String urlPath = pageConfig.composeUrlPath(pathParams);
        String urlQuery = pageConfig.composeUrlQuery(queryParams);

        // Create the requested driver
        WebDriver driver = WebDriverFactory.createDriver(driverName);

        // Create the destination URL
        String url;
        try {
            url = new URL(mConfig.getProtocol(), mConfig.getHost(), mConfig.getPort(), urlPath + "?" + urlQuery).toString();
        } catch (MalformedURLException mue) {
            LOG.fatal(String.format("FAILED to compose the URL to the Page '%s'", pageName), mue);
            throw new FailedToCreatePageException(mue);
        }

        return new Page(driver, url);
    }

    public Set<String> getAvailablePageConfigs() {
        return mPageConfigFactory.getPageConfigs().keySet();
    }

    /**
     * @return The Sulfur Configuration currently used by the PageFactory
     */
    public Config getConfig() {
        return mConfig;
    }
}

{'fraction_non_alphanumeric': 0.08798628228485693, 'fraction_numerical': 0.0002143392991104919, 'mean_word_length': 2.418315018315018, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3399302', 'n_tokens_mistral': 2440, 'n_tokens_neox': 2380, 'n_words': 735}