View Javadoc

1   package pl.aislib.fm;
2   
3   import java.util.HashMap;
4   import java.util.Iterator;
5   import java.util.Map;
6   
7   import org.apache.commons.logging.Log;
8   
9   import org.xml.sax.Attributes;
10  import org.xml.sax.SAXException;
11  
12  import pl.aislib.fm.forms.config.Handler;
13  
14  /***
15   * XML handler for handling framework's forms.
16   * 
17   * @author <a href="mailto:wswiatek@ais.pl">Wojciech Swiatek</a>, AIS.PL
18   * @version $Revision: 1.2 $
19   */
20  public class FormsHandler extends Handler {
21  
22    /***
23     * Map of forms.
24     */
25    protected Map forms = new HashMap();
26  
27    /***
28     * Form being used.
29     */
30    protected Form currentForm;
31  
32  
33    // Constructors
34    
35    /***
36     * @see pl.aislib.fm.forms.config.Handler#Handler(Log)
37     */
38    public FormsHandler(Log log) {
39      super(log);
40    }
41  
42  
43    // Public methods
44  
45    /***
46     * @param formName name of a form.
47     * @param messages map of messages.
48     * @param messageGroups map of message groups.
49     * @param lang language of the form.
50     * @return the form.
51     */
52    public Form getForm(String formName, Map messages, Map messageGroups, String lang) {
53      Form form = (Form) forms.get(formName);
54      try {
55        Form result = (Form) form.clone();
56        result.messages = messages;
57        result.messageGroups = messageGroups;
58        result.lang = lang;
59        return result;
60      } catch (CloneNotSupportedException cnse) {
61        return null;
62      }
63    }
64  
65    /***
66     * @see pl.aislib.fm.forms.config.IXMLHandler#processEndElement(String, String, String)
67     */
68    public void processEndElement(String namespaceURI, String localName, String qName) throws SAXException {
69      if ("form".equals(localName)) {
70        currentForm.setConditionalFields();
71      }
72    }
73  
74    /***
75     * @see pl.aislib.fm.forms.config.IXMLHandler#processStartElement(String, String, String, Attributes)
76     */
77    public Object processStartElement(String namespaceURI, String localName, String qName, Attributes atts)
78      throws SAXException {
79      if ("form".equals(localName)) {
80        String formName = atts.getValue("name");
81        forms.put(formName, currentForm = new Form(formName));
82        currentForm.setLog(log);
83        return currentForm;
84      }
85      return localName;
86    }
87  
88    /***
89     * @see java.lang.Object#toString()
90     */
91    public String toString() {
92      StringBuffer sb = new StringBuffer();
93      for (Iterator i = forms.entrySet().iterator(); i.hasNext();) {
94        Map.Entry me = (Map.Entry) i.next();
95        String formName = (String) me.getKey();
96        Form form = (Form) me.getValue();
97        sb.append("Form: " + formName);
98        sb.append("\n");
99        sb.append("----------\n");
100       sb.append(form);
101       sb.append("\n");
102     }
103     return sb.toString();
104   }
105 
106 
107   // Protected methods
108 
109   /***
110    * @see pl.aislib.fm.forms.config.Handler#createPartialHandlers()
111    */
112   protected void createPartialHandlers() {
113     addPartialHandler("field", new FieldHandler(this));
114     addPartialHandler("rule", new RuleHandler(this));
115   }
116 
117 } // FormsHandler class