KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > repopulation > ErrorFormMap


1 /*
2  * Copyright (C) 2003 Diez B. Roggisch [deets@web.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: ErrorFormMap.java,v 1.3 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.dbroggisch.repopulation;
21
22 import org.enhydra.barracuda.core.comp.*;
23 import org.enhydra.barracuda.core.forms.*;
24 import org.enhydra.barracuda.core.forms.validators.*;
25 import org.enhydra.barracuda.core.comp.model.*;
26 import org.apache.log4j.*;
27 import java.util.*;
28
29 /**
30  * @author Diez B. Roggisch <diez.roggisch@artnology.com>
31  * This class eases the population of errors resulting from form-validation.
32  * This is done by catching raised exceptions and if their sources are
33  * FormElements, a mapping is made which consists of the static ERROR_PREFIX
34  * and the FormElements name to a BComponent.<p>
35  * If the Exception has set a text, the mapped component is a BText-component
36  * with that text. Otherwise it's just a BComponent.<p>
37  * When using this class, you should do two things:<p>
38  * <ol>
39  * <li>Subclass it and implement the TemplateModel-interface</li>
40  * <li>Add ERROR_PREFIX + Element-names directives to your HTML-code, e.g <b>Dir::Get_Data.ModelName.e_Password</b></li>
41  * </ol>
42  * When processing the BTemplate, these directives will return null for
43  * elements without errors, resulting in discarding the nodes.
44  * Or they return the associated components so the nodes stay, and text will be substituted.
45  */

46 public class ErrorFormMap extends DefaultFormMap
47 {
48     private Map errorMap = new HashMap();
49     public static String JavaDoc ERROR_PREFIX = "e_";
50     protected static final Logger logger = Logger.getLogger(ErrorFormMap.class.getName());
51
52
53     public FormMap validate(boolean deferExceptions) throws ValidationException {
54                 errorMap.clear();
55                 try {
56                         super.validate(deferExceptions);
57                 }
58                 catch(ValidationException ex) {
59                         if(logger.isDebugEnabled()) {
60                                 logger.debug("Catched ValidationException");
61                         }
62                         List e_list;
63                         if(deferExceptions) {
64                                 e_list = ex.getExceptionList();
65                         }
66                         else {
67                                 e_list = new Vector();
68                                 e_list.add(ex);
69                         }
70
71                         for(int i = 0; i < e_list.size(); i++) {
72                                 if(logger.isDebugEnabled()) {
73                                         logger.debug("Visiting Exception " + i);
74                                 }
75                                 ValidationException vex = (ValidationException)e_list.get(i);
76                                 if(vex.getSource() instanceof FormElement) {
77                                         FormElement fel = (FormElement)vex.getSource();
78                                         BComponent comp;
79
80                                         logger.debug("Element name: " + fel.getName());
81                                         if(vex.getMessage() != null && !vex.getMessage().equals("")) {
82                                                 if(logger.isDebugEnabled()) {
83                                                         logger.debug("Adding BText with " + vex.getMessage());
84                                                 }
85                                                 comp = new BText();
86                                                 ((BText)comp).setText(vex.getMessage());
87                                         }
88                                         else {
89                                                 if(logger.isDebugEnabled()) {
90                                                         logger.debug("Adding BComponent");
91                                                 }
92                                                 comp = new BComponent();
93                                         }
94                                         errorMap.put(ERROR_PREFIX + fel.getKey(), comp);
95                                 }
96                         }
97                         throw ex;
98                 }
99                 return this;
100     }
101
102
103     /** If you want errors from whole-form-validation to be populated, simply use this as a
104      * convenient method to add mappings.
105      */

106     public void addErrorComponent(String JavaDoc key, BComponent comp) {
107                 errorMap.put(key, comp);
108     }
109
110     /** In your subclasses TemplateModel-interface call this.
111      */

112     public Object JavaDoc getErrorComponent(String JavaDoc key)
113     {
114                 if(errorMap.containsKey(key)){
115                         return errorMap.get(key);
116                 }
117                 return null;
118     }
119 }
120
Popular Tags