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  
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UIPanel;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.view.facelets.FaceletContext;
27  import javax.faces.view.facelets.TagConfig;
28  import javax.faces.view.facelets.TagException;
29  import javax.faces.view.facelets.TagHandler;
30  
31  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
32  import org.apache.myfaces.view.facelets.FaceletCompositionContext;
33  import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
34  
35  /**
36   * Defines the view metadata. It is expected that this tag contains only
37   * one or many f:viewParam tags.
38   * 
39   * @author Leonardo Uribe (latest modification by $Author$)
40   * @version $Revision$ $Date$
41   */
42  @JSFFaceletTag(name="f:metadata")
43  public final class ViewMetadataHandler extends TagHandler
44  {
45  
46      public ViewMetadataHandler(TagConfig config)
47      {
48          super(config);
49      }
50  
51      public void apply(FaceletContext ctx, UIComponent parent)
52              throws IOException
53      {
54          if (parent == null)
55          {
56              throw new TagException(this.tag, "Parent UIComponent was null");
57          }
58          if (! (parent instanceof UIViewRoot) )
59          {
60              throw new TagException(this.tag, "Parent UIComponent "+parent.getId()+" should be instance of UIViewRoot");
61          }
62          FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(ctx);
63          if (mctx.isBuildingViewMetadata())
64          {
65              UIComponent metadataFacet = parent.getFacet(UIViewRoot.METADATA_FACET_NAME);
66              if (metadataFacet == null)
67              {
68                  metadataFacet = ctx.getFacesContext().getApplication().createComponent(
69                          ctx.getFacesContext(), UIPanel.COMPONENT_TYPE, null);
70                  metadataFacet.setId(UIViewRoot.METADATA_FACET_NAME);
71                  metadataFacet.getAttributes().put(ComponentSupport.FACET_CREATED_UIPANEL_MARKER, true);
72                  metadataFacet.getAttributes().put(ComponentSupport.COMPONENT_ADDED_BY_HANDLER_MARKER, Boolean.TRUE);
73                  parent.getFacets().put(UIViewRoot.METADATA_FACET_NAME, metadataFacet);
74              }
75          }
76  
77          // We have to do nextHandler.apply() in any case, because even if we're not building ViewMetadata
78          // we still need to do it so that the mark/delete components can be applied correctly.
79          // (The only tag that needs to do something special is f:event, because in this case
80          // ComponentHandler.isNew(parent) does not work for UIViewRoot.)
81          parent.getAttributes().put(FacetHandler.KEY, UIViewRoot.METADATA_FACET_NAME);
82          mctx.startMetadataSection();
83          try
84          {
85              this.nextHandler.apply(ctx, parent);
86          }
87          finally
88          {
89              mctx.endMetadataSection();
90              parent.getAttributes().remove(FacetHandler.KEY);
91          }
92      }
93  }