KickJava   Java API By Example, From Geeks To Geeks.

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


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: HTMLComponentRenderer.java,v 1.18 2004/02/03 02:55:25 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.plankton.*;
31 import org.enhydra.barracuda.plankton.data.*;
32
33 /**
34  * This interface defines the methods needed to implement a Renderer.
35  */

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

46     public Node addChildToParent(Node parent, Node child) throws InvalidNodeException {
47         //eliminate the obvious
48
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 JavaDoc txt = ((Text)child).getData().trim();
53             if (txt.length() == 0) childIsEmptyText = true;
54         }
55
56         //make any adjustments specific to the markup
57
if (parent instanceof HTMLElement) {
58             //...<TR>
59
if (parent instanceof HTMLTableRowElement) {
60                 if (!(child instanceof HTMLTableCellElement) &&
61                     !childIsEmptyText) {
62                     child = addChildToParent(parent.getOwnerDocument().createElement("TD"), child);
63                 }
64             //...<THEAD>,<TBODY>,<TFOOT>
65
} else if (parent instanceof HTMLTableSectionElement) {
66                 if (!(child instanceof HTMLTableRowElement) &&
67                     !childIsEmptyText) {
68                     child = addChildToParent(parent.getOwnerDocument().createElement("TR"), child);
69                 }
70             //...<TABLE>
71
} 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             //...<OL>,<UL>
81
} 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             //...<OPTGROUP>
88
} else if (parent instanceof HTMLOptGroupElement) {
89                 if (!(child instanceof HTMLOptionElement) &&
90                     !childIsEmptyText) {
91                     child = addChildToParent(parent.getOwnerDocument().createElement("OPTION"), child);
92                 }
93             //...<SELECT>
94
} 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             //...<COLGROUP>
101
} 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 //List pl = Classes.getAllInterfaces(parent);
108
//CollectionsUtil.printStackTrace(pl, System.out);
109
//List cl = Classes.getAllInterfaces(child);
110
//CollectionsUtil.printStackTrace(cl, System.out);
111
String JavaDoc parentTag = ((Element) parent).getTagName().toUpperCase();
112                 String JavaDoc childTag = null;
113                 if (child instanceof Element) childTag = ((Element) child).getTagName().toUpperCase();
114                 
115                 //...<DL>
116
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         //now add the child in
127
if (child!=null) parent.appendChild(child);
128         
129         //return the parent
130
return parent;
131     }
132     
133     /**
134      * The purpose of this method is to create a default Node to be used when
135      * the component is not bound to any specific view. The default functionality
136      * is to simply clone the template node...
137      *
138      * @param doc the master Document which can be used to create elements
139      * from scratch
140      * @param comp the component that we're dealing with for the current request
141      * @param vc the view context for the current request
142      * @return a default node (created from scratch)
143      * @throws UnsupportedFormatException if the renderer has no default node
144      */

145 //csc_110501.1 public Node createDefaultNode(Document doc, ViewContext vc) throws UnsupportedFormatException;
146
public Node createDefaultNode(Document doc, BComponent comp, ViewContext vc) throws UnsupportedFormatException { //csc_110501.1
147
//ask the renderer to create the default Node
148
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     /**
155      *
156      */

157     public void renderComponent(BComponent comp, View view, ViewContext vc) throws RenderException {
158         Node node = view.getNode();
159         String JavaDoc name = comp.getName();
160
161         //first, allow the parent class to do anything it needs to
162
super.renderComponent(comp, view, vc);
163
164         //do any rendering specific to all HTML components...
165
//
166
//name: If the component has a name, try and set it in the underlying view
167
//---------------------
168
if (name!=null && node!=null) {
169         
170             //..HTMLAnchorElement - set the "name" attribute
171
if (node instanceof HTMLAnchorElement) {
172                 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLAnchorElement interface...");
173                 ((HTMLAnchorElement) node).setName(name);
174                 
175             //..HTMLAppletElement - set the "name" attribute
176
} else if (node instanceof HTMLAppletElement) {
177                 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLAppletElement interface...");
178                 ((HTMLAppletElement) node).setName(name);
179                 
180             //..HTMLButtonElement - set the "name" attribute
181
} else if (node instanceof HTMLButtonElement) {
182                 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLButtonElement interface...");
183                 ((HTMLButtonElement) node).setName(name);
184                 
185             //..HTMLFormElement - set the "name" attribute
186
} else if (node instanceof HTMLFormElement) {
187                 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLFormElement interface...");
188                 ((HTMLFormElement) node).setName(name);
189                 
190             //..HTMLFrameElement - set the "name" attribute
191
} else if (node instanceof HTMLFrameElement) {
192                 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLFrameElement interface...");
193                 ((HTMLFrameElement) node).setName(name);
194                 
195             //..HTMLIFrameElement - set the "name" attribute
196
} else if (node instanceof HTMLIFrameElement) {
197                 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLIFrameElement interface...");
198                 ((HTMLIFrameElement) node).setName(name);
199                 
200             //..HTMLInputElement - set the "name" attribute
201
} else if (node instanceof HTMLInputElement) {
202                 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLInputElement interface...");
203                 ((HTMLInputElement) node).setName(name);
204                 
205             //..HTMLMapElement - set the "name" attribute
206
} else if (node instanceof HTMLMapElement) {
207                 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLMapElement interface...");
208                 ((HTMLMapElement) node).setName(name);
209                 
210             //..HTMLMetaElement - set the "name" attribute
211
} else if (node instanceof HTMLMetaElement) {
212                 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLMetaElement interface...");
213                 ((HTMLMetaElement) node).setName(name);
214                 
215             //..HTMLObjectElement - set the "name" attribute
216
} else if (node instanceof HTMLObjectElement) {
217                 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLObjectElement interface...");
218                 ((HTMLObjectElement) node).setName(name);
219                 
220             //..HTMLParamElement - set the "name" attribute
221
} else if (node instanceof HTMLParamElement) {
222                 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLParamElement interface...");
223                 ((HTMLParamElement) node).setName(name);
224                 
225             //..HTMLSelectElement - set the "name" attribute
226
} else if (node instanceof HTMLSelectElement) {
227                 if (logger.isInfoEnabled()) logger.info("Rendering "+name+" based on HTMLSelectElement interface...");
228                 ((HTMLSelectElement) node).setName(name);
229                 
230             //..HTMLTextAreaElement - set the "name" attribute
231
} 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         //finally, make sure we reflect the components enabled/disabled status
239
//(previously this logic was in the individual renderer classes, but it
240
//can really be consolidated here. Only implication: classes like
241
//HTMLActionRenderer and HTMLLink renderer must take care not to set the
242
//target/href values if the component is disabled)
243
//csc_021102.2 - this line was commented out, and for the life of me I don't know
244
//why. Consequently, I'm re-enabling it and we'll see what happens. The reason we need
245
//this is so that we can return a plain-jane BComponent to control visibility
246
eh.setEnabled(node, comp.isEnabled());
247     }
248 }
Popular Tags