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.Map;
019:
020: import org.directwebremoting.extend.OutboundContext;
021: import org.directwebremoting.extend.OutboundVariable;
022: import org.directwebremoting.util.LocalUtil;
023:
024: /**
025: * An OutboundVariable that creates data from Maps.
026: * @author Joe Walker [joe at getahead dot ltd dot uk]
027: */
028: public class ObjectNonJsonOutboundVariable extends
029: NonJsonNestedOutboundVariable implements MapOutboundVariable {
030: /**
031: * Setup
032: * @param outboundContext A collection of objects already converted and the results
033: * @param aScriptClassName The object name or null for pure(ish) json
034: */
035: public ObjectNonJsonOutboundVariable(
036: OutboundContext outboundContext, String aScriptClassName) {
037: super (outboundContext);
038: this .scriptClassName = aScriptClassName;
039:
040: isNamed = (scriptClassName != null && !""
041: .equals(scriptClassName));
042: if (isNamed) {
043: forceOutline();
044: }
045: }
046:
047: /**
048: * Generate an map declaration for a map of Outbound variables
049: * @param children The map of the converted contents
050: */
051: public void setChildren(Map<String, OutboundVariable> children) {
052: setChildren(children.values());
053: this .childMap = children;
054: }
055:
056: /* (non-Javadoc)
057: * @see org.directwebremoting.extend.OutboundVariable#prepareAssignCode()
058: */
059: public void prepareAssignCode() {
060: if (isOutline()) {
061: setAssignCode(getVariableName());
062: } else {
063: prepareChildAssignCodes();
064: setAssignCode(ObjectJsonOutboundVariable
065: .createJsonString(childMap));
066: }
067: }
068:
069: /* (non-Javadoc)
070: * @see org.directwebremoting.extend.OutboundVariable#prepareBuildDeclareCodes()
071: */
072: public void prepareBuildDeclareCodes() {
073: if (isOutline()) {
074: String declareCode;
075: String variableName = getVariableName();
076: if (!isNamed) {
077: declareCode = "var " + variableName + "={};";
078: } else {
079: declareCode = "var " + variableName + "=new "
080: + scriptClassName + "();";
081: }
082:
083: StringBuffer buildCode = new StringBuffer();
084: for (Map.Entry<String, OutboundVariable> entry : childMap
085: .entrySet()) {
086: String name = entry.getKey();
087: OutboundVariable nested = entry.getValue();
088:
089: String nestedAssignCode = nested.getAssignCode();
090:
091: // The semi-compact syntax is only any good for simple names
092: if (LocalUtil.isSimpleName(name)) {
093: buildCode.append(variableName);
094: buildCode.append('.');
095: buildCode.append(name);
096: buildCode.append('=');
097: buildCode.append(nestedAssignCode);
098: buildCode.append(';');
099: } else {
100: buildCode.append(variableName);
101: buildCode.append("['");
102: buildCode.append(name);
103: buildCode.append("']=");
104: buildCode.append(nestedAssignCode);
105: buildCode.append(';');
106: }
107: }
108: buildCode.append("\r\n");
109:
110: setBaseDeclareCode(declareCode);
111: setBaseBuildCode(buildCode.toString());
112: } else {
113: setBaseDeclareCode("");
114: setBaseBuildCode("");
115: }
116: }
117:
118: /* (non-Javadoc)
119: * @see java.lang.Object#toString()
120: */
121: @Override
122: public String toString() {
123: return "ObjectNonJsonOutboundVariable:" + childMap;
124: }
125:
126: /**
127: * Are we named (or does {@link #scriptClassName} have some contents)
128: */
129: private boolean isNamed;
130:
131: /**
132: * The contained variables
133: */
134: private Map<String, OutboundVariable> childMap;
135:
136: /**
137: * The name of this typed class if there is one
138: */
139: private String scriptClassName;
140: }
|