KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > common > renderer > BaseRenderer


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.ui.common.renderer;
18
19 import java.io.IOException JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.faces.component.UIComponent;
25 import javax.faces.component.UIParameter;
26 import javax.faces.context.FacesContext;
27 import javax.faces.context.ResponseWriter;
28 import javax.faces.render.Renderer;
29
30 /**
31  * Base renderer class. Contains helper methods to assist most renderers.
32  *
33  * @author kevinr
34  */

35 public abstract class BaseRenderer extends Renderer
36 {
37    /**
38     * Helper to output an attribute to the output stream
39     *
40     * @param out ResponseWriter
41     * @param attr attribute value object (cannot be null)
42     * @param mapping mapping to output as e.g. style="..."
43     *
44     * @throws IOException
45     */

46    protected static void outputAttribute(ResponseWriter out, Object JavaDoc attr, String JavaDoc mapping)
47       throws IOException JavaDoc
48    {
49       if (attr != null)
50       {
51          out.write(' ');
52          out.write(mapping);
53          out.write("=\"");
54          out.write(attr.toString());
55          out.write('"');
56       }
57    }
58    
59    /**
60     * Ensures that the given context and component are not null. This method
61     * should be called by all renderer methods that are given these parameters.
62     *
63     * @param ctx Faces context
64     * @param component The component
65     */

66    protected static void assertParmeters(FacesContext ctx, UIComponent component)
67    {
68       if (ctx == null)
69       {
70          throw new IllegalStateException JavaDoc("context can not be null");
71       }
72       
73       if (component == null)
74       {
75          throw new IllegalStateException JavaDoc("component can not be null");
76       }
77    }
78    
79    /**
80     * Return the map of name/value pairs for any child UIParameter components.
81     *
82     * @param component to find UIParameter child values for
83     *
84     * @return a Map of name/value pairs or null if none found
85     */

86    protected static Map JavaDoc<String JavaDoc, String JavaDoc> getParameterMap(UIComponent component)
87    {
88       Map JavaDoc<String JavaDoc, String JavaDoc> params = null;
89       
90       if (component.getChildCount() != 0)
91       {
92          params = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(3, 1.0f);
93          for (Iterator JavaDoc i=component.getChildren().iterator(); i.hasNext(); /**/)
94          {
95             UIComponent child = (UIComponent)i.next();
96             if (child instanceof UIParameter)
97             {
98                UIParameter param = (UIParameter)child;
99                params.put(param.getName(), (String JavaDoc)param.getValue());
100             }
101          }
102       }
103       
104       return params;
105    }
106 }
107
Popular Tags