KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > util > StreamWriterComponent


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;
18
19 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
20 import org.w3c.dom.Node JavaDoc;
21
22 import javax.jbi.messaging.MessageExchange;
23 import javax.jbi.messaging.NormalizedMessage;
24 import javax.xml.transform.OutputKeys JavaDoc;
25 import javax.xml.transform.Source JavaDoc;
26 import javax.xml.transform.Transformer JavaDoc;
27 import javax.xml.transform.TransformerFactory JavaDoc;
28 import javax.xml.transform.dom.DOMSource JavaDoc;
29 import javax.xml.transform.stream.StreamResult JavaDoc;
30
31 import java.io.OutputStream JavaDoc;
32
33 /**
34  * A Component that dumps a message to a stream
35  *
36  * @version $Revision: 426415 $
37  */

38 public class StreamWriterComponent extends OutBinding {
39
40     private OutputStream JavaDoc out = System.out;
41
42     /**
43      * @return Returns the out.
44      */

45     public OutputStream JavaDoc getOut() {
46         return out;
47     }
48
49     /**
50      * @param out The out to set.
51      */

52     public void setOut(OutputStream JavaDoc out) {
53         this.out = out;
54     }
55
56     
57
58     // Implementation methods
59
// -------------------------------------------------------------------------
60

61
62     protected void process(MessageExchange exchange, NormalizedMessage message) throws Exception JavaDoc {
63         TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
64         Transformer transformer = tFactory.newTransformer();
65         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
66         Source JavaDoc content = null;
67         Node JavaDoc document = (Node JavaDoc) message.getProperty(SourceTransformer.CONTENT_DOCUMENT_PROPERTY);
68         if (document != null) {
69             content = new DOMSource JavaDoc(document);
70         }
71         else {
72             content = message.getContent();
73         }
74         transformer.transform(content, new StreamResult JavaDoc(out));
75         done(exchange);
76     }
77 }
78
Popular Tags