1 16 package org.directwebremoting.convert; 17 18 import java.lang.reflect.Modifier ; 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Set ; 25 import java.util.SortedSet ; 26 import java.util.StringTokenizer ; 27 import java.util.TreeSet ; 28 29 import org.directwebremoting.dwrp.ArrayOutboundVariable; 30 import org.directwebremoting.dwrp.ErrorOutboundVariable; 31 import org.directwebremoting.dwrp.ParseUtil; 32 import org.directwebremoting.dwrp.ProtocolConstants; 33 import org.directwebremoting.extend.Converter; 34 import org.directwebremoting.extend.ConverterManager; 35 import org.directwebremoting.extend.InboundContext; 36 import org.directwebremoting.extend.InboundVariable; 37 import org.directwebremoting.extend.MarshallException; 38 import org.directwebremoting.extend.OutboundContext; 39 import org.directwebremoting.extend.OutboundVariable; 40 import org.directwebremoting.extend.TypeHintContext; 41 import org.directwebremoting.util.LocalUtil; 42 import org.directwebremoting.util.Logger; 43 import org.directwebremoting.util.Messages; 44 45 50 public class CollectionConverter extends BaseV20Converter implements Converter 51 { 52 55 public void setConverterManager(ConverterManager newConfig) 56 { 57 this.config = newConfig; 58 } 59 60 63 public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException 64 { 65 String value = iv.getValue(); 66 67 if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) 69 { 70 return null; 71 } 72 73 if (!value.startsWith(ProtocolConstants.INBOUND_ARRAY_START)) 74 { 75 throw new MarshallException(paramType, Messages.getString("CollectionConverter.FormatError", ProtocolConstants.INBOUND_ARRAY_START)); 76 } 77 78 if (!value.endsWith(ProtocolConstants.INBOUND_ARRAY_END)) 79 { 80 throw new MarshallException(paramType, Messages.getString("CollectionConverter.FormatError", ProtocolConstants.INBOUND_ARRAY_END)); 81 } 82 83 value = value.substring(1, value.length() - 1); 84 85 try 86 { 87 TypeHintContext icc = inctx.getCurrentTypeHintContext(); 88 89 TypeHintContext subthc = icc.createChildContext(0); 90 Class subtype = subthc.getExtraTypeInfo(); 91 92 Collection col; 94 95 if (Iterator .class.isAssignableFrom(paramType)) 98 { 99 col = new ArrayList (); 100 } 101 else if (!paramType.isInterface() && !Modifier.isAbstract(paramType.getModifiers())) 103 { 104 col = (Collection ) paramType.newInstance(); 109 } 110 else if (SortedSet .class.isAssignableFrom(paramType)) 112 { 113 col = new TreeSet (); 114 } 115 else if (Set .class.isAssignableFrom(paramType)) 117 { 118 col = new HashSet (); 119 } 120 else if (List .class.isAssignableFrom(paramType)) 122 { 123 col = new ArrayList (); 124 } 125 else if (Collection .class.isAssignableFrom(paramType)) 127 { 128 col = new ArrayList (); 129 } 130 else 131 { 132 throw new MarshallException(paramType); 133 } 134 135 inctx.addConverted(iv, paramType, col); 138 139 StringTokenizer st = new StringTokenizer (value, ProtocolConstants.INBOUND_ARRAY_SEPARATOR); 140 int size = st.countTokens(); 141 for (int i = 0; i < size; i++) 142 { 143 String token = st.nextToken(); 144 145 String [] split = ParseUtil.splitInbound(token); 146 String splitType = split[LocalUtil.INBOUND_INDEX_TYPE]; 147 String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE]; 148 149 InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue); 150 151 Object output = config.convertInbound(subtype, nested, inctx, subthc); 152 col.add(output); 153 } 154 155 if (Iterator .class.isAssignableFrom(paramType)) 158 { 159 return col.iterator(); 160 } 161 else 162 { 163 return col; 164 } 165 } 166 catch (Exception ex) 167 { 168 throw new MarshallException(paramType, ex); 169 } 170 } 171 172 175 public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException 176 { 177 Iterator it; 179 if (data instanceof Collection ) 180 { 181 Collection col = (Collection ) data; 182 it = col.iterator(); 183 } 184 else if (data instanceof Iterator ) 185 { 186 it = (Iterator ) data; 187 } 188 else 189 { 190 throw new MarshallException(data.getClass()); 191 } 192 193 ArrayOutboundVariable ov = new ArrayOutboundVariable(outctx); 195 outctx.put(data, ov); 196 197 List ovs = new ArrayList (); 199 while (it.hasNext()) 200 { 201 Object member = it.next(); 202 OutboundVariable nested; 203 204 try 205 { 206 nested = config.convertOutbound(member, outctx); 207 } 208 catch (Exception ex) 209 { 210 String errorMessage = "Conversion error for " + data.getClass().getName() + "."; 211 log.warn(errorMessage, ex); 212 213 nested = new ErrorOutboundVariable(outctx, errorMessage, true); 214 } 215 216 ovs.add(nested); 217 } 218 219 ov.init(ovs); 221 222 return ov; 223 } 224 225 228 private ConverterManager config = null; 229 230 233 private static final Logger log = Logger.getLogger(CollectionConverter.class); 234 } 235
| Popular Tags
|