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  
20  package org.apache.myfaces.tobago.internal.component;
21  
22  import org.apache.myfaces.tobago.util.FacesVersion;
23  
24  import javax.faces.component.UIComponentBase;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.context.FacesContext;
27  import javax.faces.event.ComponentSystemEvent;
28  import javax.faces.event.ListenerFor;
29  import javax.faces.event.PostAddToViewEvent;
30  import javax.faces.event.PreRenderViewEvent;
31  
32  @ListenerFor(systemEventClass = PostAddToViewEvent.class)
33  public abstract class AbstractUIMetaLink extends UIComponentBase {
34  
35    @Override
36    public void processEvent(final ComponentSystemEvent event) {
37  
38      super.processEvent(event);
39  
40      if (event instanceof PreRenderViewEvent) {
41        addComponentResource();
42      } else if (event instanceof PostAddToViewEvent) {
43        if (FacesVersion.supports21() || !FacesVersion.isMyfaces()) {
44          // MyFaces core is removing the component resources in head if the view will be recreated before rendering.
45          // The view will be recreated because of expressions. For example  expressins in the ui:include src attribute
46          // The PostAddToViewEvent will not be broadcasted in this case again.
47          // A subscription to the PreRenderViewEvent avoids this problem
48          // NOTE: PreRenderViewEvent can not used in myfaces prior 2.0.3 using PostAddToView for all myfaces 2.0 versions
49          getFacesContext().getViewRoot().subscribeToEvent(PreRenderViewEvent.class, this);
50        } else {
51          addComponentResource();
52        }
53      }
54    }
55  
56    private void addComponentResource() {
57      final FacesContext facesContext = getFacesContext();
58      final UIViewRoot root = facesContext.getViewRoot();
59      root.addComponentResource(facesContext, this);
60    }
61  
62    public abstract String getCharset();
63  
64    public abstract String getRev();
65  
66    public abstract String getHref();
67  
68    public abstract String getHreflang();
69  
70    public abstract String getRel();
71  
72    public abstract String getMedia();
73  
74    public abstract String getType();
75  }