KickJava   Java API By Example, From Geeks To Geeks.

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


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: HTMLLinkRenderer.java,v 1.18 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.util.dom.*;
30 import org.enhydra.barracuda.plankton.*;
31
32 /**
33  * This class handles the default rendering of a link into an HTML view.
34  */

35 public class HTMLLinkRenderer extends HTMLActionRenderer {
36
37     protected static final Logger logger = Logger.getLogger(HTMLLinkRenderer.class.getName());
38
39     /**
40      * The purpose of this method is to create a default Node to be used when
41      * the component is not bound to any specific view. In the case of BLink,
42      * it will attempt to use the default template node if possible (ie. if
43      * it is an <a>, <button>, or <input> element); if anything else, it will
44      * create an <a> node from scratch and append an empty text node to it
45      *
46      * @param doc the master Document which can be used to create elements
47      * from scratch
48      * @param comp the component that we're dealing with for the current request
49      * @param vc the view context for the current request
50      * @return a default node (created from scratch)
51      * @throws UnsupportedFormatException if the renderer has no default node
52      */

53 //csc_110501.1 public Node createDefaultNode(Document doc, ViewContext vc) throws UnsupportedFormatException;
54
public Node createDefaultNode(Document doc, BComponent comp, ViewContext vc) throws UnsupportedFormatException { //csc_110501.1
55
//ask the renderer to create the default Node
56
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 //csc_103101.1_start - what really needs to happen here is that
66
//we need to clone the node (shallow), and then add the element
67
//node as a child to it
68

69 /*
70             Element linkEl = (HTMLAnchorElement) doc.createElement("A");
71             linkEl.appendChild(doc.createTextNode("foo88"));
72             defaultNode = linkEl;
73 */

74 /*
75             Element spanEl = doc.createElement("SPAN");
76             Element linkEl = (HTMLAnchorElement) doc.createElement("A");
77             Text text = doc.createTextNode("foo88");
78             spanEl.appendChild(linkEl);
79             linkEl.appendChild(text);
80             defaultNode = spanEl;
81             comp.addTempView(new DefaultView(linkEl));
82 */

83             //create the markup
84
defaultNode = templateNode.cloneNode(false);
85             
86             //csc_051502.1 - after cloning the node, we need to removed any directives embedded in its
87
//class attribute, since they won't be processed anyway
88
if (defaultNode.hasAttributes()) {
89                 NamedNodeMap nnm = defaultNode.getAttributes();
90 // for (int i=nnm.getLength()-1; i>=0; i--) {
91
// Node n = nnm.item(i);
92
Attr attr = (Attr) nnm.getNamedItem("class");
93                     if (attr!=null) {
94 //System.out.println("here!");
95
String JavaDoc value = attr.getValue();
96                         if (value!=null) {
97                             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
98                             String JavaDoc sep = "";
99                             StringTokenizer st = new StringTokenizer(attr.getValue());
100                             while (st.hasMoreTokens()) {
101                                 String JavaDoc s = st.nextToken();
102                                 if (!s.startsWith("Dir::")) {
103                                     sb.append(sep+s);
104                                     sep = " ";
105                                 }
106                             }
107                             String JavaDoc newValue = sb.toString().trim();
108                             if (newValue.length()>0) attr.setValue(newValue);
109                             else nnm.removeNamedItem("class");
110                         }
111 // System.out.println("attr, name:"+attr.getName()+" val:"+attr.getValue());
112
}
113 // }
114
}
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             //now create a view for the component
125
comp.addTempView(new DefaultView(linkEl));
126 //csc_103101.1_end
127
}
128         if (logger.isInfoEnabled()) logger.info("Creating default node:"+defaultNode);
129         return defaultNode;
130     }
131
132     /**
133      *
134      */

135     public void renderComponent(BComponent comp, View view, ViewContext vc) throws RenderException {
136         //make sure the component is a text component
137
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         //show what we're doing
142
if (logger.isDebugEnabled()) showNodeInterfaces(view, logger);
143
144         //first, allow the parent class to do anything it needs to
145
super.renderComponent(comp, view, vc);
146
147         //..HTMLAnchorElement
148
if (node instanceof HTMLAnchorElement) {
149             if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLAnchorElement interface...");
150
151             //set the "target" attribute, if it exists
152
String JavaDoc target = linkComp.getTarget();
153             if (target!=null) ((HTMLAnchorElement) node).setTarget(target);
154
155             //set the text value (if it exists) by delegating to a text component
156
String JavaDoc text = linkComp.getText();
157             if (text!=null) {
158 // BText textComp = new BText(text, new DefaultView(node));
159
BText textComp = new BText(text);
160                 textComp.setView(new DefaultView(node));
161                 textComp.setAllowMarkupInText(linkComp.allowMarkupInText()); //csc_092701.1
162
linkComp.addStepChild(textComp, true);
163             }
164             
165         //..HTMLButtonElement
166
} else if (node instanceof HTMLButtonElement) {
167             if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLButtonElement interface...");
168
169             //set the text value (if it exists) by delegating to a text component
170
String JavaDoc text = linkComp.getText();
171             if (text!=null) {
172 // BText textComp = new BText(text, new DefaultView(node));
173
BText textComp = new BText(text);
174                 textComp.setView(new DefaultView(node));
175                 textComp.setAllowMarkupInText(linkComp.allowMarkupInText()); //csc_092701.1
176
linkComp.addStepChild(textComp, true);
177             }
178
179         //..HTMLInputElement
180
} else if (node instanceof HTMLInputElement) {
181             if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLInputElement interface...");
182
183             //set the text value (if it exists) by delegating to a text component
184
String JavaDoc text = linkComp.getText();
185             if (text!=null) {
186 // BText textComp = new BText(text, new DefaultView(node));
187
BText textComp = new BText(text);
188                 textComp.setView(new DefaultView(node));
189                 textComp.setAllowMarkupInText(false); ////csc_092701.1 - input elements can't contain markup, so don't bother allowing it
190
linkComp.addStepChild(textComp, true);
191             }
192
193         } else {
194             String JavaDoc 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