1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.renderkit.html; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.text.MessageFormat; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import javax.faces.component.UIComponent; |
28 | |
import javax.faces.component.UIOutput; |
29 | |
import javax.faces.component.UIParameter; |
30 | |
import javax.faces.component.html.HtmlOutputFormat; |
31 | |
import javax.faces.context.FacesContext; |
32 | |
|
33 | |
import org.apache.commons.logging.Log; |
34 | |
import org.apache.commons.logging.LogFactory; |
35 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderer; |
36 | |
import org.apache.myfaces.shared_impl.renderkit.JSFAttr; |
37 | |
import org.apache.myfaces.shared_impl.renderkit.RendererUtils; |
38 | |
import org.apache.myfaces.shared_impl.renderkit.html.HtmlRenderer; |
39 | |
import org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
@JSFRenderer( |
48 | |
renderKitId="HTML_BASIC", |
49 | |
family="javax.faces.Output", |
50 | |
type="javax.faces.Format") |
51 | 0 | public class HtmlFormatRenderer |
52 | |
extends HtmlRenderer |
53 | |
{ |
54 | 0 | private static final Log log = LogFactory.getLog(HtmlFormatRenderer.class); |
55 | |
|
56 | 0 | private static final Object[] EMPTY_ARGS = new Object[0]; |
57 | |
|
58 | |
public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) |
59 | |
throws IOException |
60 | |
{ |
61 | 0 | } |
62 | |
|
63 | |
public void encodeChildren(FacesContext facescontext, UIComponent uicomponent) |
64 | |
throws IOException |
65 | |
{ |
66 | 0 | } |
67 | |
|
68 | |
public void encodeEnd(FacesContext facesContext, UIComponent component) |
69 | |
throws IOException |
70 | |
{ |
71 | 0 | RendererUtils.checkParamValidity(facesContext, component, UIOutput.class); |
72 | |
|
73 | 0 | String text = getOutputFormatText(facesContext, component); |
74 | |
boolean isEscape; |
75 | 0 | if (component instanceof HtmlOutputFormat) |
76 | |
{ |
77 | 0 | isEscape = ((HtmlOutputFormat)component).isEscape(); |
78 | |
} |
79 | |
else |
80 | |
{ |
81 | 0 | isEscape = RendererUtils.getBooleanAttribute(component, JSFAttr.ESCAPE_ATTR, true); |
82 | |
} |
83 | 0 | HtmlTextRendererBase.renderOutputText(facesContext, component, text, isEscape); |
84 | 0 | } |
85 | |
|
86 | |
private String getOutputFormatText(FacesContext facesContext, |
87 | |
UIComponent htmlOutputFormat) |
88 | |
{ |
89 | 0 | String pattern = RendererUtils.getStringValue(facesContext, htmlOutputFormat); |
90 | |
Object[] args; |
91 | 0 | if (htmlOutputFormat.getChildCount() == 0) |
92 | |
{ |
93 | 0 | args = EMPTY_ARGS; |
94 | |
} |
95 | |
else |
96 | |
{ |
97 | 0 | List argsList = new ArrayList(); |
98 | 0 | for (Iterator it = htmlOutputFormat.getChildren().iterator(); it.hasNext(); ) |
99 | |
{ |
100 | 0 | UIComponent child = (UIComponent)it.next(); |
101 | 0 | if (child instanceof UIParameter) |
102 | |
{ |
103 | 0 | argsList.add(((UIParameter)child).getValue()); |
104 | |
} |
105 | 0 | } |
106 | 0 | args = argsList.toArray(new Object[argsList.size()]); |
107 | |
} |
108 | |
|
109 | 0 | MessageFormat format = new MessageFormat(pattern, facesContext.getViewRoot().getLocale()); |
110 | |
try |
111 | |
{ |
112 | 0 | return format.format(args); |
113 | |
} |
114 | 0 | catch (Exception e) |
115 | |
{ |
116 | 0 | log.error("Error formatting message of component " + htmlOutputFormat.getClientId(facesContext)); |
117 | 0 | return ""; |
118 | |
} |
119 | |
} |
120 | |
|
121 | |
} |