001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.renderkit.dom_html_basic;
035:
036: import com.icesoft.faces.context.DOMContext;
037: import com.icesoft.faces.util.DOMUtils;
038: import org.w3c.dom.Document;
039: import org.w3c.dom.Element;
040: import org.w3c.dom.Node;
041: import org.w3c.dom.Text;
042:
043: import javax.faces.component.UIComponent;
044: import javax.faces.component.UIParameter;
045: import javax.faces.component.ValueHolder;
046: import javax.faces.context.FacesContext;
047: import java.io.IOException;
048: import java.text.MessageFormat;
049: import java.util.ArrayList;
050: import java.util.Iterator;
051:
052: public class OutputMessageRenderer extends DomBasicRenderer {
053:
054: public void encodeBegin(FacesContext facesContext,
055: UIComponent uiComponent) throws IOException {
056: validateParameters(facesContext, uiComponent, null);
057: }
058:
059: public void encodeChildren(FacesContext facesContext,
060: UIComponent uiComponent) {
061: validateParameters(facesContext, uiComponent, null);
062: }
063:
064: public void encodeEnd(FacesContext facesContext,
065: UIComponent uiComponent) throws IOException {
066: validateParameters(facesContext, uiComponent, null);
067:
068: // determine whether a span is required
069: boolean spanIsRequired = false;
070: String style = (String) uiComponent.getAttributes()
071: .get("style");
072: String styleClass = (String) uiComponent.getAttributes().get(
073: "styleClass");
074: if (style != null
075: || styleClass != null
076: || idNotNull(uiComponent)
077: || PassThruAttributeRenderer
078: .passThruAttributeExists(uiComponent)) {
079: spanIsRequired = true;
080: }
081:
082: // get a list of the UIParameter child components
083: ArrayList uiParameterChildren = new ArrayList();
084: Iterator allChildren = uiComponent.getChildren().iterator();
085: while (allChildren.hasNext()) {
086: UIComponent nextChild = (UIComponent) allChildren.next();
087: if (nextChild instanceof UIParameter) {
088: uiParameterChildren.add(((UIParameter) nextChild)
089: .getValue());
090: }
091: }
092:
093: // get the component value and convert to a string for later use
094: String uiComponentValue = null;
095: if (uiComponent instanceof ValueHolder) {
096: Object uiComponentValueObject = null;
097: if ((uiComponentValueObject = ((ValueHolder) uiComponent)
098: .getValue()) != null) {
099: if (uiComponentValueObject instanceof String) {
100: uiComponentValue = (String) uiComponentValueObject;
101: } else {
102: uiComponentValue = uiComponentValueObject
103: .toString();
104: }
105: }
106: }
107:
108: // if there is one or more parameters, format the parameters using
109: // the uiComponentValue as the pattern
110: int numberOfParameters = uiParameterChildren.size();
111: if (numberOfParameters > 0) {
112: Object[] parameters = uiParameterChildren.toArray();
113: uiComponentValue = MessageFormat.format(uiComponentValue,
114: parameters);
115: }
116:
117: // escape
118: boolean escape = DOMUtils.escapeIsRequired(uiComponent);
119: if (escape && uiComponentValue != null) {
120: uiComponentValue = DOMUtils.escapeAnsi(uiComponentValue);
121: }
122:
123: DOMContext domContext = DOMContext.attachDOMContext(
124: facesContext, uiComponent);
125:
126: if (!domContext.isInitialized()) {
127: // create the text message
128: Document document = domContext.getDocument();
129: Text textNode = (Text) document
130: .createTextNode(uiComponentValue);
131: // create a parent span, if required, otherwise set the root node
132: // to the text node
133: if (spanIsRequired) {
134: Element rootSpan = domContext
135: .createElement(HTML.SPAN_ELEM);
136: domContext.setRootNode(rootSpan);
137: if (styleClass != null) {
138: rootSpan.setAttribute("class", styleClass);
139: }
140: PassThruAttributeRenderer.renderAttributes(
141: facesContext, uiComponent, null);
142: rootSpan.appendChild(textNode);
143: } else {
144: domContext.setRootNode(textNode);
145: }
146: } else {
147: // the root node is either a text node or an element with a single
148: // text node child; find the text node and update its value
149: Node rootSpanOrText = domContext.getRootNode();
150: if (rootSpanOrText instanceof Text) {
151: ((Text) rootSpanOrText).setData(uiComponentValue);
152: } else {
153: Node textNode = null;
154: if ((textNode = rootSpanOrText.getFirstChild()) instanceof Text) {
155: ((Text) textNode).setData(uiComponentValue);
156: }
157: }
158: }
159: domContext.stepOver();
160: }
161: }
|