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.htmlTag;
20  
21  import java.io.IOException;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.Map.Entry;
26  
27  import javax.faces.component.UIComponent;
28  import javax.faces.component.UIParameter;
29  import javax.faces.context.FacesContext;
30  import javax.faces.context.ResponseWriter;
31  
32  import org.apache.myfaces.component.html.util.HtmlComponentUtils;
33  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
34  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
35  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
36  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
37  
38  /**
39   * @JSFRenderer
40   *   renderKitId = "HTML_BASIC" 
41   *   family = "javax.faces.Output"
42   *   type = "org.apache.myfaces.HtmlTagRenderer"
43   * 
44   * @author bdudney (latest modification by $Author: lu4242 $)
45   * @version $Revision: 1327315 $ $Date: 2012-04-17 17:58:45 -0500 (Tue, 17 Apr 2012) $
46   */
47  public class HtmlTagRenderer extends HtmlRenderer
48  {
49      public static final String RENDERER_TYPE = "org.apache.myfaces.HtmlTagRenderer";
50  
51      public void encodeBegin(FacesContext context, UIComponent component)
52              throws IOException
53      {
54          if ((context == null) || (component == null))
55          {
56              throw new NullPointerException();
57          }
58          HtmlTag htmlTag = (HtmlTag) component;
59  
60          if (htmlTag.isRendered())
61          {
62              String tag = htmlTag.getValue().toString();
63              if( tag.trim().length() == 0 ) // Don't render the tag, but render the children.
64                  return;
65  
66              ResponseWriter writer = context.getResponseWriter();
67  
68              writer.startElement(tag, htmlTag);
69              HtmlRendererUtils.writeIdIfNecessary(writer, htmlTag, context);
70  
71              // TODO : Use HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.COMMON_PASSTROUGH_ATTRIBUTES);
72              String[] supportedAttributes = {HTML.STYLE_CLASS_ATTR, HTML.STYLE_ATTR};
73              HtmlRendererUtils.renderHTMLAttributes(writer, htmlTag, supportedAttributes);
74              
75              if (htmlTag.getClass().equals(HtmlTag.class))
76              {
77                  // write additional attributes supplied by f:param tags
78                  // Components that extend from HtmlTag component should render attributes
79                  // on a proper encodeBegin (see Div component for details) 
80                  Map params = getParameterMap(htmlTag);
81                  for(Iterator iter = params.entrySet().iterator(); iter.hasNext();)
82                  {
83                      Entry param = (Entry) iter.next();
84                      if (null != param.getValue())
85                      {
86                          writer.writeAttribute(param.getKey().toString(), param.getValue().toString(), null);
87                      }
88                  }
89              }
90          }
91      }
92      
93      public Map getParameterMap(UIComponent component) {
94          Map result = new HashMap();
95          for (Iterator iter = component.getChildren().iterator(); iter.hasNext();) {
96              UIComponent child = (UIComponent) iter.next();
97              if (child.getClass().equals(UIParameter.class))  {
98                  UIParameter uiparam = (UIParameter) child;
99                  Object value = uiparam.getValue();
100                 if (value != null) {
101                     result.put(uiparam.getName(), value);
102                 }
103             }
104         }
105         return result;
106     }
107 
108     public void encodeChildren(FacesContext context, UIComponent component)
109             throws IOException
110     {
111         RendererUtils.renderChildren(context, component);
112     }
113     
114     public boolean getRendersChildren()
115     {
116         return true;
117     }
118 
119     public void encodeEnd(FacesContext context, UIComponent component)
120             throws IOException
121     {
122         if ((context == null) || (component == null))
123         {
124             throw new NullPointerException();
125         }
126         HtmlTag htmlTag = (HtmlTag) component;
127 
128         if (htmlTag.isRendered())
129         {
130             String tag = htmlTag.getValue().toString();
131             if( tag.trim().length() == 0 )
132                 return;
133 
134             ResponseWriter writer = context.getResponseWriter();
135             // force separate end tag
136             // -= Leonardo Uribe =- Ensure when to close the end tag
137             // using a separate one or not is responsibility of
138             // the ResponseWriter implementation provided by the 
139             // RenderKit used. This line is invalid
140             //writer.writeText("", null);
141             writer.endElement( tag );
142         }
143     }
144 }