KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > renderkit > html > HtmlFormatRenderer


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.renderkit.html;
17
18 import org.apache.myfaces.renderkit.JSFAttr;
19 import org.apache.myfaces.renderkit.RendererUtils;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.faces.component.UIComponent;
25 import javax.faces.component.UIOutput;
26 import javax.faces.component.UIParameter;
27 import javax.faces.component.html.HtmlOutputFormat;
28 import javax.faces.context.FacesContext;
29 import java.io.IOException JavaDoc;
30 import java.text.MessageFormat JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 /**
36  * @author Manfred Geiler (latest modification by $Author: matze $)
37  * @author Thomas Spiegl
38  * @version $Revision: 1.7 $ $Date: 2004/10/13 11:51:00 $
39  */

40 public class HtmlFormatRenderer
41         extends HtmlRenderer
42 {
43     private static final Log log = LogFactory.getLog(HtmlFormatRenderer.class);
44
45     private static final Object JavaDoc[] EMPTY_ARGS = new Object JavaDoc[0];
46
47     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
48             throws IOException JavaDoc
49     {
50     }
51
52     public void encodeChildren(FacesContext facescontext, UIComponent uicomponent)
53             throws IOException JavaDoc
54     {
55     }
56
57     public void encodeEnd(FacesContext facesContext, UIComponent component)
58             throws IOException JavaDoc
59     {
60         RendererUtils.checkParamValidity(facesContext, component, UIOutput.class);
61
62         String JavaDoc text = getOutputFormatText(facesContext, component);
63         boolean isEscape;
64         if (component instanceof HtmlOutputFormat)
65         {
66             isEscape = ((HtmlOutputFormat)component).isEscape();
67         }
68         else
69         {
70             isEscape = RendererUtils.getBooleanAttribute(component, JSFAttr.ESCAPE_ATTR, true);
71         }
72         HtmlTextRendererBase.renderOutputText(facesContext, component, text, isEscape);
73     }
74
75     private String JavaDoc getOutputFormatText(FacesContext facesContext,
76                                        UIComponent htmlOutputFormat)
77     {
78         String JavaDoc pattern = RendererUtils.getStringValue(facesContext, htmlOutputFormat);
79         Object JavaDoc[] args;
80         if (htmlOutputFormat.getChildCount() == 0)
81         {
82             args = EMPTY_ARGS;
83         }
84         else
85         {
86             List JavaDoc argsList = new ArrayList JavaDoc();
87             for (Iterator JavaDoc it = htmlOutputFormat.getChildren().iterator(); it.hasNext(); )
88             {
89                 UIComponent child = (UIComponent)it.next();
90                 if (child instanceof UIParameter)
91                 {
92                     argsList.add(((UIParameter)child).getValue());
93                 }
94             }
95             args = argsList.toArray(new Object JavaDoc[argsList.size()]);
96         }
97
98         MessageFormat JavaDoc format = new MessageFormat JavaDoc(pattern, facesContext.getViewRoot().getLocale());
99         try
100         {
101             return format.format(args);
102         }
103         catch (Exception JavaDoc e)
104         {
105             log.error("Error formatting message of component " + htmlOutputFormat.getClientId(facesContext));
106             return "";
107         }
108     }
109
110 }
111
Popular Tags