1 20 package org.enhydra.barracuda.core.comp.renderer.html; 21 22 import java.util.*; 23 24 import org.apache.log4j.*; 25 import org.w3c.dom.*; 26 import org.w3c.dom.html.*; 27 28 import org.enhydra.barracuda.core.comp.*; 29 import org.enhydra.barracuda.core.comp.renderer.*; 30 import org.enhydra.barracuda.plankton.*; 31 import org.enhydra.barracuda.plankton.data.*; 32 33 36 public class HTMLComponentRenderer extends DOMComponentRenderer { 37 38 protected static final Logger logger = Logger.getLogger(HTMLComponentRenderer.class.getName()); 39 40 protected EnabledHelper eh = new EnabledHelper(); 41 42 43 46 public Node addChildToParent(Node parent, Node child) throws InvalidNodeException { 47 if (parent==null || child==null) throw new InvalidNodeException("Invalid node: cannot add child:"+child+" to parent:"+parent); 49 50 boolean childIsEmptyText = false; 51 if (child instanceof Text) { 52 String txt = ((Text)child).getData().trim(); 53 if (txt.length() == 0) childIsEmptyText = true; 54 } 55 56 if (parent instanceof HTMLElement) { 58 if (parent instanceof HTMLTableRowElement) { 60 if (!(child instanceof HTMLTableCellElement) && 61 !childIsEmptyText) { 62 child = addChildToParent(parent.getOwnerDocument().createElement("TD"), child); 63 } 64 } else if (parent instanceof HTMLTableSectionElement) { 66 if (!(child instanceof HTMLTableRowElement) && 67 !childIsEmptyText) { 68 child = addChildToParent(parent.getOwnerDocument().createElement("TR"), child); 69 } 70 } else if (parent instanceof HTMLTableElement) { 72 if (!(child instanceof HTMLTableCaptionElement) && 73 !(child instanceof HTMLTableCellElement) && 74 !(child instanceof HTMLTableColElement) && 75 !(child instanceof HTMLTableRowElement) && 76 !(child instanceof HTMLTableSectionElement) && 77 !childIsEmptyText) { 78 child = addChildToParent(parent.getOwnerDocument().createElement("TR"), child); 79 } 80 } else if ((parent instanceof HTMLOListElement) || 82 (parent instanceof HTMLUListElement)) { 83 if (!(child instanceof HTMLLIElement) && 84 !childIsEmptyText) { 85 child = addChildToParent(parent.getOwnerDocument().createElement("LI"), child); 86 } 87 } else if (parent instanceof HTMLOptGroupElement) { 89 if (!(child instanceof HTMLOptionElement) && 90 !childIsEmptyText) { 91 child = addChildToParent(parent.getOwnerDocument().createElement("OPTION"), child); 92 } 93 } else if (parent instanceof HTMLSelectElement) { 95 if (!(child instanceof HTMLOptGroupElement) && 96 !(child instanceof HTMLOptionElement) && 97 !childIsEmptyText) { 98 child = addChildToParent(parent.getOwnerDocument().createElement("OPTION"), child); 99 } 100 } else if (parent instanceof HTMLTableColElement) { 102 if (!(child instanceof HTMLTableColElement) && 103 !childIsEmptyText) { 104 throw new InvalidNodeException("Child:"+child+" cannot be added to parent:"+parent); 105 } 106 } else { 107 String parentTag = ((Element) parent).getTagName().toUpperCase(); 112 String childTag = null; 113 if (child instanceof Element) childTag = ((Element) child).getTagName().toUpperCase(); 114 115 if (parentTag.equals("DL")) { 117 if (!childTag.equals("DD") && 118 !childTag.equals("DT") && 119 !childIsEmptyText) { 120 child = addChildToParent(parent.getOwnerDocument().createElement("DD"), child); 121 } 122 } 123 } 124 } 125 126 if (child!=null) parent.appendChild(child); 128 129 return parent; 131 } 132 133 145 public Node createDefaultNode(Document doc, BComponent comp, ViewContext vc) throws UnsupportedFormatException { Node templateNode = vc.getTemplateNode(); 149 Node defaultNode = templateNode.cloneNode(true); 150 if (logger.isInfoEnabled()) logger.info("Creating default node:"+defaultNode); 151 return defaultNode; 152 } 153 154 157 public void renderComponent(BComponent comp, View view, ViewContext vc) throws RenderException { 158 Node node = view.getNode(); 159 String name = comp.getName(); 160 161 super.renderComponent(comp, view, vc); 163 164 if (name!=null && node!=null) { 169 170 if (node instanceof HTMLAnchorElement) { 172 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLAnchorElement interface..."); 173 ((HTMLAnchorElement) node).setName(name); 174 175 } else if (node instanceof HTMLAppletElement) { 177 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLAppletElement interface..."); 178 ((HTMLAppletElement) node).setName(name); 179 180 } else if (node instanceof HTMLButtonElement) { 182 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLButtonElement interface..."); 183 ((HTMLButtonElement) node).setName(name); 184 185 } else if (node instanceof HTMLFormElement) { 187 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLFormElement interface..."); 188 ((HTMLFormElement) node).setName(name); 189 190 } else if (node instanceof HTMLFrameElement) { 192 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLFrameElement interface..."); 193 ((HTMLFrameElement) node).setName(name); 194 195 } else if (node instanceof HTMLIFrameElement) { 197 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLIFrameElement interface..."); 198 ((HTMLIFrameElement) node).setName(name); 199 200 } else if (node instanceof HTMLInputElement) { 202 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLInputElement interface..."); 203 ((HTMLInputElement) node).setName(name); 204 205 } else if (node instanceof HTMLMapElement) { 207 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLMapElement interface..."); 208 ((HTMLMapElement) node).setName(name); 209 210 } else if (node instanceof HTMLMetaElement) { 212 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLMetaElement interface..."); 213 ((HTMLMetaElement) node).setName(name); 214 215 } else if (node instanceof HTMLObjectElement) { 217 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLObjectElement interface..."); 218 ((HTMLObjectElement) node).setName(name); 219 220 } else if (node instanceof HTMLParamElement) { 222 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLParamElement interface..."); 223 ((HTMLParamElement) node).setName(name); 224 225 } else if (node instanceof HTMLSelectElement) { 227 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLSelectElement interface..."); 228 ((HTMLSelectElement) node).setName(name); 229 230 } else if (node instanceof HTMLTextAreaElement) { 232 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLTextAreaElement interface..."); 233 ((HTMLTextAreaElement) node).setName(name); 234 235 } 236 } 237 238 eh.setEnabled(node, comp.isEnabled()); 247 } 248 } | Popular Tags |