001: /*
002: * $Id: WriteRenderer.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.faces.renderer;
023:
024: import java.io.IOException;
025: import javax.faces.component.UIComponent;
026: import javax.faces.component.UIViewRoot;
027: import javax.faces.component.ValueHolder;
028: import javax.faces.context.FacesContext;
029: import javax.faces.context.ResponseWriter;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.struts.util.ResponseUtils;
033:
034: /**
035: * <p><code>Renderer</code> implementation for the <code>write</code> tag
036: * from the <em>Struts-Faces Integration Library</em>.</p>
037: *
038: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
039: */
040:
041: public class WriteRenderer extends AbstractRenderer {
042:
043: // -------------------------------------------------------- Static Variables
044:
045: /**
046: * <p>The <code>Log</code> instance for this class.</p>
047: */
048: private static Log log = LogFactory.getLog(WriteRenderer.class);
049:
050: // ---------------------------------------------------------- Public Methods
051:
052: /**
053: * <p>Encode the specified text to our response.</p>
054: *
055: * @param context FacesContext for the response we are creating
056: * @param component Component to be rendered
057: *
058: * @exception IOException if an input/output error occurs
059: * @exception NullPointerException if <code>context</code>
060: * or <code>component</code> is <code>null</code>
061: */
062: public void encodeEnd(FacesContext context, UIComponent component)
063: throws IOException {
064:
065: if ((context == null) || (component == null)) {
066: throw new NullPointerException();
067: }
068:
069: ResponseWriter writer = context.getResponseWriter();
070: String id = component.getId();
071: if ((id != null) && id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) {
072: id = null;
073: }
074: String style = (String) component.getAttributes().get("style");
075: String styleClass = (String) component.getAttributes().get(
076: "styleClass");
077: if (log.isTraceEnabled()) {
078: log.trace("id='" + id + "', style='" + style
079: + "', styleClass='" + styleClass + "'");
080: }
081: if ((id != null) || (style != null) || (styleClass != null)) {
082: writer.startElement("span", component);
083: if (id != null) {
084: writer.writeAttribute("id", component
085: .getClientId(context), "id");
086: }
087: if (style != null) {
088: writer.writeAttribute("style", style, "style");
089: }
090: if (styleClass != null) {
091: writer
092: .writeAttribute("class", styleClass,
093: "styleClass");
094: }
095: writer.writeText("", null);
096: }
097: String text = getText(context, component);
098: if (log.isTraceEnabled()) {
099: 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: // ------------------------------------------------------- Protected Methods
110:
111: /**
112: * <p>Return the text to be rendered for this component, optionally
113: * filtered if requested.</p>
114: *
115: * @param context FacesContext for the response we are creating
116: * @param component Component to be rendered
117: */
118: protected String getText(FacesContext context, UIComponent component) {
119:
120: String text = getAsString(context, component,
121: ((ValueHolder) component).getValue());
122: Boolean filter = (Boolean) component.getAttributes().get(
123: "filter");
124: if (filter == null) {
125: filter = Boolean.FALSE;
126: }
127: if (filter.booleanValue()) {
128: return (ResponseUtils.filter(text));
129: } else {
130: return (text);
131: }
132:
133: }
134:
135: }
|