1 16 package org.apache.myfaces.custom.jsvalueset; 17 18 import org.apache.myfaces.renderkit.RendererUtils; 19 import org.apache.myfaces.renderkit.html.HTML; 20 import org.apache.myfaces.renderkit.html.HtmlRenderer; 21 22 import javax.faces.application.Application; 23 import javax.faces.component.UIComponent; 24 import javax.faces.context.FacesContext; 25 import javax.faces.context.ResponseWriter; 26 import java.io.IOException ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 46 public class HtmlJsValueSetRenderer 47 extends HtmlRenderer 48 { 49 public void encodeEnd(FacesContext facesContext, UIComponent component) 50 throws IOException 51 { 52 RendererUtils.checkParamValidity(facesContext, component, HtmlJsValueSet.class); 53 54 HtmlJsValueSet htmlJsValueSet = (HtmlJsValueSet) component; 55 56 Object value = htmlJsValueSet.getValue(); 57 String name = htmlJsValueSet.getName(); 58 59 60 ResponseWriter writer = getFacesContext().getResponseWriter(); 61 62 writer.startElement(HTML.SCRIPT_ELEM,null); 63 writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR,HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,null); 64 65 if(value instanceof String || 66 value instanceof Number || value instanceof Boolean || value == null) 67 { 68 writer.writeText("var " + name+"="+getValueString(value)+";",null); 69 } 70 else if(value instanceof Object []) 71 { 72 Object [] objs = (Object []) value; 73 74 startArray(writer, name); 75 76 for (int i = 0; i < objs.length; i++) 77 { 78 Object obj = objs[i]; 79 writeArrayElement(writer, name, i, obj); 80 } 81 } 82 else if(value instanceof Collection ) 83 { 84 Collection coll = (Collection ) value; 85 86 startArray(writer, name); 87 88 int i=0; 89 90 for (Iterator iterator = coll.iterator(); iterator.hasNext();) 91 { 92 Object obj = iterator.next(); 93 writeArrayElement(writer, name, i, obj); 94 i++; 95 } 96 } 97 else if(value instanceof Map ) 98 { 99 Map map = (Map ) value; 100 Iterator it = map.entrySet().iterator(); 101 102 startArray(writer,name); 103 104 while (it.hasNext()) 105 { 106 Map.Entry entry = (Map.Entry ) it.next(); 107 writeArrayElement(writer, name, entry.getKey(), entry.getValue()); 108 } 109 } 110 else 111 throw new IOException ("Type of value not handled. Allowed are String, Number, Boolean, Object[], Collection, Map. Type of value : "+value.getClass()); 112 113 114 writer.endElement(HTML.SCRIPT_ELEM); 115 } 116 117 private void writeArrayElement(ResponseWriter writer, String name, Object key, Object obj) 118 throws IOException 119 { 120 String prefix = name+"["+getValueString(key)+"]"; 121 122 if(obj instanceof Map ) 123 { 124 writer.writeText(prefix + "= new Array();",null); 125 126 Iterator it = ((Map ) obj).entrySet().iterator(); 127 128 while (it.hasNext()) 129 { 130 Map.Entry entry = (Map.Entry ) it.next(); 131 132 writeArrayElement(writer,prefix,entry.getKey(),entry.getValue()); 133 } 134 } 135 else 136 { 137 writer.writeText(prefix + "="+getValueString(obj)+";",null); 138 } 139 } 140 141 private void writeArrayElement(ResponseWriter writer, String name, int i, Object obj) 142 throws IOException 143 { 144 writer.writeText(name+"["+i+"]="+getValueString(obj)+";",null); 145 } 146 147 private void startArray(ResponseWriter writer, String name) 148 throws IOException 149 { 150 writer.writeText("var " + name+"=new Array();",null); 151 } 152 153 private String getValueString(Object value) throws IOException 154 { 155 if(value instanceof String ) 156 { 157 return "'"+value+"'"; 158 } 159 else if(value instanceof Number ) 160 { 161 return value.toString(); 162 } 163 else if(value instanceof Boolean ) 164 { 165 return value.toString(); 166 } 167 else if(value == null) 168 { 169 return "null"; 170 } 171 else 172 { 173 throw new IOException ("value : "+value+" is of type : "+value.getClass()+ 174 ", provide a method to convert this."); 175 } 176 } 177 178 protected Application getApplication() 179 { 180 return getFacesContext().getApplication(); 181 } 182 183 protected FacesContext getFacesContext() 184 { 185 return FacesContext.getCurrentInstance(); 186 } 187 188 } 189 | Popular Tags |