KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > convert > ArrayConverter


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.convert;
17
18 import java.lang.reflect.Array JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
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 /**
38  * An implementation of Converter for Arrays.
39  * @author Joe Walker [joe at eireneh dot com]
40  * @version $Id: StringConverter.java,v 1.2 2004/11/04 15:54:07 joe_walker Exp $
41  * @noinspection RefusedBequest
42  */

43 public class ArrayConverter extends BaseV20Converter implements Converter
44 {
45     /* (non-Javadoc)
46      * @see org.directwebremoting.convert.BaseV20Converter#setConverterManager(org.directwebremoting.ConverterManager)
47      */

48     public void setConverterManager(ConverterManager newConfig)
49     {
50         this.converterManager = newConfig;
51     }
52
53     /* (non-Javadoc)
54      * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
55      */

56     public Object JavaDoc convertInbound(Class JavaDoc paramType, InboundVariable iv, InboundContext inctx) throws MarshallException
57     {
58         if (!paramType.isArray())
59         {
60             throw new MarshallException(paramType);
61         }
62
63         String JavaDoc 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 JavaDoc st = new StringTokenizer JavaDoc(value, ProtocolConstants.INBOUND_ARRAY_SEPARATOR);
74         int size = st.countTokens();
75
76         Class JavaDoc componentType = paramType.getComponentType();
77         //componentType = LocalUtil.getNonPrimitiveType(componentType);
78
Object JavaDoc array = Array.newInstance(componentType, size);
79
80         // We should put the new object into the working map in case it
81
// is referenced later nested down in the conversion process.
82
inctx.addConverted(iv, paramType, array);
83         InboundContext incx = iv.getLookup();
84
85         for (int i = 0; i < size; i++)
86         {
87             String JavaDoc token = st.nextToken();
88             String JavaDoc[] split = ParseUtil.splitInbound(token);
89             String JavaDoc splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
90             String JavaDoc splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
91
92             InboundVariable nested = new InboundVariable(incx, null, splitType, splitValue);
93             Object JavaDoc output = converterManager.convertInbound(componentType, nested, inctx, inctx.getCurrentTypeHintContext());
94             Array.set(array, i, output);
95         }
96
97         return array;
98     }
99
100     /* (non-Javadoc)
101      * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
102      */

103     public OutboundVariable convertOutbound(Object JavaDoc data, OutboundContext outctx) throws MarshallException
104     {
105         if (!data.getClass().isArray())
106         {
107             throw new MarshallException(data.getClass());
108         }
109
110         // Stash this bit of data to cope with recursion
111
ArrayOutboundVariable ov = new ArrayOutboundVariable(outctx);
112         outctx.put(data, ov);
113
114         // Convert all the data members
115
int size = Array.getLength(data);
116         List JavaDoc ovs = new ArrayList JavaDoc();
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 JavaDoc ex)
125             {
126                 String JavaDoc 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         // Group the list of converted objects into this OutboundVariable
135
ov.init(ovs);
136
137         return ov;
138     }
139
140     /**
141      * The log stream
142      */

143     private static final Logger log = Logger.getLogger(ArrayConverter.class);
144
145     /**
146      * The converter manager to which we forward array members for conversion
147      */

148     private ConverterManager converterManager = null;
149 }
150
Popular Tags