1 17 package org.apache.servicemix.jbi.messaging; 18 19 import org.apache.servicemix.jbi.jaxp.StringSource; 20 import org.w3c.dom.Node ; 21 22 import javax.jbi.messaging.MessageExchange; 23 import javax.jbi.messaging.MessagingException; 24 import javax.jbi.messaging.NormalizedMessage; 25 import javax.xml.transform.Source ; 26 import javax.xml.transform.dom.DOMSource ; 27 28 35 public class DefaultMarshaler implements PojoMarshaler { 36 37 private PojoMarshaler parent; 38 39 public DefaultMarshaler() { 40 } 41 42 public DefaultMarshaler(PojoMarshaler parent) { 43 this.parent = parent; 44 } 45 46 public PojoMarshaler getParent() { 47 return parent; 48 } 49 50 public void marshal(MessageExchange exchange, NormalizedMessage message, Object body) throws MessagingException { 51 if (body instanceof Source) { 52 message.setContent((Source) body); 53 } 54 else { 55 message.setProperty(BODY, body); 56 Source content = asContent(message, body); 57 message.setContent(content); 58 } 59 } 60 61 public Object unmarshal(MessageExchange exchange, NormalizedMessage message) throws MessagingException { 62 Object answer = message.getProperty(BODY); 63 if (answer == null) { 64 if (parent != null) { 65 answer = parent.unmarshal(exchange, message); 66 } 67 if (answer == null) { 68 answer = defaultUnmarshal(exchange, message); 69 } 70 } 71 return answer; 72 } 73 74 protected Object defaultUnmarshal(MessageExchange exchange, NormalizedMessage message) { 75 Source content = message.getContent(); 76 if (content instanceof DOMSource ) { 77 DOMSource source = (DOMSource ) content; 78 return source.getNode(); 79 } 80 return content; 81 } 82 83 protected Source asContent(NormalizedMessage message, Object body) { 84 if (body instanceof Source) { 85 return (Source) body; 86 } 87 else if (body instanceof String ) { 88 return new StringSource((String ) body); 90 } 91 else if (body instanceof Node ) { 92 return new DOMSource ((Node ) body); 93 } 94 return null; 95 } 96 } 97 | Popular Tags |