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