KickJava   Java API By Example, From Geeks To Geeks.

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


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.Writer JavaDoc;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.el.ELException;
24 import javax.faces.FacesException;
25 import javax.faces.component.UIComponent;
26
27 import com.sun.facelets.FaceletContext;
28 import com.sun.facelets.FaceletException;
29 import com.sun.facelets.FaceletHandler;
30 import com.sun.facelets.el.ELText;
31 import com.sun.facelets.tag.TextHandler;
32 import com.sun.facelets.tag.jsf.ComponentSupport;
33 import com.sun.facelets.util.FastWriter;
34
35 /**
36  * @author Adam Winer
37  * @version $Id: UIInstructionHandler.java,v 1.2 2006/03/29 04:10:04 jhook Exp $
38  */

39 final class UIInstructionHandler implements FaceletHandler, TextHandler {
40     private final String JavaDoc alias;
41
42     private final ELText txt;
43     
44     private final Instruction[] instructions;
45
46     private final int length;
47   
48     private final boolean literal;
49
50     public UIInstructionHandler(String JavaDoc alias, Instruction[] instructions, ELText txt) {
51         this.alias = alias;
52         this.instructions = instructions;
53         this.txt = txt;
54         this.length = txt.toString().length();
55
56         boolean literal = true;
57         int size = instructions.length;
58
59         for (int i = 0; i < size; i++) {
60             Instruction ins = (Instruction) this.instructions[i];
61             if (!ins.isLiteral()) {
62                 literal = false;
63                 break;
64             }
65         }
66
67         this.literal = literal;
68     }
69
70     public void apply(FaceletContext ctx, UIComponent parent)
71             throws IOException JavaDoc, FacesException, FaceletException, ELException {
72         if (parent != null) {
73             Instruction[] applied;
74             if (this.literal) {
75                 applied = this.instructions;
76             } else {
77                 int size = this.instructions.length;
78                 applied = new Instruction[size];
79                 // Create a new list with all of the necessary applied
80
// instructions
81
Instruction ins;
82                 for (int i = 0; i < size; i++) {
83                     ins = this.instructions[i];
84                     applied[i] = ins.apply(ctx.getExpressionFactory(), ctx);
85                 }
86             }
87
88             UIComponent c = new UIInstructions(txt, applied);
89             c.setId(ComponentSupport.getViewRoot(ctx, parent).createUniqueId());
90             parent.getChildren().add(c);
91         }
92     }
93
94     public String JavaDoc toString() {
95         return this.txt.toString();
96     }
97
98     public String JavaDoc getText() {
99         return this.txt.toString();
100     }
101
102     public String JavaDoc getText(FaceletContext ctx) {
103         Writer JavaDoc writer = new FastWriter(this.length);
104         try {
105             this.txt.apply(ctx.getExpressionFactory(), ctx).write(writer, ctx);
106         } catch (IOException JavaDoc e) {
107             throw new ELException(this.alias + ": "+ e.getMessage(), e.getCause());
108         }
109         return writer.toString();
110     }
111 }
112
Popular Tags