KickJava   Java API By Example, From Geeks To Geeks.

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


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.context.DOMContext;
37 import com.icesoft.faces.util.DOMUtils;
38 import org.w3c.dom.Document JavaDoc;
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.UIParameter;
45 import javax.faces.component.ValueHolder;
46 import javax.faces.context.FacesContext;
47 import java.io.IOException JavaDoc;
48 import java.text.MessageFormat JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.Iterator JavaDoc;
51
52 public class OutputMessageRenderer extends DomBasicRenderer {
53
54     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
55             throws IOException JavaDoc {
56         validateParameters(facesContext, uiComponent, null);
57     }
58
59     public void encodeChildren(FacesContext facesContext,
60                                UIComponent uiComponent) {
61         validateParameters(facesContext, uiComponent, null);
62     }
63
64     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
65             throws IOException JavaDoc {
66         validateParameters(facesContext, uiComponent, null);
67
68         // determine whether a span is required
69
boolean spanIsRequired = false;
70         String JavaDoc style = (String JavaDoc) uiComponent.getAttributes().get("style");
71         String JavaDoc styleClass =
72                 (String JavaDoc) uiComponent.getAttributes().get("styleClass");
73         if (style != null
74             || styleClass != null
75             || idNotNull(uiComponent)
76             ||
77             PassThruAttributeRenderer.passThruAttributeExists(uiComponent)) {
78             spanIsRequired = true;
79         }
80
81         // get a list of the UIParameter child components
82
ArrayList JavaDoc uiParameterChildren = new ArrayList JavaDoc();
83         Iterator JavaDoc allChildren = uiComponent.getChildren().iterator();
84         while (allChildren.hasNext()) {
85             UIComponent nextChild = (UIComponent) allChildren.next();
86             if (nextChild instanceof UIParameter) {
87                 uiParameterChildren.add(((UIParameter) nextChild).getValue());
88             }
89         }
90
91         // get the component value and convert to a string for later use
92
String JavaDoc uiComponentValue = null;
93         if (uiComponent instanceof ValueHolder) {
94             Object JavaDoc uiComponentValueObject = null;
95             if ((uiComponentValueObject =
96                     ((ValueHolder) uiComponent).getValue()) != null) {
97                 if (uiComponentValueObject instanceof String JavaDoc) {
98                     uiComponentValue = (String JavaDoc) uiComponentValueObject;
99                 } else {
100                     uiComponentValue = uiComponentValueObject.toString();
101                 }
102             }
103         }
104
105         // if there is one or more parameters, format the parameters using
106
// the uiComponentValue as the pattern
107
int numberOfParameters = uiParameterChildren.size();
108         if (numberOfParameters > 0) {
109             Object JavaDoc[] parameters = uiParameterChildren.toArray();
110             uiComponentValue =
111                     MessageFormat.format(uiComponentValue, parameters);
112         }
113
114         // escape
115
boolean escape = DOMUtils.escapeIsRequired(uiComponent);
116         if (escape && uiComponentValue != null) {
117             uiComponentValue = DOMUtils.escapeAnsi(uiComponentValue);
118         }
119
120         DOMContext domContext =
121                 DOMContext.attachDOMContext(facesContext, uiComponent);
122
123         if (!domContext.isInitialized()) {
124             // create the text message
125
Document JavaDoc document = domContext.getDocument();
126             Text JavaDoc textNode = (Text JavaDoc) document.createTextNode(uiComponentValue);
127             // create a parent span, if required, otherwise set the root node
128
// to the text node
129
if (spanIsRequired) {
130                 Element JavaDoc rootSpan = domContext.createElement(HTML.SPAN_ELEM);
131                 domContext.setRootNode(rootSpan);
132                 if (styleClass != null) {
133                     rootSpan.setAttribute("class", styleClass);
134                 }
135                 PassThruAttributeRenderer
136                         .renderAttributes(facesContext, uiComponent, null);
137                 rootSpan.appendChild(textNode);
138             } else {
139                 domContext.setRootNode(textNode);
140             }
141         } else {
142             // the root node is either a text node or an element with a single
143
// text node child; find the text node and update its value
144
Node JavaDoc rootSpanOrText = domContext.getRootNode();
145             if (rootSpanOrText instanceof Text JavaDoc) {
146                 ((Text JavaDoc) rootSpanOrText).setData(uiComponentValue);
147             } else {
148                 Node JavaDoc textNode = null;
149                 if ((textNode = rootSpanOrText.getFirstChild()) instanceof Text JavaDoc)
150                 {
151                     ((Text JavaDoc) textNode).setData(uiComponentValue);
152                 }
153             }
154         }
155         domContext.stepOver();
156     }
157 }
158
Popular Tags