// Assumes that we only really care about the preferences, not the comments private Map<String, String> readExistingPrefs(File userPrefs) { Map<String, String> prefs = new HashMap<String, String>(); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(userPrefs)); String line = reader.readLine(); while (line != null) { if (!line.startsWith("user_pref(\"")) { line = reader.readLine(); continue; } line = line.substring("user_pref(\"".length()); line = line.substring(0, line.length() - ");".length()); String[] parts = line.split(","); parts[0] = parts[0].substring(0, parts[0].length() - 1); prefs.put(parts[0].trim(), parts[1].trim()); line = reader.readLine(); } } catch (IOException e) { throw new WebDriverException(e); } finally { Cleanly.close(reader); } return prefs; }
protected void writeNewPrefs(Map<String, String> prefs) { Writer writer = null; try { writer = new FileWriter(userPrefs); for (Map.Entry<String, String> entry : prefs.entrySet()) { writer.append(String.format("user_pref(\"%s\", %s);\n", entry.getKey(), entry.getValue())); } } catch (IOException e) { throw new WebDriverException(e); } finally { Cleanly.close(writer); } }
protected void installDevelopmentExtension() throws IOException { if (!FileHandler.createDir(extensionsDir)) throw new IOException( "Cannot create extensions directory: " + extensionsDir.getAbsolutePath()); String home = findFirefoxExtensionRootInSourceCode(); File writeTo = new File(extensionsDir, EXTENSION_NAME); if (writeTo.exists() && !FileHandler.delete(writeTo)) { throw new IOException( "Cannot delete existing extensions directory: " + extensionsDir.getAbsolutePath()); } FileWriter writer = null; try { writer = new FileWriter(writeTo); writer.write(home); } catch (IOException e) { throw new WebDriverException(e); } finally { Cleanly.close(writer); } }