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.shared.renderkit.html;
20  
21  import org.apache.myfaces.shared.renderkit.JSFAttr;
22  
23  import javax.faces.application.ViewHandler;
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.context.FacesContext;
27  import javax.faces.render.Renderer;
28  import java.io.IOException;
29  import java.util.List;
30  
31  public abstract class HtmlRenderer
32          extends Renderer
33  {
34  
35      /**
36       * Return the list of children of the specified component.
37       * <p>
38       * This default implementation simply returns component.getChildren().
39       * However this method should always be used in order to allow
40       * renderer subclasses to override it and provide filtered or
41       * reordered views of the component children to rendering
42       * methods defined in their ancestor classes.
43       * <p>
44       * Any method that overrides this to "hide" child components
45       * should also override the getChildCount method.
46       * 
47       * @return a list of UIComponent objects.
48       */
49      public List<UIComponent> getChildren(UIComponent component) 
50      {
51          return component.getChildren();
52      }
53  
54      /**
55       * Return the number of children of the specified component.
56       * <p>
57       * See {@link #getChildren(UIComponent)} for more information.
58       */
59      public int getChildCount(UIComponent component) 
60      {
61          return component.getChildCount();
62      }
63      
64      /**
65       * @param facesContext
66       * @return String A String representing the action URL
67       */
68      protected String getActionUrl(FacesContext facesContext)
69      {
70          ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
71          String viewId = facesContext.getViewRoot().getViewId();
72          return viewHandler.getActionURL(facesContext, viewId);
73      }
74  
75      /**
76       * Renders the client ID as an "id".
77       */
78      protected void renderId(
79        FacesContext context,
80        UIComponent  component) throws IOException
81      {
82        if (shouldRenderId(context, component))
83        {
84          String clientId = getClientId(context, component);
85          context.getResponseWriter().writeAttribute(HTML.ID_ATTR, clientId, JSFAttr.ID_ATTR);
86        }
87      }
88  
89      /**
90       * Returns the client ID that should be used for rendering (if
91       * {@link #shouldRenderId} returns true).
92       */
93      protected String getClientId(
94        FacesContext context,
95        UIComponent  component)
96      {
97        return component.getClientId(context);
98      }
99  
100     /**
101      * Returns true if the component should render an ID.  Components
102      * that deliver events should always return "true".
103      * @todo Is this a bottleneck?  If so, optimize!
104      */
105     protected boolean shouldRenderId(
106       FacesContext context,
107       UIComponent  component)
108     {
109       String id = component.getId();
110 
111       // Otherwise, if ID isn't set, don't bother
112       if (id == null)
113       {
114           return false;
115       }
116 
117       // ... or if the ID was generated, don't bother
118       if (id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
119       {
120           return false;
121       }
122 
123       return true;
124     }
125 
126     /**
127      * Coerces an object into a URI, accounting for JSF rules
128      * with initial slashes.
129      */
130     static public String toUri(Object o)
131     {
132       if (o == null)
133       {
134           return null;
135       }
136 
137       String uri = o.toString();
138       if (uri.startsWith("/"))
139       {
140         // Treat two slashes as server-relative
141         if (uri.startsWith("//"))
142         {
143           uri = uri.substring(1);
144         }
145         else
146         {
147           FacesContext fContext = FacesContext.getCurrentInstance();
148           uri = fContext.getExternalContext().getRequestContextPath() + uri;
149         }
150       }
151 
152       return uri;
153     }
154     
155     protected boolean isCommonPropertiesOptimizationEnabled(FacesContext facesContext)
156     {
157         return false;
158     }
159     
160     protected boolean isCommonEventsOptimizationEnabled(FacesContext facesContext)
161     {
162         return false;
163     }
164 }