KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.StringWriter 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.extend.EnginePrivate;
29 import org.directwebremoting.util.LocalUtil;
30 import org.jdom.Document;
31 import org.jdom.Element;
32 import org.jdom.input.SAXBuilder;
33 import org.jdom.output.Format;
34 import org.jdom.output.XMLOutputter;
35
36 /**
37  * An implementation of Converter for DOM objects.
38  * @author Joe Walker [joe at eireneh dot com]
39  * @version $Id: StringConverter.java,v 1.2 2004/11/04 15:54:07 joe_walker Exp $
40  */

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

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

79     public OutboundVariable convertOutbound(Object JavaDoc data, OutboundContext outctx) throws MarshallException
80     {
81         try
82         {
83             Format outformat = Format.getCompactFormat();
84             outformat.setEncoding("UTF-8");
85
86             // Setup the destination
87
StringWriter JavaDoc xml = new StringWriter JavaDoc();
88
89             XMLOutputter writer = new XMLOutputter(outformat);
90
91             // Using XSLT to convert to a stream. Setup the source
92
if (data instanceof Document)
93             {
94                 Document doc = (Document) data;
95                 writer.output(doc, xml);
96             }
97             else if (data instanceof Element)
98             {
99                 Element ele = (Element) data;
100                 writer.output(ele, xml);
101             }
102             else
103             {
104                 throw new MarshallException(data.getClass());
105             }
106
107             xml.flush();
108
109             String JavaDoc script = EnginePrivate.xmlStringToJavascriptDom(xml.toString());
110             OutboundVariable ov = new SimpleOutboundVariable(script, outctx, false);
111
112             outctx.put(data, ov);
113
114             return ov;
115         }
116         catch (MarshallException ex)
117         {
118             throw ex;
119         }
120         catch (Exception JavaDoc ex)
121         {
122             throw new MarshallException(data.getClass(), ex);
123         }
124     }
125 }
126
Popular Tags