Skip to main content
summaryrefslogtreecommitdiffstats
blob: 764b37e548d8ec51e2c1b824f7bd6dec2e912c70 (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
/*******************************************************************************
 * Copyright (c) 2004 - 2006 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.internal.bugzilla.core;

import java.io.IOException;
import java.io.Reader;
import java.text.ParseException;

import org.eclipse.mylar.internal.tasklist.util.HtmlStreamTokenizer;
import org.eclipse.mylar.internal.tasklist.util.HtmlTag;
import org.eclipse.mylar.internal.tasklist.util.HtmlStreamTokenizer.Token;

/**
 * This is in place to escape & characters within the resource and rdf:about
 * attributes. Currently the values are not escaped which causes sax parser
 * errors. This bug has been filed and can be found here:
 * https://bugzilla.mozilla.org/show_bug.cgi?id=264785
 * 
 * @author Rob Elves
 */
public class XmlCleaner {

	public static StringBuffer clean(Reader in) {

		HtmlStreamTokenizer tokenizer = new HtmlStreamTokenizer(in, null);
		StringBuffer content = new StringBuffer();

		// Hack since HtmlStreamTokenizer not familiar with xml tag.
		content.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		try {
			for (Token token = tokenizer.nextToken(); token.getType() != Token.EOF; token = tokenizer.nextToken()) {

				if (token.getType() == Token.TAG) {
					HtmlTag tag = (HtmlTag) token.getValue();
					if (tag.getAttribute("resource") != null) {
						String resourceID = tag.getAttribute("resource");
						tag.setAttribute("resource", resourceID.replace("&", "&amp;"));
					}
					if (tag.getAttribute("rdf:about") != null) {
						String resourceID = tag.getAttribute("rdf:about");
						tag.setAttribute("rdf:about", resourceID.replace("&", "&amp;"));
					}
				}
				if (!token.toString().startsWith("<?xml")) {
					content.append(token.toString());
				}
			}
		} catch (IOException e) {

		} catch (ParseException e) {

		}
		return content;
	}

}

Back to the top