KickJava   Java API By Example, From Geeks To Geeks.

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


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.dom4j.Document;
31 import org.dom4j.Element;
32 import org.dom4j.Node;
33 import org.dom4j.io.OutputFormat;
34 import org.dom4j.io.SAXReader;
35 import org.dom4j.io.XMLWriter;
36
37 /**
38  * An implementation of Converter for DOM objects.
39  * @author Joe Walker [joe at eireneh dot com]
40  * @version $Id: StringConverter.java,v 1.2 2004/11/04 15:54:07 joe_walker Exp $
41  */

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

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

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