1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.dbforms.config;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.dbforms.util.StringUtil;
28
29 import java.util.Hashtable;
30 import java.util.Vector;
31
32 import javax.servlet.ServletConfig;
33 import javax.servlet.ServletContext;
34
35
36
37 /***
38 * <p>
39 * This class gets populated with data from the dbforms-errors.xml file by the
40 * ConfigServlet. This class is a kind of "single point of entry" for error
41 * messages: it contains the definitions of error message id's, descriptions,
42 * severity etc.
43 * </p>
44 *
45 * @author Philip Grunikiewicz
46 */
47 public class DbFormsErrors {
48 private static Log logCat = LogFactory.getLog(DbFormsErrors.class.getName());
49
50 /*** DOCUMENT ME! */
51 public static final String ERRORS = "dbformsErrors";
52
53 /*** DOCUMENT ME! */
54 public static final char PARAMETER_DELIMITER = '%';
55 private Hashtable errorIDHash;
56 private ServletConfig servletConfig;
57
58 /***
59 * Creates a new DbFormsErrors object.
60 */
61 public DbFormsErrors() {
62 logCat.info("Create instance of DbFormsErrors");
63 errorIDHash = new Hashtable();
64 }
65
66 /***
67 * DOCUMENT ME!
68 *
69 * @param id DOCUMENT ME!
70 *
71 * @return DOCUMENT ME!
72 */
73 public Error getErrorById(String id) {
74 return (Error) errorIDHash.get(id);
75 }
76
77
78 /***
79 * DOCUMENT ME!
80 *
81 * @param servletConfig DOCUMENT ME!
82 */
83 public void setServletConfig(ServletConfig servletConfig) {
84 this.servletConfig = servletConfig;
85 }
86
87
88 /***
89 * get access to configuration of config servlet
90 *
91 * @return DOCUMENT ME!
92 */
93 public ServletConfig getServletConfig() {
94 return servletConfig;
95 }
96
97
98 /***
99 * get access to servlet context in order to interoperate with other
100 * components of the web application
101 *
102 * @return DOCUMENT ME!
103 */
104 public ServletContext getServletContext() {
105 return servletConfig.getServletContext();
106 }
107
108
109 /***
110 * DOCUMENT ME!
111 *
112 * @param message DOCUMENT ME!
113 *
114 * @return DOCUMENT ME!
115 */
116 public String getXMLErrorMessage(String message) {
117 String langCode = null;
118 String language = null;
119 String errorCode = null;
120 String paramList = null;
121 org.dbforms.config.Error anError = null;
122 String xmlMessage = null;
123
124
125 if ((message == null) || (message.trim().length() == 0)) {
126 return null;
127 }
128
129
130
131
132 try {
133 langCode = getEmbeddedStringForErrors(message, 0, ':');
134 language = getEmbeddedStringForErrors(langCode, 0, '-');
135 errorCode = getEmbeddedStringForErrors(langCode, 1, '-');
136 paramList = getEmbeddedStringForErrors(message, 1, ':');
137 } catch (Exception e) {
138
139 logCat.error("Not in proper format - do not try to convert!");
140 }
141
142
143 if (errorCode != null) {
144
145 if (errorCode.trim().length() == 0) {
146 return "";
147 }
148
149 anError = getErrorById(errorCode);
150 }
151
152
153 if (anError != null) {
154 xmlMessage = anError.getMessage(language);
155
156 if (xmlMessage != null) {
157
158 xmlMessage = insertParametersInString(xmlMessage, paramList);
159 } else {
160 xmlMessage = "No message defined! - check dbForms-error.xml";
161 }
162
163 return xmlMessage;
164 }
165
166
167
168 return message;
169 }
170
171
172 /***
173 * DOCUMENT ME!
174 *
175 * @param error DOCUMENT ME!
176 */
177 public void addError(Error error) {
178 logCat.info("error added: " + error);
179 errorIDHash.put(error.getId(), error);
180 }
181
182
183 /***
184 * <p>
185 * Method for parsing substring embedded by constant delimeters
186 * </p>
187 *
188 * <p>
189 * consider the following string s: English-001:param1, param2
190 * </p>
191 *
192 * <p>
193 * <pre>
194 * </pre>
195 * </p>
196 *
197 * @param str DOCUMENT ME!
198 * @param afterDelims DOCUMENT ME!
199 * @param delim DOCUMENT ME!
200 *
201 * @return DOCUMENT ME!
202 */
203 private String getEmbeddedStringForErrors(String str, int afterDelims,
204 char delim) {
205 int lastIndex = 0;
206
207 for (int i = 0; i < afterDelims; i++) {
208 lastIndex = str.indexOf(delim, lastIndex) + 1;
209 }
210
211 int nextIndex = str.indexOf(delim, lastIndex);
212
213 if (nextIndex == -1) {
214 nextIndex = str.length();
215 }
216
217 return str.substring(lastIndex, nextIndex);
218 }
219
220
221 /***
222 * Grunikiewicz.philip 2001-12-04 Replace placeholders by parameters Make
223 * sure that parameters required = parameters provided!
224 *
225 * @param xmlMessage DOCUMENT ME!
226 * @param paramList DOCUMENT ME!
227 *
228 * @return DOCUMENT ME!
229 */
230 private String insertParametersInString(String xmlMessage, String paramList) {
231
232 int pos = xmlMessage.indexOf(PARAMETER_DELIMITER);
233
234 if ((pos < 0) || (paramList == null)) {
235 return xmlMessage;
236 }
237
238
239 Vector v = StringUtil.splitString(paramList, ",");
240 int count = 0;
241
242 while (pos >= 0) {
243
244 String prefix = xmlMessage.substring(0, pos);
245 String suffix = xmlMessage.substring(pos + 1);
246 xmlMessage = prefix + (String) v.elementAt(count);
247 xmlMessage += suffix;
248 pos = xmlMessage.indexOf(PARAMETER_DELIMITER);
249 count++;
250 }
251
252 return xmlMessage;
253 }
254 }