1 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.dwrp.SimpleOutboundVariable; 26 import org.directwebremoting.extend.Converter; 27 import org.directwebremoting.extend.InboundContext; 28 import org.directwebremoting.extend.InboundVariable; 29 import org.directwebremoting.extend.MarshallException; 30 import org.directwebremoting.extend.OutboundContext; 31 import org.directwebremoting.extend.OutboundVariable; 32 import org.directwebremoting.extend.EnginePrivate; 33 import org.directwebremoting.util.LocalUtil; 34 35 40 public class XOMConverter extends BaseV20Converter implements Converter 41 { 42 45 public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException 46 { 47 String value = LocalUtil.decode(iv.getValue()); 48 49 try 50 { 51 Builder builder = new Builder(); 52 Document doc = builder.build(new StringReader (value)); 53 54 if (paramType == Document.class) 55 { 56 return doc; 57 } 58 else if (paramType == Element.class) 59 { 60 return doc.getRootElement(); 61 } 62 63 throw new MarshallException(paramType); 64 } 65 catch (MarshallException ex) 66 { 67 throw ex; 68 } 69 catch (Exception ex) 70 { 71 throw new MarshallException(paramType, ex); 72 } 73 } 74 75 78 public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException 79 { 80 try 81 { 82 if (!(data instanceof Node)) 84 { 85 throw new MarshallException(data.getClass()); 86 } 87 88 Node node = (Node) data; 89 90 String script = EnginePrivate.xmlStringToJavascriptDom(node.toXML()); 91 OutboundVariable ov = new SimpleOutboundVariable(script, outctx, false); 92 93 outctx.put(data, ov); 94 95 return ov; 96 } 97 catch (MarshallException ex) 98 { 99 throw ex; 100 } 101 catch (Exception ex) 102 { 103 throw new MarshallException(data.getClass(), ex); 104 } 105 } 106 } 107
| Popular Tags
|