KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > problem > DefaultProblemFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.problem;
12
13 import java.util.Enumeration JavaDoc;
14 import java.util.Locale JavaDoc;
15 import java.util.MissingResourceException JavaDoc;
16 import java.util.ResourceBundle JavaDoc;
17
18 import org.eclipse.jdt.core.compiler.*;
19 import org.eclipse.jdt.core.compiler.IProblem;
20 import org.eclipse.jdt.internal.compiler.IProblemFactory;
21 import org.eclipse.jdt.internal.compiler.util.HashtableOfInt;
22 import org.eclipse.jdt.internal.compiler.util.Util;
23
24 public class DefaultProblemFactory implements IProblemFactory {
25
26     public HashtableOfInt messageTemplates;
27     private Locale JavaDoc locale;
28     private static HashtableOfInt DEFAULT_LOCALE_TEMPLATES;
29     private final static char[] DOUBLE_QUOTES = "''".toCharArray(); //$NON-NLS-1$
30
private final static char[] SINGLE_QUOTE = "'".toCharArray(); //$NON-NLS-1$
31

32 public DefaultProblemFactory() {
33     this(Locale.getDefault());
34 }
35 /**
36  * @param loc the locale used to get the right message
37  */

38 public DefaultProblemFactory(Locale JavaDoc loc) {
39     setLocale(loc);
40 }
41 /**
42  * Answer a new IProblem created according to the parameters value
43  * <ul>
44  * <li>originatingFileName the name of the file name from which the problem is originated
45  * <li>problemId the problem id
46  * <li>problemArguments the fully qualified arguments recorded inside the problem
47  * <li>messageArguments the arguments needed to set the error message (shorter names than problemArguments ones)
48  * <li>severity the severity of the problem
49  * <li>startPosition the starting position of the problem
50  * <li>endPosition the end position of the problem
51  * <li>lineNumber the line on which the problem occured
52  * </ul>
53  * @param originatingFileName char[]
54  * @param problemId int
55  * @param problemArguments String[]
56  * @param messageArguments String[]
57  * @param severity int
58  * @param startPosition int
59  * @param endPosition int
60  * @param lineNumber int
61  * @return CategorizedProblem
62  */

63 public CategorizedProblem createProblem(
64     char[] originatingFileName,
65     int problemId,
66     String JavaDoc[] problemArguments,
67     String JavaDoc[] messageArguments,
68     int severity,
69     int startPosition,
70     int endPosition,
71     int lineNumber,
72     int columnNumber) {
73
74     return new DefaultProblem(
75         originatingFileName,
76         this.getLocalizedMessage(problemId, messageArguments),
77         problemId,
78         problemArguments,
79         severity,
80         startPosition,
81         endPosition,
82         lineNumber,
83         columnNumber);
84 }
85 private final static int keyFromID(int id) {
86     return id + 1; // keys are offsetted by one in table, since it cannot handle 0 key
87
}
88 /**
89  * Answer the locale used to retrieve the error messages
90  * @return java.util.Locale
91  */

92 public Locale JavaDoc getLocale() {
93     return this.locale;
94 }
95 public void setLocale(Locale JavaDoc locale) {
96     if (locale == this.locale) return;
97     this.locale = locale;
98     if (Locale.getDefault().equals(locale)){
99         if (DEFAULT_LOCALE_TEMPLATES == null){
100             DEFAULT_LOCALE_TEMPLATES = loadMessageTemplates(locale);
101         }
102         this.messageTemplates = DEFAULT_LOCALE_TEMPLATES;
103     } else {
104         this.messageTemplates = loadMessageTemplates(locale);
105     }
106 }
107
108 public final String JavaDoc getLocalizedMessage(int id, String JavaDoc[] problemArguments) {
109     String JavaDoc message = (String JavaDoc) this.messageTemplates.get(keyFromID(id & IProblem.IgnoreCategoriesMask));
110     if (message == null) {
111         return "Unable to retrieve the error message for problem id: " //$NON-NLS-1$
112
+ (id & IProblem.IgnoreCategoriesMask)
113             + ". Check compiler resources."; //$NON-NLS-1$
114
}
115
116     // for compatibility with MessageFormat which eliminates double quotes in original message
117
char[] messageWithNoDoubleQuotes =
118         CharOperation.replace(message.toCharArray(), DOUBLE_QUOTES, SINGLE_QUOTE);
119
120     if (problemArguments == null) return new String JavaDoc(messageWithNoDoubleQuotes);
121
122     int length = messageWithNoDoubleQuotes.length;
123     int start = 0;
124     int end = length;
125     StringBuffer JavaDoc output = null;
126     if ((id & IProblem.Javadoc) != 0) {
127         output = new StringBuffer JavaDoc(10+length+problemArguments.length*20);
128         output.append((String JavaDoc) this.messageTemplates.get(keyFromID(IProblem.JavadocMessagePrefix & IProblem.IgnoreCategoriesMask)));
129     }
130     while (true) {
131         if ((end = CharOperation.indexOf('{', messageWithNoDoubleQuotes, start)) > -1) {
132             if (output == null) output = new StringBuffer JavaDoc(length+problemArguments.length*20);
133             output.append(messageWithNoDoubleQuotes, start, end - start);
134             if ((start = CharOperation.indexOf('}', messageWithNoDoubleQuotes, end + 1)) > -1) {
135                 int index = -1;
136                 String JavaDoc argId = new String JavaDoc(messageWithNoDoubleQuotes, end + 1, start - end - 1);
137                 try {
138                     index = Integer.parseInt(argId);
139                     output.append(problemArguments[index]);
140                 } catch (NumberFormatException JavaDoc nfe) {
141                     output.append(messageWithNoDoubleQuotes, end + 1, start - end);
142                 } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
143                     return "Cannot bind message for problem (id: " //$NON-NLS-1$
144
+ (id & IProblem.IgnoreCategoriesMask)
145                         + ") \"" //$NON-NLS-1$
146
+ message
147                         + "\" with arguments: {" //$NON-NLS-1$
148
+ Util.toString(problemArguments)
149                         +"}"; //$NON-NLS-1$
150
}
151                 start++;
152             } else {
153                 output.append(messageWithNoDoubleQuotes, end, length);
154                 break;
155             }
156         } else {
157             if (output == null) return new String JavaDoc(messageWithNoDoubleQuotes);
158             output.append(messageWithNoDoubleQuotes, start, length - start);
159             break;
160         }
161     }
162
163     // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=120410
164
return new String JavaDoc(output.toString());
165 }
166 /**
167  * @param problem CategorizedProblem
168  * @return String
169  */

170 public final String JavaDoc localizedMessage(CategorizedProblem problem) {
171     return getLocalizedMessage(problem.getID(), problem.getArguments());
172 }
173
174 /**
175  * This method initializes the MessageTemplates class variable according
176  * to the current Locale.
177  * @param loc Locale
178  * @return HashtableOfInt
179  */

180 public static HashtableOfInt loadMessageTemplates(Locale JavaDoc loc) {
181     ResourceBundle JavaDoc bundle = null;
182     String JavaDoc bundleName = "org.eclipse.jdt.internal.compiler.problem.messages"; //$NON-NLS-1$
183
try {
184         bundle = ResourceBundle.getBundle(bundleName, loc);
185     } catch(MissingResourceException JavaDoc e) {
186         System.out.println("Missing resource : " + bundleName.replace('.', '/') + ".properties for locale " + loc); //$NON-NLS-1$//$NON-NLS-2$
187
throw e;
188     }
189     HashtableOfInt templates = new HashtableOfInt(700);
190     Enumeration JavaDoc keys = bundle.getKeys();
191     while (keys.hasMoreElements()) {
192         String JavaDoc key = (String JavaDoc)keys.nextElement();
193         try {
194             int messageID = Integer.parseInt(key);
195             templates.put(keyFromID(messageID), bundle.getString(key));
196         } catch(NumberFormatException JavaDoc e) {
197             // key ill-formed
198
} catch (MissingResourceException JavaDoc e) {
199             // available ID
200
}
201     }
202     return templates;
203 }
204
205 }
206
Popular Tags