View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.compiler;
20  
21  import java.io.IOException;
22  import java.io.Writer;
23  
24  import javax.el.ELException;
25  import javax.faces.FacesException;
26  import javax.faces.component.UIComponent;
27  import javax.faces.component.UniqueIdVendor;
28  import javax.faces.view.facelets.FaceletContext;
29  import javax.faces.view.facelets.FaceletException;
30  
31  import org.apache.myfaces.view.facelets.FaceletCompositionContext;
32  import org.apache.myfaces.view.facelets.el.ELText;
33  import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
34  import org.apache.myfaces.view.facelets.util.FastWriter;
35  
36  /**
37   * @author Jacob Hookom
38   * @version $Id: UITextHandler.java 1194849 2011-10-29 09:28:45Z struberg $
39   */
40  final class UITextHandler extends AbstractUIHandler
41  {
42  
43      private final ELText txt;
44  
45      private final String alias;
46  
47      private final int length;
48  
49      public UITextHandler(String alias, ELText txt)
50      {
51          this.alias = alias;
52          this.txt = txt;
53          this.length = txt.toString().length();
54      }
55  
56      public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
57              ELException
58      {
59          if (parent != null)
60          {
61              try
62              {
63                  ELText nt = this.txt.apply(ctx.getExpressionFactory(), ctx);
64                  UIComponent c = new UIText(this.alias, nt);
65  
66                  UniqueIdVendor uniqueIdVendor
67                          = FaceletCompositionContext.getCurrentInstance(ctx).getUniqueIdVendorFromStack();
68                  if (uniqueIdVendor == null)
69                  {
70                      uniqueIdVendor = ComponentSupport.getViewRoot(ctx, parent);
71                  }
72                  if (uniqueIdVendor != null)
73                  {
74                      // UIViewRoot implements UniqueIdVendor, so there is no need to cast to UIViewRoot
75                      // and call createUniqueId()
76                      String uid = uniqueIdVendor.createUniqueId(ctx.getFacesContext(), null);
77                      c.setId(uid);
78                  }
79                  this.addComponent(ctx, parent, c);
80              }
81              catch (Exception e)
82              {
83                  throw new ELException(this.alias + ": " + e.getMessage(), e.getCause());
84              }
85          }
86      }
87  
88      public String toString()
89      {
90          return this.txt.toString();
91      }
92  
93      public String getText()
94      {
95          return this.txt.toString();
96      }
97  
98      public String getText(FaceletContext ctx)
99      {
100         Writer writer = new FastWriter(this.length);
101         try
102         {
103             this.txt.apply(ctx.getExpressionFactory(), ctx).write(writer, ctx);
104         }
105         catch (IOException e)
106         {
107             throw new ELException(this.alias + ": " + e.getMessage(), e.getCause());
108         }
109         return writer.toString();
110     }
111 }