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.Map;
19:
20: import org.directwebremoting.extend.OutboundVariable;
21: import org.directwebremoting.util.LocalUtil;
22:
23: /**
24: * An OutboundVariable that creates data from Maps.
25: * @author Joe Walker [joe at getahead dot ltd dot uk]
26: */
27: public class ObjectJsonOutboundVariable extends
28: JsonNestedOutboundVariable implements MapOutboundVariable {
29: /* (non-Javadoc)
30: * @see org.directwebremoting.dwrp.CollectionOutboundVariable#setChildren(java.lang.Object)
31: */
32: public void setChildren(Map<String, 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 ObjectJsonOutboundVariable} and it's
46: * brother {@link ObjectNonJsonOutboundVariable}.
47: * @return A JSON string that represents the set of {@link OutboundVariable}s
48: */
49: protected static String createJsonString(
50: Map<String, OutboundVariable> children) {
51: StringBuffer buffer = new StringBuffer();
52: buffer.append('{');
53:
54: boolean first = true;
55: for (Map.Entry<String, OutboundVariable> entry : children
56: .entrySet()) {
57: String name = entry.getKey();
58: OutboundVariable nested = entry.getValue();
59:
60: String innerAssignCode = nested.getAssignCode();
61:
62: if (!first) {
63: buffer.append(',');
64: }
65:
66: // The compact JSON style syntax is only any good for simple names
67: // and when we are not recursive
68: if (LocalUtil.isSimpleName(name)) {
69: buffer.append(name);
70: buffer.append(':');
71: buffer.append(innerAssignCode);
72: } else {
73: buffer.append('\'');
74: buffer.append(name);
75: buffer.append("\':");
76: buffer.append(innerAssignCode);
77: }
78:
79: // we don't need to do this one the hard way
80: first = false;
81: }
82: buffer.append('}');
83:
84: return buffer.toString();
85: }
86:
87: /* (non-Javadoc)
88: * @see java.lang.Object#toString()
89: */
90: @Override
91: public String toString() {
92: return "ObjectJsonOutboundVariable:" + children;
93: }
94:
95: /**
96: * The contained variables
97: */
98: private Map<String, OutboundVariable> children;
99: }
|