1 17 package org.apache.servicemix.components.util.xstream; 18 19 import com.thoughtworks.xstream.XStream; 20 import com.thoughtworks.xstream.io.xml.DomReader; 21 import com.thoughtworks.xstream.io.xml.DomWriter; 22 23 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 24 import org.apache.servicemix.jbi.jaxp.StringSource; 25 import org.apache.servicemix.jbi.messaging.DefaultMarshaler; 26 import org.apache.servicemix.jbi.messaging.PojoMarshaler; 27 import org.logicblaze.lingo.LingoInvocation; 28 import org.w3c.dom.Document ; 29 30 import javax.jbi.messaging.MessageExchange; 31 import javax.jbi.messaging.MessagingException; 32 import javax.jbi.messaging.NormalizedMessage; 33 import javax.xml.parsers.ParserConfigurationException ; 34 import javax.xml.transform.Source ; 35 import javax.xml.transform.TransformerException ; 36 import javax.xml.transform.dom.DOMResult ; 37 import javax.xml.transform.dom.DOMSource ; 38 import javax.xml.transform.stream.StreamSource ; 39 40 47 public class XStreamMarshaler extends DefaultMarshaler { 48 private XStream xStream; 49 private SourceTransformer transformer = new SourceTransformer(); 50 private boolean useDOM = false; 51 52 public void marshal(MessageExchange exchange, NormalizedMessage message, Object body) throws MessagingException { 53 if (useDOM) { 54 try { 55 Document document = transformer.createDocument(); 56 getXStream().marshal(body, new DomWriter(document)); 57 message.setContent(new DOMSource (document)); 58 } 59 catch (ParserConfigurationException e) { 60 throw new MessagingException("Failed to marshal: " + body + " to DOM document: " + e, e); 61 } 62 } 63 else { 64 String xml = getXStream().toXML(body); 65 message.setContent(new StringSource(xml)); 66 } 67 } 68 69 public Object unmarshal(MessageExchange exchange, NormalizedMessage message) throws MessagingException { 70 Source content = message.getContent(); 71 if (content != null) { 72 if (content instanceof StreamSource ) { 73 StreamSource source = (StreamSource ) content; 74 return getXStream().fromXML(source.getReader()); 75 } 76 Document document = null; 77 if (content instanceof DOMSource ) { 78 DOMSource domSource = (DOMSource ) content; 79 document = (Document ) domSource.getNode(); 80 } 81 else { 82 DOMResult result = new DOMResult (); 83 try { 84 transformer.toResult(content, result); 85 } 86 catch (TransformerException e) { 87 throw new MessagingException("Failed to transform content: " + content + " to DOMResult: " + e, e); 88 } 89 document = (Document ) result.getNode(); 90 } 91 return getXStream().unmarshal(new DomReader(document)); 92 } 93 return super.unmarshal(exchange, message); 94 } 95 96 public XStream getXStream() { 99 if (xStream == null) { 100 xStream = createXStream(); 101 } 102 return xStream; 103 } 104 105 public void setXStream(XStream xStream) { 106 this.xStream = xStream; 107 } 108 109 public SourceTransformer getTransformer() { 110 return transformer; 111 } 112 113 public void setTransformer(SourceTransformer transformer) { 114 this.transformer = transformer; 115 } 116 117 123 public boolean isUseDOM() { 124 return useDOM; 125 } 126 127 133 public void setUseDOM(boolean useDOM) { 134 this.useDOM = useDOM; 135 } 136 137 protected XStream createXStream() { 138 XStream answer = new XStream(); 139 answer.alias("invoke", LingoInvocation.class); 140 return answer; 141 } 142 143 } 144 | Popular Tags |