1 /*
2 * $Id$
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 package org.apache.struts.faces.renderer;
23
24
25 import java.io.IOException;
26
27 import org.apache.struts.util.ResponseUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import jakarta.faces.component.UIComponent;
32 import jakarta.faces.component.UIViewRoot;
33 import jakarta.faces.component.ValueHolder;
34 import jakarta.faces.context.FacesContext;
35 import jakarta.faces.context.ResponseWriter;
36
37
38 /**
39 * <p><code>Renderer</code> implementation for the <code>write</code> tag
40 * from the <em>Struts-Faces Integration Library</em>.</p>
41 *
42 * @version $Rev$ $Date$
43 */
44
45 public class WriteRenderer extends AbstractRenderer {
46
47
48 // -------------------------------------------------------- Static Variables
49
50
51 /**
52 * The {@code Log} instance for this class.
53 */
54 private final Logger log =
55 LoggerFactory.getLogger(WriteRenderer.class);
56
57
58 // ---------------------------------------------------------- Public Methods
59
60
61 /**
62 * <p>Encode the specified text to our response.</p>
63 *
64 * @param context FacesContext for the response we are creating
65 * @param component Component to be rendered
66 *
67 * @exception IOException if an input/output error occurs
68 * @exception NullPointerException if <code>context</code>
69 * or <code>component</code> is <code>null</code>
70 */
71 public void encodeEnd(FacesContext context, UIComponent component)
72 throws IOException {
73
74 if ((context == null) || (component == null)) {
75 throw new NullPointerException();
76 }
77
78 ResponseWriter writer = context.getResponseWriter();
79 String id = component.getId();
80 if ((id != null) && id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) {
81 id = null;
82 }
83 String style =
84 (String) component.getAttributes().get("style");
85 String styleClass =
86 (String) component.getAttributes().get("styleClass");
87 log.trace("id='{}', style='{}', styleClass='{}'",
88 id, style, styleClass);
89 if ((id != null) || (style != null) || (styleClass != null)) {
90 writer.startElement("span", component);
91 if (id != null) {
92 writer.writeAttribute("id", component.getClientId(context),
93 "id");
94 }
95 if (style != null) {
96 writer.writeAttribute("style", style, "style");
97 }
98 if (styleClass != null) {
99 writer.writeAttribute("class", styleClass, "styleClass");
100 }
101 writer.writeText("", null);
102 }
103 String text = getText(context, component);
104 log.atTrace()
105 .setMessage("encodeEnd({},{})")
106 .addArgument(() -> component.getClientId(context))
107 .addArgument(text)
108 .log();
109 writer.write(text);
110 if ((id != null) || (style != null) || (styleClass != null)) {
111 writer.endElement("span");
112 }
113
114 }
115
116
117 // ------------------------------------------------------- Protected Methods
118
119
120 /**
121 * <p>Return the text to be rendered for this component, optionally
122 * filtered if requested.</p>
123 *
124 * @param context FacesContext for the response we are creating
125 * @param component Component to be rendered
126 */
127 protected String getText(FacesContext context, UIComponent component) {
128
129 String text = getAsString(context, component,
130 ((ValueHolder) component).getValue());
131 Boolean filter = (Boolean) component.getAttributes().get("filter");
132 if (filter == null) {
133 filter = Boolean.FALSE;
134 }
135 if (filter.booleanValue()) {
136 return (ResponseUtils.filter(text));
137 } else {
138 return (text);
139 }
140
141 }
142 }