1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.problem;
13 import java.util.Locale;
14 import java.util.MissingResourceException;
15 import java.util.ResourceBundle;
17 import net.sourceforge.phpdt.core.compiler.IProblem;
18 import net.sourceforge.phpdt.internal.compiler.IProblemFactory;
19 import net.sourceforge.phpdt.internal.compiler.util.CharOperation;
21 public class DefaultProblemFactory implements IProblemFactory {
24 public String[] messageTemplates;
25 private Locale locale;
26 private static String[] DEFAULT_LOCALE_TEMPLATES;
27 private final static char[] DOUBLE_QUOTES = "''".toCharArray(); //$NON-NLS-1$
28 private final static char[] SINGLE_QUOTE = "'".toCharArray(); //$NON-NLS-1$
31 * @param loc the locale used to get the right message
33 public DefaultProblemFactory(Locale loc) {
35 if (Locale.getDefault().equals(loc)){
36 if (DEFAULT_LOCALE_TEMPLATES == null){
37 DEFAULT_LOCALE_TEMPLATES = loadMessageTemplates(loc);
39 this.messageTemplates = DEFAULT_LOCALE_TEMPLATES;
41 this.messageTemplates = loadMessageTemplates(loc);
45 * Answer a new IProblem created according to the parameters value
47 * <li>originatingFileName the name of the file name from which the problem is originated
48 * <li>problemId the problem id
49 * <li>arguments the arguments needed to set the error message
50 * <li>severity the severity of the problem
51 * <li>startPosition the starting position of the problem
52 * <li>endPosition the end position of the problem
53 * <li>lineNumber the line on which the problem occured
55 * @param originatingFileName char[]
56 * @param problemId int
57 * @param arguments String[]
59 * @param startPosition int
60 * @param endPosition int
61 * @param lineNumber int
62 * @return org.eclipse.jdt.internal.compiler.IProblem
64 public IProblem createProblem(
65 char[] originatingFileName,
73 return new DefaultProblem(
75 this.getLocalizedMessage(problemId, arguments),
84 * Answer the locale used to retrieve the error messages
85 * @return java.util.Locale
87 public Locale getLocale() {
90 public final String getLocalizedMessage(int id, String[] problemArguments) {
91 StringBuffer output = new StringBuffer(80);
93 messageTemplates[(id & IProblem.IgnoreCategoriesMask)];
94 if (message == null) {
95 return "Unable to retrieve the error message for problem id: " //$NON-NLS-1$
97 + ". Check compiler resources."; //$NON-NLS-1$
100 // for compatibility with MessageFormat which eliminates double quotes in original message
101 char[] messageWithNoDoubleQuotes =
102 CharOperation.replace(message.toCharArray(), DOUBLE_QUOTES, SINGLE_QUOTE);
103 message = new String(messageWithNoDoubleQuotes);
105 int length = message.length();
106 int start = -1, end = length;
108 if ((end = message.indexOf('{', start)) > -1) {
109 output.append(message.substring(start + 1, end));
110 if ((start = message.indexOf('}', end)) > -1) {
113 problemArguments[Integer.parseInt(message.substring(end + 1, start))]);
114 } catch (NumberFormatException nfe) {
115 output.append(message.substring(end + 1, start + 1));
116 } catch (ArrayIndexOutOfBoundsException e) {
117 return "Corrupted compiler resources for problem id: " //$NON-NLS-1$
118 + (id & IProblem.IgnoreCategoriesMask)
119 + ". Check compiler resources."; //$NON-NLS-1$
122 output.append(message.substring(end, length));
126 output.append(message.substring(start + 1, length));
130 return output.toString();
133 * @param problem org.eclipse.jdt.internal.compiler.IProblem
136 public final String localizedMessage(IProblem problem) {
137 return getLocalizedMessage(problem.getID(), problem.getArguments());
141 * This method initializes the MessageTemplates class variable according
142 * to the current Locale.
144 public static String[] loadMessageTemplates(Locale loc) {
145 ResourceBundle bundle = ResourceBundle.getBundle("org.eclipse.jdt.internal.compiler.problem.messages", loc); //$NON-NLS-1$
146 String[] templates = new String[500];
147 for (int i = 0, max = templates.length; i < max; i++) {
149 templates[i] = bundle.getString(String.valueOf(i));
150 } catch (MissingResourceException e) {
157 public DefaultProblemFactory() {
158 this(Locale.getDefault());