1 16 package org.directwebremoting.convert; 17 18 import java.io.StringReader ; 19 import java.io.StringWriter ; 20 21 import org.directwebremoting.dwrp.SimpleOutboundVariable; 22 import org.directwebremoting.extend.Converter; 23 import org.directwebremoting.extend.InboundContext; 24 import org.directwebremoting.extend.InboundVariable; 25 import org.directwebremoting.extend.MarshallException; 26 import org.directwebremoting.extend.OutboundContext; 27 import org.directwebremoting.extend.OutboundVariable; 28 import org.directwebremoting.extend.EnginePrivate; 29 import org.directwebremoting.util.LocalUtil; 30 import org.jdom.Document; 31 import org.jdom.Element; 32 import org.jdom.input.SAXBuilder; 33 import org.jdom.output.Format; 34 import org.jdom.output.XMLOutputter; 35 36 41 public class JDOMConverter extends BaseV20Converter implements Converter 42 { 43 46 public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException 47 { 48 String value = LocalUtil.decode(iv.getValue()); 49 50 try 51 { 52 SAXBuilder builder = new SAXBuilder(); 53 Document doc = builder.build(new StringReader (value)); 54 55 if (paramType == Document.class) 56 { 57 return doc; 58 } 59 else if (paramType == Element.class) 60 { 61 return doc.getRootElement(); 62 } 63 64 throw new MarshallException(paramType); 65 } 66 catch (MarshallException ex) 67 { 68 throw ex; 69 } 70 catch (Exception ex) 71 { 72 throw new MarshallException(paramType, ex); 73 } 74 } 75 76 79 public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException 80 { 81 try 82 { 83 Format outformat = Format.getCompactFormat(); 84 outformat.setEncoding("UTF-8"); 85 86 StringWriter xml = new StringWriter (); 88 89 XMLOutputter writer = new XMLOutputter(outformat); 90 91 if (data instanceof Document) 93 { 94 Document doc = (Document) data; 95 writer.output(doc, xml); 96 } 97 else if (data instanceof Element) 98 { 99 Element ele = (Element) data; 100 writer.output(ele, xml); 101 } 102 else 103 { 104 throw new MarshallException(data.getClass()); 105 } 106 107 xml.flush(); 108 109 String script = EnginePrivate.xmlStringToJavascriptDom(xml.toString()); 110 OutboundVariable ov = new SimpleOutboundVariable(script, outctx, false); 111 112 outctx.put(data, ov); 113 114 return ov; 115 } 116 catch (MarshallException ex) 117 { 118 throw ex; 119 } 120 catch (Exception ex) 121 { 122 throw new MarshallException(data.getClass(), ex); 123 } 124 } 125 } 126
| Popular Tags
|