KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
19
20 import org.directwebremoting.extend.OutboundContext;
21 import org.directwebremoting.extend.OutboundVariable;
22
23 /**
24  * An OutboundVariable that creates data from Collections.
25  * @author Joe Walker [joe at getahead dot ltd dot uk]
26  */

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

33     public ArrayOutboundVariable(OutboundContext outboundContext)
34     {
35         super(outboundContext);
36     }
37
38     /**
39      * Generate an array declaration for a list of Outbound variables
40      * @param aOvs The list of contents of this array
41      */

42     public void init(List JavaDoc aOvs)
43     {
44         this.ovs = aOvs;
45         setChildren(ovs);
46     }
47
48     /* (non-Javadoc)
49      * @see org.directwebremoting.dwrp.AbstractOutboundVariable#getNotInlineDefinition()
50      */

51     protected NotInlineDefinition getNotInlineDefinition()
52     {
53         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
54
55         for (int i = 0; i < ovs.size(); i++)
56         {
57             OutboundVariable nested = (OutboundVariable) ovs.get(i);
58             String JavaDoc varName = getVariableName();
59
60             if (nested != null)
61             {
62                 buffer.append(varName);
63                 buffer.append('[');
64                 buffer.append(i);
65                 buffer.append("]=");
66                 buffer.append(nested.getAssignCode());
67                 buffer.append(';');
68             }
69         }
70         buffer.append("\r\n");
71
72         return new NotInlineDefinition("var " + getVariableName() + "=[];", buffer.toString());
73     }
74
75     /* (non-Javadoc)
76      * @see org.directwebremoting.dwrp.AbstractOutboundVariable#getInlineDefinition()
77      */

78     protected String JavaDoc getInlineDefinition()
79     {
80         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
81
82         buffer.append("[");
83
84         boolean first = true;
85         for (int i = 0; i < ovs.size(); i++)
86         {
87             OutboundVariable ov = (OutboundVariable) ovs.get(i);
88
89             if (!first)
90             {
91                 buffer.append(',');
92             }
93
94             buffer.append(ov.getAssignCode());
95
96             first = false;
97         }
98         buffer.append("]");
99
100         return buffer.toString();
101     }
102
103     /* (non-Javadoc)
104      * @see java.lang.Object#toString()
105      */

106     public String JavaDoc toString()
107     {
108         return "Array:" + toStringDefinitionHint() + ":" + ovs;
109     }
110
111     /**
112      * The contained variables
113      */

114     private List JavaDoc ovs;
115 }
116
Popular Tags