KickJava   Java API By Example, From Geeks To Geeks.

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


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: HTMLTextRenderer.java,v 1.17 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.util.dom.*;
31 import org.enhydra.barracuda.plankton.*;
32
33 /**
34  * This class handles the default rendering of text into an HTML view.
35  *
36  * Q: maybe rather than just setting the text in Text node, the text renderer
37  * should represent the string as a CDATASection. This would enable you to
38  * easily set text as chunks of HTML. Hmmm...need to think about this...
39  */

40 public class HTMLTextRenderer extends HTMLComponentRenderer {
41
42     protected static final Logger logger = Logger.getLogger(HTMLTextRenderer.class.getName());
43
44     protected EnabledHelper eh = new EnabledHelper();
45     
46     /**
47      * The purpose of this method is for a renderer to provide
48      * a default node (if none exists). This component currently
49      * does not provide a default, so it throws an UnsupportedFormatException
50      * instead.
51      *
52      * @param doc the master Document which can be used to create elements
53      * from scratch
54      * @param comp the component that we're dealing with for the current request
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 //csc_110501.1 public Node createDefaultNode(Document doc, ViewContext vc) throws UnsupportedFormatException;
60
public Node createDefaultNode(Document doc, BComponent comp, ViewContext vc) throws UnsupportedFormatException { //csc_110501.1
61
//ask the renderer to create the default Node
62
Node templateNode = vc.getTemplateNode();
63         Node defaultNode = templateNode.cloneNode(true);
64         if (logger.isInfoEnabled()) logger.info("Creating default node:"+defaultNode);
65         return defaultNode;
66 // throw new UnsupportedFormatException("Cannot create default node");
67
}
68     
69     /**
70      *
71      */

72     public void renderComponent(BComponent comp, View view, ViewContext vc) throws RenderException {
73         //make sure the component is a text component
74
if (!(comp instanceof BText)) throw new NoSuitableRendererException("This renderer can only render BText components");
75         BText btext = (BText) comp;
76         Node node = view.getNode();
77         String JavaDoc text = btext.getText(); //csc_121201.1
78

79         //show what we're doing
80
if (logger.isDebugEnabled()) showNodeInterfaces(view, logger);
81
82         //first, allow the parent class to do anything it needs to
83
super.renderComponent(comp, view, vc);
84
85         //HTMLDocument Interface - set the "title" attribute
86
//----------------------
87
if (node instanceof HTMLDocument) {
88             //..HTMLDocument - set the "title" attribute
89
if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLDocument interface...");
90             ((HTMLDocument) node).setTitle(btext.getText());
91     
92         //HTMLElement Interface
93
//---------------------
94
//Supported Elements:
95
//..HTMLAreaElement - set the "alt" attribute
96
//..HTMLImageElement - set the "alt" attribute
97
//..HTMLInputElement - set the "value" attribute
98
//..HTMLTitleElement - set the "text" attribute
99
//..For everything else - get the first Text child and set
100
//....value if it exists
101
} else if (node instanceof HTMLElement) {
102
103             //..HTMLAreaElement - set the "alt" attribute
104
if (node instanceof HTMLAreaElement) {
105                 if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLAreaElement interface...");
106 //csc_121201.1 ((HTMLAreaElement) node).setAlt(btext.getText());
107
if (text!=null) ((HTMLAreaElement) node).setAlt(text); //csc_121201.1
108

109             //..HTMLImageElement - set the "alt" attribute
110
} else if (node instanceof HTMLImageElement) {
111                 if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLImageElement interface...");
112 //csc_121201.1 ((HTMLImageElement) node).setAlt(btext.getText());
113
if (text!=null) ((HTMLImageElement) node).setAlt(text); //csc_121201.1
114

115             //..HTMLInputElement - set the "value" attribute
116
} else if (node instanceof HTMLInputElement) {
117                 if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLInputElement interface...");
118 //csc_121201.1 ((HTMLInputElement) node).setValue(btext.getText());
119
if (text!=null) ((HTMLInputElement) node).setValue(text); //csc_121201.1
120

121             //..HTMLTitleElement - set the "text" attribute
122
} else if (node instanceof HTMLTitleElement) {
123                 if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLTitleElement interface...");
124 //csc_121201.1 ((HTMLTitleElement) node).setText(btext.getText());
125
if (text!=null) ((HTMLTitleElement) node).setText(text); //csc_121201.1
126

127             //..For everything else - get the first Text child and set
128
//....value if it exists
129
} else {
130                 if (logger.isInfoEnabled()) logger.info("Rendering based on "+Classes.getShortClassName(node.getClass())+" interface in first child Text element...");
131 //csc_121201.1 DOMUtil.setTextInNode((Element) node, btext.getText(), btext.allowMarkupInText()); //csc_092701.1
132
if (text!=null) DOMUtil.setTextInNode((Element) node, text, btext.allowMarkupInText()); //csc_121201.1
133
}
134         } else {
135             String JavaDoc errmsg = "Node does not implement HTMLElement or HTMLDocument and cannot be rendered: "+node;
136             logger.warn(errmsg);
137             throw new NoSuitableRendererException(errmsg);
138         }
139                 
140         //finally, make sure we reflect the components enabled/disabled status
141
eh.setEnabled(node, comp.isEnabled());
142     }
143     
144 }
Popular Tags