01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.extend;
17:
18: import java.util.ArrayList;
19: import java.util.List;
20:
21: import org.directwebremoting.ScriptBuffer;
22:
23: /**
24: * A simple utility class to extract a {@link String} from a {@link ScriptBuffer}.
25: * @author Joe Walker [joe at getahead dot ltd dot uk]
26: */
27: public class ScriptBufferUtil {
28: /**
29: * Ensure we can't be created
30: */
31: private ScriptBufferUtil() {
32: }
33:
34: /**
35: * Return a string ready for output.
36: * @param buffer The source of the script data
37: * @param converterManager How we convert script variable to Javascript
38: * @return Some Javascript to be eval()ed by a browser.
39: * @throws MarshallException If an error happens during parameter marshalling
40: */
41: public static String createOutput(ScriptBuffer buffer,
42: ConverterManager converterManager) throws MarshallException {
43: return createOutput(buffer, converterManager, false);
44: }
45:
46: /**
47: * Return a string ready for output.
48: * @param buffer The source of the script data
49: * @param converterManager How we convert script variable to Javascript
50: * @param jsonOutput Are we doing strict JSON output?
51: * @return Some Javascript to be eval()ed by a browser.
52: * @throws MarshallException If an error happens during parameter marshalling
53: */
54: public static String createOutput(ScriptBuffer buffer,
55: ConverterManager converterManager, boolean jsonOutput)
56: throws MarshallException {
57: OutboundContext context = new OutboundContext(jsonOutput);
58: List<OutboundVariable> scriptParts = new ArrayList<OutboundVariable>();
59:
60: // First convert everything into OutboundVariables
61: for (Object part : buffer.getParts()) {
62: OutboundVariable ov = converterManager.convertOutbound(
63: part, context);
64: scriptParts.add(ov);
65: }
66:
67: StringBuffer output = new StringBuffer();
68: context.prepareForOutput();
69:
70: // First we look for the declaration code
71: for (OutboundVariable ov : scriptParts) {
72: output.append(ov.getDeclareCode());
73: }
74:
75: // Then we look for the construction code
76: for (OutboundVariable ov : scriptParts) {
77: output.append(ov.getBuildCode());
78: }
79:
80: // Then we output everything else
81: for (OutboundVariable ov : scriptParts) {
82: String assignCode = ov.getAssignCode();
83: if (assignCode == null) {
84: throw new NullPointerException();
85: }
86: output.append(assignCode);
87: }
88:
89: return output.toString();
90: }
91: }
|