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.core.comp.scripting.*; 31 import org.enhydra.barracuda.core.util.dom.*; 32 import org.enhydra.barracuda.core.view.*; 33 import org.enhydra.barracuda.plankton.*; 34 35 38 public class HTMLScriptResourceRenderer extends DOMComponentRenderer { 39 40 protected static final Logger logger = Logger.getLogger(HTMLScriptResourceRenderer.class.getName()); 41 42 protected EnabledHelper eh = new EnabledHelper(); 43 44 47 public void renderComponent(BComponent comp, View view, ViewContext vc) throws RenderException { 48 if (!(comp instanceof BScriptResource)) throw new NoSuitableRendererException("This renderer can only render BScriptResource components"); 51 BScriptResource bsr = (BScriptResource) comp; 52 Node node = view.getNode(); 53 54 String src = bsr.getSrc(); 56 57 58 67 super.renderComponent(comp, view, vc); 69 70 if (logger.isDebugEnabled()) showNodeInterfaces(view, logger); 72 73 Document doc = node.getOwnerDocument(); 75 Element elRoot = doc.getDocumentElement(); 76 77 Element elHead = null; 79 Node child = elRoot.getFirstChild(); 80 while (child!=null) { 81 if (child instanceof HTMLHeadElement) { 82 elHead = (HTMLHeadElement) child; 83 break; 84 } 85 child = child.getNextSibling(); 86 } 87 88 if (elHead==null) { 90 elHead = doc.createElement("HEAD"); 91 elRoot.insertBefore(elHead, elRoot.getFirstChild()); 92 } 93 94 HTMLScriptElement elScript = null; 97 child = elHead.getFirstChild(); 98 while (child!=null) { 99 if (child instanceof HTMLScriptElement) { 100 HTMLScriptElement scel = (HTMLScriptElement) child; 101 if (src.equals(scel.getSrc())) { 102 elScript = scel; 103 break; 104 } 105 } 106 child = child.getNextSibling(); 107 } 108 109 if (elScript==null) { 111 elScript = (HTMLScriptElement) doc.createElement("SCRIPT"); 112 elHead.appendChild(elScript); 113 elScript.setType("text/javascript"); 114 elScript.setSrc(src); 115 } 116 117 } 121 122 } | Popular Tags |