KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > renderkit > dom_html_basic > TextRenderer


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.renderkit.dom_html_basic;
35
36 import com.icesoft.faces.component.UIXhtmlComponent;
37 import com.icesoft.faces.context.DOMContext;
38 import com.icesoft.faces.util.DOMUtils;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41 import org.w3c.dom.Text JavaDoc;
42
43 import javax.faces.component.UIComponent;
44 import javax.faces.component.UIInput;
45 import javax.faces.component.UIOutput;
46 import javax.faces.context.FacesContext;
47 import java.io.IOException JavaDoc;
48 import java.util.HashSet JavaDoc;
49 import java.util.Map JavaDoc;
50
51
52 public class TextRenderer extends DomBasicInputRenderer {
53
54     public void encodeChildren(FacesContext facesContext,
55                                UIComponent uiComponent) {
56         validateParameters(facesContext, uiComponent, null);
57     }
58
59     protected void renderEnd(FacesContext facesContext, UIComponent uiComponent,
60                              String JavaDoc currentValue)
61             throws IOException JavaDoc {
62         validateParameters(facesContext, uiComponent, null);
63         if (uiComponent instanceof UIInput) {
64             renderUIInput(facesContext, uiComponent, currentValue);
65         } else if (uiComponent instanceof UIOutput) {
66             renderUIOutput(facesContext, uiComponent, currentValue);
67         }
68     }
69
70     /**
71      * @param facesContext
72      * @param uiComponent
73      * @param currentValue
74      * @throws IOException
75      */

76     private void renderUIOutput(FacesContext facesContext,
77                                 UIComponent uiComponent,
78                                 String JavaDoc currentValue)
79             throws IOException JavaDoc {
80
81         DOMContext domContext =
82                 DOMContext.attachDOMContext(facesContext, uiComponent);
83
84         boolean requiresContainingSpan = requiresContainingSpan(uiComponent);
85
86         if (requiresContainingSpan) {
87             renderContainingSpan(facesContext, uiComponent, domContext);
88         } else {
89             renderTextNode(domContext);
90         }
91
92         if (currentValue == null) {
93             // We need to set the data in the text node to the empty string.
94
// The text node can be either the root node or it can be
95
// the first child of the root node in the case where we
96
// rendered a containing span
97
Text JavaDoc textNode = null;
98             if (requiresContainingSpan) {
99                 textNode = (Text JavaDoc) domContext.getRootNode()
100                         .getFirstChild();
101             } else {
102                 textNode = ((Text JavaDoc) domContext.getRootNode());
103             }
104             if (textNode != null) {
105                 textNode.setData("");
106             }
107         } else {
108             renderCurrentValue(uiComponent, currentValue, domContext,
109                                requiresContainingSpan);
110         }
111         domContext.streamWrite(facesContext, uiComponent);
112     }
113
114     /**
115      * @param domContext
116      */

117     private void renderTextNode(DOMContext domContext) {
118         if (!domContext.isInitialized()) {
119             Node JavaDoc root = domContext.getDocument().createTextNode("");
120             domContext.setRootNode(root);
121         } else if (!(domContext.getRootNode() instanceof Text JavaDoc)) {
122             // Need to switch from a root span to a root text node.
123
// This type of change can occur when the binding attribute is defined
124
// and the model has altered one of the attributes requiring a span
125
// from non-null to null.
126
domContext.getRootNode().getParentNode()
127                     .removeChild(domContext.getRootNode());
128             domContext.setRootNode(domContext.getDocument().createTextNode(""));
129         }
130     }
131
132     /**
133      * @param facesContext
134      * @param uiComponent
135      * @param domContext
136      */

137     private void renderContainingSpan(FacesContext facesContext,
138                                       UIComponent uiComponent,
139                                       DOMContext domContext) {
140         if (!domContext.isInitialized()) {
141             Node JavaDoc root = domContext.createElement(HTML.SPAN_ELEM);
142             domContext.setRootNode(root);
143         } else if (!(domContext.getRootNode() instanceof Element JavaDoc)) {
144             // if we require a span but the existing root node is not a span then we
145
// need to switch from a text node at the root to a span at the root
146
// This type of change can occur when the binding attribute is defined
147
// and the model has altered one of the attributes requiring a span
148
// from null to some non-null value
149
domContext.getRootNode().getParentNode()
150                     .removeChild(domContext.getRootNode());
151             domContext.setRootNode(domContext.createElement(HTML.SPAN_ELEM));
152         }
153         Element JavaDoc rootSpan = (Element JavaDoc) domContext.getRootNode();
154         setRootElementId(facesContext, rootSpan, uiComponent);
155         // render styleClass as the value of the class attribute;
156
// leave the style to be passed through
157
String JavaDoc styleClass =
158                 (String JavaDoc) uiComponent.getAttributes().get("styleClass");
159         if (styleClass != null) {
160             ((Element JavaDoc) rootSpan).setAttribute("class", styleClass);
161         }
162         PassThruAttributeRenderer
163                 .renderAttributes(facesContext, uiComponent, null);
164     }
165
166     /**
167      * @param uiComponent
168      * @param currentValue
169      * @param domContext
170      * @param requiresContainingSpan
171      */

172     private void renderCurrentValue(UIComponent uiComponent,
173                                     String JavaDoc currentValue,
174                                     DOMContext domContext,
175                                     boolean requiresContainingSpan) {
176
177         // escape
178
boolean valueTextRequiresEscape =
179                 DOMUtils.escapeIsRequired(uiComponent);
180         if (valueTextRequiresEscape) {
181             currentValue = DOMUtils.escapeAnsi(currentValue);
182         }
183
184         // Avoid severing and recreating the node
185
Node JavaDoc rootNode = domContext.getRootNode();
186         if (requiresContainingSpan) {
187             domContext.setCursorParent(rootNode);
188             if (rootNode.getFirstChild() != null
189                 && rootNode.getFirstChild() instanceof Text JavaDoc) {
190                 ((Text JavaDoc) rootNode.getFirstChild()).setData(currentValue);
191             } else {
192                 Text JavaDoc text = domContext.getDocument()
193                         .createTextNode(currentValue);
194                 rootNode.appendChild(text);
195             }
196         } else {
197             ((Text JavaDoc) rootNode).setData(currentValue);
198         }
199     }
200
201     /**
202      * @param facesContext
203      * @param uiComponent
204      * @param currentValue
205      * @throws IOException
206      */

207     private void renderUIInput(FacesContext facesContext,
208                                UIComponent uiComponent,
209                                String JavaDoc currentValue)
210             throws IOException JavaDoc {
211
212         DOMContext domContext =
213                 DOMContext.attachDOMContext(facesContext, uiComponent);
214
215         if (!domContext.isInitialized()) {
216             Element JavaDoc root = domContext.createElement("input");
217             domContext.setRootNode(root);
218             setRootElementId(facesContext, root, uiComponent);
219             root.setAttribute("type", "text");
220             root.setAttribute("name", uiComponent.getClientId(facesContext));
221         }
222         Element JavaDoc root = (Element JavaDoc) domContext.getRootNode();
223
224         String JavaDoc bidi = (String JavaDoc) uiComponent.getAttributes().get("dir");
225         if (bidi != null) {
226             root.setAttribute("dir", bidi);
227         }
228
229         if (currentValue != null) {
230             root.setAttribute("value", currentValue);
231         } else {
232             root.removeAttribute("value");
233         }
234
235         String JavaDoc styleClass =
236                 (String JavaDoc) uiComponent.getAttributes().get("styleClass");
237         if (styleClass != null) {
238             root.setAttribute("class", styleClass);
239         }
240         
241         HashSet JavaDoc excludes = new HashSet JavaDoc();
242         addJavaScript(facesContext, uiComponent, root, currentValue, excludes);
243         PassThruAttributeRenderer
244                 .renderAttributes(facesContext, uiComponent, getExcludesArray(excludes));
245         domContext.streamWrite(facesContext, uiComponent);
246
247     }
248
249     /**
250      * We require a containing span if either styleClass or style attributes are
251      * present, or there is an id to accommodate.
252      *
253      * @param uiComponent
254      * @return boolean
255      */

256     private boolean requiresContainingSpan(UIComponent uiComponent) {
257
258         // special case for title element
259
UIComponent parent = uiComponent.getParent();
260         if (parent != null && parent instanceof UIXhtmlComponent) {
261             String JavaDoc tag = ((UIXhtmlComponent) parent).getTag();
262             if (tag != null && tag.equalsIgnoreCase("title")) {
263                 return false;
264             }
265         }
266
267         String JavaDoc style = (String JavaDoc) uiComponent.getAttributes().get("style");
268         String JavaDoc styleClass =
269                 (String JavaDoc) uiComponent.getAttributes().get("styleClass");
270         String JavaDoc title = (String JavaDoc) uiComponent.getAttributes().get("title");
271         if (styleClass != null
272             || style != null
273             || title != null) {
274             return true;
275         }
276         Map JavaDoc attributes = uiComponent.getAttributes();
277         if (attributes.size() != 0) {
278             if (PassThruAttributeRenderer
279                     .passThruAttributeExists(uiComponent)) {
280                 return true;
281             }
282         }
283         if (idNotNull(uiComponent) && !uiComponent.getId().startsWith("_")) {
284             return true;
285         }
286         return false;
287     }
288
289     protected void addJavaScript(FacesContext facesContext,
290                                  UIComponent uiComponent, Element JavaDoc root,
291                                  String JavaDoc currentValue,
292                                  HashSet JavaDoc excludes) {
293
294
295     }
296 }
297
298
Popular Tags