001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsf.html;
030:
031: import java.io.*;
032: import java.util.*;
033: import java.util.logging.*;
034:
035: import java.text.MessageFormat;
036:
037: import javax.faces.*;
038: import javax.faces.component.*;
039: import javax.faces.component.html.*;
040: import javax.faces.context.*;
041: import javax.faces.convert.*;
042: import javax.faces.render.*;
043:
044: /**
045: * The HTML text renderer
046: */
047: class HtmlOutputFormatRenderer extends Renderer {
048: private static final Logger log = Logger
049: .getLogger(HtmlOutputFormatRenderer.class.getName());
050:
051: public static final Renderer RENDERER = new HtmlOutputFormatRenderer();
052:
053: /**
054: * True if the renderer is responsible for rendering the children.
055: */
056: @Override
057: public boolean getRendersChildren() {
058: return true;
059: }
060:
061: /**
062: * Renders the open tag for the text.
063: */
064: @Override
065: public void encodeBegin(FacesContext context, UIComponent component)
066: throws IOException {
067: ResponseWriter out = context.getResponseWriter();
068:
069: String id = component.getId();
070: String dir;
071: String lang;
072: String style;
073: String styleClass;
074: String title;
075:
076: if (component instanceof HtmlOutputFormat) {
077: HtmlOutputFormat htmlOutput = (HtmlOutputFormat) component;
078:
079: dir = htmlOutput.getDir();
080: lang = htmlOutput.getLang();
081: style = htmlOutput.getStyle();
082: styleClass = htmlOutput.getStyleClass();
083: title = htmlOutput.getTitle();
084: } else {
085: Map<String, Object> attrMap = component.getAttributes();
086:
087: dir = (String) attrMap.get("dir");
088: lang = (String) attrMap.get("lang");
089: style = (String) attrMap.get("style");
090: styleClass = (String) attrMap.get("styleClass");
091: title = (String) attrMap.get("title");
092: }
093:
094: if (dir == null && lang == null && style == null
095: && styleClass == null)
096: return;
097:
098: out.startElement("span", component);
099:
100: if (id != null && !id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
101: out.writeAttribute("id", component.getClientId(context),
102: "id");
103:
104: if (dir != null)
105: out.writeAttribute("dir", dir, "dir");
106:
107: if (lang != null)
108: out.writeAttribute("lang", lang, "dir");
109:
110: if (style != null)
111: out.writeAttribute("style", style, "style");
112:
113: if (styleClass != null)
114: out.writeAttribute("class", styleClass, "class");
115:
116: if (title != null)
117: out.writeAttribute("title", title, "title");
118: }
119:
120: /**
121: * Renders the content for the component.
122: */
123: @Override
124: public void encodeChildren(FacesContext context,
125: UIComponent component) throws IOException {
126: ResponseWriter out = context.getResponseWriter();
127:
128: if (component instanceof HtmlOutputFormat) {
129: HtmlOutputFormat htmlOutput = (HtmlOutputFormat) component;
130:
131: Object value = htmlOutput.getValue();
132:
133: if (value == null)
134: return;
135:
136: List<String> paramList = new ArrayList<String>();
137:
138: List children = component.getChildren();
139:
140: for (int i = 0; i < children.size(); i++) {
141: Object child = children.get(i);
142: if (child instanceof UIParameter) {
143: UIParameter param = (UIParameter) child;
144: Object paramValue = param.getValue();
145: String string = toString(context, param, paramValue);
146: paramList.add(string);
147: }
148: }
149:
150: String[] params = paramList.toArray(new String[paramList
151: .size()]);
152:
153: String pattern = toString(context, component, value);
154:
155: String string = new MessageFormat(pattern, context
156: .getViewRoot().getLocale()).format(params);
157:
158: out.writeText(string, "value");
159: } else {
160: Map<String, Object> attrMap = component.getAttributes();
161:
162: Object value = attrMap.get("value");
163:
164: if (value == null)
165: return;
166:
167: out.writeText(value, "value");
168: }
169: }
170:
171: /**
172: * Renders the closing tag for the component.
173: */
174: @Override
175: public void encodeEnd(FacesContext context, UIComponent component)
176: throws IOException {
177: ResponseWriter out = context.getResponseWriter();
178:
179: if (component instanceof HtmlOutputFormat) {
180: HtmlOutputFormat htmlOutput = (HtmlOutputFormat) component;
181:
182: if (htmlOutput.getStyleClass() != null
183: || htmlOutput.getStyle() != null
184: || htmlOutput.getDir() != null
185: || htmlOutput.getLang() != null) {
186: out.endElement("span");
187: }
188: } else {
189: Map<String, Object> attrMap = component.getAttributes();
190:
191: if (attrMap.get("styleClass") != null
192: || attrMap.get("style") != null
193: || attrMap.get("dir") != null
194: || attrMap.get("lang") != null) {
195: out.endElement("span");
196: }
197: }
198: }
199:
200: protected String toString(FacesContext context,
201: UIComponent component, Object value) {
202: if (component instanceof ValueHolder) {
203: Converter converter = ((ValueHolder) component)
204: .getConverter();
205:
206: if (converter != null) {
207: String result = converter.getAsString(context,
208: component, value);
209:
210: return result;
211: }
212: }
213:
214: if (value != null)
215: return value.toString();
216: else
217: return "";
218: }
219:
220: public String toString() {
221: return "HtmlOutputFormatRenderer[]";
222: }
223: }
|