KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > renderer > html > HTMLScriptResourceRenderer


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
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: HTMLScriptResourceRenderer.java,v 1.6 2004/02/01 05:16:27 christianc Exp $
19  */

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 /**
36  * This class handles the default rendering script references into an HTML view.
37  */

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     /**
45      *
46      */

47     public void renderComponent(BComponent comp, View view, ViewContext vc) throws RenderException {
48         //unlike other components, this one really doesn't care what we're bound
49
//to...we do need to make sure the component is a script component
50
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         //if the src attribute is null don't render this component
55
String JavaDoc src = bsr.getSrc();
56
57
58         //note that we _always_ want to render this script...this way if the client
59
//has scripting disabled now, but re-enables it later, we will know about it
60
//the next time around. If a client doesn't have scripting enabled, this
61
//can't hurt.
62
//csc_102201.1
63
// //if the client doesn't support scripting then bail
64
// ViewCapabilities vcap = vc.getViewCapabilities();
65
// if (!(vcap.getScriptingType() instanceof ScriptingType.JavaScript1x)) return;
66

67         //now allow the parent class to do anything it needs to
68
super.renderComponent(comp, view, vc);
69
70         //show what we're doing
71
if (logger.isDebugEnabled()) showNodeInterfaces(view, logger);
72
73         //find the root
74
Document doc = node.getOwnerDocument();
75         Element elRoot = doc.getDocumentElement();
76
77         //now see if there is a head element
78
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 not, create one and add it in
89
if (elHead==null) {
90             elHead = doc.createElement("HEAD");
91             elRoot.insertBefore(elHead, elRoot.getFirstChild());
92         }
93
94         //now see if we have script elements that match already in place
95
//(if so, we don't need to do anything)
96
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 we don't, create one
110
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 //for script components, I don't think we need to worry about whether they're enabled or not
118
//finally, make sure we reflect the components enabled/disabled status
119
// eh.setEnabled(node, comp.isEnabled());
120
}
121
122 }
Popular Tags