001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.convert;
017:
018: import java.io.StringReader;
019: import java.io.StringWriter;
020:
021: import org.directwebremoting.extend.Converter;
022: import org.directwebremoting.extend.EnginePrivate;
023: import org.directwebremoting.extend.InboundContext;
024: import org.directwebremoting.extend.InboundVariable;
025: import org.directwebremoting.extend.MarshallException;
026: import org.directwebremoting.extend.NonNestedOutboundVariable;
027: import org.directwebremoting.extend.OutboundContext;
028: import org.directwebremoting.extend.OutboundVariable;
029: import org.directwebremoting.util.LocalUtil;
030: import org.dom4j.Document;
031: import org.dom4j.Element;
032: import org.dom4j.Node;
033: import org.dom4j.io.OutputFormat;
034: import org.dom4j.io.SAXReader;
035: import org.dom4j.io.XMLWriter;
036:
037: /**
038: * An implementation of Converter for DOM objects.
039: * @author Joe Walker [joe at getahead dot ltd dot uk]
040: */
041: public class DOM4JConverter extends BaseV20Converter implements
042: Converter {
043: /* (non-Javadoc)
044: * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
045: */
046: public Object convertInbound(Class<?> paramType,
047: InboundVariable data, InboundContext inctx)
048: throws MarshallException {
049: String value = LocalUtil.decode(data.getValue());
050:
051: try {
052: SAXReader xmlReader = new SAXReader();
053: Document doc = xmlReader.read(new StringReader(value));
054:
055: if (paramType == Document.class) {
056: return doc;
057: } else if (paramType == Element.class) {
058: return doc.getRootElement();
059: }
060:
061: throw new MarshallException(paramType);
062: } catch (MarshallException ex) {
063: throw ex;
064: } catch (Exception ex) {
065: throw new MarshallException(paramType, ex);
066: }
067: }
068:
069: /* (non-Javadoc)
070: * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
071: */
072: public OutboundVariable convertOutbound(Object data,
073: OutboundContext outctx) throws MarshallException {
074: try {
075: // Using XSLT to convert to a stream. Setup the source
076: if (!(data instanceof Node)) {
077: throw new MarshallException(data.getClass());
078: }
079:
080: Node node = (Node) data;
081:
082: OutputFormat outformat = OutputFormat.createCompactFormat();
083: outformat.setEncoding("UTF-8");
084:
085: // Setup the destination
086: StringWriter xml = new StringWriter();
087:
088: XMLWriter writer = new XMLWriter(xml, outformat);
089: writer.write(node);
090: writer.flush();
091: xml.flush();
092:
093: String script = EnginePrivate.xmlStringToJavascriptDom(xml
094: .toString());
095: OutboundVariable ov = new NonNestedOutboundVariable(script);
096:
097: outctx.put(data, ov);
098:
099: return ov;
100: } catch (MarshallException ex) {
101: throw ex;
102: } catch (Exception ex) {
103: throw new MarshallException(data.getClass(), ex);
104: }
105: }
106: }
|