fe0896d21efed36da81c2eb896162c4d8930524e
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / problem / DefaultProblemFactory.java
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
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>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
55  * </ul>
56  * @param originatingFileName char[]
57  * @param problemId int
58  * @param arguments String[]
59  * @param severity int
60  * @param startPosition int
61  * @param endPosition int
62  * @param lineNumber int
63  * @return org.eclipse.jdt.internal.compiler.IProblem
64  */
65 public IProblem createProblem(
66         char[] originatingFileName, 
67         int problemId, 
68         String[] problemArguments, 
69         String[] messageArguments, 
70         int severity, 
71         int startPosition, 
72         int endPosition, 
73         int lineNumber) {
74
75         return new DefaultProblem(
76                 originatingFileName, 
77                 this.getLocalizedMessage(problemId, messageArguments),
78                 problemId, 
79                 problemArguments, 
80                 severity, 
81                 startPosition, 
82                 endPosition, 
83                 lineNumber); 
84 }
85 /**
86  * Answer the locale used to retrieve the error messages
87  * @return java.util.Locale
88  */
89 public Locale getLocale() {
90         return locale;
91 }
92 public final String getLocalizedMessage(int id, String[] problemArguments) {
93         StringBuffer output = new StringBuffer(80);
94         String message = 
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$
100         }
101
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);
106
107         int length = message.length();
108         int start = -1, end = length;
109         while (true) {
110                 if ((end = message.indexOf('{', start)) > -1) {
111                         output.append(message.substring(start + 1, end));
112                         if ((start = message.indexOf('}', end)) > -1) {
113                                 try {
114                                         output.append(
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$
122                                 }
123                         } else {
124                                 output.append(message.substring(end, length));
125                                 break;
126                         }
127                 } else {
128                         output.append(message.substring(start + 1, length));
129                         break;
130                 }
131         }
132         return output.toString();
133 }
134 /**
135  * @param problem org.eclipse.jdt.internal.compiler.IProblem
136  * @return String
137  */
138 public final String localizedMessage(IProblem problem) {
139         return getLocalizedMessage(problem.getID(), problem.getArguments());
140 }
141
142 /**
143  * This method initializes the MessageTemplates class variable according
144  * to the current Locale.
145  */
146 public static String[] loadMessageTemplates(Locale loc) {
147         ResourceBundle bundle = null;
148         String bundleName = "net.sourceforge.phpdt.internal.compiler.problem.messages"; //$NON-NLS-1$
149         try {
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$
153                 throw e;
154         }
155         String[] templates = new String[500];
156         for (int i = 0, max = templates.length; i < max; i++) {
157                 try {
158                         templates[i] = bundle.getString(String.valueOf(i));
159                 } catch (MissingResourceException e) {
160                         // available ID
161                 }
162         }
163         return templates;
164 }
165
166 public DefaultProblemFactory() {
167         this(Locale.getDefault());
168 }
169 }