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.jstl.core;
20  
21  import java.io.IOException;
22  
23  import javax.el.ELException;
24  import javax.faces.FacesException;
25  import javax.faces.application.StateManager;
26  import javax.faces.component.UIComponent;
27  import javax.faces.event.PhaseId;
28  import javax.faces.view.facelets.FaceletContext;
29  import javax.faces.view.facelets.TagAttribute;
30  import javax.faces.view.facelets.TagConfig;
31  import javax.faces.view.facelets.TagHandler;
32  
33  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
34  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
35  import org.apache.myfaces.view.facelets.AbstractFaceletContext;
36  import org.apache.myfaces.view.facelets.FaceletCompositionContext;
37  import org.apache.myfaces.view.facelets.tag.ComponentContainerHandler;
38  import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
39  
40  /**
41   * Simple conditional tag, which evalutes its body if the
42   * supplied condition is true and optionally exposes a Boolean
43   * scripting variable representing the evaluation of this condition
44   * 
45   * @author Jacob Hookom
46   * @version $Id$
47   */
48  @JSFFaceletTag(name="c:if")
49  @JSFFaceletAttribute(
50          name="scope",
51          className="java.lang.String",
52          longDescription="Scope for var.")
53  public final class IfHandler extends TagHandler implements ComponentContainerHandler
54  {
55  
56      /**
57       * The test condition that determines whether or
58       * not the body content should be processed.
59       */
60      @JSFFaceletAttribute(className="boolean", required=true)
61      private final TagAttribute test;
62  
63      /**
64       * Name of the exported scoped variable for the
65       * resulting value of the test condition. The type
66       * of the scoped variable is Boolean.  
67       */
68      @JSFFaceletAttribute(className="java.lang.String")
69      private final TagAttribute var;
70  
71      /**
72       * @param config
73       */
74      public IfHandler(TagConfig config)
75      {
76          super(config);
77          this.test = this.getRequiredAttribute("test");
78          this.var = this.getAttribute("var");
79      }
80  
81      public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, ELException
82      {
83          FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
84          AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
85          String uniqueId = actx.generateUniqueFaceletTagId(
86              fcc.startComponentUniqueIdSection(), tagId);
87          Boolean restoredValue = (Boolean) ComponentSupport.restoreInitialTagState(ctx, fcc, parent, uniqueId);
88          boolean b = false;
89          boolean markInitialState = false;
90          try
91          {
92              if (restoredValue != null)
93              {
94                  if (!PhaseId.RESTORE_VIEW.equals(ctx.getFacesContext().getCurrentPhaseId()))
95                  {
96                      b = this.test.getBoolean(ctx);
97                      if (!restoredValue.equals(b))
98                      {
99                          markInitialState = true;
100                     }
101                 }
102                 else
103                 {
104                     b = restoredValue;
105                 }
106             }
107             else
108             {
109                 // No state restored, calculate
110                 b = this.test.getBoolean(ctx);
111             }
112             //boolean b = getTestValue(ctx, fcc, parent, uniqueId);
113             if (this.var != null)
114             {
115                 ctx.setAttribute(var.getValue(ctx), Boolean.valueOf(b));
116             }
117             if (b)
118             {
119                 boolean oldMarkInitialState = false;
120                 Boolean isBuildingInitialState = null;
121                 try
122                 {
123                     if (markInitialState)
124                     {
125                         //set markInitialState flag
126                         oldMarkInitialState = fcc.isMarkInitialState();
127                         fcc.setMarkInitialState(true);
128                         isBuildingInitialState = (Boolean) ctx.getFacesContext().getAttributes().put(
129                                 StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE);
130                     }
131                     this.nextHandler.apply(ctx, parent);
132                 }
133                 finally
134                 {
135                     if (markInitialState)
136                     {
137                         //unset markInitialState flag
138                         if (isBuildingInitialState == null)
139                         {
140                             ctx.getFacesContext().getAttributes().remove(
141                                     StateManager.IS_BUILDING_INITIAL_STATE);
142                         }
143                         else
144                         {
145                             ctx.getFacesContext().getAttributes().put(
146                                     StateManager.IS_BUILDING_INITIAL_STATE, isBuildingInitialState);
147                         }
148                         fcc.setMarkInitialState(oldMarkInitialState);
149                     }
150                 }
151             }
152         }
153         finally
154         {
155             fcc.endComponentUniqueIdSection();
156         }
157         //AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
158         ComponentSupport.saveInitialTagState(ctx, fcc, parent, uniqueId, b);
159         if (fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() && !fcc.isRefreshingTransientBuild())
160         {
161             //Mark the parent component to be saved and restored fully.
162             ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent);
163         }
164         if (fcc.isDynamicComponentSection())
165         {
166             ComponentSupport.markComponentToRefreshDynamically(ctx.getFacesContext(), parent);
167         }
168     }
169 }