1 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 37 public abstract class XmlFormViewHandler extends ModifyDomViewHandler { 38 39 protected static Logger logger = Logger.getLogger(XmlFormViewHandler.class.getName()); 40 41 42 protected abstract XmlFormMap getXmlForm(ViewContext vc); 43 44 47 public Object 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 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 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 textNode = null; 76 HTMLElement textNameNode = null; 77 HTMLElement textLabelNode = null; 78 HTMLInputElement textInputNode = null; 79 org.w3c.dom.Element selectNode = null; 80 HTMLElement selectNameNode = null; 81 HTMLElement selectLabelNode = null; 82 HTMLSelectElement selectInputNode = null; 83 org.w3c.dom.Element textareaNode = null; 84 HTMLElement textareaNameNode = null; 85 HTMLElement textareaLabelNode = null; 86 HTMLTextAreaElement textareaInputNode = null; 87 88 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 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 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 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 iter = form.getElementList().iterator(); 138 while (iter.hasNext()) { 139 Element ele = (Element) iter.next(); 140 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 } 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 |