a9bcabcb80ccb159b344fdb3b86c7a2fa170f6d5
[phpeclipse.git] /
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.problem;
12
13 import java.util.Locale;
14 import java.util.MissingResourceException;
15 import java.util.ResourceBundle;
16
17 import net.sourceforge.phpdt.core.compiler.CharOperation;
18 import net.sourceforge.phpdt.core.compiler.IProblem;
19 import net.sourceforge.phpdt.internal.compiler.IProblemFactory;
20
21 public class DefaultProblemFactory implements IProblemFactory {
22
23         public String[] messageTemplates;
24
25         private Locale locale;
26
27         private static String[] DEFAULT_LOCALE_TEMPLATES;
28
29         private final static char[] DOUBLE_QUOTES = "''".toCharArray(); //$NON-NLS-1$
30
31         private final static char[] SINGLE_QUOTE = "'".toCharArray(); //$NON-NLS-1$
32
33         /**
34          * @param loc
35          *            the locale used to get the right message
36          */
37         public DefaultProblemFactory(Locale loc) {
38                 this.locale = loc;
39                 if (Locale.getDefault().equals(loc)) {
40                         if (DEFAULT_LOCALE_TEMPLATES == null) {
41                                 DEFAULT_LOCALE_TEMPLATES = loadMessageTemplates(loc);
42                         }
43                         this.messageTemplates = DEFAULT_LOCALE_TEMPLATES;
44                 } else {
45                         this.messageTemplates = loadMessageTemplates(loc);
46                 }
47         }
48
49         /**
50          * Answer a new IProblem created according to the parameters value
51          * <ul>
52          * <li>originatingFileName the name of the file name from which the problem
53          * is originated
54          * <li>problemId the problem id
55          * <li>problemArguments the fully qualified arguments recorded inside the
56          * problem
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
63          * </ul>
64          * 
65          * @param originatingFileName
66          *            char[]
67          * @param problemId
68          *            int
69          * @param arguments
70          *            String[]
71          * @param severity
72          *            int
73          * @param startPosition
74          *            int
75          * @param endPosition
76          *            int
77          * @param lineNumber
78          *            int
79          * @return net.sourceforge.phpdt.internal.compiler.IProblem
80          */
81         public IProblem createProblem(char[] originatingFileName, int problemId,
82                         String[] problemArguments, String[] messageArguments, int severity,
83                         int startPosition, int endPosition, int lineNumber) {
84
85                 return new DefaultProblem(originatingFileName, this
86                                 .getLocalizedMessage(problemId, messageArguments), problemId,
87                                 problemArguments, severity, startPosition, endPosition,
88                                 lineNumber);
89         }
90
91         /**
92          * Answer the locale used to retrieve the error messages
93          * 
94          * @return java.util.Locale
95          */
96         public Locale getLocale() {
97                 return locale;
98         }
99
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$
107                 }
108
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);
114
115                 int length = message.length();
116                 int start = -1, end = length;
117                 while (true) {
118                         if ((end = message.indexOf('{', start)) > -1) {
119                                 output.append(message.substring(start + 1, end));
120                                 if ((start = message.indexOf('}', end)) > -1) {
121                                         try {
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$
130                                         }
131                                 } else {
132                                         output.append(message.substring(end, length));
133                                         break;
134                                 }
135                         } else {
136                                 output.append(message.substring(start + 1, length));
137                                 break;
138                         }
139                 }
140                 return output.toString();
141         }
142
143         /**
144          * @param problem
145          *            net.sourceforge.phpdt.internal.compiler.IProblem
146          * @return String
147          */
148         public final String localizedMessage(IProblem problem) {
149                 return getLocalizedMessage(problem.getID(), problem.getArguments());
150         }
151
152         /**
153          * This method initializes the MessageTemplates class variable according to
154          * the current Locale.
155          */
156         public static String[] loadMessageTemplates(Locale loc) {
157                 ResourceBundle bundle = null;
158                 String bundleName = "net.sourceforge.phpdt.internal.compiler.problem.messages"; //$NON-NLS-1$
159                 try {
160                         bundle = ResourceBundle.getBundle(bundleName, loc);
161                 } catch (MissingResourceException e) {
162                         System.out
163                                         .println("Missing resource : " + bundleName.replace('.', '/') + ".properties for locale " + loc); //$NON-NLS-1$//$NON-NLS-2$
164                         throw e;
165                 }
166                 String[] templates = new String[500];
167                 for (int i = 0, max = templates.length; i < max; i++) {
168                         try {
169                                 templates[i] = bundle.getString(String.valueOf(i));
170                         } catch (MissingResourceException e) {
171                                 // available ID
172                         }
173                 }
174                 return templates;
175         }
176
177         public DefaultProblemFactory() {
178                 this(Locale.getDefault());
179         }
180 }