Skip to main content
summaryrefslogtreecommitdiffstats
blob: 87c723795911c85b19158f0638e7810eb3986857 (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
package org.eclipse.jdt.internal.core.builder.impl;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
import org.eclipse.jdt.internal.compiler.IProblem;
import org.eclipse.jdt.internal.compiler.IProblemFactory;
import org.eclipse.jdt.internal.core.builder.IProblemDetail;
import org.eclipse.jdt.internal.compiler.problem.ProblemIrritants;

import java.util.*;

/**
 * @see IProblemFactory
 */
public class ProblemFactory implements IProblemFactory {
	protected Locale fLocale;
	protected ResourceBundle fCompilerResources;
	protected String[] fMessageTemplates;

	protected static Hashtable fgFactories = new Hashtable(5);
/**
 * Creates a problem factory for the given locale.
 */
private ProblemFactory(Locale locale) {
	fLocale = locale;
	fCompilerResources = ResourceBundle.getBundle("org.eclipse.jdt.internal.compiler.problem.Messages"/*nonNLS*/, locale);
	initializeMessageTemplates();
}
/**
 * @see IProblemFactory
 */
public IProblem createProblem(char[] originatingFileName, int problemId, String[] arguments, int severity, int startPosition, int endPosition, int lineNumber) {
	String message = getLocalizedMessage(problemId, arguments);
	int sev = (severity & IProblem.Error) != 0 ? IProblemDetail.S_ERROR : 0;
	// SourceEntry is filled in later when problem is actually recorded.
	SourceEntry sEntry = null;
	if (lineNumber == 0)
		lineNumber = -1;
	return new ProblemDetailImpl(message, problemId, sev, sEntry, startPosition, endPosition, lineNumber);
}
/**
 * @see IProblemFactory
 */
public Locale getLocale() {
	return fLocale;
}
/**
 * @see IProblemFactory
 */
public String getLocalizedMessage(int id, String[] problemArguments) {
	StringBuffer output = new StringBuffer(80);
	String message = fMessageTemplates[ (id & ProblemIrritants.IgnoreCategoriesMask)];
	if (message == null) {
		return "Unable to retrieve the error message for problem id: "/*nonNLS*/+ id + ". Check compiler resources."/*nonNLS*/;
	}
	int length = message.length();
	int start = -1, end = length;
	while (true) {
		if ((end = message.indexOf('{', start)) > -1) {
			output.append(message.substring(start + 1, end));
			if ((start = message.indexOf('}', end)) > -1) {
				try {
					output.append(problemArguments[Integer.parseInt(message.substring(end + 1, start))]);
				} catch (NumberFormatException nfe) {
					output.append(message.substring(end + 1, start + 1));
				} catch (ArrayIndexOutOfBoundsException e) {
					return "Corrupted compiler resources for problem id: "/*nonNLS*/ + (id & ProblemIrritants.IgnoreCategoriesMask) + ". Check compiler resources."/*nonNLS*/;
				}
			} else {
				output.append(message.substring(end, length));
				break;
			}
		} else {
			output.append(message.substring(start + 1, length));
			break;
		}
	}
	return output.toString();
}
/**
 * Returns the problem factory for the given locale.
 */
public static ProblemFactory getProblemFactory(Locale locale) {
	ProblemFactory factory = (ProblemFactory) fgFactories.get(locale);
	if (factory == null) {
		factory = new ProblemFactory(locale);
		fgFactories.put(locale, factory);
	}
	return factory;
}
/**
 * This method initializes the messageTemplates variable according
 * to the current Locale.
 */
protected void initializeMessageTemplates() {
	fMessageTemplates = new String[500];
	for (int i = 0; i < fMessageTemplates.length; i++) {
		try {
			fMessageTemplates[i] = fCompilerResources.getString(String.valueOf(i)); 
		} catch (MissingResourceException e) {
		}
	}
}
}

Back to the top