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.io;
17:
18: import org.directwebremoting.extend.InboundContext;
19: import org.directwebremoting.extend.InboundVariable;
20: import org.directwebremoting.json.InvalidJsonException;
21: import org.directwebremoting.util.JavascriptUtil;
22:
23: /**
24: * Sometimes DWR can't know at runtime the type of the inbound data, this class
25: * allows us to defer conversion until we know more about the type we should
26: * be converting to.
27: * @author Joe Walker [joe at getahead dot ltd dot uk]
28: */
29: public class RawData {
30: /**
31: * RawData is immutable. Setup the raw string data that we wrap
32: * @param inboundContext The context variables that could be associated with this
33: * @param inboundVariable The variable we are delaying marshalling
34: */
35: public RawData(InboundVariable inboundVariable,
36: InboundContext inboundContext) {
37: this .inboundVariable = inboundVariable;
38: this .inboundContext = inboundContext;
39: }
40:
41: /**
42: * @return The context variables that could be associated with this
43: */
44: public InboundContext getInboundContext() {
45: return inboundContext;
46: }
47:
48: /**
49: * @return The variable we are delaying marshalling
50: */
51: public InboundVariable getInboundVariable() {
52: return inboundVariable;
53: }
54:
55: /**
56: * @see InboundVariable#getJsonValue(org.directwebremoting.extend.InboundVariable.OnJsonParseError)
57: * @return A JSON approximation of this data
58: */
59: public String toJsonString() {
60: try {
61: return inboundVariable.getJsonValue(
62: InboundVariable.OnJsonParseError.Hack)
63: .toExternalRepresentation();
64: } catch (InvalidJsonException ex) {
65: return "{ 'error':'The object could not be represented in JSON: "
66: + JavascriptUtil.escapeJavaScript(ex.toString())
67: + "' }";
68: }
69: }
70:
71: /* (non-Javadoc)
72: * @see java.lang.Object#toString()
73: */
74: @Override
75: public String toString() {
76: return toJsonString();
77: }
78:
79: /**
80: * The context variables that could be associated with this
81: */
82: private InboundContext inboundContext;
83:
84: /**
85: * The variable we are delaying marshalling
86: */
87: private InboundVariable inboundVariable;
88: }
|