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 javax.xml.parsers.DocumentBuilder;
022: import javax.xml.parsers.DocumentBuilderFactory;
023: import javax.xml.transform.Source;
024: import javax.xml.transform.Transformer;
025: import javax.xml.transform.TransformerFactory;
026: import javax.xml.transform.dom.DOMSource;
027: import javax.xml.transform.stream.StreamResult;
028:
029: import org.directwebremoting.extend.Converter;
030: import org.directwebremoting.extend.EnginePrivate;
031: import org.directwebremoting.extend.InboundContext;
032: import org.directwebremoting.extend.InboundVariable;
033: import org.directwebremoting.extend.MarshallException;
034: import org.directwebremoting.extend.NonNestedOutboundVariable;
035: import org.directwebremoting.extend.OutboundContext;
036: import org.directwebremoting.extend.OutboundVariable;
037: import org.directwebremoting.util.LocalUtil;
038: import org.w3c.dom.Document;
039: import org.w3c.dom.Element;
040: import org.w3c.dom.Node;
041: import org.xml.sax.InputSource;
042:
043: /**
044: * An implementation of Converter for DOM objects.
045: * @author Joe Walker [joe at getahead dot ltd dot uk]
046: */
047: public class DOMConverter extends BaseV20Converter implements Converter {
048: /* (non-Javadoc)
049: * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
050: */
051: public Object convertInbound(Class<?> paramType,
052: InboundVariable data, InboundContext inctx)
053: throws MarshallException {
054: String value = LocalUtil.decode(data.getValue());
055:
056: try {
057: if (buildFactory == null) {
058: buildFactory = DocumentBuilderFactory.newInstance();
059: }
060:
061: DocumentBuilder builder = buildFactory.newDocumentBuilder();
062:
063: InputSource is = new InputSource(new StringReader(value));
064: Document doc = builder.parse(is);
065:
066: if (paramType == Document.class) {
067: return doc;
068: } else if (paramType == Element.class) {
069: return doc.getDocumentElement();
070: }
071:
072: throw new MarshallException(paramType);
073: } catch (MarshallException ex) {
074: throw ex;
075: } catch (Exception ex) {
076: throw new MarshallException(paramType, ex);
077: }
078: }
079:
080: /* (non-Javadoc)
081: * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
082: */
083: public OutboundVariable convertOutbound(Object data,
084: OutboundContext outctx) throws MarshallException {
085: try {
086: Transformer transformer = xslFact.newTransformer();
087:
088: // Using XSLT to convert to a stream. Setup the source
089: Source source;
090: if (data instanceof Node) {
091: Node node = (Node) data;
092: source = new DOMSource(node);
093: } else {
094: throw new MarshallException(data.getClass());
095: }
096:
097: // Setup the destination
098: StringWriter xml = new StringWriter();
099: StreamResult result = new StreamResult(xml);
100:
101: transformer.transform(source, result);
102:
103: xml.flush();
104:
105: String script = EnginePrivate.xmlStringToJavascriptDom(xml
106: .toString());
107: OutboundVariable ov = new NonNestedOutboundVariable(script);
108:
109: outctx.put(data, ov);
110:
111: return ov;
112: } catch (MarshallException ex) {
113: throw ex;
114: } catch (Exception ex) {
115: throw new MarshallException(data.getClass(), ex);
116: }
117: }
118:
119: /**
120: * How we create new transformers
121: */
122: private final TransformerFactory xslFact = TransformerFactory
123: .newInstance();
124:
125: /**
126: * How we create new documents
127: */
128: private DocumentBuilderFactory buildFactory = null;
129: }
|