KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > wap > renderkit > wml > ImageRenderer


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.wap.renderkit.wml;
17
18 import javax.faces.component.UIComponent;
19 import javax.faces.context.FacesContext;
20 import javax.faces.context.ResponseWriter;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.myfaces.wap.component.GraphicImage;
25 import org.apache.myfaces.wap.renderkit.Attributes;
26 import org.apache.myfaces.wap.renderkit.RendererUtils;
27 import org.apache.myfaces.wap.renderkit.WmlRenderer;
28
29 /**
30  * @author <a HREF="mailto:Jiri.Zaloudek@ivancice.cz">Jiri Zaloudek</a> (latest modification by $Author: matzew $)
31  * @version $Revision: 1.1 $ $Date: 2004/12/30 09:37:26 $
32  * $Log: ImageRenderer.java,v $
33  * Revision 1.1 2004/12/30 09:37:26 matzew
34  * added a new RenderKit for WML. Thanks to Jirí Žaloudek
35  *
36  */

37 public class ImageRenderer extends WmlRenderer {
38     private static Log log = LogFactory.getLog(ImageRenderer.class);
39     
40     /** Creates a new instance of ImageRenderer */
41     public ImageRenderer() {
42         super();
43         log.debug("created object " + this.getClass().getName());
44     }
45     
46     public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
47         log.debug("encodeBegin(" + component.getId() + ")");
48         if (context == null || component == null) {
49             throw new NullPointerException JavaDoc();
50         }
51     }
52     
53     public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
54         log.debug("encodeChildren(" + component.getId() + ")");
55         if (context == null || component == null) {
56             throw new NullPointerException JavaDoc();
57         }
58     }
59
60     public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
61         log.debug("encodeEnd(" + component.getId() + ")");
62         if (context == null || component == null) {
63             throw new NullPointerException JavaDoc();
64         }
65         if (!component.isRendered()) return;
66         
67         GraphicImage comp = (GraphicImage)component;
68         String JavaDoc contextPath = context.getExternalContext().getRequestContextPath();
69         //String url = (String)component.getAttributes().get("url");
70

71         ResponseWriter writer = context.getResponseWriter();
72         
73         writer.startElement(Attributes.IMG, comp);
74         
75         RendererUtils.writeAttribute(Attributes.ID, comp.getClientId(context), writer);
76         RendererUtils.writeAttribute(Attributes.STYLE_CLASS, comp.getStyleClass(), writer);
77         RendererUtils.writeAttribute(Attributes.XML_LANG, comp.getXmllang(), writer);
78         RendererUtils.writeAttribute(Attributes.ALT, comp.getAlt(), writer);
79
80         // src
81
String JavaDoc url = null;
82         if (comp.getValue() != null) url = (String JavaDoc)comp.getValue(); // 'value' have a priority
83
else if (comp.getUrl() != null) url = (String JavaDoc)comp.getUrl();
84         RendererUtils.writeAttribute(Attributes.SRC, getURL(context, url), writer);
85             
86         RendererUtils.writeAttribute(Attributes.LOCAL_SRC, comp.getLocalsrc(), writer);
87         RendererUtils.writeAttribute(Attributes.VSPACE, comp.getVspace(), writer);
88         RendererUtils.writeAttribute(Attributes.HSPACE, comp.getHspace(), writer);
89         RendererUtils.writeAttribute(Attributes.ALIGN, comp.getAlign(), writer);
90         RendererUtils.writeAttribute(Attributes.HEIGHT, comp.getHeight(), writer);
91         RendererUtils.writeAttribute(Attributes.WIDTH, comp.getWidth(), writer);
92
93         writer.endElement(Attributes.IMG);
94     }
95     
96     
97     public void decode(FacesContext context, UIComponent component) {
98         if (component == null) throw new NullPointerException JavaDoc();
99     }
100     
101     /** Passing it to the getResourceURL() method of the ViewHandler for this application,
102      * and passing the result through the encodeResourceURL() method of the ExternalContext
103      */

104     private String JavaDoc getURL(FacesContext context, String JavaDoc url) throws java.io.IOException JavaDoc {
105         if (url == null) throw new java.io.WriteAbortedException JavaDoc("URL parameter in tag graphicImage is null.", new java.lang.NullPointerException JavaDoc());
106         url = context.getApplication().getViewHandler().getResourceURL(context, url);
107         url = context.getExternalContext().encodeResourceURL(url);
108         return (url);
109     }
110     
111 }
112
113
Popular Tags