KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > sam > xmlform > XmlFormViewHandler


1 /*
2  * Copyright (C) 2003 Stefan Armbruster [stefan@armbruster-it.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: XmlFormViewHandler.java,v 1.3 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.sam.xmlform;
21
22 import java.util.*;
23 import javax.servlet.http.*;
24
25 import org.w3c.dom.*;
26 import org.w3c.dom.html.*;
27
28 import org.enhydra.barracuda.core.comp.*;
29 import org.enhydra.barracuda.contrib.sam.xmlform.dtd.*;
30 import org.enhydra.barracuda.contrib.sam.xmlform.dtd.Element;
31 import org.enhydra.barracuda.contrib.sam.xmlform.dtd.Text;
32 import org.apache.log4j.*;
33
34 /** extends ModifyDomViewHandler in order to automatically creation of a XML Form
35  * based view.
36  */

37 public abstract class XmlFormViewHandler extends ModifyDomViewHandler {
38
39     protected static Logger logger = Logger.getLogger(XmlFormViewHandler.class.getName());
40
41     /** overwrite in subclasses, must return a XmlFormMap defining the fields of the form */
42     protected abstract XmlFormMap getXmlForm(ViewContext vc);
43
44     /**
45      * needed because getTemplateModels does not have a context
46      */

47     public Object JavaDoc getTemplateModels(ViewContext vc) {
48         List models = new Vector();
49         XmlFormMap xfm = getXmlForm(vc);
50         models.add(xfm);
51
52         List cModels = xfm.getChildModels();
53         for(Iterator it = cModels.iterator(); it.hasNext();) {
54             models.add((TemplateModel)it.next());
55         }
56
57         return models;
58     }
59
60     /**
61      * helper function for inserting a input element into the dom
62      */

63     private void insertIntoDOM(Node parent, Node node, boolean useInsertBefore, Node insert) {
64         if (useInsertBefore) parent.insertBefore( node, insert);
65         else parent.appendChild(node);
66     }
67
68     /** change the given Document, copy the templates of the various form types
69      * as defined in the XmlFormMap */

70     protected void modifyDOM(Document page, ViewContext vc) {
71         HttpServletRequest request = vc.getRequest();
72         HttpSession session = request.getSession();
73         XmlFormMap xmlForm = getXmlForm(vc);
74
75         org.w3c.dom.Element JavaDoc textNode = null;
76         HTMLElement textNameNode = null;
77         HTMLElement textLabelNode = null;
78         HTMLInputElement textInputNode = null;
79         org.w3c.dom.Element JavaDoc selectNode = null;
80         HTMLElement selectNameNode = null;
81         HTMLElement selectLabelNode = null;
82         HTMLSelectElement selectInputNode = null;
83         org.w3c.dom.Element JavaDoc textareaNode = null;
84         HTMLElement textareaNameNode = null;
85         HTMLElement textareaLabelNode = null;
86         HTMLTextAreaElement textareaInputNode = null;
87
88         // get references to the template nodes for varios form field types
89
Form form = xmlForm.getForm();
90         Iterator iter = form.getRef_idList().iterator();
91         while (iter.hasNext()) {
92             Ref_id r = (Ref_id)iter.next();
93             String JavaDoc type = r.getType();
94             if (type.equals("text")) {
95                 textNode = page.getElementById(r.getId());
96                 if (r.getName_id() != null) textNameNode = (HTMLElement)page.getElementById(r.getName_id());
97                 textLabelNode = (HTMLElement)page.getElementById(r.getLabel_id());
98                 textInputNode = (HTMLInputElement)page.getElementById(r.getInput_id());
99             } else if (type.equals("select")) {
100                 selectNode = page.getElementById(r.getId());
101                 if (r.getName_id() != null) selectNameNode = (HTMLElement)page.getElementById(r.getName_id());
102                 selectLabelNode = (HTMLElement)page.getElementById(r.getLabel_id());
103                 selectInputNode = (HTMLSelectElement)page.getElementById(r.getInput_id());
104             } else if (type.equals("textarea")) {
105                 textareaNode = page.getElementById(r.getId());
106                 if (r.getName_id() != null) textareaNameNode = (HTMLElement)page.getElementById(r.getName_id());
107                 textareaLabelNode = (HTMLElement)page.getElementById(r.getLabel_id());
108                 textareaInputNode = (HTMLTextAreaElement)page.getElementById(r.getInput_id());
109             }
110         }
111
112         // determine insertion mode: insertBefore or appendChild
113
boolean isInsertBefore = (form.getInsertBefore() != null);
114         Node insert = null;
115         Node parent = null;
116         if (isInsertBefore) {
117             insert = page.getElementById( form.getInsertBefore().getId());
118             parent = page.getElementById( form.getInsertBefore().getParent());
119         } else parent = page.getElementById( form.getAppendChild().getId());
120
121         // all id attributes need to be removed, since id is unique in a document
122
if (textNode != null) textNode.removeAttribute("id");
123         if (textNameNode != null) textNameNode.removeAttribute("id");
124         if (textLabelNode!=null) textLabelNode.removeAttribute("id");
125         if (textInputNode!=null) textInputNode.removeAttribute("id");
126         if (selectNode!=null) selectNode .removeAttribute("id");
127         if (selectNameNode != null) selectNameNode.removeAttribute("id");
128         if (selectLabelNode != null) selectLabelNode.removeAttribute("id");
129         if (selectInputNode != null) selectInputNode.removeAttribute("id");
130         if (textareaNode != null) textareaNode.removeAttribute("id");
131         if (textareaNameNode != null) textareaNameNode.removeAttribute("id");
132         if (textareaLabelNode != null) textareaLabelNode.removeAttribute("id");
133         if (textareaInputNode != null) textareaInputNode.removeAttribute("id");
134
135         // all preparation is done, so do the dirty work:
136
// for each form element, find the matching form type template and copy it
137
iter = form.getElementList().iterator();
138         while (iter.hasNext()) {
139             Element ele = (Element) iter.next();
140             //if (!ele.getName().startsWith(EntryFormMap.PREFIX)) {
141
if (ele.getText() != null) {
142                 if (logger.isDebugEnabled()) logger.debug("creating a text element");
143                 Text t = ele.getText();
144                 if (textNameNode != null) textNameNode.getFirstChild().setNodeValue(ele.getName());
145                 textLabelNode.getFirstChild().setNodeValue(ele.getLabel());
146                 if (t.getMaxlength() != null) textInputNode.setMaxLength(Integer.parseInt(t.getMaxlength()));
147                 else textInputNode.removeAttribute("maxlength");
148                 textInputNode.setSize(t.getSize());
149                 textInputNode.setAttribute("class", "Dir::Get_Data." + xmlForm.getName() + "." + ele.getName());
150                 insertIntoDOM(parent, textNode.cloneNode(true), isInsertBefore, insert);
151             } else if (ele.getTextarea() != null) {
152                 if (logger.isDebugEnabled()) logger.debug("creating a textarea element");
153                 Textarea t = ele.getTextarea();
154                 if (textareaNameNode != null) textareaNameNode.getFirstChild().setNodeValue(ele.getName());
155                 textareaLabelNode.getFirstChild().setNodeValue(ele.getLabel());
156                 textareaInputNode.setAttribute("class", "Dir::Get_Data." + xmlForm.getName() + "." + ele.getName());
157                 if (t.getRows() != null) textareaInputNode.setRows(Integer.parseInt(t.getRows()));
158                 else textareaInputNode.removeAttribute("rows");
159                 if (t.getCols() != null) textareaInputNode.setCols(Integer.parseInt(t.getCols()));
160                 else textareaInputNode.removeAttribute("cols");
161                 insertIntoDOM(parent, textareaNode.cloneNode(true), isInsertBefore, insert);
162             } else if (ele.getSelect() != null) {
163                 if (logger.isDebugEnabled()) logger.debug("creating a select element");
164                 if (selectNameNode != null) selectNameNode.getFirstChild().setNodeValue(ele.getName());
165                 selectLabelNode.getFirstChild().setNodeValue(ele.getLabel());
166                 selectInputNode.setAttribute("class", "Dir::Get_Data." + xmlForm.getName() + "." + ele.getName());
167                 logger.debug( "Dir::Get_Data." + xmlForm.getName() + "." + ele.getName());
168                 insertIntoDOM(parent, selectNode.cloneNode(true), isInsertBefore, insert);
169             }
170             //}
171
}
172         if (textNode!=null) parent.removeChild(textNode);
173         if (selectNode!=null) parent.removeChild(selectNode);
174         if (textareaNode!=null) parent.removeChild(textareaNode);
175
176     }
177 }
178
Popular Tags