8809e158558c7649f471ef9815d468af0dc8e81a
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / problem / DefaultProblemFactory.java
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
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.IProblem;
18 import net.sourceforge.phpdt.internal.compiler.IProblemFactory;
19 import net.sourceforge.phpdt.internal.compiler.util.CharOperation;
20
21 public class DefaultProblemFactory implements IProblemFactory {
22
23
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$
29
30 /**
31  * @param loc the locale used to get the right message
32  */
33 public DefaultProblemFactory(Locale loc) {
34         this.locale = loc;
35         if (Locale.getDefault().equals(loc)){
36                 if (DEFAULT_LOCALE_TEMPLATES == null){
37                         DEFAULT_LOCALE_TEMPLATES = loadMessageTemplates(loc);
38                 }
39                 this.messageTemplates = DEFAULT_LOCALE_TEMPLATES;
40         } else {
41                 this.messageTemplates = loadMessageTemplates(loc);
42         }
43 }
44 /**
45  * Answer a new IProblem created according to the parameters value
46  * <ul>
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
54  * </ul>
55  * @param originatingFileName char[]
56  * @param problemId int
57  * @param arguments String[]
58  * @param severity int
59  * @param startPosition int
60  * @param endPosition int
61  * @param lineNumber int
62  * @return org.eclipse.jdt.internal.compiler.IProblem
63  */
64 public IProblem createProblem(
65         char[] originatingFileName, 
66         int problemId, 
67         String[] arguments, 
68         int severity, 
69         int startPosition, 
70         int endPosition, 
71         int lineNumber) {
72
73         return new DefaultProblem(
74                 originatingFileName, 
75                 this.getLocalizedMessage(problemId, arguments),
76                 problemId, 
77                 arguments, 
78                 severity, 
79                 startPosition, 
80                 endPosition, 
81                 lineNumber); 
82 }
83 /**
84  * Answer the locale used to retrieve the error messages
85  * @return java.util.Locale
86  */
87 public Locale getLocale() {
88         return locale;
89 }
90 public final String getLocalizedMessage(int id, String[] problemArguments) {
91         StringBuffer output = new StringBuffer(80);
92         String message = 
93                 messageTemplates[(id & IProblem.IgnoreCategoriesMask)]; 
94         if (message == null) {
95                 return "Unable to retrieve the error message for problem id: " //$NON-NLS-1$
96                         + id
97                         + ". Check compiler resources.";  //$NON-NLS-1$
98         }
99
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);
104
105         int length = message.length();
106         int start = -1, end = length;
107         while (true) {
108                 if ((end = message.indexOf('{', start)) > -1) {
109                         output.append(message.substring(start + 1, end));
110                         if ((start = message.indexOf('}', end)) > -1) {
111                                 try {
112                                         output.append(
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$
120                                 }
121                         } else {
122                                 output.append(message.substring(end, length));
123                                 break;
124                         }
125                 } else {
126                         output.append(message.substring(start + 1, length));
127                         break;
128                 }
129         }
130         return output.toString();
131 }
132 /**
133  * @param problem org.eclipse.jdt.internal.compiler.IProblem
134  * @return String
135  */
136 public final String localizedMessage(IProblem problem) {
137         return getLocalizedMessage(problem.getID(), problem.getArguments());
138 }
139
140 /**
141  * This method initializes the MessageTemplates class variable according
142  * to the current Locale.
143  */
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++) {
148                 try {
149                         templates[i] = bundle.getString(String.valueOf(i));
150                 } catch (MissingResourceException e) {
151                         // available ID
152                 }
153         }
154         return templates;
155 }
156
157 public DefaultProblemFactory() {
158         this(Locale.getDefault());
159 }
160 }