View Javadoc

1   // ////////////////////////////////////////////////////////////////////////////
2   // dexterIM - Instant Messaging Framework
3   // Copyright (C) 2003 Christoph Walcher
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or
8   // (at your option) any later version.
9   //
10  // This program is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  // GNU General Public License for more details.
14  //
15  // You should have received a copy of the GNU General Public License
16  // along with this program; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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  }