1 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 47 public class DbFormsErrors { 48 private static Log logCat = LogFactory.getLog(DbFormsErrors.class.getName()); 50 51 public static final String ERRORS = "dbformsErrors"; 52 53 54 public static final char PARAMETER_DELIMITER = '%'; 55 private Hashtable errorIDHash; private ServletConfig servletConfig; 57 58 61 public DbFormsErrors() { 62 logCat.info("Create instance of DbFormsErrors"); 63 errorIDHash = new Hashtable (); 64 } 65 66 73 public Error getErrorById(String id) { 74 return (Error ) errorIDHash.get(id); 75 } 76 77 78 83 public void setServletConfig(ServletConfig servletConfig) { 84 this.servletConfig = servletConfig; 85 } 86 87 88 93 public ServletConfig getServletConfig() { 94 return servletConfig; 95 } 96 97 98 104 public ServletContext getServletContext() { 105 return servletConfig.getServletContext(); 106 } 107 108 109 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 == null) || (message.trim().length() == 0)) { 126 return null; 127 } 128 129 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 logCat.error("Not in proper format - do not try to convert!"); 140 } 141 142 if (errorCode != null) { 144 if (errorCode.trim().length() == 0) { 146 return ""; 147 } 148 149 anError = getErrorById(errorCode); 150 } 151 152 if (anError != null) { 154 xmlMessage = anError.getMessage(language); 155 156 if (xmlMessage != null) { 157 xmlMessage = insertParametersInString(xmlMessage, paramList); 159 } else { 160 xmlMessage = "No message defined! - check dbForms-error.xml"; 161 } 162 163 return xmlMessage; 164 } 165 166 return message; 169 } 170 171 172 177 public void addError(Error error) { 178 logCat.info("error added: " + error); 179 errorIDHash.put(error.getId(), error); 180 } 181 182 183 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; } 210 211 int nextIndex = str.indexOf(delim, lastIndex); 213 if (nextIndex == -1) { 214 nextIndex = str.length(); 215 } 216 217 return str.substring(lastIndex, nextIndex); 218 } 219 220 221 230 private String insertParametersInString(String xmlMessage, String paramList) { 231 int pos = xmlMessage.indexOf(PARAMETER_DELIMITER); 233 234 if ((pos < 0) || (paramList == null)) { 235 return xmlMessage; } 237 238 Vector v = StringUtil.splitString(paramList, ","); 240 int count = 0; 241 242 while (pos >= 0) { 243 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 } 255 | Popular Tags |