KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > webapp > UIComponentClassicTagBase


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 javax.faces.webapp;
30
31 import java.io.*;
32
33 import java.util.*;
34
35 import javax.el.*;
36
37 import javax.faces.application.*;
38 import javax.faces.component.*;
39 import javax.faces.component.html.*;
40 import javax.faces.context.*;
41
42 import javax.servlet.jsp.*;
43 import javax.servlet.jsp.tagext.*;
44
45 public abstract class UIComponentClassicTagBase extends UIComponentTagBase
46   implements JspIdConsumer, BodyTag
47 {
48   protected static final String JavaDoc UNIQUE_ID_PREFIX = "_id_";
49
50   private String JavaDoc _id;
51   private String JavaDoc _jspId;
52
53   protected PageContext pageContext;
54   protected BodyContent bodyContent;
55
56   private FacesContext _facesContext;
57
58   private UIComponentClassicTagBase _parentUIComponentTag;
59   
60   private Tag _parent;
61
62   private UIComponent _component;
63   private boolean _created;
64
65   protected String JavaDoc getFacetName()
66   {
67     return null;
68   }
69
70   public void setJspId(String JavaDoc id)
71   {
72     _jspId = id;
73   }
74
75   public String JavaDoc getJspId()
76   {
77     return _jspId;
78   }
79
80   public void setPageContext(PageContext pageContext)
81   {
82     this.pageContext = pageContext;
83   }
84
85   public Tag getParent()
86   {
87     return _parent;
88   }
89
90   public void setParent(Tag parent)
91   {
92     _parent = parent;
93   }
94
95   public void setBodyContent(BodyContent bodyContent)
96   {
97     this.bodyContent = bodyContent;
98   }
99
100   public BodyContent getBodyContent()
101   {
102     return this.bodyContent;
103   }
104   
105   public JspWriter getPreviousOut()
106   {
107     throw new UnsupportedOperationException JavaDoc();
108   }
109
110   public void setId(String JavaDoc id)
111   {
112     if (id.startsWith(UNIQUE_ID_PREFIX))
113       throw new IllegalArgumentException JavaDoc("id may not begin with " +
114                      UNIQUE_ID_PREFIX);
115
116     _id = id;
117   }
118
119   public String JavaDoc getId()
120   {
121     return _id;
122   }
123
124   protected abstract boolean hasBinding();
125
126   public UIComponent getComponentInstance()
127   {
128     return _component;
129   }
130
131   public boolean getCreated()
132   {
133     return _created;
134   }
135
136   public int doStartTag()
137     throws JspException
138   {
139     PageContext pageContext = this.pageContext;
140
141     _facesContext = FacesContext.getCurrentInstance();
142
143     _parentUIComponentTag
144       = getParentUIComponentClassicTagBase(pageContext);
145
146     _component = null;
147     
148     _component = findComponent(_facesContext);
149
150     pageContext.setAttribute("caucho.jsf.parent", this);
151     
152     return getDoStartValue();
153   }
154
155   /**
156    * Returns the doStart value for the tag. Defaults to
157    * EVAL_BODY_BUFFERED.
158    */

159   protected int getDoStartValue()
160     throws JspException
161   {
162     return BodyTag.EVAL_BODY_BUFFERED;
163   }
164
165   public void doInitBody()
166     throws JspException
167   {
168   }
169
170   public int doAfterBody()
171     throws JspException
172   {
173     UIComponent verbatim = createVerbatimComponentFromBodyContent();
174
175     if (verbatim != null && _component != null)
176       _component.getChildren().add(verbatim);
177
178     return getDoAfterBodyValue();
179   }
180
181   protected int getDoAfterBodyValue()
182     throws JspException
183   {
184     return BodyTag.SKIP_PAGE;
185   }
186
187   public int doEndTag()
188     throws JspException
189   {
190     pageContext.setAttribute("caucho.jsf.parent", _parentUIComponentTag);
191     
192     return getDoEndValue();
193   }
194
195   protected int getDoEndValue()
196     throws JspException
197   {
198     return Tag.EVAL_PAGE;
199   }
200
201   protected abstract UIComponent createComponent(FacesContext context,
202                          String JavaDoc newId)
203     throws JspException;
204     
205   protected abstract void setProperties(UIComponent component);
206   
207   protected UIComponent findComponent(FacesContext context)
208     throws JspException
209   {
210     if (_component != null)
211       return _component;
212
213     UIComponentClassicTagBase parentTag = _parentUIComponentTag;
214
215     if (parentTag == null) {
216       _component = context.getViewRoot();
217       return _component;
218     }
219
220     UIComponent verbatim = parentTag.createVerbatimComponentFromBodyContent();
221
222     UIComponent parent = parentTag.getComponentInstance();
223
224     // XXX: facet
225

226     String JavaDoc id = getId();
227
228     if (id == null)
229       id = UIViewRoot.UNIQUE_ID_PREFIX + getJspId();
230
231     _component = parent.findComponent(id);
232
233     if (_component != null) {
234       if (verbatim != null)
235     addVerbatimBeforeComponent(parentTag, verbatim, _component);
236       
237       return _component;
238     }
239
240     if (verbatim != null) {
241       parent.getChildren().add(verbatim);
242     }
243
244     String JavaDoc componentType = getComponentType();
245
246     // XXX binding
247

248     _component = context.getApplication().createComponent(componentType);
249
250     _component.setId(id);
251
252     setProperties(_component);
253
254     parent.getChildren().add(_component);
255
256     return _component;
257   }
258
259   protected int getIndexOfNextChildTag()
260   {
261     throw new UnsupportedOperationException JavaDoc();
262   }
263
264   protected void addChild(UIComponent child)
265   {
266     getComponentInstance().getChildren().add(child);
267   }
268
269   protected void addFacet(String JavaDoc name)
270   {
271     throw new UnsupportedOperationException JavaDoc();
272   }
273
274   protected UIComponent createVerbatimComponentFromBodyContent()
275   {
276     BodyContent bodyContent = this.bodyContent;
277
278     if (bodyContent == null)
279       return null;
280
281     String JavaDoc text = bodyContent.getString();
282     bodyContent.clearBody();
283     boolean isWhitespace = true;
284
285     for (int i = text.length() - 1; i >= 0; i--) {
286       char ch = text.charAt(i);
287
288       if (! Character.isWhitespace(ch)) {
289     isWhitespace = false;
290     break;
291       }
292     }
293
294     if (isWhitespace)
295       return null;
296
297     UIOutput verbatim = createVerbatimComponent();
298
299     verbatim.setValue(text);
300
301     return verbatim;
302   }
303
304   protected UIOutput createVerbatimComponent()
305   {
306     Application app = _facesContext.getApplication();
307     UIOutput output
308       = (UIOutput) app.createComponent(HtmlOutputText.COMPONENT_TYPE);
309
310     output.setId(_facesContext.getViewRoot().createUniqueId());
311     output.setTransient(true);
312     //output.setEscape(false);
313
output.setRendered(true);
314
315     return output;
316   }
317
318   protected void addVerbatimBeforeComponent(UIComponentClassicTagBase parent,
319                         UIComponent verbatim,
320                         UIComponent component)
321   {
322     throw new UnsupportedOperationException JavaDoc();
323   }
324
325   protected void addVerbatimAfterComponent(UIComponentClassicTagBase parent,
326                         UIComponent verbatim,
327                         UIComponent component)
328   {
329     throw new UnsupportedOperationException JavaDoc();
330   }
331
332   public List<String JavaDoc> getCreatedComponents()
333   {
334     throw new UnsupportedOperationException JavaDoc();
335   }
336
337   protected FacesContext getFacesContext()
338   {
339     return _facesContext;
340   }
341
342   @Deprecated JavaDoc
343   protected void setupResponseWriter()
344   {
345   }
346
347   @Deprecated JavaDoc
348   protected void encodeBegin()
349     throws IOException
350   {
351     UIComponent component = getComponentInstance();
352     FacesContext context = getFacesContext();
353
354     if (component != null && context != null)
355       component.encodeBegin(context);
356   }
357
358   @Deprecated JavaDoc
359   protected void encodeChildren()
360     throws IOException
361   {
362     UIComponent component = getComponentInstance();
363     FacesContext context = getFacesContext();
364
365     if (component != null && context != null)
366       component.encodeChildren(context);
367   }
368
369   @Deprecated JavaDoc
370   protected void encodeEnd()
371     throws IOException
372   {
373     UIComponent component = getComponentInstance();
374     FacesContext context = getFacesContext();
375
376     if (component != null && context != null)
377       component.encodeEnd(context);
378   }
379
380   public void release()
381   {
382   }
383   
384   public static UIComponentClassicTagBase
385     getParentUIComponentClassicTagBase(PageContext pageContext)
386   {
387     return (UIComponentClassicTagBase)
388       pageContext.getAttribute("caucho.jsf.parent");
389   }
390 }
391
Popular Tags