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