Example usage for org.openqa.selenium.io TemporaryFilesystem createTempDir

List of usage examples for org.openqa.selenium.io TemporaryFilesystem createTempDir

Introduction

In this page you can find the example usage for org.openqa.selenium.io TemporaryFilesystem createTempDir.

Prototype

public File createTempDir(String prefix, String suffix) 

Source Link

Document

Create a temporary directory, and track it for deletion.

Usage

From source file:com.opera.core.systems.preferences.OperaFilePreferences.java

License:Apache License

/**
 * Constructs a new representation of Opera's preferences based on the given preference file.
 *
 * @param preferenceFile an INI style preference file
 *///from   w w  w  . j av  a 2s  . com
public OperaFilePreferences(File preferenceFile) {
    this.preferenceFile = preferenceFile;

    // Create new preference file if it doesn't exist
    if (!preferenceFile.exists()) {
        try {
            if (!preferenceFile.createNewFile()) {
                throw new IOException("File exists");
            }
        } catch (IOException e) {
            throw new WebDriverException("Unable to create new preference file: " + e.getMessage());
        }

        return;
    }

    // Due to the sucky nature of Opera's invalid preference files, we are forced to remove the
    // first line of the file.
    //
    // opera.ini looks much like this:
    //
    //   <BOM>
    //   Opera Preferences version 2.1
    //   ; Do not edit this file while Opera is running
    //   ; This file is stored in UTF-8 encoding
    //
    //   [User Prefs]
    //   Language Files Directory=
    //
    //     &c.

    TemporaryFilesystem fs = null;
    File temporaryPreferenceFile;
    Ini ini;

    try {
        fs = TemporaryFilesystem.getDefaultTmpFS();
        temporaryPreferenceFile = new File(fs.createTempDir("operadriver", "preferences").getAbsolutePath()
                + File.separator + "opera.ini");

        BufferedReader reader = new BufferedReader(new FileReader(preferenceFile));
        BufferedWriter writer = new BufferedWriter(new FileWriter(temporaryPreferenceFile));

        String newLine = System.getProperty("line.separator");
        String currentLine;
        while ((currentLine = reader.readLine()) != null) {
            if (!currentLine.contains("Opera Preferences version")) {
                writer.write(currentLine + newLine);
            }
        }

        // Make sure channels are closed so that last line is flushed
        reader.close();
        writer.close();

        // Read new preference file
        ini = new Ini(temporaryPreferenceFile);
    } catch (FileNotFoundException e) {
        throw new WebDriverException("Unknown file: " + preferenceFile.getAbsolutePath());
    } catch (IOException e) {
        throw new WebDriverException("Unable to read file: " + preferenceFile.getPath());
    } finally {
        if (fs != null) {
            fs.deleteTemporaryFiles();
        }
    }

    // Add each preference entry
    for (Map.Entry<String, Profile.Section> section : ini.entrySet()) {
        for (Map.Entry<String, String> entry : section.getValue().entrySet()) {
            set(section.getValue().getName(), entry.getKey(), entry.getValue());
        }
    }
}