1 16 package org.directwebremoting.convert; 17 18 import java.lang.reflect.Array ; 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.util.StringTokenizer ; 22 23 import org.directwebremoting.dwrp.ArrayOutboundVariable; 24 import org.directwebremoting.dwrp.ErrorOutboundVariable; 25 import org.directwebremoting.dwrp.ParseUtil; 26 import org.directwebremoting.dwrp.ProtocolConstants; 27 import org.directwebremoting.extend.Converter; 28 import org.directwebremoting.extend.ConverterManager; 29 import org.directwebremoting.extend.InboundContext; 30 import org.directwebremoting.extend.InboundVariable; 31 import org.directwebremoting.extend.MarshallException; 32 import org.directwebremoting.extend.OutboundContext; 33 import org.directwebremoting.extend.OutboundVariable; 34 import org.directwebremoting.util.LocalUtil; 35 import org.directwebremoting.util.Logger; 36 37 43 public class ArrayConverter extends BaseV20Converter implements Converter 44 { 45 48 public void setConverterManager(ConverterManager newConfig) 49 { 50 this.converterManager = newConfig; 51 } 52 53 56 public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException 57 { 58 if (!paramType.isArray()) 59 { 60 throw new MarshallException(paramType); 61 } 62 63 String value = iv.getValue(); 64 if (value.startsWith(ProtocolConstants.INBOUND_ARRAY_START)) 65 { 66 value = value.substring(1); 67 } 68 if (value.endsWith(ProtocolConstants.INBOUND_ARRAY_END)) 69 { 70 value = value.substring(0, value.length() - 1); 71 } 72 73 StringTokenizer st = new StringTokenizer (value, ProtocolConstants.INBOUND_ARRAY_SEPARATOR); 74 int size = st.countTokens(); 75 76 Class componentType = paramType.getComponentType(); 77 Object array = Array.newInstance(componentType, size); 79 80 inctx.addConverted(iv, paramType, array); 83 InboundContext incx = iv.getLookup(); 84 85 for (int i = 0; i < size; i++) 86 { 87 String token = st.nextToken(); 88 String [] split = ParseUtil.splitInbound(token); 89 String splitType = split[LocalUtil.INBOUND_INDEX_TYPE]; 90 String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE]; 91 92 InboundVariable nested = new InboundVariable(incx, null, splitType, splitValue); 93 Object output = converterManager.convertInbound(componentType, nested, inctx, inctx.getCurrentTypeHintContext()); 94 Array.set(array, i, output); 95 } 96 97 return array; 98 } 99 100 103 public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException 104 { 105 if (!data.getClass().isArray()) 106 { 107 throw new MarshallException(data.getClass()); 108 } 109 110 ArrayOutboundVariable ov = new ArrayOutboundVariable(outctx); 112 outctx.put(data, ov); 113 114 int size = Array.getLength(data); 116 List ovs = new ArrayList (); 117 for (int i = 0; i < size; i++) 118 { 119 OutboundVariable nested; 120 try 121 { 122 nested = converterManager.convertOutbound(Array.get(data, i), outctx); 123 } 124 catch (Exception ex) 125 { 126 String errorMessage = "Conversion error for " + data.getClass().getName() + "."; 127 log.warn(errorMessage, ex); 128 129 nested = new ErrorOutboundVariable(outctx, errorMessage, true); 130 } 131 ovs.add(nested); 132 } 133 134 ov.init(ovs); 136 137 return ov; 138 } 139 140 143 private static final Logger log = Logger.getLogger(ArrayConverter.class); 144 145 148 private ConverterManager converterManager = null; 149 } 150 | Popular Tags |