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.renderkit.html;
20  
21  import java.io.IOException;
22  import java.util.List;
23  
24  import javax.faces.application.ProjectStage;
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIViewRoot;
27  import javax.faces.context.FacesContext;
28  import javax.faces.context.ResponseWriter;
29  import javax.faces.render.Renderer;
30  
31  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderer;
32  import org.apache.myfaces.shared.config.MyfacesConfig;
33  import org.apache.myfaces.shared.renderkit.html.HTML;
34  import org.apache.myfaces.shared.renderkit.html.HtmlRendererUtils;
35  
36  /**
37   * Renderer used by h:head component
38   * 
39   * @since 2.0
40   * @author Leonardo Uribe (latest modification by $Author$)
41   * @version $Revision$ $Date$
42   */
43  @JSFRenderer(renderKitId = "HTML_BASIC", family = "javax.faces.Output", type = "javax.faces.Head")
44  public class HtmlHeadRenderer extends Renderer
45  {
46      //TODO: Move constants to shared HTML class
47      private final static String HEAD_ELEM = "head";
48      private final static String HEAD_TARGET = HEAD_ELEM;
49  
50      private final static String PROFILE_ATTR = "profile";
51  
52      private final static String[] HEAD_PASSTHROUGH_ATTRIBUTES = { HTML.DIR_ATTR,
53              HTML.LANG_ATTR, PROFILE_ATTR};
54  
55      @Override
56      public void encodeBegin(FacesContext facesContext, UIComponent component)
57              throws IOException
58      {
59          super.encodeBegin(facesContext, component); //check for NP
60  
61          ResponseWriter writer = facesContext.getResponseWriter();
62          writer.startElement(HEAD_ELEM, component);
63          HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
64          HtmlRendererUtils.renderHTMLAttributes(writer, component,
65                  HEAD_PASSTHROUGH_ATTRIBUTES);
66          HtmlRendererUtils.renderHTMLAttribute(writer, component, HTML.XMLNS_ATTR , HTML.XMLNS_ATTR);
67      }
68  
69      @Override
70      public void encodeEnd(FacesContext facesContext, UIComponent component)
71              throws IOException
72      {
73          super.encodeEnd(facesContext, component); //check for NP
74  
75          ResponseWriter writer = facesContext.getResponseWriter();
76          UIViewRoot root = facesContext.getViewRoot();
77          List<UIComponent> componentResources = root.getComponentResources(facesContext,
78                  HEAD_TARGET);
79          
80          for (int i = 0, childCount = componentResources.size(); i < childCount; i++)
81          {
82              UIComponent child = componentResources.get(i);
83              child.encodeAll(facesContext);
84          }
85          writer.endElement(HEAD_ELEM);
86  
87          if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isEarlyFlushEnabled() &&
88                  facesContext.isProjectStage(ProjectStage.Production))
89          {
90              writer.flush();
91          }
92      }
93  }