KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > compiler > UITextHandler


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.compiler;
16
17 import java.io.IOException JavaDoc;
18 import java.io.StringWriter JavaDoc;
19 import java.io.Writer JavaDoc;
20
21 import javax.el.ELException;
22 import javax.faces.FacesException;
23 import javax.faces.component.UIComponent;
24
25 import com.sun.facelets.FaceletContext;
26 import com.sun.facelets.FaceletException;
27 import com.sun.facelets.FaceletHandler;
28 import com.sun.facelets.el.ELText;
29 import com.sun.facelets.tag.TextHandler;
30 import com.sun.facelets.tag.jsf.ComponentSupport;
31 import com.sun.facelets.util.FastWriter;
32
33 /**
34  * @author Jacob Hookom
35  * @version $Id: UITextHandler.java,v 1.8 2006/03/29 04:10:05 jhook Exp $
36  */

37 final class UITextHandler implements FaceletHandler, TextHandler {
38
39     private final ELText txt;
40     
41     private final String JavaDoc alias;
42     
43     private final int length;
44
45     public UITextHandler(String JavaDoc alias, ELText txt) {
46         this.alias = alias;
47         this.txt = txt;
48         this.length = txt.toString().length();
49     }
50
51     public void apply(FaceletContext ctx, UIComponent parent)
52             throws IOException JavaDoc, FacesException, FaceletException, ELException {
53         if (parent != null) {
54             try {
55                 ELText nt = this.txt.apply(ctx.getExpressionFactory(), ctx);
56                 UIComponent c = new UIText(this.alias, nt);
57                 c.setId(ComponentSupport.getViewRoot(ctx, parent).createUniqueId());
58                 //c.getAttributes().put(ComponentSupport.MARK_CREATED, ctx.generateUniqueId(this.tagId));
59
parent.getChildren().add(c);
60             } catch (Exception JavaDoc e) {
61                 throw new ELException(this.alias + ": "+ e.getMessage(), e.getCause());
62             }
63         }
64     }
65
66     public String JavaDoc toString() {
67         return this.txt.toString();
68     }
69
70     public String JavaDoc getText() {
71         return this.txt.toString();
72     }
73
74     public String JavaDoc getText(FaceletContext ctx) {
75         Writer JavaDoc writer = new FastWriter(this.length);
76         try {
77             this.txt.apply(ctx.getExpressionFactory(), ctx).write(writer, ctx);
78         } catch (IOException JavaDoc e) {
79             throw new ELException(this.alias + ": "+ e.getMessage(), e.getCause());
80         }
81         return writer.toString();
82     }
83 }
84
Popular Tags