001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsf.html;
030:
031: import java.io.*;
032: import java.util.*;
033:
034: import javax.faces.*;
035: import javax.faces.component.*;
036: import javax.faces.component.html.*;
037: import javax.faces.context.*;
038: import javax.faces.render.*;
039:
040: /**
041: * The HTML link renderer
042: */
043: class HtmlOutputLinkRenderer extends BaseRenderer {
044: public static final Renderer RENDERER = new HtmlOutputLinkRenderer();
045:
046: /**
047: * True if the renderer is responsible for rendering the children.
048: */
049: @Override
050: public boolean getRendersChildren() {
051: return true;
052: }
053:
054: /**
055: * Renders the open tag for the text.
056: */
057: @Override
058: public void encodeBegin(FacesContext context, UIComponent component)
059: throws IOException {
060: ResponseWriter out = context.getResponseWriter();
061:
062: String id = component.getId();
063: String dir;
064: String lang;
065: String style;
066: String styleClass;
067: String target;
068: String title;
069:
070: Object value;
071:
072: if (component instanceof HtmlOutputLink) {
073: HtmlOutputLink htmlOutput = (HtmlOutputLink) component;
074:
075: dir = htmlOutput.getDir();
076: lang = htmlOutput.getLang();
077: target = htmlOutput.getTarget();
078: style = htmlOutput.getStyle();
079: styleClass = htmlOutput.getStyleClass();
080: title = htmlOutput.getTitle();
081:
082: value = htmlOutput.getValue();
083: } else {
084: Map<String, Object> attrMap = component.getAttributes();
085:
086: dir = (String) attrMap.get("dir");
087: lang = (String) attrMap.get("lang");
088: target = (String) attrMap.get("target");
089: style = (String) attrMap.get("style");
090: styleClass = (String) attrMap.get("styleClass");
091: title = (String) attrMap.get("title");
092:
093: value = attrMap.get("value");
094: }
095:
096: out.startElement("a", component);
097:
098: if (id != null && !id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
099: out.writeAttribute("id", component.getClientId(context),
100: "id");
101:
102: if (dir != null)
103: out.writeAttribute("dir", dir, "dir");
104:
105: if (lang != null)
106: out.writeAttribute("lang", lang, "lang");
107:
108: if (style != null)
109: out.writeAttribute("style", style, "style");
110:
111: if (target != null)
112: out.writeAttribute("target", lang, "target");
113:
114: if (styleClass != null)
115: out.writeAttribute("class", styleClass, "class");
116:
117: if (title != null)
118: out.writeAttribute("title", title, "title");
119:
120: int childCount = component.getChildCount();
121:
122: String href = toString(context, component, value);
123:
124: StringBuilder sb = null;
125: for (int i = 0; i < childCount; i++) {
126: UIComponent child = component.getChildren().get(i);
127:
128: if (child instanceof UIParameter) {
129: if (sb == null) {
130: sb = new StringBuilder().append(href);
131:
132: if (href.indexOf('?') < 0)
133: sb.append('?');
134: else
135: sb.append('&');
136: } else
137: sb.append('&');
138:
139: UIParameter param = (UIParameter) child;
140:
141: String name = param.getName();
142: Object paramValue = param.getValue();
143:
144: if (name != null) {
145: sb.append(name);
146: sb.append('=');
147: }
148:
149: sb.append(toString(context, param, paramValue));
150: }
151: }
152:
153: if (sb != null)
154: out.writeAttribute("href", sb.toString(), "href");
155: else
156: out.writeAttribute("href", href, "href");
157:
158: if (childCount > 0) {
159: List<UIComponent> children = component.getChildren();
160:
161: for (int i = 0; i < childCount; i++) {
162: UIComponent child = children.get(i);
163:
164: if (child instanceof UIParameter)
165: continue;
166:
167: if (child.isRendered()) {
168: child.encodeBegin(context);
169: child.encodeChildren(context);
170: child.encodeEnd(context);
171: }
172: }
173: }
174:
175: out.endElement("a");
176: }
177:
178: /**
179: * Renders the content for the component.
180: */
181: @Override
182: public void encodeChildren(FacesContext context,
183: UIComponent component) throws IOException {
184: ResponseWriter out = context.getResponseWriter();
185: }
186:
187: /**
188: * Renders the closing tag for the component.
189: */
190: @Override
191: public void encodeEnd(FacesContext context, UIComponent component)
192: throws IOException {
193: }
194:
195: public String toString() {
196: return "HtmlOutputTextRenderer[]";
197: }
198: }
|