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.Debug;
038: import org.w3c.dom.Element;
039:
040: import javax.faces.component.UIComponent;
041: import javax.faces.component.UIGraphic;
042: import javax.faces.context.FacesContext;
043: import javax.faces.context.ResponseWriter;
044: import java.io.IOException;
045:
046: public class ImageRenderer extends DomBasicRenderer {
047:
048: public void encodeBegin(FacesContext facesContext,
049: UIComponent uiComponent) throws IOException {
050:
051: validateParameters(facesContext, uiComponent, UIGraphic.class);
052: UIGraphic uiGraphic = (UIGraphic) uiComponent;
053:
054: DOMContext domContext = DOMContext.attachDOMContext(
055: facesContext, uiComponent);
056: if (!domContext.isInitialized()) {
057: Element root = domContext.createElement("img");
058: domContext.setRootNode(root);
059: }
060: Element root = (Element) domContext.getRootNode();
061:
062: setRootElementId(facesContext, root, uiGraphic);
063:
064: String srcAttribute = processSrcAttribute(facesContext,
065: uiGraphic);
066: root.setAttribute("src", srcAttribute);
067:
068: String altAttribute = (String) uiComponent.getAttributes().get(
069: "alt");
070: if (altAttribute == null) {
071: altAttribute = "";
072: }
073: root.setAttribute("alt", altAttribute);
074:
075: String styleClass = String.valueOf(uiComponent.getAttributes()
076: .get("styleClass"));
077: if (styleClass != null) {
078: root.setAttribute("class", styleClass);
079: }
080:
081: ResponseWriter responseWriter = facesContext
082: .getResponseWriter();
083: Debug.assertTrue(responseWriter != null,
084: "ResponseWriter is null");
085: PassThruAttributeRenderer.renderAttributes(facesContext,
086: uiComponent, null);
087:
088: domContext.stepOver();
089:
090: domContext.streamWrite(facesContext, uiComponent);
091: }
092:
093: public void encodeChildren(FacesContext facesContext,
094: UIComponent uiComponent) {
095: validateParameters(facesContext, uiComponent, UIGraphic.class);
096: }
097:
098: public void encodeEnd(FacesContext facesContext,
099: UIComponent uiComponent) throws IOException {
100: validateParameters(facesContext, uiComponent, UIGraphic.class);
101: }
102:
103: private String processSrcAttribute(FacesContext facesContext,
104: UIGraphic uiGraphic) {
105: String value = (String) uiGraphic.getValue();
106: // support url as an alias for value
107: if (value == null) {
108: value = uiGraphic.getUrl();
109: }
110: if (value != null) {
111: value = facesContext.getApplication().getViewHandler()
112: .getResourceURL(facesContext, value);
113: return value;
114: } else {
115: return "";
116: }
117: }
118: }
|