Source code: com/wilko/jaim/ErrorTocResponse.java
1 /*
2 * (C) 2002 Paul Wilkinson wilko@users.sourceforge.net
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19
20 /*
21 * ErrorTocResponse.java
22 *
23 * Created on 4 May 2002, 14:52
24 */
25
26 package com.wilko.jaim;
27
28 import java.util.MissingResourceException;
29
30 /** This TOC response is sent to a {@link JaimEventListener } when an error message is received from the TOC server
31 *
32 * @author paulw
33 * @version $Revision: 1.7 $
34 */
35 public class ErrorTocResponse extends TocResponse implements TocResponseHandler {
36
37 int errorCode;
38 String errorText;
39
40 public static final String RESPONSE_TYPE="ERROR";
41
42 /** Creates new ErrorTocResponse */
43 public ErrorTocResponse() {
44 errorCode=0;
45 errorText="";
46 }
47
48
49 /** Parse the error response string sent by the TOC server
50 * @param str The error response string
51 */
52 public TocResponse parseString(String str)
53 {
54 ErrorTocResponse tr=new ErrorTocResponse();
55 tr.doParse(str);
56 return(tr);
57 }
58
59 private void doParse(String str)
60 {
61
62 cmd=str;
63 int colonPos=str.indexOf(':');
64 if (colonPos!=-1)
65 {
66 str=str.substring(colonPos+1);
67 colonPos=str.indexOf(':');
68 if (colonPos!=-1)
69 {
70 errorCode=Integer.parseInt(str.substring(0,colonPos));
71 errorText=str.substring(colonPos+1);
72 }
73 else
74 {
75 errorCode=Integer.parseInt(str);
76 }
77 }
78
79 }
80
81 /** Obtain the error code for this response
82 * @return The error code
83 */
84 public int getErrorCode()
85 {
86 return(errorCode);
87 }
88
89 /** Get the error text (if any) associated with this error response
90 * @return The error text
91 */
92 public String getErrorText()
93 {
94 return(errorText);
95 }
96
97
98 /** Obtain the error message that corresponds to this error.
99 * @return The error text with any applicable error argument text inserted
100 */
101 public String getErrorDescription() {
102 try {
103 StringBuffer desc=new StringBuffer(java.util.ResourceBundle.getBundle("com/wilko/jaim/TocErrorDescriptions").getString(Integer.toString(errorCode)));
104 String sDesc=desc.toString();
105 int argpos=sDesc.indexOf("%s");
106 if (argpos != -1) {
107 desc.replace(argpos,argpos+1,errorText);
108 }
109 return(desc.toString());
110
111 }
112 catch (MissingResourceException e) {
113 return("Unable to locate error description:"+e.toString());
114 }
115 }
116
117 /** Obtain the error message that corresponds to the specified error code
118 * @param code The error code
119 * @return The error text
120 */
121 static public String getErrorDescription(int code)
122 {
123 try
124 {
125 return(java.util.ResourceBundle.getBundle("com/wilko/jaim/TocErrorDescriptions").getString(Integer.toString(code)));
126 }
127 catch (MissingResourceException e)
128 {
129 return("Unable to locate error description:"+e.toString());
130 }
131 }
132
133 public String getResponseType() {
134 return RESPONSE_TYPE;
135 }
136
137 /** Returns true if this response handler can handle the specified response.
138 * @param Response - the response string from TOC. This is the part of the response before the first ':'
139 * @return true if the response can be handled
140 */
141 public boolean canHandle(String Response) {
142 return(Response.equalsIgnoreCase(RESPONSE_TYPE));
143 }
144
145 }