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