KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > dwrp > ObjectOutboundVariable


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.dwrp;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.directwebremoting.extend.OutboundContext;
22 import org.directwebremoting.extend.OutboundVariable;
23 import org.directwebremoting.util.LocalUtil;
24
25 /**
26  * An OutboundVariable that creates data from Maps.
27  * @author Joe Walker [joe at getahead dot ltd dot uk]
28  */

29 public class ObjectOutboundVariable extends AbstractOutboundVariable implements OutboundVariable
30 {
31     /**
32      * Setup
33      * @param outboundContext A collection of objects already converted and the results
34      */

35     public ObjectOutboundVariable(OutboundContext outboundContext)
36     {
37         super(outboundContext);
38     }
39
40     /**
41      * Generate an map declaration for a map of Outbound variables
42      * @param aOvs The map of the converted contents
43      * @param aScriptClassName The object name or null for pure(ish) json
44      */

45     public void init(Map JavaDoc aOvs, String JavaDoc aScriptClassName)
46     {
47         this.ovs = aOvs;
48         this.scriptClassName = aScriptClassName;
49
50         isNamed = (scriptClassName != null && !scriptClassName.equals(""));
51         if (isNamed)
52         {
53             forceInline(false);
54         }
55
56         setChildren(ovs.values());
57     }
58
59     /* (non-Javadoc)
60      * @see org.directwebremoting.dwrp.AbstractOutboundVariable#getNotInlineDefinition()
61      */

62     protected NotInlineDefinition getNotInlineDefinition()
63     {
64         String JavaDoc declareCode;
65         if (!isNamed)
66         {
67             declareCode = "var " + getVariableName() + "={};";
68         }
69         else
70         {
71             declareCode = "var " + getVariableName() + "=new " + scriptClassName + "();";
72         }
73
74         StringBuffer JavaDoc buildCode = new StringBuffer JavaDoc();
75         for (Iterator JavaDoc it = ovs.entrySet().iterator(); it.hasNext();)
76         {
77             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
78             String JavaDoc name = (String JavaDoc) entry.getKey();
79             OutboundVariable nested = (OutboundVariable) entry.getValue();
80
81             String JavaDoc nestedAssignCode = nested.getAssignCode();
82             String JavaDoc varName = getVariableName();
83
84             // The semi-compact syntax is only any good for simple names
85
// I dont think we need this check: && !isRecursive()
86
if (LocalUtil.isSimpleName(name))
87             {
88                 buildCode.append(varName);
89                 buildCode.append('.');
90                 buildCode.append(name);
91                 buildCode.append('=');
92                 buildCode.append(nestedAssignCode);
93                 buildCode.append(';');
94             }
95             else
96             {
97                 buildCode.append(varName);
98                 buildCode.append("['");
99                 buildCode.append(name);
100                 buildCode.append("']=");
101                 buildCode.append(nestedAssignCode);
102                 buildCode.append(';');
103             }
104         }
105         buildCode.append("\r\n");
106
107         return new NotInlineDefinition(declareCode, buildCode.toString());
108     }
109
110     /* (non-Javadoc)
111      * @see org.directwebremoting.dwrp.AbstractOutboundVariable#getInlineDefinition()
112      */

113     protected String JavaDoc getInlineDefinition()
114     {
115         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
116         buffer.append('{');
117
118         boolean first = true;
119         for (Iterator JavaDoc it = ovs.entrySet().iterator(); it.hasNext();)
120         {
121             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
122             String JavaDoc name = (String JavaDoc) entry.getKey();
123             OutboundVariable nested = (OutboundVariable) entry.getValue();
124
125             String JavaDoc innerAssignCode = nested.getAssignCode();
126
127             if (!first)
128             {
129                 buffer.append(',');
130             }
131
132             // The compact JSON style syntax is only any good for simple names
133
// and when we are not recursive
134
if (LocalUtil.isSimpleName(name))
135             {
136                 buffer.append(name);
137                 buffer.append(':');
138                 buffer.append(innerAssignCode);
139             }
140             else
141             {
142                 buffer.append('\'');
143                 buffer.append(name);
144                 buffer.append("\':");
145                 buffer.append(innerAssignCode);
146             }
147
148             // we don't need to do this one the hard way
149
first = false;
150         }
151         buffer.append('}');
152
153         return buffer.toString();
154     }
155
156     /* (non-Javadoc)
157      * @see java.lang.Object#toString()
158      */

159     public String JavaDoc toString()
160     {
161         return "Object:" + toStringDefinitionHint() + ":" + ovs;
162     }
163
164     /**
165      * Are we named (or does {@link #scriptClassName} have some contents)
166      */

167     private boolean isNamed;
168
169     /**
170      * The contained variables
171      */

172     private Map JavaDoc ovs;
173
174     /**
175      * The name of this typed class if there is one
176      */

177     private String JavaDoc scriptClassName;
178 }
179
Popular Tags