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.jsvalueset;
20  
21  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
22  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
23  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
24  
25  import org.apache.commons.lang.StringEscapeUtils;
26  import javax.faces.application.Application;
27  import javax.faces.component.UIComponent;
28  import javax.faces.context.FacesContext;
29  import javax.faces.context.ResponseWriter;
30  import java.io.IOException;
31  import java.util.Collection;
32  import java.util.Iterator;
33  import java.util.Map;
34  
35  /**
36   * @JSFRenderer
37   *   renderKitId = "HTML_BASIC" 
38   *   family = "javax.faces.Output"
39   *   type = "org.apache.myfaces.JsValueSet"
40   * 
41   * @author Martin Marinschek (latest modification by $Author: lu4242 $)
42   * @version $Revision: 659874 $ $Date: 2008-05-24 15:59:15 -0500 (Sat, 24 May 2008) $
43   */
44  public class HtmlJsValueSetRenderer
45  extends HtmlRenderer
46  {
47     private void writeValue(String varName, StringBuffer valueSb, StringBuffer prependSb, Object value) throws IOException 
48     {
49        if(value instanceof String || value instanceof Number || value instanceof Boolean || value == null) 
50        {
51           valueSb.append(getValueString(value));
52        }
53        else if(value instanceof Object[])
54        {
55           Object[] objs = (Object[]) value;
56           
57           valueSb.append("[");
58           
59           String comma = "";
60           for (int i = 0; i < objs.length; i++) 
61           {
62              valueSb.append(comma);
63              writeValue(varName + "_h" + i, valueSb, prependSb, objs[i]);
64              comma = ",";
65           }
66           
67           valueSb.append("]");
68        }
69        else if(value instanceof Collection)
70        {
71           Collection coll = (Collection) value;
72           
73           valueSb.append("[");
74           
75           String comma = "";
76           int i = 0;
77           for (Iterator iterator = coll.iterator(); iterator.hasNext();) 
78           {
79              valueSb.append(comma);
80              writeValue(varName + "_h" + i, valueSb, prependSb, iterator.next());
81              comma = ",";
82              i++;
83           }
84           
85           valueSb.append("]");
86        }
87        else if(value instanceof Map)
88        {
89           Map map = (Map) value;
90           
91           varName = varName + "_hm";
92            prependSb.append("var ").append(varName).append("=new Object();\n");
93           
94           Iterator it = map.entrySet().iterator();
95           int i = 0;
96           while (it.hasNext()) 
97           {
98              Map.Entry entry = (Map.Entry) it.next();
99              StringBuffer helpValueSb = new StringBuffer();
100             StringBuffer helpHelpSb = new StringBuffer();
101             writeValue(varName + "_h" + i, helpValueSb, helpHelpSb, entry.getValue());
102             
103             prependSb.append(helpHelpSb);
104              prependSb.append(varName).append("[").append(getValueString(entry.getKey())).append("]=");
105              prependSb.append(helpValueSb).append(";\n");
106             
107             i++;
108          }
109          
110          valueSb.append(varName);
111       }
112       else
113          throw new IOException("Type of value not handled. Allowed are String, Number, Boolean, Object[], Collection, Map. Type of value : "+value.getClass());
114       
115    }
116    
117    public void encodeEnd(FacesContext facesContext, UIComponent component)
118    throws IOException
119    {
120       RendererUtils.checkParamValidity(facesContext, component, HtmlJsValueSet.class);
121       
122       HtmlJsValueSet jsValue = (HtmlJsValueSet) component;
123       
124       Object value = jsValue.getValue();
125       String varName = jsValue.getName();
126       
127       
128       ResponseWriter writer = getFacesContext().getResponseWriter();
129       
130       writer.startElement(HTML.SCRIPT_ELEM, null);
131       writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
132       
133       StringBuffer valueSb = new StringBuffer("var " + varName + "=");
134       StringBuffer prependSb = new StringBuffer("");
135       
136       writeValue(varName, valueSb, prependSb, value);
137       
138       writer.write(prependSb.toString());
139       writer.write(valueSb.toString() + ";\n");
140       
141       writer.endElement(HTML.SCRIPT_ELEM);
142    }
143    
144    
145    private String getValueString(Object value) throws IOException
146    {
147       if(value instanceof String)
148       {
149          return "'"+StringEscapeUtils.escapeJavaScript(value.toString())+"'";
150       }
151       else if(value instanceof Number)
152       {
153          return value.toString();
154       }
155       else if(value instanceof Boolean)
156       {
157          return value.toString();
158       }
159       else if(value == null)
160       {
161          return "null";
162       }
163       else
164       {
165          throw new IOException("value : "+value+" is of type : "+value.getClass()+
166          ", provide a method to convert this.");
167       }
168    }
169    
170    protected Application getApplication()
171    {
172       return getFacesContext().getApplication();
173    }
174    
175    protected FacesContext getFacesContext()
176    {
177       return FacesContext.getCurrentInstance();
178    }
179    
180 }