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