001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.renderkit.html;
017:
018: import org.apache.myfaces.shared_impl.renderkit.JSFAttr;
019: import org.apache.myfaces.shared_impl.renderkit.RendererUtils;
020: import org.apache.myfaces.shared_impl.renderkit.html.HtmlRenderer;
021: import org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: import javax.faces.component.UIComponent;
027: import javax.faces.component.UIOutput;
028: import javax.faces.component.UIParameter;
029: import javax.faces.component.html.HtmlOutputFormat;
030: import javax.faces.context.FacesContext;
031: import java.io.IOException;
032: import java.text.MessageFormat;
033: import java.util.ArrayList;
034: import java.util.Iterator;
035: import java.util.List;
036:
037: /**
038: * @author Manfred Geiler (latest modification by $Author: schof $)
039: * @author Thomas Spiegl
040: * @version $Revision: 382015 $ $Date: 2006-03-01 14:47:11 +0100 (Mi, 01 Mrz 2006) $
041: */
042: public class HtmlFormatRenderer extends HtmlRenderer {
043: private static final Log log = LogFactory
044: .getLog(HtmlFormatRenderer.class);
045:
046: private static final Object[] EMPTY_ARGS = new Object[0];
047:
048: public void encodeBegin(FacesContext facesContext,
049: UIComponent uiComponent) throws IOException {
050: }
051:
052: public void encodeChildren(FacesContext facescontext,
053: UIComponent uicomponent) throws IOException {
054: }
055:
056: public void encodeEnd(FacesContext facesContext,
057: UIComponent component) throws IOException {
058: RendererUtils.checkParamValidity(facesContext, component,
059: UIOutput.class);
060:
061: String text = getOutputFormatText(facesContext, component);
062: boolean isEscape;
063: if (component instanceof HtmlOutputFormat) {
064: isEscape = ((HtmlOutputFormat) component).isEscape();
065: } else {
066: isEscape = RendererUtils.getBooleanAttribute(component,
067: JSFAttr.ESCAPE_ATTR, true);
068: }
069: HtmlTextRendererBase.renderOutputText(facesContext, component,
070: text, isEscape);
071: }
072:
073: private String getOutputFormatText(FacesContext facesContext,
074: UIComponent htmlOutputFormat) {
075: String pattern = RendererUtils.getStringValue(facesContext,
076: htmlOutputFormat);
077: Object[] args;
078: if (htmlOutputFormat.getChildCount() == 0) {
079: args = EMPTY_ARGS;
080: } else {
081: List argsList = new ArrayList();
082: for (Iterator it = htmlOutputFormat.getChildren()
083: .iterator(); it.hasNext();) {
084: UIComponent child = (UIComponent) it.next();
085: if (child instanceof UIParameter) {
086: argsList.add(((UIParameter) child).getValue());
087: }
088: }
089: args = argsList.toArray(new Object[argsList.size()]);
090: }
091:
092: MessageFormat format = new MessageFormat(pattern, facesContext
093: .getViewRoot().getLocale());
094: try {
095: return format.format(args);
096: } catch (Exception e) {
097: log.error("Error formatting message of component "
098: + htmlOutputFormat.getClientId(facesContext));
099: return "";
100: }
101: }
102:
103: }
|