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.convert;
17:
18: import java.net.URI;
19: import java.net.URISyntaxException;
20:
21: import org.apache.commons.logging.Log;
22: import org.apache.commons.logging.LogFactory;
23: import org.directwebremoting.extend.Converter;
24: import org.directwebremoting.extend.InboundContext;
25: import org.directwebremoting.extend.InboundVariable;
26: import org.directwebremoting.extend.MarshallException;
27: import org.directwebremoting.extend.NonNestedOutboundVariable;
28: import org.directwebremoting.extend.OutboundContext;
29: import org.directwebremoting.extend.OutboundVariable;
30: import org.directwebremoting.util.JavascriptUtil;
31: import org.directwebremoting.util.LocalUtil;
32:
33: /**
34: * An implementation of Converter for Strings.
35: * @author Joe Walker [joe at getahead dot ltd dot uk]
36: */
37: public class URIConverter extends BaseV20Converter implements Converter {
38: /* (non-Javadoc)
39: * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
40: */
41: public Object convertInbound(Class<?> paramType,
42: InboundVariable data, InboundContext inctx)
43: throws MarshallException {
44: String uriString = LocalUtil.decode(data.getValue());
45: try {
46: return new URI(uriString);
47: } catch (URISyntaxException ex) {
48: log.warn("Failed to create URL from string '" + uriString
49: + "'. Returning null");
50: return null;
51: }
52: }
53:
54: /* (non-Javadoc)
55: * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
56: */
57: public OutboundVariable convertOutbound(Object data,
58: OutboundContext outctx) throws MarshallException {
59: URI uri = (URI) data;
60: String escaped = JavascriptUtil
61: .escapeJavaScript(uri.toString());
62: return new NonNestedOutboundVariable('\"' + escaped + '\"');
63: }
64:
65: /**
66: * The log stream
67: */
68: private static final Log log = LogFactory
69: .getLog(URIConverter.class);
70: }
|