001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.dwrp;
017:
018: import java.util.Collection;
019:
020: import org.directwebremoting.extend.JsonModeMarshallException;
021: import org.directwebremoting.extend.NonNestedOutboundVariable;
022: import org.directwebremoting.extend.OutboundContext;
023: import org.directwebremoting.extend.OutboundVariable;
024:
025: /**
026: * A helper class for people that want to implement {@link OutboundVariable}.
027: * @author Joe Walker [joe at getahead dot ltd dot uk]
028: */
029: public abstract class NonJsonNestedOutboundVariable implements
030: OutboundVariable {
031: /**
032: * @param outboundContext the OutboundContext to set
033: */
034: protected NonJsonNestedOutboundVariable(
035: OutboundContext outboundContext) {
036: this .outboundContext = outboundContext;
037: }
038:
039: /**
040: * We might want to force us into predefined mode.
041: */
042: protected void forceOutline() {
043: // if we are in JSON mode then we are not allowed to be non-inline.
044: if (outboundContext.isJsonMode()) {
045: throw new JsonModeMarshallException(
046: NonJsonNestedOutboundVariable.class,
047: "Outline in JSON mode is illegal");
048: }
049:
050: this .outline = true;
051: }
052:
053: /**
054: * @return the outline
055: */
056: public boolean isOutline() {
057: return outline;
058: }
059:
060: /* (non-Javadoc)
061: * @see org.directwebremoting.OutboundVariable#getDeclareCode()
062: */
063: public String getDeclareCode() {
064: if (declareCode == null || children == null) {
065: throw new NullPointerException();
066: }
067:
068: return declareCode + getChildDeclareCodes();
069: }
070:
071: /**
072: * @param declareCode the declareCode to set
073: */
074: public void setBaseDeclareCode(String declareCode) {
075: this .declareCode = declareCode;
076: }
077:
078: /* (non-Javadoc)
079: * @see org.directwebremoting.OutboundVariable#getBuildCode()
080: */
081: public String getBuildCode() {
082: if (declareCode == null || children == null) {
083: throw new NullPointerException();
084: }
085:
086: return buildCode + getChildBuildCodes();
087: }
088:
089: /**
090: * @param buildCode the buildCode to set
091: */
092: public void setBaseBuildCode(String buildCode) {
093: this .buildCode = buildCode;
094: }
095:
096: /* (non-Javadoc)
097: * @see org.directwebremoting.OutboundVariable#getAssignCode()
098: */
099: public String getAssignCode() {
100: return assignCode;
101: }
102:
103: /**
104: * @param assignCode the assignCode to set
105: */
106: public void setAssignCode(String assignCode) {
107: this .assignCode = assignCode;
108: }
109:
110: /* (non-Javadoc)
111: * @see org.directwebremoting.OutboundVariable#getReference()
112: */
113: public OutboundVariable getReferenceVariable() {
114: if (reference == null) {
115: reference = new NonNestedOutboundVariable(getVariableName());
116: forceOutline();
117: }
118:
119: return reference;
120: }
121:
122: /**
123: * In JSON mode we need to recursively prepare assign codes because the
124: * order in which they are created matters.
125: */
126: protected void prepareChildAssignCodes() {
127: for (OutboundVariable child : children) {
128: child.prepareAssignCode();
129: }
130: }
131:
132: /**
133: * Grab all the build codes together
134: * @return A build string
135: */
136: private String getChildBuildCodes() {
137: if (children == null) {
138: return "";
139: }
140:
141: StringBuffer buffer = new StringBuffer();
142:
143: // Make sure the nested things are declared
144: for (OutboundVariable nested : children) {
145: buffer.append(nested.getBuildCode());
146: }
147:
148: return buffer.toString();
149: }
150:
151: /**
152: * Grab all the declare codes together
153: * @return A declare string
154: */
155: private String getChildDeclareCodes() {
156: if (children == null) {
157: return "";
158: }
159:
160: StringBuffer buffer = new StringBuffer();
161:
162: // Make sure the nested things are declared
163: for (OutboundVariable nested : children) {
164: buffer.append(nested.getDeclareCode());
165: }
166:
167: return buffer.toString();
168: }
169:
170: /**
171: * @return the varName
172: */
173: protected String getVariableName() {
174: if (varName == null) {
175: varName = outboundContext.getNextVariableName();
176: }
177:
178: return varName;
179: }
180:
181: /**
182: * When we create buildCode and declareCode, what do we need to add?
183: * @param children The list of dependent {@link OutboundVariable}s
184: */
185: public void setChildren(Collection<OutboundVariable> children) {
186: this .children = children;
187: }
188:
189: /**
190: * Does anything refer to us?
191: */
192: private OutboundVariable reference;
193:
194: /**
195: * Are we known to be recursive
196: */
197: private boolean outline = false;
198:
199: /**
200: * The code to be executed to do basic initialization
201: */
202: private String declareCode;
203:
204: /**
205: * The code to be executed to setup the data structure
206: */
207: private String buildCode;
208:
209: /**
210: * The code to use to refer to this data structure
211: */
212: private String assignCode;
213:
214: /**
215: * If we get recursive, this is the variable name we declare
216: */
217: private String varName;
218:
219: /**
220: * When we create buildCode and declareCode, what do we need to add?
221: */
222: protected Collection<OutboundVariable> children;
223:
224: /**
225: * The conversion context
226: */
227: private OutboundContext outboundContext;
228: }
|