Skip to main content
summaryrefslogtreecommitdiffstats
blob: b7736df57448e757bc96cb6884a9d57277c7b2fa (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*******************************************************************************
 * Copyright (c) 2004 - 2005 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylar.bugzilla.core.internal;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.eclipse.mylar.bugzilla.BugzillaPlugin;


/**
 * A factory for creating ProductConfiguration objects that encapsulate valid
 * combinations of products, components, and versions.
 */
public class ProductConfigurationFactory {
	/** Singleton factory instance */
	private static ProductConfigurationFactory instance;

	/**
	 * Private constructor to ensure singleton instances.
	 */
	private ProductConfigurationFactory() {
		// no initial setup needed
	}
	
	/**
	 * Returns the factory singletoninstance.
	 */
	public static synchronized ProductConfigurationFactory getInstance() {
		if (instance == null) {
			instance = new ProductConfigurationFactory();
		}
		return instance;
	}

	/**
	 * Builds a ProductConfiguration object by parsing the source of the
	 * Bugzilla query page.
	 */
	public ProductConfiguration getConfiguration(String server) throws IOException {
		URL serverURL = new URL(server + "/query.cgi");
		ProductConfiguration configuration = new ProductConfiguration();
		ArrayList<String []> componentsMatrix = new ArrayList<String []>();
		ArrayList<String []> versionsMatrix = new ArrayList<String []>();
		URLConnection c = serverURL.openConnection();
		BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
		String line;
		while ((line = in.readLine()) != null) {
			if (line.startsWith("  cpts[")) {
				String [] components = parseComponents(line);
				if (components.length > 0) componentsMatrix.add(components);
			}
			else if (line.startsWith("  vers[")) {
				String [] versions = parseComponents(line);
				if (versions.length > 0) versionsMatrix.add(versions);
			}
			else if (line.indexOf("<select name=\"product\"") != -1) {
				String [] products = parseProducts(in);
				for (int i = 0; i < products.length; i++) {
					String product = products[i];
					configuration.addProduct(product);
					// If components don't jibe with the products, just don't make them available.
					if (products.length == componentsMatrix.size()) {
						configuration.addComponents(product, componentsMatrix.get(i));
					}
					
					// If versions don't jibe with the products, just don't make them available
					if (products.length == versionsMatrix.size()) {
						configuration.addVersions(product, versionsMatrix.get(i));
					}
				}
			}
		}
		return configuration;
	}
	
	/**
	 * Returns an array of valid components or versions by parsing the JavaScript
	 * array in the Bugzilla query page.
	 */
	protected String [] parseComponents(String line) {
		ArrayList<String> components = new ArrayList<String>();
		int start = line.indexOf('\'');
		if (start >= 0) {
			boolean inName = true;
			StringBuffer name = new StringBuffer();
			for (int i = start+1; i < line.length(); i++){
				char ch = line.charAt(i);
				if (inName) {
					if (ch == '\'') {
						components.add(name.toString());
						name.setLength(0);
						inName = false;
					}
					else name.append(ch);
				}
				else {
					if (ch == '\'') {
						inName = true;
					}
				}
			}
		}
		return components.toArray(new String[0]);
	}
	
	/**
	 * Returns an array of valid product names by parsing the product selection list
	 * in the Bugzilla query page.
	 */
	protected String[] parseProducts(BufferedReader in) throws IOException {
		ArrayList<String> products = new ArrayList<String>();
		String line;
		while ((line = in.readLine()) != null) {
			if (line.indexOf("</select>") != -1) break;
			int optionIndex = line.indexOf("<option value=\"");
			if (optionIndex != -1) {
				boolean inName = false;
				StringBuffer name = new StringBuffer();
				for (int i = optionIndex; i< line.length(); i++) {
					char ch = line.charAt(i);
					if (inName) {
						if (ch == '<') {
							products.add(name.toString());
							break;
						}
						else name.append(ch);
					}
					else {
						if (ch == '>') {
							inName = true;
						}
					}
				}
			}
		}
		return products.toArray(new String[0]);
	}
	
	/**
	 * Restores a ProductConfiguration from a file.
	 */
	public ProductConfiguration readConfiguration(File file) throws IOException {
		if (!file.exists()) return null;
		FileInputStream fin = null;
		ProductConfiguration configuration = null;
		try {
			fin = new FileInputStream(file);
			ObjectInputStream in = new ObjectInputStream(fin);
			configuration = (ProductConfiguration) in.readObject();
		} catch (ClassNotFoundException e) {
			BugzillaPlugin.log(e);
			IOException ex = new IOException();
			ex.initCause(e);
			throw ex;
		} finally {
			if (fin != null)
				fin.close();
		}
		return configuration;
	}
	
	/**
	 * Saves a ProductConfiguration to a file.
	 */
	public void writeConfiguration(ProductConfiguration configuration, File file) throws IOException {
		FileOutputStream fout = null;
		try {
			fout = new FileOutputStream(file);
			ObjectOutputStream out = new ObjectOutputStream(fout);
			out.writeObject(configuration);
		} finally {
			if (fout != null)
				fout.close();
		}
	}
}

Back to the top