01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
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.extend.Converter;
26: import org.directwebremoting.extend.EnginePrivate;
27: import org.directwebremoting.extend.InboundContext;
28: import org.directwebremoting.extend.InboundVariable;
29: import org.directwebremoting.extend.MarshallException;
30: import org.directwebremoting.extend.NonNestedOutboundVariable;
31: import org.directwebremoting.extend.OutboundContext;
32: import org.directwebremoting.extend.OutboundVariable;
33: import org.directwebremoting.util.LocalUtil;
34:
35: /**
36: * An implementation of Converter for DOM objects.
37: * @author Joe Walker [joe at getahead dot ltd dot uk]
38: */
39: public class XOMConverter extends BaseV20Converter implements Converter {
40: /* (non-Javadoc)
41: * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
42: */
43: public Object convertInbound(Class<?> paramType,
44: InboundVariable data, InboundContext inctx)
45: throws MarshallException {
46: String value = LocalUtil.decode(data.getValue());
47:
48: try {
49: Builder builder = new Builder();
50: Document doc = builder.build(new StringReader(value));
51:
52: if (paramType == Document.class) {
53: return doc;
54: } else if (paramType == Element.class) {
55: return doc.getRootElement();
56: }
57:
58: throw new MarshallException(paramType);
59: } catch (MarshallException ex) {
60: throw ex;
61: } catch (Exception ex) {
62: throw new MarshallException(paramType, ex);
63: }
64: }
65:
66: /* (non-Javadoc)
67: * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
68: */
69: public OutboundVariable convertOutbound(Object data,
70: OutboundContext outctx) throws MarshallException {
71: try {
72: // Using XSLT to convert to a stream. Setup the source
73: if (!(data instanceof Node)) {
74: throw new MarshallException(data.getClass());
75: }
76:
77: Node node = (Node) data;
78:
79: String script = EnginePrivate.xmlStringToJavascriptDom(node
80: .toXML());
81: OutboundVariable ov = new NonNestedOutboundVariable(script);
82:
83: outctx.put(data, ov);
84:
85: return ov;
86: } catch (MarshallException ex) {
87: throw ex;
88: } catch (Exception ex) {
89: throw new MarshallException(data.getClass(), ex);
90: }
91: }
92: }
|