1 16 package org.directwebremoting.dwrp; 17 18 import java.util.Iterator ; 19 import java.util.Map ; 20 21 import org.directwebremoting.extend.OutboundContext; 22 import org.directwebremoting.extend.OutboundVariable; 23 import org.directwebremoting.util.LocalUtil; 24 25 29 public class ObjectOutboundVariable extends AbstractOutboundVariable implements OutboundVariable 30 { 31 35 public ObjectOutboundVariable(OutboundContext outboundContext) 36 { 37 super(outboundContext); 38 } 39 40 45 public void init(Map aOvs, String 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 62 protected NotInlineDefinition getNotInlineDefinition() 63 { 64 String declareCode; 65 if (!isNamed) 66 { 67 declareCode = "var " + getVariableName() + "={};"; 68 } 69 else 70 { 71 declareCode = "var " + getVariableName() + "=new " + scriptClassName + "();"; 72 } 73 74 StringBuffer buildCode = new StringBuffer (); 75 for (Iterator it = ovs.entrySet().iterator(); it.hasNext();) 76 { 77 Map.Entry entry = (Map.Entry ) it.next(); 78 String name = (String ) entry.getKey(); 79 OutboundVariable nested = (OutboundVariable) entry.getValue(); 80 81 String nestedAssignCode = nested.getAssignCode(); 82 String varName = getVariableName(); 83 84 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 113 protected String getInlineDefinition() 114 { 115 StringBuffer buffer = new StringBuffer (); 116 buffer.append('{'); 117 118 boolean first = true; 119 for (Iterator it = ovs.entrySet().iterator(); it.hasNext();) 120 { 121 Map.Entry entry = (Map.Entry ) it.next(); 122 String name = (String ) entry.getKey(); 123 OutboundVariable nested = (OutboundVariable) entry.getValue(); 124 125 String innerAssignCode = nested.getAssignCode(); 126 127 if (!first) 128 { 129 buffer.append(','); 130 } 131 132 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 first = false; 150 } 151 buffer.append('}'); 152 153 return buffer.toString(); 154 } 155 156 159 public String toString() 160 { 161 return "Object:" + toStringDefinitionHint() + ":" + ovs; 162 } 163 164 167 private boolean isNamed; 168 169 172 private Map ovs; 173 174 177 private String scriptClassName; 178 } 179 | Popular Tags |