KickJava   Java API By Example, From Geeks To Geeks.

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


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: HTMLScriptRenderer.java,v 1.12 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 HTMLScriptRenderer extends DOMComponentRenderer {
39
40     protected static final Logger logger = Logger.getLogger(HTMLScriptRenderer.class.getName());
41
42     protected EnabledHelper eh = new EnabledHelper();
43
44     //csc_090303_1 - added
45
/**
46      * The purpose of this method is to create a default Node to be used when
47      * the component is not bound to any specific view. In the case of BAction,
48      * it will attempt to use the default template node if possible (ie. if
49      * it is an <a>, <form>, <input>, <button>, or <select> element); if anything else,
50      * it will defer to the super() method
51      *
52      * @param doc the master Document which can be used to create elements
53      * from scratch
54      * @param comp the component to be rendered
55      * @param vc the view context for the current request
56      * @return a default node (created from scratch)
57      * @throws UnsupportedFormatException if the renderer has no default node
58      */

59     public Node createDefaultNode(Document doc, BComponent comp, ViewContext vc) throws UnsupportedFormatException { //csc_110501.1
60
//ask the renderer to create the default Node
61
Node templateNode = vc.getTemplateNode();
62         Node defaultNode = null;
63         defaultNode = templateNode.cloneNode(true);
64         if (logger.isInfoEnabled()) logger.info("Creating default node: "+defaultNode);
65         return defaultNode;
66     }
67
68     /**
69      *
70      */

71     public void renderComponent(BComponent comp, View view, ViewContext vc) throws RenderException {
72         //unlike other components, this one really doesn't care what we're bound
73
//to...we do need to make sure the component is a script component
74
if (!(comp instanceof BScript)) throw new NoSuitableRendererException("This renderer can only render BScript components");
75         BScript bsComp = (BScript) comp;
76         Node node = view.getNode();
77
78         //sanity checks
79
String JavaDoc jsattr = bsComp.getAttr();
80         String JavaDoc jscmd = bsComp.getCmd();
81         int mode = bsComp.getMode();
82         if (jsattr==null || jscmd==null) return;
83         if (!(node instanceof Element)) return;
84         Element el = (Element) node;
85
86         //now allow the parent class to do anything it needs to
87
super.renderComponent(comp, view, vc);
88
89         //show what we're doing
90
if (logger.isDebugEnabled()) showNodeInterfaces(view, logger);
91
92         //note that we always want to render this script...this way if the client
93
//has scripting disabled now, but re-enables it later, we will know about it
94
//the next time around. If they don't have scripting enabled this can't hurt.
95

96         //render the component
97
if (!jscmd.endsWith(";")) jscmd = jscmd + "; ";
98         String JavaDoc cur_attr = el.getAttribute(jsattr);
99         if (cur_attr==null) mode = BScript.REPLACE;
100         //...append
101
if (mode==BScript.APPEND) {
102             if (!cur_attr.endsWith(";")) cur_attr = cur_attr + "; ";
103             el.setAttribute(jsattr, cur_attr+jscmd);
104         //...prepend
105
} else if (mode==BScript.PREPEND) {
106             el.setAttribute(jsattr, jscmd+cur_attr);
107         //...replace
108
} else {
109             el.setAttribute(jsattr, jscmd);
110         }
111         
112         //add in BScriptResource components that are needed
113
List resources = bsComp.getResources();
114         if (resources!=null) {
115             Iterator it = resources.iterator();
116             while (it.hasNext()) {
117                 String JavaDoc scr = (String JavaDoc) it.next();
118                 BScriptResource bsr = new BScriptResource(scr);
119                 bsr.setView(new DefaultView(node));
120                 bsComp.addStepChild(bsr, true);
121             }
122         }
123
124 //for script components, I don't think we need to worry about whether they're enabled or not
125
//finally, make sure we reflect the components enabled/disabled status
126
// eh.setEnabled(node, comp.isEnabled());
127
}
128
129 }
Popular Tags