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.jdom.Document;
031: import org.jdom.Element;
032: import org.jdom.input.SAXBuilder;
033: import org.jdom.output.Format;
034: import org.jdom.output.XMLOutputter;
035:
036: /**
037: * An implementation of Converter for DOM objects.
038: * @author Joe Walker [joe at getahead dot ltd dot uk]
039: */
040: public class JDOMConverter extends BaseV20Converter implements
041: Converter {
042: /* (non-Javadoc)
043: * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
044: */
045: public Object convertInbound(Class<?> paramType,
046: InboundVariable data, InboundContext inctx)
047: throws MarshallException {
048: String value = LocalUtil.decode(data.getValue());
049:
050: try {
051: SAXBuilder builder = new SAXBuilder();
052: Document doc = builder.build(new StringReader(value));
053:
054: if (paramType == Document.class) {
055: return doc;
056: } else if (paramType == Element.class) {
057: return doc.getRootElement();
058: }
059:
060: throw new MarshallException(paramType);
061: } catch (MarshallException ex) {
062: throw ex;
063: } catch (Exception ex) {
064: throw new MarshallException(paramType, ex);
065: }
066: }
067:
068: /* (non-Javadoc)
069: * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
070: */
071: public OutboundVariable convertOutbound(Object data,
072: OutboundContext outctx) throws MarshallException {
073: try {
074: Format outformat = Format.getCompactFormat();
075: outformat.setEncoding("UTF-8");
076:
077: // Setup the destination
078: StringWriter xml = new StringWriter();
079:
080: XMLOutputter writer = new XMLOutputter(outformat);
081:
082: // Using XSLT to convert to a stream. Setup the source
083: if (data instanceof Document) {
084: Document doc = (Document) data;
085: writer.output(doc, xml);
086: } else if (data instanceof Element) {
087: Element ele = (Element) data;
088: writer.output(ele, xml);
089: } else {
090: throw new MarshallException(data.getClass());
091: }
092:
093: xml.flush();
094:
095: String script = EnginePrivate.xmlStringToJavascriptDom(xml
096: .toString());
097: OutboundVariable ov = new NonNestedOutboundVariable(script);
098:
099: outctx.put(data, ov);
100:
101: return ov;
102: } catch (MarshallException ex) {
103: throw ex;
104: } catch (Exception ex) {
105: throw new MarshallException(data.getClass(), ex);
106: }
107: }
108: }
|