View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/config/DbFormsErrors.java,v 1.7 2004/10/20 10:51:26 hkollmann Exp $
3    * $Revision: 1.7 $
4    * $Date: 2004/10/20 10:51:26 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
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()); // logging category for this class
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; // for quicker lookup by ID
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       // If message is empty, return immediately
125       if ((message == null) || (message.trim().length() == 0)) {
126          return null;
127       }
128 
129       // Extract Language , Error Code ID, and parameters
130       // ie: English-001:FieldName,200
131       // ie: ORA-00001:Oracle Message
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       // Reference to listing of predefined errors (xml file)
143       if (errorCode != null) {
144          // Error does not contain an id, ignore!				
145          if (errorCode.trim().length() == 0) {
146             return "";
147          }
148 
149          anError = getErrorById(errorCode);
150       }
151 
152       // Get message associated to language
153       if (anError != null) {
154          xmlMessage = anError.getMessage(language);
155 
156          if (xmlMessage != null) {
157             // Replace placeholder with supplied parameters
158             xmlMessage = insertParametersInString(xmlMessage, paramList);
159          } else {
160             xmlMessage = "No message defined! - check dbForms-error.xml";
161          }
162 
163          return xmlMessage;
164       }
165 
166       // An error has occured, 
167       // however a custom error messages is not available		
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; // search end of cutting
209       }
210 
211       int nextIndex = str.indexOf(delim, lastIndex); // end of cutting
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       // Check if their are parameters expected
232       int pos = xmlMessage.indexOf(PARAMETER_DELIMITER);
233 
234       if ((pos < 0) || (paramList == null)) {
235          return xmlMessage; // No parameters found!
236       }
237 
238       // Sort parameter list
239       Vector v     = StringUtil.splitString(paramList, ",");
240       int    count = 0;
241 
242       while (pos >= 0) {
243          // Replace
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 }