KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.xml.parsers.DocumentBuilder JavaDoc;
22 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
23 import javax.xml.transform.Source JavaDoc;
24 import javax.xml.transform.Transformer JavaDoc;
25 import javax.xml.transform.TransformerFactory JavaDoc;
26 import javax.xml.transform.dom.DOMSource JavaDoc;
27 import javax.xml.transform.stream.StreamResult JavaDoc;
28
29 import org.directwebremoting.dwrp.SimpleOutboundVariable;
30 import org.directwebremoting.extend.Converter;
31 import org.directwebremoting.extend.InboundContext;
32 import org.directwebremoting.extend.InboundVariable;
33 import org.directwebremoting.extend.MarshallException;
34 import org.directwebremoting.extend.OutboundContext;
35 import org.directwebremoting.extend.OutboundVariable;
36 import org.directwebremoting.extend.EnginePrivate;
37 import org.directwebremoting.util.LocalUtil;
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42
43 /**
44  * An implementation of Converter for DOM objects.
45  * @author Joe Walker [joe at eireneh dot com]
46  * @version $Id: StringConverter.java,v 1.2 2004/11/04 15:54:07 joe_walker Exp $
47  */

48 public class DOMConverter extends BaseV20Converter implements Converter
49 {
50     /* (non-Javadoc)
51      * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
52      */

53     public Object JavaDoc convertInbound(Class JavaDoc paramType, InboundVariable iv, InboundContext inctx) throws MarshallException
54     {
55         String JavaDoc value = LocalUtil.decode(iv.getValue());
56
57         try
58         {
59             if (buildFactory == null)
60             {
61                 buildFactory = DocumentBuilderFactory.newInstance();
62             }
63
64             DocumentBuilder JavaDoc builder = buildFactory.newDocumentBuilder();
65
66             InputSource JavaDoc is = new InputSource JavaDoc(new StringReader JavaDoc(value));
67             Document JavaDoc doc = builder.parse(is);
68
69             if (paramType == Document JavaDoc.class)
70             {
71                 return doc;
72             }
73             else if (paramType == Element JavaDoc.class)
74             {
75                 return doc.getDocumentElement();
76             }
77
78             throw new MarshallException(paramType);
79         }
80         catch (MarshallException ex)
81         {
82             throw ex;
83         }
84         catch (Exception JavaDoc ex)
85         {
86             throw new MarshallException(paramType, ex);
87         }
88     }
89
90     /* (non-Javadoc)
91      * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
92      */

93     public OutboundVariable convertOutbound(Object JavaDoc data, OutboundContext outctx) throws MarshallException
94     {
95         try
96         {
97             Transformer JavaDoc transformer = xslFact.newTransformer();
98
99             // Using XSLT to convert to a stream. Setup the source
100
Source JavaDoc source;
101             if (data instanceof Node JavaDoc)
102             {
103                 Node JavaDoc node = (Node JavaDoc) data;
104                 source = new DOMSource JavaDoc(node);
105             }
106             else
107             {
108                 throw new MarshallException(data.getClass());
109             }
110
111             // Setup the destination
112
StringWriter JavaDoc xml = new StringWriter JavaDoc();
113             StreamResult JavaDoc result = new StreamResult JavaDoc(xml);
114
115             transformer.transform(source, result);
116
117             xml.flush();
118
119             String JavaDoc script = EnginePrivate.xmlStringToJavascriptDom(xml.toString());
120             OutboundVariable ov = new SimpleOutboundVariable(script, outctx, false);
121
122             outctx.put(data, ov);
123
124             return ov;
125         }
126         catch (MarshallException ex)
127         {
128             throw ex;
129         }
130         catch (Exception JavaDoc ex)
131         {
132             throw new MarshallException(data.getClass(), ex);
133         }
134     }
135
136     /**
137      * How we create new transformers
138      */

139     private TransformerFactory JavaDoc xslFact = TransformerFactory.newInstance();
140
141     /**
142      * How we create new documents
143      */

144     private DocumentBuilderFactory JavaDoc buildFactory = null;
145 }
146
Popular Tags