1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.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.CharOperation;
18 import net.sourceforge.phpdt.core.compiler.IProblem;
19 import net.sourceforge.phpdt.internal.compiler.IProblemFactory;
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>problemArguments the fully qualified arguments recorded inside the problem
50 * <li>messageArguments the arguments needed to set the error message (shorter names than problemArguments ones)
51 * <li>severity the severity of the problem
52 * <li>startPosition the starting position of the problem
53 * <li>endPosition the end position of the problem
54 * <li>lineNumber the line on which the problem occured
56 * @param originatingFileName char[]
57 * @param problemId int
58 * @param arguments String[]
60 * @param startPosition int
61 * @param endPosition int
62 * @param lineNumber int
63 * @return org.eclipse.jdt.internal.compiler.IProblem
65 public IProblem createProblem(
66 char[] originatingFileName,
68 String[] problemArguments,
69 String[] messageArguments,
75 return new DefaultProblem(
77 this.getLocalizedMessage(problemId, messageArguments),
86 * Answer the locale used to retrieve the error messages
87 * @return java.util.Locale
89 public Locale getLocale() {
92 public final String getLocalizedMessage(int id, String[] problemArguments) {
93 StringBuffer output = new StringBuffer(80);
95 messageTemplates[(id & IProblem.IgnoreCategoriesMask)];
96 if (message == null) {
97 return "Unable to retrieve the error message for problem id: " //$NON-NLS-1$
98 + (id & IProblem.IgnoreCategoriesMask)
99 + ". Check compiler resources."; //$NON-NLS-1$
102 // for compatibility with MessageFormat which eliminates double quotes in original message
103 char[] messageWithNoDoubleQuotes =
104 CharOperation.replace(message.toCharArray(), DOUBLE_QUOTES, SINGLE_QUOTE);
105 message = new String(messageWithNoDoubleQuotes);
107 int length = message.length();
108 int start = -1, end = length;
110 if ((end = message.indexOf('{', start)) > -1) {
111 output.append(message.substring(start + 1, end));
112 if ((start = message.indexOf('}', end)) > -1) {
115 problemArguments[Integer.parseInt(message.substring(end + 1, start))]);
116 } catch (NumberFormatException nfe) {
117 output.append(message.substring(end + 1, start + 1));
118 } catch (ArrayIndexOutOfBoundsException e) {
119 return "Corrupted compiler resources for problem id: " //$NON-NLS-1$
120 + (id & IProblem.IgnoreCategoriesMask)
121 + ". Check compiler resources."; //$NON-NLS-1$
124 output.append(message.substring(end, length));
128 output.append(message.substring(start + 1, length));
132 return output.toString();
135 * @param problem org.eclipse.jdt.internal.compiler.IProblem
138 public final String localizedMessage(IProblem problem) {
139 return getLocalizedMessage(problem.getID(), problem.getArguments());
143 * This method initializes the MessageTemplates class variable according
144 * to the current Locale.
146 public static String[] loadMessageTemplates(Locale loc) {
147 ResourceBundle bundle = null;
148 String bundleName = "net.sourceforge.phpdt.internal.compiler.problem.messages"; //$NON-NLS-1$
150 bundle = ResourceBundle.getBundle(bundleName, loc);
151 } catch(MissingResourceException e) {
152 System.out.println("Missing resource : " + bundleName.replace('.', '/') + ".properties for locale " + loc); //$NON-NLS-1$//$NON-NLS-2$
155 String[] templates = new String[500];
156 for (int i = 0, max = templates.length; i < max; i++) {
158 templates[i] = bundle.getString(String.valueOf(i));
159 } catch (MissingResourceException e) {
166 public DefaultProblemFactory() {
167 this(Locale.getDefault());