1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.dexterim.oscar.io;
20
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.Map;
25
26 import org.jdom.Document;
27 import org.jdom.Element;
28 import org.jdom.JDOMException;
29 import org.jdom.input.SAXBuilder;
30 import org.xml.sax.InputSource;
31
32 /***
33 * @author christoph
34 *
35 */
36 public class ErrorCodeFactory {
37
38 private static ErrorCodeFactory instance;
39 private Map codes = new HashMap();
40 private String UNKNOWN = "Unknown Error Code";
41
42 private ErrorCodeFactory() {
43 Document doc;
44 try {
45 doc = new SAXBuilder(false).build(new InputSource(getClass()
46 .getResourceAsStream("errorcodes.xml")));
47
48 Element errorCodes = doc.getRootElement();
49
50 for (Iterator iter = errorCodes.getChildren().iterator(); iter.hasNext();) {
51 Element element = (Element) iter.next();
52
53 int code = element.getAttribute("code").getIntValue();
54 String message = element.getTextTrim();
55 codes.put(new Integer(code), new ErrorCode(code, message));
56 }
57 }
58 catch (JDOMException e1) {
59 e1.printStackTrace();
60 }
61 catch (IOException e1) {
62 e1.printStackTrace();
63 }
64
65 }
66
67 public static synchronized ErrorCodeFactory getInstance() {
68 if (instance == null) {
69 instance = new ErrorCodeFactory();
70 }
71
72 return instance;
73 }
74
75 /***
76 * @param code
77 */
78 public ErrorCode createError(int code) {
79 Object errorCode = codes.get(new Integer(code));
80
81 if (errorCode != null && errorCode instanceof ErrorCode) {
82 return (ErrorCode)errorCode;
83 }
84 else {
85 return new ErrorCode(code, UNKNOWN);
86 }
87 }
88 }