01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.dwrp;
17:
18: import java.util.Collection;
19:
20: import org.directwebremoting.extend.OutboundVariable;
21:
22: /**
23: * An OutboundVariable that creates data from Collections.
24: * @author Joe Walker [joe at getahead dot ltd dot uk]
25: */
26: public class ArrayJsonOutboundVariable extends
27: JsonNestedOutboundVariable implements
28: CollectionOutboundVariable {
29: /* (non-Javadoc)
30: * @see org.directwebremoting.dwrp.CollectionOutboundVariable#setChildren(java.util.List)
31: */
32: public void setChildren(Collection<OutboundVariable> children) {
33: this .children = children;
34: }
35:
36: /* (non-Javadoc)
37: * @see org.directwebremoting.extend.OutboundVariable#getAssignCode()
38: */
39: public String getAssignCode() {
40: return createJsonString(children);
41: }
42:
43: /**
44: * JSON strings sometimes need to be created in a non JSON mode too, so
45: * this method is shared between {@link ArrayJsonOutboundVariable} and it's
46: * brother {@link ArrayNonJsonOutboundVariable}.
47: * @return A JSON string that represents the set of {@link OutboundVariable}s
48: */
49: protected static String createJsonString(
50: Collection<OutboundVariable> children) {
51: StringBuffer buffer = new StringBuffer();
52:
53: buffer.append("[");
54:
55: boolean first = true;
56: for (OutboundVariable child : children) {
57: if (!first) {
58: buffer.append(',');
59: }
60:
61: buffer.append(child.getAssignCode());
62:
63: first = false;
64: }
65: buffer.append("]");
66:
67: return buffer.toString();
68: }
69:
70: /* (non-Javadoc)
71: * @see java.lang.Object#toString()
72: */
73: @Override
74: public String toString() {
75: return "ArrayJsonOutboundVariable:" + children;
76: }
77:
78: /**
79: * The contained variables
80: */
81: private Collection<OutboundVariable> children = null;
82: }
|