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 {
23 public String[] messageTemplates;
25 private Locale locale;
27 private static String[] DEFAULT_LOCALE_TEMPLATES;
29 private final static char[] DOUBLE_QUOTES = "''".toCharArray(); //$NON-NLS-1$
31 private final static char[] SINGLE_QUOTE = "'".toCharArray(); //$NON-NLS-1$
35 * the locale used to get the right message
37 public DefaultProblemFactory(Locale loc) {
39 if (Locale.getDefault().equals(loc)) {
40 if (DEFAULT_LOCALE_TEMPLATES == null) {
41 DEFAULT_LOCALE_TEMPLATES = loadMessageTemplates(loc);
43 this.messageTemplates = DEFAULT_LOCALE_TEMPLATES;
45 this.messageTemplates = loadMessageTemplates(loc);
50 * Answer a new IProblem created according to the parameters value
52 * <li>originatingFileName the name of the file name from which the problem
54 * <li>problemId the problem id
55 * <li>problemArguments the fully qualified arguments recorded inside the
57 * <li>messageArguments the arguments needed to set the error message
58 * (shorter names than problemArguments ones)
59 * <li>severity the severity of the problem
60 * <li>startPosition the starting position of the problem
61 * <li>endPosition the end position of the problem
62 * <li>lineNumber the line on which the problem occured
65 * @param originatingFileName
73 * @param startPosition
79 * @return net.sourceforge.phpdt.internal.compiler.IProblem
81 public IProblem createProblem(char[] originatingFileName, int problemId,
82 String[] problemArguments, String[] messageArguments, int severity,
83 int startPosition, int endPosition, int lineNumber) {
85 return new DefaultProblem(originatingFileName, this
86 .getLocalizedMessage(problemId, messageArguments), problemId,
87 problemArguments, severity, startPosition, endPosition,
92 * Answer the locale used to retrieve the error messages
94 * @return java.util.Locale
96 public Locale getLocale() {
100 public final String getLocalizedMessage(int id, String[] problemArguments) {
101 StringBuffer output = new StringBuffer(80);
102 String message = messageTemplates[(id & IProblem.IgnoreCategoriesMask)];
103 if (message == null) {
104 return "Unable to retrieve the error message for problem id: " //$NON-NLS-1$
105 + (id & IProblem.IgnoreCategoriesMask)
106 + ". Check compiler resources."; //$NON-NLS-1$
109 // for compatibility with MessageFormat which eliminates double quotes
110 // in original message
111 char[] messageWithNoDoubleQuotes = CharOperation.replace(message
112 .toCharArray(), DOUBLE_QUOTES, SINGLE_QUOTE);
113 message = new String(messageWithNoDoubleQuotes);
115 int length = message.length();
116 int start = -1, end = length;
118 if ((end = message.indexOf('{', start)) > -1) {
119 output.append(message.substring(start + 1, end));
120 if ((start = message.indexOf('}', end)) > -1) {
122 output.append(problemArguments[Integer.parseInt(message
123 .substring(end + 1, start))]);
124 } catch (NumberFormatException nfe) {
125 output.append(message.substring(end + 1, start + 1));
126 } catch (ArrayIndexOutOfBoundsException e) {
127 return "Corrupted compiler resources for problem id: " //$NON-NLS-1$
128 + (id & IProblem.IgnoreCategoriesMask)
129 + ". Check compiler resources."; //$NON-NLS-1$
132 output.append(message.substring(end, length));
136 output.append(message.substring(start + 1, length));
140 return output.toString();
145 * net.sourceforge.phpdt.internal.compiler.IProblem
148 public final String localizedMessage(IProblem problem) {
149 return getLocalizedMessage(problem.getID(), problem.getArguments());
153 * This method initializes the MessageTemplates class variable according to
154 * the current Locale.
156 public static String[] loadMessageTemplates(Locale loc) {
157 ResourceBundle bundle = null;
158 String bundleName = "net.sourceforge.phpdt.internal.compiler.problem.messages"; //$NON-NLS-1$
160 bundle = ResourceBundle.getBundle(bundleName, loc);
161 } catch (MissingResourceException e) {
163 .println("Missing resource : " + bundleName.replace('.', '/') + ".properties for locale " + loc); //$NON-NLS-1$//$NON-NLS-2$
166 String[] templates = new String[500];
167 for (int i = 0, max = templates.length; i < max; i++) {
169 templates[i] = bundle.getString(String.valueOf(i));
170 } catch (MissingResourceException e) {
177 public DefaultProblemFactory() {
178 this(Locale.getDefault());