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.List; |
25 | |
import java.util.logging.Level; |
26 | |
import java.util.logging.Logger; |
27 | |
|
28 | |
import javax.faces.component.UIComponent; |
29 | |
import javax.faces.component.UIOutput; |
30 | |
import javax.faces.component.UIParameter; |
31 | |
import javax.faces.component.UIViewRoot; |
32 | |
import javax.faces.component.html.HtmlOutputFormat; |
33 | |
import javax.faces.context.FacesContext; |
34 | |
import javax.faces.context.ResponseWriter; |
35 | |
|
36 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderer; |
37 | |
import org.apache.myfaces.shared.renderkit.JSFAttr; |
38 | |
import org.apache.myfaces.shared.renderkit.RendererUtils; |
39 | |
import org.apache.myfaces.shared.renderkit.html.CommonPropertyConstants; |
40 | |
import org.apache.myfaces.shared.renderkit.html.CommonPropertyUtils; |
41 | |
import org.apache.myfaces.shared.renderkit.html.HTML; |
42 | |
import org.apache.myfaces.shared.renderkit.html.HtmlRenderer; |
43 | |
import org.apache.myfaces.shared.renderkit.html.HtmlRendererUtils; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
@JSFRenderer(renderKitId = "HTML_BASIC", family = "javax.faces.Output", type = "javax.faces.Format") |
52 | 0 | public class HtmlFormatRenderer extends HtmlRenderer |
53 | |
{ |
54 | |
|
55 | 0 | private static final Logger log = Logger.getLogger(HtmlFormatRenderer.class.getName()); |
56 | |
|
57 | 0 | private static final Object[] EMPTY_ARGS = new Object[0]; |
58 | |
|
59 | |
@Override |
60 | |
protected boolean isCommonPropertiesOptimizationEnabled(FacesContext facesContext) |
61 | |
{ |
62 | 0 | return true; |
63 | |
} |
64 | |
|
65 | |
@Override |
66 | |
protected boolean isCommonEventsOptimizationEnabled(FacesContext facesContext) |
67 | |
{ |
68 | 0 | return true; |
69 | |
} |
70 | |
|
71 | |
@Override |
72 | |
public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException |
73 | |
{ |
74 | 0 | } |
75 | |
|
76 | |
@Override |
77 | |
public void encodeChildren(FacesContext facescontext, UIComponent uicomponent) throws IOException |
78 | |
{ |
79 | 0 | } |
80 | |
|
81 | |
@Override |
82 | |
public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException |
83 | |
{ |
84 | 0 | RendererUtils.checkParamValidity(facesContext, component, UIOutput.class); |
85 | |
|
86 | 0 | String text = getOutputFormatText(facesContext, component); |
87 | |
boolean escape; |
88 | 0 | if (component instanceof HtmlOutputFormat) |
89 | |
{ |
90 | 0 | escape = ((HtmlOutputFormat) component).isEscape(); |
91 | |
} |
92 | |
else |
93 | |
{ |
94 | 0 | escape = RendererUtils.getBooleanAttribute(component, JSFAttr.ESCAPE_ATTR, true); |
95 | |
} |
96 | 0 | if (text != null) |
97 | |
{ |
98 | 0 | ResponseWriter writer = facesContext.getResponseWriter(); |
99 | 0 | boolean span = false; |
100 | |
|
101 | 0 | if (isCommonPropertiesOptimizationEnabled(facesContext)) |
102 | |
{ |
103 | 0 | long commonPropertiesMarked = CommonPropertyUtils.getCommonPropertiesMarked(component); |
104 | |
|
105 | 0 | if ( (commonPropertiesMarked & ~(CommonPropertyConstants.ESCAPE_PROP)) > 0) |
106 | |
{ |
107 | 0 | span = true; |
108 | 0 | writer.startElement(HTML.SPAN_ELEM, component); |
109 | 0 | HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext); |
110 | |
} |
111 | 0 | else if (CommonPropertyUtils.isIdRenderingNecessary(component)) |
112 | |
{ |
113 | 0 | span = true; |
114 | 0 | writer.startElement(HTML.SPAN_ELEM, component); |
115 | 0 | writer.writeAttribute(HTML.ID_ATTR, component.getClientId(facesContext), null); |
116 | |
} |
117 | |
|
118 | 0 | CommonPropertyUtils.renderUniversalProperties(writer, commonPropertiesMarked, component); |
119 | 0 | CommonPropertyUtils.renderStyleProperties(writer, commonPropertiesMarked, component); |
120 | 0 | } |
121 | |
else |
122 | |
{ |
123 | 0 | if(component.getId()!=null && !component.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) |
124 | |
{ |
125 | 0 | span = true; |
126 | |
|
127 | 0 | writer.startElement(HTML.SPAN_ELEM, component); |
128 | |
|
129 | 0 | HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext); |
130 | |
|
131 | 0 | HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.COMMON_PASSTROUGH_ATTRIBUTES); |
132 | |
|
133 | |
} |
134 | |
else |
135 | |
{ |
136 | 0 | span = HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement(writer,component, |
137 | |
HTML.SPAN_ELEM,HTML.COMMON_PASSTROUGH_ATTRIBUTES); |
138 | |
} |
139 | |
} |
140 | |
|
141 | 0 | if (escape) |
142 | |
{ |
143 | 0 | if (log.isLoggable(Level.FINE)) |
144 | |
{ |
145 | 0 | log.fine("renderOutputText writing '" + text + "'"); |
146 | |
} |
147 | 0 | writer.writeText(text, org.apache.myfaces.shared.renderkit.JSFAttr.VALUE_ATTR); |
148 | |
} |
149 | |
else |
150 | |
{ |
151 | 0 | writer.write(text); |
152 | |
} |
153 | |
|
154 | 0 | if(span) |
155 | |
{ |
156 | 0 | writer.endElement(org.apache.myfaces.shared.renderkit.html.HTML.SPAN_ELEM); |
157 | |
} |
158 | |
} |
159 | 0 | } |
160 | |
|
161 | |
private String getOutputFormatText(FacesContext facesContext, UIComponent htmlOutputFormat) |
162 | |
{ |
163 | 0 | String pattern = RendererUtils.getStringValue(facesContext, htmlOutputFormat); |
164 | |
Object[] args; |
165 | 0 | if (htmlOutputFormat.getChildCount() == 0) |
166 | |
{ |
167 | 0 | args = EMPTY_ARGS; |
168 | |
} |
169 | |
else |
170 | |
{ |
171 | 0 | List<Object> argsList = null; |
172 | |
|
173 | 0 | if (htmlOutputFormat.getChildCount() > 0) |
174 | |
{ |
175 | 0 | List<UIParameter> validParams = HtmlRendererUtils.getValidUIParameterChildren( |
176 | |
facesContext, htmlOutputFormat.getChildren(), false, false, false); |
177 | 0 | for (int i = 0, size = validParams.size(); i < size; i++) |
178 | |
{ |
179 | 0 | UIParameter param = validParams.get(i); |
180 | 0 | if (argsList == null) |
181 | |
{ |
182 | 0 | argsList = new ArrayList<Object>(); |
183 | |
} |
184 | 0 | argsList.add(param.getValue()); |
185 | |
} |
186 | |
} |
187 | |
|
188 | 0 | if (argsList != null) |
189 | |
{ |
190 | 0 | args = argsList.toArray(new Object[argsList.size()]); |
191 | |
} |
192 | |
else |
193 | |
{ |
194 | 0 | args = EMPTY_ARGS; |
195 | |
} |
196 | |
} |
197 | |
|
198 | 0 | MessageFormat format = new MessageFormat(pattern, facesContext.getViewRoot().getLocale()); |
199 | |
try |
200 | |
{ |
201 | 0 | return format.format(args); |
202 | |
} |
203 | 0 | catch (Exception e) |
204 | |
{ |
205 | 0 | log.log(Level.SEVERE, "Error formatting message of component " |
206 | |
+ htmlOutputFormat.getClientId(facesContext)); |
207 | 0 | return ""; |
208 | |
} |
209 | |
} |
210 | |
|
211 | |
} |