View Javadoc

1   /*
2    * Copyright 2002,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  
17  package org.apache.struts.faces.renderer;
18  
19  
20  import java.io.IOException;
21  import javax.faces.component.UIComponent;
22  import javax.faces.component.UIViewRoot;
23  import javax.faces.component.ValueHolder;
24  import javax.faces.context.FacesContext;
25  import javax.faces.context.ResponseWriter;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.struts.util.ResponseUtils;
29  
30  
31  /***
32   * <p><code>Renderer</code> implementation for the <code>write</code> tag
33   * from the <em>Struts-Faces Integration Library</em>.</p>
34   *
35   * @version $Rev: 421138 $ $Date: 2006-07-11 22:41:40 -0700 (Tue, 11 Jul 2006) $
36   */
37  
38  public class WriteRenderer extends AbstractRenderer {
39  
40  
41      // -------------------------------------------------------- Static Variables
42  
43  
44      /***
45       * <p>The <code>Log</code> instance for this class.</p>
46       */
47      private static Log log = LogFactory.getLog(WriteRenderer.class);
48  
49  
50      // ---------------------------------------------------------- Public Methods
51  
52  
53      /***
54       * <p>Encode the specified text to our response.</p>
55       *
56       * @param context FacesContext for the response we are creating
57       * @param component Component to be rendered
58       *
59       * @exception IOException if an input/output error occurs
60       * @exception NullPointerException if <code>context</code>
61       *  or <code>component</code> is <code>null</code>
62       */
63      public void encodeEnd(FacesContext context, UIComponent component)
64          throws IOException {
65  
66          if ((context == null) || (component == null)) {
67              throw new NullPointerException();
68          }
69  
70          ResponseWriter writer = context.getResponseWriter();
71          String id = component.getId();
72          if ((id != null) && id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) {
73              id = null;
74          }
75          String style =
76              (String) component.getAttributes().get("style");
77          String styleClass =
78              (String) component.getAttributes().get("styleClass");
79          if (log.isTraceEnabled()) {
80              log.trace("id='" + id + "', style='" + style + "', styleClass='" +
81                        styleClass + "'");
82          }
83          if ((id != null) || (style != null) || (styleClass != null)) {
84              writer.startElement("span", component);
85              if (id != null) {
86                  writer.writeAttribute("id", component.getClientId(context),
87                                        "id");
88              }
89              if (style != null) {
90                  writer.writeAttribute("style", style, "style");
91              }
92              if (styleClass != null) {
93                  writer.writeAttribute("class", styleClass, "styleClass");
94              }
95              writer.writeText("", null);
96          }
97          String text = getText(context, component);
98          if (log.isTraceEnabled()) {
99              log.trace("encodeEnd(" + component.getClientId(context) +
100                       "," + text + ")");
101         }
102         writer.write(text);
103         if ((id != null) || (style != null) || (styleClass != null)) {
104             writer.endElement("span");
105         }
106 
107     }
108 
109 
110     // ------------------------------------------------------- Protected Methods
111 
112 
113     /***
114      * <p>Return the text to be rendered for this component, optionally
115      * filtered if requested.</p>
116      *
117      * @param context FacesContext for the response we are creating
118      * @param component Component to be rendered
119      */
120     protected String getText(FacesContext context, UIComponent component) {
121 
122         String text = getAsString(context, component,
123                                   ((ValueHolder) component).getValue());
124         Boolean filter = (Boolean) component.getAttributes().get("filter");
125         if (filter == null) {
126             filter = Boolean.FALSE;
127         }
128         if (filter.booleanValue()) {
129             return (ResponseUtils.filter(text));
130         } else {
131             return (text);
132         }
133 
134     }
135 
136 
137 }