summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/btr/proxy/search/browser/firefox/FirefoxSettingParser.java
blob: b793299f177bb05f443a936a4e935fc148a6a685 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.btr.proxy.search.browser.firefox;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

/*****************************************************************************
 * Parser for the Firefox settings file.
 * Will extract all relevant proxy settings form the configuration file.
 *
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
 ****************************************************************************/

class FirefoxSettingParser {

	/*************************************************************************
	 * Constructor
	 ************************************************************************/
	
	public FirefoxSettingParser() {
		super();
	}
	
	/*************************************************************************
	 * Parse the settings file and extract all network.proxy.* settings from it.
	 * @param source of the Firefox profiles.
	 * @return the parsed properties.
	 * @throws IOException on read error.
	 ************************************************************************/
	
	public Properties parseSettings(FirefoxProfileSource source) throws IOException {
		// Search settings folder
		File profileFolder = source.getProfileFolder();
		
		// Read settings from file
		File settingsFile = new File(profileFolder, "prefs.js");
		
		BufferedReader fin = new BufferedReader(
				new InputStreamReader(
					new FileInputStream(settingsFile)));

		Properties result = new Properties();
		try {
			String line = fin.readLine();
			while (line != null) {
				line = line.trim();
				if (line.startsWith("user_pref(\"network.proxy")) {
					line = line.substring(10, line.length()-2);
					int index = line.indexOf(",");
					String key = line.substring(0, index).trim();
					if (key.startsWith("\"")) {
						key = key.substring(1);
					}
					if (key.endsWith("\"")) {
						key = key.substring(0, key.length()-1);
					}
					String value = line.substring(index+1).trim();
					if (value.startsWith("\"")) {
						value = value.substring(1);
					}
					if (value.endsWith("\"")) {
						value = value.substring(0, value.length()-1);
					}
					result.put(key, value);
				}
				line = fin.readLine();
			}
		} finally {
			fin.close();
		}

		return result;
	}
	
	
}