KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > convert > URLConverter


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.MalformedURLException JavaDoc;
19 import java.net.URL JavaDoc;
20
21 import org.directwebremoting.dwrp.SimpleOutboundVariable;
22 import org.directwebremoting.extend.Converter;
23 import org.directwebremoting.extend.InboundContext;
24 import org.directwebremoting.extend.InboundVariable;
25 import org.directwebremoting.extend.MarshallException;
26 import org.directwebremoting.extend.OutboundContext;
27 import org.directwebremoting.extend.OutboundVariable;
28 import org.directwebremoting.util.JavascriptUtil;
29 import org.directwebremoting.util.LocalUtil;
30 import org.directwebremoting.util.Logger;
31
32 /**
33  * An implementation of Converter for Strings.
34  * @author Joe Walker [joe at eireneh dot com]
35  * @version $Id: URLConverter.java,v 1.4 2006/10/08 09:22:31 joe_walker Exp $
36  */

37 public class URLConverter extends BaseV20Converter implements Converter
38 {
39     /* (non-Javadoc)
40      * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
41      */

42     public Object JavaDoc convertInbound(Class JavaDoc paramType, InboundVariable iv, InboundContext inctx) throws MarshallException
43     {
44         String JavaDoc urlString = LocalUtil.decode(iv.getValue());
45         try
46         {
47             return new URL JavaDoc(urlString);
48         }
49         catch (MalformedURLException JavaDoc ex)
50         {
51             log.warn("Failed to create URL from string '" + urlString + "'. Returning null");
52             return null;
53         }
54     }
55
56     /* (non-Javadoc)
57      * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
58      */

59     public OutboundVariable convertOutbound(Object JavaDoc data, OutboundContext outctx) throws MarshallException
60     {
61         URL JavaDoc url = (URL JavaDoc) data;
62         String JavaDoc escaped = JavascriptUtil.escapeJavaScript(url.toExternalForm());
63         return new SimpleOutboundVariable('\"' + escaped + '\"', outctx, true);
64     }
65
66     /**
67      * The log stream
68      */

69     private static final Logger log = Logger.getLogger(URLConverter.class);
70 }
71
Popular Tags