1 16 package org.directwebremoting.convert; 17 18 import java.io.StringReader ; 19 import java.io.StringWriter ; 20 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.DocumentBuilderFactory ; 23 import javax.xml.transform.Source ; 24 import javax.xml.transform.Transformer ; 25 import javax.xml.transform.TransformerFactory ; 26 import javax.xml.transform.dom.DOMSource ; 27 import javax.xml.transform.stream.StreamResult ; 28 29 import org.directwebremoting.dwrp.SimpleOutboundVariable; 30 import org.directwebremoting.extend.Converter; 31 import org.directwebremoting.extend.InboundContext; 32 import org.directwebremoting.extend.InboundVariable; 33 import org.directwebremoting.extend.MarshallException; 34 import org.directwebremoting.extend.OutboundContext; 35 import org.directwebremoting.extend.OutboundVariable; 36 import org.directwebremoting.extend.EnginePrivate; 37 import org.directwebremoting.util.LocalUtil; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.Node ; 41 import org.xml.sax.InputSource ; 42 43 48 public class DOMConverter extends BaseV20Converter implements Converter 49 { 50 53 public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException 54 { 55 String value = LocalUtil.decode(iv.getValue()); 56 57 try 58 { 59 if (buildFactory == null) 60 { 61 buildFactory = DocumentBuilderFactory.newInstance(); 62 } 63 64 DocumentBuilder builder = buildFactory.newDocumentBuilder(); 65 66 InputSource is = new InputSource (new StringReader (value)); 67 Document doc = builder.parse(is); 68 69 if (paramType == Document .class) 70 { 71 return doc; 72 } 73 else if (paramType == Element .class) 74 { 75 return doc.getDocumentElement(); 76 } 77 78 throw new MarshallException(paramType); 79 } 80 catch (MarshallException ex) 81 { 82 throw ex; 83 } 84 catch (Exception ex) 85 { 86 throw new MarshallException(paramType, ex); 87 } 88 } 89 90 93 public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException 94 { 95 try 96 { 97 Transformer transformer = xslFact.newTransformer(); 98 99 Source source; 101 if (data instanceof Node ) 102 { 103 Node node = (Node ) data; 104 source = new DOMSource (node); 105 } 106 else 107 { 108 throw new MarshallException(data.getClass()); 109 } 110 111 StringWriter xml = new StringWriter (); 113 StreamResult result = new StreamResult (xml); 114 115 transformer.transform(source, result); 116 117 xml.flush(); 118 119 String script = EnginePrivate.xmlStringToJavascriptDom(xml.toString()); 120 OutboundVariable ov = new SimpleOutboundVariable(script, outctx, false); 121 122 outctx.put(data, ov); 123 124 return ov; 125 } 126 catch (MarshallException ex) 127 { 128 throw ex; 129 } 130 catch (Exception ex) 131 { 132 throw new MarshallException(data.getClass(), ex); 133 } 134 } 135 136 139 private TransformerFactory xslFact = TransformerFactory.newInstance(); 140 141 144 private DocumentBuilderFactory buildFactory = null; 145 } 146 | Popular Tags |