KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsf > html > HtmlOutputTextRenderer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2
11  * as published by the Free Software Foundation.
12  *
13  * Resin Open Source is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
16  * of NON-INFRINGEMENT. See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with Resin Open Source; if not, write to the
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsf.html;
30
31 import java.io.*;
32 import java.util.*;
33
34 import javax.faces.*;
35 import javax.faces.component.*;
36 import javax.faces.component.html.*;
37 import javax.faces.context.*;
38 import javax.faces.render.*;
39
40 /**
41  * The HTML text renderer
42  */

43 class HtmlOutputTextRenderer extends Renderer
44 {
45   public static final Renderer RENDERER = new HtmlOutputTextRenderer();
46
47   /**
48    * True if the renderer is responsible for rendering the children.
49    */

50   @Override JavaDoc
51   public boolean getRendersChildren()
52   {
53     return true;
54   }
55   /**
56    * Renders the open tag for the text.
57    */

58   @Override JavaDoc
59   public void encodeBegin(FacesContext context, UIComponent component)
60     throws IOException
61   {
62     ResponseWriter out = context.getResponseWriter();
63
64     String JavaDoc id = component.getId();
65     String JavaDoc dir;
66     String JavaDoc lang;
67     String JavaDoc style;
68     String JavaDoc styleClass;
69     String JavaDoc title;
70     
71     if (component instanceof HtmlOutputText) {
72       HtmlOutputText htmlOutput = (HtmlOutputText) component;
73
74       dir = htmlOutput.getDir();
75       lang = htmlOutput.getLang();
76       style = htmlOutput.getStyle();
77       styleClass = htmlOutput.getStyleClass();
78       title = htmlOutput.getTitle();
79     }
80     else {
81       Map<String JavaDoc,Object JavaDoc> attrMap = component.getAttributes();
82     
83       dir = (String JavaDoc) attrMap.get("dir");
84       lang = (String JavaDoc) attrMap.get("lang");
85       style = (String JavaDoc) attrMap.get("style");
86       styleClass = (String JavaDoc) attrMap.get("styleClass");
87       title = (String JavaDoc) attrMap.get("title");
88     }
89
90     if (dir == null && lang == null && style == null && styleClass == null)
91       return;
92
93     out.startElement("span", component);
94
95     if (id != null && ! id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
96       out.writeAttribute("id", component.getClientId(context), "id");
97
98     if (dir != null)
99       out.writeAttribute("dir", dir, "dir");
100
101     if (lang != null)
102       out.writeAttribute("lang", lang, "dir");
103
104     if (style != null)
105       out.writeAttribute("style", style, "style");
106
107     if (styleClass != null)
108       out.writeAttribute("class", styleClass, "class");
109
110     if (title != null)
111       out.writeAttribute("title", title, "title");
112   }
113
114   /**
115    * Renders the content for the component.
116    */

117   @Override JavaDoc
118   public void encodeChildren(FacesContext context, UIComponent component)
119     throws IOException
120   {
121     ResponseWriter out = context.getResponseWriter();
122
123     if (component instanceof HtmlOutputText) {
124       HtmlOutputText htmlOutput = (HtmlOutputText) component;
125
126       Object JavaDoc value = htmlOutput.getValue();
127
128       if (value == null)
129     return;
130
131       out.writeText(value, "value");
132     }
133     else {
134       Map<String JavaDoc,Object JavaDoc> attrMap = component.getAttributes();
135
136       Object JavaDoc value = attrMap.get("value");
137
138       if (value == null)
139     return;
140
141       out.writeText(value, "value");
142     }
143   }
144
145   /**
146    * Renders the closing tag for the component.
147    */

148   @Override JavaDoc
149   public void encodeEnd(FacesContext context, UIComponent component)
150     throws IOException
151   {
152     ResponseWriter out = context.getResponseWriter();
153     
154     if (component instanceof HtmlOutputText) {
155       HtmlOutputText htmlOutput = (HtmlOutputText) component;
156
157       if (htmlOutput.getStyleClass() != null
158       || htmlOutput.getStyle() != null
159       || htmlOutput.getDir() != null
160       || htmlOutput.getLang() != null) {
161     out.endElement("span");
162       }
163     }
164     else {
165       Map<String JavaDoc,Object JavaDoc> attrMap = component.getAttributes();
166
167       if (attrMap.get("styleClass") != null
168       || attrMap.get("style") != null
169       || attrMap.get("dir") != null
170       || attrMap.get("lang") != null) {
171     out.endElement("span");
172       }
173     }
174   }
175
176   public String JavaDoc toString()
177   {
178     return "HtmlOutputTextRenderer[]";
179   }
180 }
181
Popular Tags