KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > util > xstream > XStreamMarshaler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
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 JavaDoc;
34 import javax.xml.transform.Source JavaDoc;
35 import javax.xml.transform.TransformerException JavaDoc;
36 import javax.xml.transform.dom.DOMResult JavaDoc;
37 import javax.xml.transform.dom.DOMSource JavaDoc;
38 import javax.xml.transform.stream.StreamSource JavaDoc;
39
40 /**
41  * A {@link PojoMarshaler} for <a HREF="http://xStream.codehaus.org/">XStream</a> which
42  * streams the object to an a W3C DOM Document so that other components can access
43  * the XML without an extra parse.
44  *
45  * @version $Revision: 426415 $
46  */

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 JavaDoc body) throws MessagingException {
53         if (useDOM) {
54             try {
55                 Document JavaDoc document = transformer.createDocument();
56                 getXStream().marshal(body, new DomWriter(document));
57                 message.setContent(new DOMSource JavaDoc(document));
58             }
59             catch (ParserConfigurationException JavaDoc e) {
60                 throw new MessagingException("Failed to marshal: " + body + " to DOM document: " + e, e);
61             }
62         }
63         else {
64             String JavaDoc xml = getXStream().toXML(body);
65             message.setContent(new StringSource(xml));
66         }
67     }
68
69     public Object JavaDoc unmarshal(MessageExchange exchange, NormalizedMessage message) throws MessagingException {
70         Source content = message.getContent();
71         if (content != null) {
72             if (content instanceof StreamSource JavaDoc) {
73                 StreamSource JavaDoc source = (StreamSource JavaDoc) content;
74                 return getXStream().fromXML(source.getReader());
75             }
76             Document JavaDoc document = null;
77             if (content instanceof DOMSource JavaDoc) {
78                 DOMSource JavaDoc domSource = (DOMSource JavaDoc) content;
79                 document = (Document JavaDoc) domSource.getNode();
80             }
81             else {
82                 DOMResult JavaDoc result = new DOMResult JavaDoc();
83                 try {
84                     transformer.toResult(content, result);
85                 }
86                 catch (TransformerException JavaDoc e) {
87                     throw new MessagingException("Failed to transform content: " + content + " to DOMResult: " + e, e);
88                 }
89                 document = (Document JavaDoc) result.getNode();
90             }
91             return getXStream().unmarshal(new DomReader(document));
92         }
93         return super.unmarshal(exchange, message);
94     }
95
96     // Properties
97
//-------------------------------------------------------------------------
98
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     /**
118      * Whether or not DOM should be used for marshalling XML - which is preferable if another component
119      * in the pipeline will want to parse the body.
120      *
121      * @return
122      */

123     public boolean isUseDOM() {
124         return useDOM;
125     }
126
127     /**
128      * Enables DOM output when marshalling in case other components in the pipeline wish to perform
129      * parsing.
130      *
131      * @param useDOM
132      */

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