1 21 22 package com.coi.tools.os.win; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.MissingResourceException ; 28 import java.util.ResourceBundle ; 29 30 38 public class NativeLibException extends Exception 39 { 40 41 private static final long serialVersionUID = 3257002172494721080L; 42 43 44 private final static HashMap messageResourceBundles = new HashMap (); 45 46 47 private int libErr; 48 49 50 private int osErr; 51 52 53 private String libErrString; 54 55 56 private String osErrString; 57 58 59 private ArrayList args = new ArrayList (); 60 61 static 62 { 63 addResourceBundle("com.coi.tools.os.win.resources.NativeLibErr"); 65 } 66 67 77 public static void addResourceBundle(String bundlePath) 78 { 79 ResourceBundle bd = null; 80 if (messageResourceBundles.containsKey(bundlePath)) return; 81 try 82 { 83 bd = ResourceBundle.getBundle(bundlePath); 84 } 85 catch (MissingResourceException mre) 86 { 87 mre.printStackTrace(); 88 } 89 messageResourceBundles.put(bundlePath, bd); 90 91 } 92 93 96 public NativeLibException() 97 { 98 super(); 99 } 100 101 106 public NativeLibException(String message) 107 { 108 super(message); 109 } 110 111 116 public NativeLibException(Throwable cause) 117 { 118 super(cause); 119 } 120 121 127 public NativeLibException(String message, Throwable cause) 128 { 129 super(message, cause); 130 } 131 132 140 public NativeLibException(int libErr, int osErr, String libString, String osString) 141 { 142 super(); 143 this.libErr = libErr; 144 this.osErr = osErr; 145 libErrString = libString; 146 osErrString = osString; 147 } 148 149 154 public String getMessage() 155 { 156 StringBuffer retval = new StringBuffer (); 157 boolean next = false; 158 if (libErrString != null) 159 { 160 retval.append(getLocalizedLibMessage()); 161 next = true; 162 } 163 else if (libErr != 0) 164 { 165 if (next) retval.append("\n"); 166 next = true; 167 retval.append(getMsg("libErrNumber." + Integer.toString(libErr))); 168 } 169 if (osErr != 0) 170 { 171 if (next) retval.append("\n"); 172 next = true; 173 retval.append(getMsg("libInternal.OsErrNumPraefix")).append(Integer.toString(osErr)); 174 } 175 if (osErrString != null) 176 { 177 if (next) retval.append("\n"); 178 next = true; 179 retval.append(getMsg("libInternal.OsErrStringPraefix")).append(getOsMessage()); 181 } 182 if (retval.length() > 0) return (reviseMsgWithArgs(retval.toString())); 183 return null; 184 } 185 186 191 public int getLibErr() 192 { 193 return libErr; 194 } 195 196 201 public String getLibMessage() 202 { 203 return libErrString; 204 } 205 206 211 public String getLocalizedLibMessage() 212 { 213 return (getMsg(libErrString)); 214 } 215 216 221 public int getOsErr() 222 { 223 return (osErr); 224 } 225 226 231 public String getOsMessage() 232 { 233 return (osErrString); 234 } 235 236 241 public void addArgument(String arg) 242 { 243 args.add(arg); 244 } 245 246 251 public ArrayList getArguments() 252 { 253 return (args); 254 } 255 256 261 public String reviseMsgWithArgs(String msg) 262 { 263 for (int i = 0; i < args.size(); ++i) 264 { 265 String key = "{" + Integer.toString(i) + "}"; 266 msg = replaceString(msg, key, (String ) args.get(i)); 267 } 268 return (msg); 269 } 270 271 277 278 private String getMsg(String s) 279 { 280 Iterator it = messageResourceBundles.values().iterator(); 281 while (it.hasNext()) 282 { 283 try 284 { 285 return (((ResourceBundle ) it.next()).getString(s)); 286 } 287 catch (MissingResourceException missingresourceexception) 288 { ; 290 } 291 } 292 return (s); 293 } 294 295 305 private static String replaceString(String destination, String what, String with) 306 { 307 if (destination.indexOf(what) >= 0) 308 { StringBuffer buf = new StringBuffer (); 311 int last = 0; 312 int current = destination.indexOf(what); 313 int whatLength = what.length(); 314 while (current >= 0) 315 { if (current > 0) buf.append(destination.substring(last, current)); 317 buf.append(with); 318 last = current + whatLength; 319 current = destination.indexOf(what, last); 320 } 321 if (destination.length() > last) buf.append(destination.substring(last)); 322 return buf.toString(); 323 } 324 return destination; 325 } 326 327 } 328 | Popular Tags |