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.tag.jsf.core;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import javax.el.ELException;
26  import javax.el.MethodExpression;
27  import javax.faces.FacesException;
28  import javax.faces.component.UIComponent;
29  import javax.faces.component.UIViewRoot;
30  import javax.faces.event.PhaseEvent;
31  import javax.faces.view.facelets.FaceletContext;
32  import javax.faces.view.facelets.FaceletException;
33  import javax.faces.view.facelets.TagAttribute;
34  import javax.faces.view.facelets.TagConfig;
35  import javax.faces.view.facelets.TagHandler;
36  
37  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
38  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
39  import org.apache.myfaces.shared.util.StringUtils;
40  import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
41  
42  /**
43   * Container for all JavaServer Faces core and custom component actions used on a page. <p/> See <a target="_new"
44   * href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/tlddocs/f/view.html">tag documentation</a>.
45   * 
46   * @author Jacob Hookom
47   * @version $Id$
48   */
49  @JSFFaceletTag(
50          name = "f:view",
51          bodyContent = "empty", 
52          componentClass="javax.faces.component.UIViewRoot")
53  public final class ViewHandler extends TagHandler
54  {
55  
56      private final static Class<?>[] LISTENER_SIG = new Class<?>[] { PhaseEvent.class };
57  
58      @JSFFaceletAttribute
59      private final TagAttribute locale;
60  
61      @JSFFaceletAttribute
62      private final TagAttribute renderKitId;
63  
64      @JSFFaceletAttribute
65      private final TagAttribute contentType;
66  
67      @JSFFaceletAttribute
68      private final TagAttribute encoding;
69  
70      @JSFFaceletAttribute
71      private final TagAttribute beforePhase;
72  
73      @JSFFaceletAttribute
74      private final TagAttribute afterPhase;
75      
76      @JSFFaceletAttribute(name="transient")
77      private final TagAttribute transientAttribute;
78      
79      private final TagAttribute contracts;
80      
81      private final TagAttribute oamEnableViewPool;
82  
83      /**
84       * @param config
85       */
86      public ViewHandler(TagConfig config)
87      {
88          super(config);
89          this.locale = this.getAttribute("locale");
90          this.renderKitId = this.getAttribute("renderKitId");
91          this.contentType = this.getAttribute("contentType");
92          this.encoding = this.getAttribute("encoding");
93          this.beforePhase = this.getAttribute("beforePhase");
94          this.afterPhase = this.getAttribute("afterPhase");
95          this.transientAttribute = this.getAttribute("transient");
96          this.contracts = this.getAttribute("contracts");
97          this.oamEnableViewPool = this.getAttribute("oamEnableViewPool");
98      }
99  
100     /**
101      * See taglib documentation.
102      * 
103      * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext, javax.faces.component.UIComponent)
104      */
105     public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
106             ELException
107     {
108         UIViewRoot root = ComponentSupport.getViewRoot(ctx, parent);
109         if (root != null)
110         {
111             if (this.locale != null)
112             {
113                 root.setLocale(ComponentSupport.getLocale(ctx, this.locale));
114             }
115             if (this.renderKitId != null)
116             {
117                 String v = this.renderKitId.getValue(ctx);
118                 root.setRenderKitId(v);
119             }
120             String encodingValue = null;
121             if (this.contentType != null)
122             {
123                 // This value is read as rfc2616 section 3.7 Media Types.
124                 // We should check and extract the param "charset" and assing
125                 // it as encoding for this page.
126                 String v = this.contentType.getValue(ctx);
127                 if (v != null)
128                 {
129                     int j = v.indexOf(';');
130                     if (j >= 0)
131                     {
132                         int i = v.indexOf("charset",j);
133                         if (i >= 0)
134                         {
135                             i = v.indexOf('=',i)+1;
136                             if (v.length() > i)
137                             {
138                                 encodingValue = v.substring(i);
139                             }
140                             // Substract charset from encoding, it will be added 
141                             // later on FaceletViewDeclarationLanguage.createResponseWriter
142                             // by calling response.setContentType
143                             v = v.substring(0 , j);
144                         }
145                     }
146                 }
147                 ctx.getFacesContext().getAttributes().put("facelets.ContentType", v);
148             }
149             if (this.encoding != null)
150             {
151                 String v = this.encoding.getValue(ctx);
152                 ctx.getFacesContext().getAttributes().put("facelets.Encoding", v);
153             }
154             else if (encodingValue != null)
155             {
156                 ctx.getFacesContext().getAttributes().put("facelets.Encoding", encodingValue);
157             }
158             if (this.beforePhase != null)
159             {
160                 MethodExpression m = this.beforePhase.getMethodExpression(ctx, null, LISTENER_SIG);
161                 root.setBeforePhaseListener(m);
162             }
163             if (this.afterPhase != null)
164             {
165                 MethodExpression m = this.afterPhase.getMethodExpression(ctx, null, LISTENER_SIG);
166                 root.setAfterPhaseListener(m);
167             }
168             if (this.transientAttribute != null)
169             {
170                 root.setTransient(this.transientAttribute.getBoolean(ctx));
171             }
172             if (this.contracts != null)
173             {
174                 String contractsValue = this.contracts.getValue(ctx);
175                 if (contractsValue != null)
176                 {
177                     String[] values = StringUtils.trim(
178                         StringUtils.splitShortString(contractsValue, ','));
179                     if (values != null)
180                     {
181                         List<String> list = new ArrayList<String>();
182                         for (String v : values)
183                         {
184                             list.add(v);
185                         }
186                         ctx.getFacesContext().setResourceLibraryContracts(list);
187                     }
188                 }
189             }
190             if (this.oamEnableViewPool != null)
191             {
192                 root.getAttributes().put("oamEnableViewPool", this.oamEnableViewPool.getBoolean(ctx));
193             }
194         }
195         this.nextHandler.apply(ctx, parent);
196     }
197 
198 }