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 javax.faces.component.UIComponent;
037: import javax.faces.component.UIOutput;
038: import javax.faces.context.FacesContext;
039: import javax.faces.context.ResponseWriter;
040: import java.io.IOException;
041: import java.util.Iterator;
042: import java.util.Map;
043:
044: public class OutputLinkRenderer extends DomBasicRenderer {
045: /**
046: * @return false as this component does not specially
047: * handle rendering its children.
048: */
049: public boolean getRendersChildren() {
050: return false;
051: }
052:
053: /**
054: * Returns the value of this component as an Object.
055: *
056: * @param uiComponent the component to retrieve the value from.
057: * @return value as an Object.
058: */
059: protected Object getValue(UIComponent uiComponent) {
060: return ((UIOutput) uiComponent).getValue();
061: }
062:
063: String clientId;
064:
065: /**
066: * @param facesContext
067: * @param uiComponent
068: * @throws IOException
069: */
070: public void encodeBegin(FacesContext facesContext,
071: UIComponent uiComponent) throws IOException {
072: if (facesContext == null || uiComponent == null) {
073: throw new NullPointerException(
074: "facesContext or uiComponent is null");
075: }
076:
077: UIOutput output = (UIOutput) uiComponent;
078: String linkVal = getValue(facesContext, uiComponent);
079:
080: if (!output.isRendered())
081: return;
082:
083: ResponseWriter writer = facesContext.getResponseWriter();
084:
085: if (writer == null) {
086: throw new NullPointerException("ResponseWriter is null");
087: }
088:
089: writer.startElement("a", uiComponent);
090: writer.writeAttribute("id", uiComponent
091: .getClientId(facesContext), "id");
092:
093: if (null == linkVal || 0 == linkVal.length()) {
094: linkVal = "";
095: }
096:
097: clientId = output.getClientId(facesContext);
098:
099: linkVal = appendParameters(facesContext, uiComponent, linkVal);
100:
101: if (!checkDisabled(uiComponent))
102: writer.writeURIAttribute("href", facesContext
103: .getExternalContext().encodeResourceURL(linkVal),
104: "href");
105:
106: PassThruAttributeWriter.renderAttributes(writer, uiComponent,
107: null);
108:
109: String styleClass = (String) output.getAttributes().get(
110: "styleClass");
111: if (styleClass != null)
112: writer.writeAttribute("class", styleClass, "styleClass");
113:
114: writer.flush();
115: }
116:
117: /**
118: * @param uiComponent
119: * @return boolean
120: */
121: protected boolean checkDisabled(UIComponent uiComponent) {
122: return false;
123: }
124:
125: /**
126: * @param facesContext
127: * @param uiComponent
128: * @param hrefAttribute
129: * @return String
130: */
131: private String appendParameters(FacesContext facesContext,
132: UIComponent uiComponent, String hrefAttribute) {
133: StringBuffer hrefBuffer = new StringBuffer(hrefAttribute);
134: Map parameters = getParameterMap(uiComponent);
135: int numberOfParameters = parameters.size();
136: boolean parametersExist = numberOfParameters > 0;
137: if (parametersExist) {
138: hrefBuffer.append("?");
139: Iterator parameterKeys = parameters.keySet().iterator();
140: while (parameterKeys.hasNext()) {
141: String nextKey = (String) parameterKeys.next();
142: Object nextValue = parameters.get(nextKey);
143: hrefBuffer.append(nextKey);
144: hrefBuffer.append("=");
145: hrefBuffer.append(nextValue == null ? null : nextValue
146: .toString());
147: if (parameterKeys.hasNext()) {
148: hrefBuffer.append("&");
149: }
150: }
151: }
152: return hrefBuffer.toString();
153: }
154:
155: /**
156: * @param facesContext
157: * @param uiComponent
158: * @throws IOException
159: */
160: public void encodeEnd(FacesContext facesContext,
161: UIComponent uiComponent) throws IOException {
162:
163: if (facesContext == null || uiComponent == null) {
164: throw new NullPointerException(
165: "facesContext or uiComponent is null");
166: }
167:
168: UIOutput output = (UIOutput) uiComponent;
169:
170: if (!output.isRendered())
171: return;
172:
173: ResponseWriter writer = facesContext.getResponseWriter();
174:
175: if (writer == null) {
176: throw new NullPointerException("ResponseWriter is null");
177: }
178:
179: writer.endElement("a");
180:
181: }
182:
183: }
|