1 11 package org.eclipse.jdt.internal.compiler.problem; 12 13 import java.util.Enumeration ; 14 import java.util.Locale ; 15 import java.util.MissingResourceException ; 16 import java.util.ResourceBundle ; 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 locale; 28 private static HashtableOfInt DEFAULT_LOCALE_TEMPLATES; 29 private final static char[] DOUBLE_QUOTES = "''".toCharArray(); private final static char[] SINGLE_QUOTE = "'".toCharArray(); 32 public DefaultProblemFactory() { 33 this(Locale.getDefault()); 34 } 35 38 public DefaultProblemFactory(Locale loc) { 39 setLocale(loc); 40 } 41 63 public CategorizedProblem createProblem( 64 char[] originatingFileName, 65 int problemId, 66 String [] problemArguments, 67 String [] 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; } 88 92 public Locale getLocale() { 93 return this.locale; 94 } 95 public void setLocale(Locale 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 getLocalizedMessage(int id, String [] problemArguments) { 109 String message = (String ) this.messageTemplates.get(keyFromID(id & IProblem.IgnoreCategoriesMask)); 110 if (message == null) { 111 return "Unable to retrieve the error message for problem id: " + (id & IProblem.IgnoreCategoriesMask) 113 + ". Check compiler resources."; } 115 116 char[] messageWithNoDoubleQuotes = 118 CharOperation.replace(message.toCharArray(), DOUBLE_QUOTES, SINGLE_QUOTE); 119 120 if (problemArguments == null) return new String (messageWithNoDoubleQuotes); 121 122 int length = messageWithNoDoubleQuotes.length; 123 int start = 0; 124 int end = length; 125 StringBuffer output = null; 126 if ((id & IProblem.Javadoc) != 0) { 127 output = new StringBuffer (10+length+problemArguments.length*20); 128 output.append((String ) 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 (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 argId = new String (messageWithNoDoubleQuotes, end + 1, start - end - 1); 137 try { 138 index = Integer.parseInt(argId); 139 output.append(problemArguments[index]); 140 } catch (NumberFormatException nfe) { 141 output.append(messageWithNoDoubleQuotes, end + 1, start - end); 142 } catch (ArrayIndexOutOfBoundsException e) { 143 return "Cannot bind message for problem (id: " + (id & IProblem.IgnoreCategoriesMask) 145 + ") \"" + message 147 + "\" with arguments: {" + Util.toString(problemArguments) 149 +"}"; } 151 start++; 152 } else { 153 output.append(messageWithNoDoubleQuotes, end, length); 154 break; 155 } 156 } else { 157 if (output == null) return new String (messageWithNoDoubleQuotes); 158 output.append(messageWithNoDoubleQuotes, start, length - start); 159 break; 160 } 161 } 162 163 return new String (output.toString()); 165 } 166 170 public final String localizedMessage(CategorizedProblem problem) { 171 return getLocalizedMessage(problem.getID(), problem.getArguments()); 172 } 173 174 180 public static HashtableOfInt loadMessageTemplates(Locale loc) { 181 ResourceBundle bundle = null; 182 String bundleName = "org.eclipse.jdt.internal.compiler.problem.messages"; try { 184 bundle = ResourceBundle.getBundle(bundleName, loc); 185 } catch(MissingResourceException e) { 186 System.out.println("Missing resource : " + bundleName.replace('.', '/') + ".properties for locale " + loc); throw e; 188 } 189 HashtableOfInt templates = new HashtableOfInt(700); 190 Enumeration keys = bundle.getKeys(); 191 while (keys.hasMoreElements()) { 192 String key = (String )keys.nextElement(); 193 try { 194 int messageID = Integer.parseInt(key); 195 templates.put(keyFromID(messageID), bundle.getString(key)); 196 } catch(NumberFormatException e) { 197 } catch (MissingResourceException e) { 199 } 201 } 202 return templates; 203 } 204 205 } 206 | Popular Tags |