1 16 package org.directwebremoting.dwrp; 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 21 import org.directwebremoting.extend.OutboundContext; 22 import org.directwebremoting.extend.OutboundVariable; 23 24 28 public abstract class AbstractOutboundVariable implements OutboundVariable 29 { 30 33 protected AbstractOutboundVariable(OutboundContext outboundContext) 34 { 35 this.outboundContext = outboundContext; 36 } 37 38 42 protected void forceInline(boolean inlineStatus) 43 { 44 setInline(inlineStatus); 45 forcedInlineStatus = true; 46 } 47 48 51 protected void setChildren(Collection children) 52 { 53 this.children = children; 54 } 55 56 59 public String getDeclareCode() 60 { 61 if (!calculated) 62 { 63 calculate(); 64 } 65 66 if (inline) 67 { 68 return getChildDeclareCodes(); 69 } 70 else 71 { 72 return notInlineDefinition.declareCode + getChildDeclareCodes(); 73 } 74 } 75 76 79 public String getBuildCode() 80 { 81 if (!calculated) 82 { 83 calculate(); 84 } 85 86 if (inline) 87 { 88 return getChildBuildCodes(); 89 } 90 else 91 { 92 return notInlineDefinition.buildCode + getChildBuildCodes(); 93 } 94 } 95 96 99 public String getAssignCode() 100 { 101 if (calculated) 102 { 103 if (inline) 104 { 105 return assignCode; 106 } 107 else 108 { 109 return varName; 110 } 111 } 112 else 113 { 114 if (forcedInlineStatus) 119 { 120 if (inline) 121 { 122 return getInlineDefinition(); 123 } 124 else 125 { 126 return getVariableName(); 127 } 128 } 129 else 130 { 131 setInline(false); 133 return getVariableName(); 134 } 135 } 136 } 137 138 141 public OutboundVariable getReferenceVariable() 142 { 143 if (reference == null) 144 { 145 reference = new ReferenceOutboundVariable(getVariableName()); 146 if (forcedInlineStatus) 147 { 148 throw new IllegalStateException ("Ignoring request to inline on reference for: " + this); 149 } 150 else 151 { 152 setInline(false); 153 } 154 } 155 156 return reference; 157 } 158 159 163 private void calculate() 164 { 165 if (inline) 166 { 167 assignCode = getInlineDefinition(); 168 } 169 else 170 { 171 notInlineDefinition = getNotInlineDefinition(); 172 } 173 174 calculated = true; 175 } 176 177 181 private String getChildBuildCodes() 182 { 183 if (children == null) 184 { 185 return ""; 186 } 187 188 StringBuffer buffer = new StringBuffer (); 189 190 for (Iterator it = children.iterator(); it.hasNext();) 192 { 193 OutboundVariable nested = (OutboundVariable) it.next(); 194 buffer.append(nested.getBuildCode()); 195 } 196 197 return buffer.toString(); 198 } 199 200 204 private String getChildDeclareCodes() 205 { 206 if (children == null) 207 { 208 return ""; 209 } 210 211 StringBuffer buffer = new StringBuffer (); 212 213 for (Iterator it = children.iterator(); it.hasNext();) 215 { 216 OutboundVariable nested = (OutboundVariable) it.next(); 217 buffer.append(nested.getDeclareCode()); 218 } 219 220 return buffer.toString(); 221 } 222 223 226 protected String getVariableName() 227 { 228 if (varName == null) 229 { 230 varName = outboundContext.getNextVariableName(); 231 } 232 233 return varName; 234 } 235 236 240 protected abstract NotInlineDefinition getNotInlineDefinition(); 241 242 246 protected abstract String getInlineDefinition(); 247 248 251 protected class NotInlineDefinition 252 { 253 protected NotInlineDefinition(String declareCode, String buildCode) 254 { 255 this.declareCode = declareCode; 256 this.buildCode = buildCode; 257 } 258 259 262 String declareCode; 263 264 267 String buildCode; 268 } 269 270 274 protected String toStringDefinitionHint() 275 { 276 if (inline) 277 { 278 return "inline"; 279 } 280 else 281 { 282 if (varName != null) 283 { 284 return varName; 285 } 286 else 287 { 288 return "?"; 289 } 290 } 291 } 292 293 296 private void setInline(boolean isInline) 297 { 298 if (calculated) 299 { 300 throw new IllegalStateException ("Attempt to change inline status after calculation"); 301 } 302 303 this.inline = isInline; 304 } 305 306 309 private OutboundVariable reference; 310 311 314 private boolean inline = true; 315 316 319 private boolean forcedInlineStatus = false; 320 321 324 private boolean calculated = false; 325 326 329 private NotInlineDefinition notInlineDefinition; 330 331 334 private String assignCode; 335 336 339 private String varName; 340 341 344 private OutboundContext outboundContext; 345 346 349 private Collection children; 350 } 351 | Popular Tags |