KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > renderkit > dom_html_basic > OutputLinkRenderer


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.renderkit.dom_html_basic;
35
36 import javax.faces.component.UIComponent;
37 import javax.faces.component.UIOutput;
38 import javax.faces.context.FacesContext;
39 import javax.faces.context.ResponseWriter;
40 import java.io.IOException JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Map JavaDoc;
43
44 public class OutputLinkRenderer extends DomBasicRenderer {
45     /**
46      * Return true as this component renders its children.
47      */

48     public boolean getRendersChildren() {
49         return true;
50     }
51
52     /**
53      * Returns the value of this component as an Object.
54      *
55      * @param the component to retrieve the value from.
56      * @return value as an Object.
57      */

58     protected Object JavaDoc getValue(UIComponent uiComponent) {
59         return ((UIOutput) uiComponent).getValue();
60     }
61
62     String JavaDoc clientId;
63
64     /**
65      * @param facesContext
66      * @param uiComponent
67      * @throws IOException
68      */

69     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
70             throws IOException JavaDoc {
71         if (facesContext == null || uiComponent == null) {
72             throw new NullPointerException JavaDoc(
73                     "facesContext or uiComponent is null");
74         }
75
76         UIOutput output = (UIOutput) uiComponent;
77         String JavaDoc linkVal = getValue(facesContext, uiComponent);
78
79         if (!output.isRendered()) return;
80
81         ResponseWriter writer = facesContext.getResponseWriter();
82
83         if (writer == null) {
84             throw new NullPointerException JavaDoc("ResponseWriter is null");
85         }
86
87         writer.startElement("a", uiComponent);
88         writer.writeAttribute("id", uiComponent.getClientId(facesContext),
89                               "id");
90
91         if (null == linkVal || 0 == linkVal.length()) {
92             linkVal = "";
93         }
94
95         clientId = output.getClientId(facesContext);
96
97         linkVal = appendParameters(facesContext, uiComponent, linkVal);
98
99         if (!checkDisabled(uiComponent))
100             writer.writeURIAttribute("href",
101                                      facesContext
102                                              .getExternalContext().encodeResourceURL(
103                                              linkVal), "href");
104
105         PassThruAttributeWriter.renderAttributes(writer, uiComponent, null);
106
107         String JavaDoc styleClass = (String JavaDoc) output.getAttributes().get("styleClass");
108         if (styleClass != null)
109             writer.writeAttribute("class", styleClass, "styleClass");
110
111         writer.flush();
112     }
113
114     /**
115      * @param uiComponent
116      * @return boolean
117      */

118     protected boolean checkDisabled(UIComponent uiComponent) {
119         return false;
120     }
121
122     /**
123      * @param facesContext
124      * @param uiComponent
125      * @param hrefAttribute
126      * @return String
127      */

128     private String JavaDoc appendParameters(FacesContext facesContext, UIComponent
129             uiComponent, String JavaDoc hrefAttribute) {
130         StringBuffer JavaDoc hrefBuffer = new StringBuffer JavaDoc(hrefAttribute);
131         Map JavaDoc parameters = getParameterMap(uiComponent);
132         int numberOfParameters = parameters.size();
133         boolean parametersExist = numberOfParameters > 0;
134         if (parametersExist) {
135             hrefBuffer.append("?");
136             Iterator JavaDoc parameterKeys = parameters.keySet().iterator();
137             while (parameterKeys.hasNext()) {
138                 String JavaDoc nextKey = (String JavaDoc) parameterKeys.next();
139                 Object JavaDoc nextValue = parameters.get(nextKey);
140                 hrefBuffer.append(nextKey);
141                 hrefBuffer.append("=");
142                 hrefBuffer.append(
143                         nextValue == null ? null : nextValue.toString());
144                 if (parameterKeys.hasNext()) {
145                     hrefBuffer.append("&");
146                 }
147             }
148         }
149         return hrefBuffer.toString();
150     }
151
152     /**
153      * @param facesContext
154      * @param uiComponent
155      * @throws IOException
156      */

157     public void encodeChildren(FacesContext facesContext,
158                                UIComponent uiComponent)
159             throws IOException JavaDoc {
160         validateParameters(facesContext, uiComponent, UIOutput.class);
161         Iterator JavaDoc children = uiComponent.getChildren().iterator();
162         while (children.hasNext()) {
163             UIComponent nextChild = (UIComponent) children.next();
164             nextChild.encodeBegin(facesContext);
165             if (nextChild.getRendersChildren()) {
166                 nextChild.encodeChildren(facesContext);
167             }
168             nextChild.encodeEnd(facesContext);
169         }
170     }
171
172     /**
173      * @param facesContext
174      * @param uiComponent
175      * @throws IOException
176      */

177     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
178             throws IOException JavaDoc {
179
180         if (facesContext == null || uiComponent == null) {
181             throw new NullPointerException JavaDoc(
182                     "facesContext or uiComponent is null");
183         }
184
185         UIOutput output = (UIOutput) uiComponent;
186
187         if (!output.isRendered()) return;
188
189         ResponseWriter writer = facesContext.getResponseWriter();
190
191         if (writer == null) {
192             throw new NullPointerException JavaDoc("ResponseWriter is null");
193         }
194
195         writer.endElement("a");
196
197     }
198
199
200 }
Popular Tags