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.util.dom.*; 30 import org.enhydra.barracuda.plankton.*; 31 32 35 public class HTMLLinkRenderer extends HTMLActionRenderer { 36 37 protected static final Logger logger = Logger.getLogger(HTMLLinkRenderer.class.getName()); 38 39 53 public Node createDefaultNode(Document doc, BComponent comp, ViewContext vc) throws UnsupportedFormatException { Node templateNode = vc.getTemplateNode(); 57 Node defaultNode = null; 58 if (templateNode instanceof HTMLAnchorElement) { 59 defaultNode = templateNode.cloneNode(true); 60 } else if (templateNode instanceof HTMLButtonElement) { 61 defaultNode = templateNode.cloneNode(true); 62 } else if (templateNode instanceof HTMLInputElement) { 63 defaultNode = templateNode.cloneNode(true); 64 } else { 65 69 74 83 defaultNode = templateNode.cloneNode(false); 85 86 if (defaultNode.hasAttributes()) { 89 NamedNodeMap nnm = defaultNode.getAttributes(); 90 Attr attr = (Attr) nnm.getNamedItem("class"); 93 if (attr!=null) { 94 String value = attr.getValue(); 96 if (value!=null) { 97 StringBuffer sb = new StringBuffer (100); 98 String sep = ""; 99 StringTokenizer st = new StringTokenizer(attr.getValue()); 100 while (st.hasMoreTokens()) { 101 String s = st.nextToken(); 102 if (!s.startsWith("Dir::")) { 103 sb.append(sep+s); 104 sep = " "; 105 } 106 } 107 String newValue = sb.toString().trim(); 108 if (newValue.length()>0) attr.setValue(newValue); 109 else nnm.removeNamedItem("class"); 110 } 111 } 113 } 115 116 Element linkEl = (HTMLAnchorElement) doc.createElement("A"); 117 linkEl.appendChild(doc.createTextNode("foo99")); 118 try { 119 addChildToParent(defaultNode, linkEl); 120 } catch (InvalidNodeException e) { 121 throw new UnsupportedFormatException("Error creating default link node:"+e); 122 } 123 124 comp.addTempView(new DefaultView(linkEl)); 126 } 128 if (logger.isInfoEnabled()) logger.info("Creating default node:"+defaultNode); 129 return defaultNode; 130 } 131 132 135 public void renderComponent(BComponent comp, View view, ViewContext vc) throws RenderException { 136 if (!(comp instanceof BLink)) throw new NoSuitableRendererException("This renderer can only render BLink components; comp is of type:"+comp.getClass().getName()); 138 BLink linkComp = (BLink) comp; 139 Node node = view.getNode(); 140 141 if (logger.isDebugEnabled()) showNodeInterfaces(view, logger); 143 144 super.renderComponent(comp, view, vc); 146 147 if (node instanceof HTMLAnchorElement) { 149 if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLAnchorElement interface..."); 150 151 String target = linkComp.getTarget(); 153 if (target!=null) ((HTMLAnchorElement) node).setTarget(target); 154 155 String text = linkComp.getText(); 157 if (text!=null) { 158 BText textComp = new BText(text); 160 textComp.setView(new DefaultView(node)); 161 textComp.setAllowMarkupInText(linkComp.allowMarkupInText()); linkComp.addStepChild(textComp, true); 163 } 164 165 } else if (node instanceof HTMLButtonElement) { 167 if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLButtonElement interface..."); 168 169 String text = linkComp.getText(); 171 if (text!=null) { 172 BText textComp = new BText(text); 174 textComp.setView(new DefaultView(node)); 175 textComp.setAllowMarkupInText(linkComp.allowMarkupInText()); linkComp.addStepChild(textComp, true); 177 } 178 179 } else if (node instanceof HTMLInputElement) { 181 if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLInputElement interface..."); 182 183 String text = linkComp.getText(); 185 if (text!=null) { 186 BText textComp = new BText(text); 188 textComp.setView(new DefaultView(node)); 189 textComp.setAllowMarkupInText(false); linkComp.addStepChild(textComp, true); 191 } 192 193 } else { 194 String errmsg = "Node does not implement HTMLAnchorElement, HTMLButtonElement, or HTMLInputElement and cannot be rendered: "+node; 195 logger.warn(errmsg); 196 throw new NoSuitableRendererException(errmsg); 197 } 198 } 199 200 } | Popular Tags |