KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.StringReader JavaDoc;
19
20 import nu.xom.Builder;
21 import nu.xom.Document;
22 import nu.xom.Element;
23 import nu.xom.Node;
24
25 import org.directwebremoting.dwrp.SimpleOutboundVariable;
26 import org.directwebremoting.extend.Converter;
27 import org.directwebremoting.extend.InboundContext;
28 import org.directwebremoting.extend.InboundVariable;
29 import org.directwebremoting.extend.MarshallException;
30 import org.directwebremoting.extend.OutboundContext;
31 import org.directwebremoting.extend.OutboundVariable;
32 import org.directwebremoting.extend.EnginePrivate;
33 import org.directwebremoting.util.LocalUtil;
34
35 /**
36  * An implementation of Converter for DOM objects.
37  * @author Joe Walker [joe at eireneh dot com]
38  * @version $Id: StringConverter.java,v 1.2 2004/11/04 15:54:07 joe_walker Exp $
39  */

40 public class XOMConverter extends BaseV20Converter implements Converter
41 {
42     /* (non-Javadoc)
43      * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
44      */

45     public Object JavaDoc convertInbound(Class JavaDoc paramType, InboundVariable iv, InboundContext inctx) throws MarshallException
46     {
47         String JavaDoc value = LocalUtil.decode(iv.getValue());
48
49         try
50         {
51             Builder builder = new Builder();
52             Document doc = builder.build(new StringReader JavaDoc(value));
53
54             if (paramType == Document.class)
55             {
56                 return doc;
57             }
58             else if (paramType == Element.class)
59             {
60                 return doc.getRootElement();
61             }
62
63             throw new MarshallException(paramType);
64         }
65         catch (MarshallException ex)
66         {
67             throw ex;
68         }
69         catch (Exception JavaDoc ex)
70         {
71             throw new MarshallException(paramType, ex);
72         }
73     }
74
75     /* (non-Javadoc)
76      * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
77      */

78     public OutboundVariable convertOutbound(Object JavaDoc data, OutboundContext outctx) throws MarshallException
79     {
80         try
81         {
82             // Using XSLT to convert to a stream. Setup the source
83
if (!(data instanceof Node))
84             {
85                 throw new MarshallException(data.getClass());
86             }
87
88             Node node = (Node) data;
89
90             String JavaDoc script = EnginePrivate.xmlStringToJavascriptDom(node.toXML());
91             OutboundVariable ov = new SimpleOutboundVariable(script, outctx, false);
92
93             outctx.put(data, ov);
94
95             return ov;
96         }
97         catch (MarshallException ex)
98         {
99             throw ex;
100         }
101         catch (Exception JavaDoc ex)
102         {
103             throw new MarshallException(data.getClass(), ex);
104         }
105     }
106 }
107
Free Books   Free Magazines  
Popular Tags