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.AbstractFaceletContext;
32  import org.apache.myfaces.view.facelets.FaceletCompositionContext;
33  import org.apache.myfaces.view.facelets.el.ELText;
34  import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
35  import org.apache.myfaces.view.facelets.util.FastWriter;
36  
37  /**
38   * @author Jacob Hookom
39   * @version $Id: UITextHandler.java 1187700 2011-10-22 12:19:37Z bommel $
40   */
41  final class UITextHandler extends AbstractUIHandler
42  {
43  
44      private final ELText txt;
45  
46      private final String alias;
47  
48      private final int length;
49  
50      public UITextHandler(String alias, ELText txt)
51      {
52          this.alias = alias;
53          this.txt = txt;
54          this.length = txt.toString().length();
55      }
56  
57      public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
58              ELException
59      {
60          if (parent != null)
61          {
62              try
63              {
64                  ELText nt = this.txt.apply(ctx.getExpressionFactory(), ctx);
65                  UIComponent c = new UIText(this.alias, nt);
66                  //c.setId(ComponentSupport.getViewRoot(ctx, parent).createUniqueId());
67  
68                  UniqueIdVendor uniqueIdVendor = FaceletCompositionContext.getCurrentInstance(ctx).getUniqueIdVendorFromStack();
69                  if (uniqueIdVendor == null)
70                  {
71                      uniqueIdVendor = ComponentSupport.getViewRoot(ctx, parent);
72                  }
73                  if (uniqueIdVendor != null)
74                  {
75                      // UIViewRoot implements UniqueIdVendor, so there is no need to cast to UIViewRoot
76                      // and call createUniqueId()
77                      String uid = uniqueIdVendor.createUniqueId(ctx.getFacesContext(), null);
78                      c.setId(uid);
79                  }
80                  this.addComponent(ctx, parent, c);
81              }
82              catch (Exception e)
83              {
84                  throw new ELException(this.alias + ": " + e.getMessage(), e.getCause());
85              }
86          }
87      }
88  
89      public String toString()
90      {
91          return this.txt.toString();
92      }
93  
94      public String getText()
95      {
96          return this.txt.toString();
97      }
98  
99      public String getText(FaceletContext ctx)
100     {
101         Writer writer = new FastWriter(this.length);
102         try
103         {
104             this.txt.apply(ctx.getExpressionFactory(), ctx).write(writer, ctx);
105         }
106         catch (IOException e)
107         {
108             throw new ELException(this.alias + ": " + e.getMessage(), e.getCause());
109         }
110         return writer.toString();
111     }
112 }