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.custom.document;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIViewRoot;
27  import javax.faces.component.behavior.ClientBehavior;
28  import javax.faces.component.behavior.ClientBehaviorHolder;
29  import javax.faces.context.FacesContext;
30  import javax.faces.context.ResponseWriter;
31  
32  import org.apache.myfaces.renderkit.html.util.AddResource;
33  import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
34  import org.apache.myfaces.renderkit.html.util.ExtensionsPhaseListener;
35  import org.apache.myfaces.shared_tomahawk.renderkit.html.util.JavascriptUtils;
36  import org.apache.myfaces.shared_tomahawk.renderkit.html.util.ResourceUtils;
37  import org.apache.myfaces.shared_tomahawk.renderkit.ClientBehaviorEvents;
38  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
39  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
40  import org.apache.myfaces.tomahawk.util.TomahawkResourceUtils;
41  
42  /**
43   * Document to enclose the whole document. If not otherwise possible you can use
44   * state="start|end" to demarkate the document boundaries
45   *
46   * @JSFRenderer
47   *   renderKitId = "HTML_BASIC"
48   *   family = "javax.faces.Data"
49   *   type = "org.apache.myfaces.DocumentBody"
50   *
51   * @author Mario Ivankovits (latest modification by $Author: lu4242 $)
52   * @version $Revision: 943798 $ $Date: 2010-05-13 00:00:09 -0500 (Jue, 13 May 2010) $
53   */
54  public class DocumentBodyRenderer extends AbstractDocumentRenderer
55  {
56      public static final String RENDERER_TYPE = "org.apache.myfaces.DocumentBody";
57      private String BODY_ELEM = "body";
58      private String[] ATTRS = new String[] {"onload", "onunload", "onresize", "onkeypress", "style", "styleClass", "id"};
59      
60      private final String ONRESIZE = "onresize";
61      private final String RESIZE = "resize";
62  
63      protected String getHtmlTag()
64      {
65          return BODY_ELEM;
66      }
67  
68      protected Class getDocumentClass()
69      {
70          return DocumentBody.class;
71      }
72  
73      protected void openTag(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent)
74          throws IOException
75      {
76          //HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, ATTRS);
77          
78          Map<String, List<ClientBehavior>> behaviors = null;
79          if (uiComponent instanceof ClientBehaviorHolder && JavascriptUtils.isJavascriptAllowed(facesContext.getExternalContext()))
80          {
81              behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
82              if (!behaviors.isEmpty())
83              {
84                  ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, writer);
85              }
86              super.openTag(facesContext, writer, uiComponent);
87              
88              if (behaviors.isEmpty())
89              {
90                  HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
91              }
92              else
93              {
94                  writer.writeAttribute(HTML.ID_ATTR, uiComponent.getClientId(facesContext), null);
95              }
96              
97              HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, uiComponent, behaviors);
98              HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONLOAD_ATTR, uiComponent,
99                      ClientBehaviorEvents.LOAD, behaviors, HTML.ONLOAD_ATTR);
100             HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONUNLOAD_ATTR, uiComponent,
101                     ClientBehaviorEvents.UNLOAD, behaviors, HTML.ONUNLOAD_ATTR);
102             HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, ONRESIZE, uiComponent,
103                     RESIZE, behaviors, ONRESIZE);
104             
105             HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
106                     HTML.BODY_PASSTHROUGH_ATTRIBUTES_WITHOUT_EVENTS);
107         }
108         else
109         {
110             super.openTag(facesContext, writer, uiComponent);
111             HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
112             HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
113                     HTML.BODY_PASSTHROUGH_ATTRIBUTES);
114             HtmlRendererUtils.renderHTMLAttribute(writer, uiComponent, ONRESIZE, ONRESIZE);
115         }
116     }
117 
118     protected void writeBeforeEnd(FacesContext facesContext) throws IOException
119     {
120         super.writeBeforeEnd(facesContext);
121         
122         AddResource addResource = AddResourceFactory.getInstance(facesContext);
123         if (!addResource.requiresBuffer())
124         {
125             // This code is rendered only if this request don't require
126             // buffering, because when it is buffered, the buffer is responsible
127             // of render it.
128             ExtensionsPhaseListener.writeCodeBeforeBodyEnd(facesContext);
129 
130             // fake string, so the ExtensionsPhaseListener will not create the javascript again
131             facesContext.getExternalContext().getRequestMap().put(ExtensionsPhaseListener.ORG_APACHE_MYFACES_MY_FACES_JAVASCRIPT, "");
132         }
133         
134         UIViewRoot root = facesContext.getViewRoot();
135         for (UIComponent child : root.getComponentResources(facesContext,
136                 TomahawkResourceUtils.BODY_LOCATION))
137         {
138             child.encodeAll(facesContext);
139         }
140     }
141 }