Skip to main content
summaryrefslogtreecommitdiffstats
blob: b33b7e7060534e9217fc123812caa535951218a2 (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
/*******************************************************************************
 * Copyright (c) 2003 - 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.IOException;
import java.io.Reader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.security.auth.login.LoginException;

import org.eclipse.mylar.bugzilla.core.internal.HtmlStreamTokenizer.Token;



/**
 * @author Shawn Minto
 *
 * This class is used to parse the available products to log a bug for
 */
public class ProductParser 
{
    /** Tokenizer used on the stream */
    private HtmlStreamTokenizer tokenizer;

    public ProductParser(Reader in) {
        tokenizer = new HtmlStreamTokenizer(in, null);
    }
    
	/**
	 * Parse the product page for the valid products that a bug can be logged for
	 * @param in The input stream for the products page
	 * @return A list of the products that we can enter bugs for
	 * @throws IOException
	 * @throws ParseException
	 */
	public List<String> getProducts() throws IOException, ParseException, LoginException
	{
		ArrayList<String> products = null;

		boolean isTitle = false;
		boolean possibleBadLogin = false;
		String title = "";

		for (HtmlStreamTokenizer.Token token = tokenizer.nextToken(); token.getType() != Token.EOF; token = tokenizer.nextToken()) {
				
			// make sure that bugzilla doesn't want us to login
			if(token.getType() == Token.TAG && ((HtmlTag)(token.getValue())).getTagType() == HtmlTag.Type.TITLE && !((HtmlTag)(token.getValue())).isEndTag())
			{
				isTitle = true;
				continue;
			}
			
			if(isTitle)
			{
				// get all of the data in the title tag
				if(token.getType() != Token.TAG)
				{
					title += ((StringBuffer)token.getValue()).toString().toLowerCase() + " ";
					continue;
				}
				else if(token.getType() == Token.TAG && ((HtmlTag)token.getValue()).getTagType() == HtmlTag.Type.TITLE && ((HtmlTag)token.getValue()).isEndTag())
				{
					// compare the title to see if we think that there is a problem with login
					if((title.indexOf("login") != -1 || (title.indexOf("invalid") != -1 && title.indexOf("password") != -1) ||  title.indexOf("check e-mail") != -1 || title.indexOf("error") != -1))
						possibleBadLogin = true;
					isTitle = false;
					title = "";
				}
					continue;
			}
				
			if (token.getType() == Token.TAG ) {
				HtmlTag tag = (HtmlTag) token.getValue();
				if (tag.getTagType() == HtmlTag.Type.TR) 
				{
					token = tokenizer.nextToken();
					if(token.getType() != Token.EOF && token.getType() == Token.TAG)
					{
						tag = (HtmlTag)token.getValue();
						if(tag.getTagType() != HtmlTag.Type.TH)
							continue;
						else
						{
							if(products == null)
                                products = new ArrayList<String>();
                            parseProducts(products);
							
						}
					}
					continue;
				}
			}
		}
		
		// if we have no products and we suspect a login error, assume that it was a login error
		if(products == null && possibleBadLogin)
			throw new LoginException("Bugzilla login information incorrect");
		return products;
	}

	/**
	 * Parse the products that we can enter bugs for
	 * @param products The list of products to add this new product to
	 * @return
	 */
	private void parseProducts(List<String> products) throws IOException, ParseException 
	{
		StringBuffer sb = new StringBuffer();

		for (HtmlStreamTokenizer.Token token = tokenizer.nextToken(); token.getType() != Token.EOF; token = tokenizer.nextToken()) 
		{
			if(token.getType() == Token.TAG)
			{
				HtmlTag tag = (HtmlTag)token.getValue();
				if(tag.getTagType() == HtmlTag.Type.TH && tag.isEndTag())
					break;
			}
			else if(token.getType() == Token.TEXT)
				sb.append(token.toString());
		}
		
		String prod = HtmlStreamTokenizer.unescape(sb).toString();
		if(prod.endsWith(":"))
			prod = prod.substring(0, prod.length() - 1);
		products.add(prod);
				
		for (HtmlStreamTokenizer.Token token = tokenizer.nextToken(); token.getType() != Token.EOF; token = tokenizer.nextToken()) 
		{
			if(token.getType() == Token.TAG)
			{
				HtmlTag tag = (HtmlTag)token.getValue();
				if(tag.getTagType() == HtmlTag.Type.TR && tag.isEndTag())
					break;
		
			}
		}
	}
}

Back to the top