1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.rss.transformers; |
8 | |
|
9 | |
import org.mule.api.transformer.TransformerException; |
10 | |
import org.mule.transformer.AbstractDiscoverableTransformer; |
11 | |
import org.mule.transformer.types.DataTypeFactory; |
12 | |
|
13 | |
import com.sun.syndication.feed.synd.SyndFeed; |
14 | |
import com.sun.syndication.io.SyndFeedInput; |
15 | |
import com.sun.syndication.io.XmlReader; |
16 | |
|
17 | |
import java.io.ByteArrayInputStream; |
18 | |
import java.io.File; |
19 | |
import java.io.InputStream; |
20 | |
import java.io.StringReader; |
21 | |
|
22 | |
import org.w3c.dom.Document; |
23 | |
import org.xml.sax.InputSource; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class ObjectToRssFeed extends AbstractDiscoverableTransformer |
29 | |
{ |
30 | |
public ObjectToRssFeed() |
31 | 0 | { |
32 | 0 | registerSourceType(DataTypeFactory.BYTE_ARRAY); |
33 | 0 | registerSourceType(DataTypeFactory.STRING); |
34 | 0 | registerSourceType(DataTypeFactory.INPUT_STREAM); |
35 | 0 | registerSourceType(DataTypeFactory.create(Document.class)); |
36 | 0 | registerSourceType(DataTypeFactory.create(InputSource.class)); |
37 | 0 | registerSourceType(DataTypeFactory.create(File.class)); |
38 | 0 | setReturnDataType(DataTypeFactory.create(SyndFeed.class)); |
39 | 0 | } |
40 | |
|
41 | |
@Override |
42 | |
protected Object doTransform(Object src, String outputEncoding) throws TransformerException |
43 | |
{ |
44 | 0 | SyndFeedInput feedInput = new SyndFeedInput(); |
45 | 0 | SyndFeed feed = null; |
46 | |
try |
47 | |
{ |
48 | 0 | if (src instanceof String) |
49 | |
{ |
50 | 0 | feed = feedInput.build(new StringReader(src.toString())); |
51 | |
|
52 | |
} |
53 | 0 | else if (src instanceof InputStream) |
54 | |
{ |
55 | 0 | feed = feedInput.build(new XmlReader((InputStream) src)); |
56 | |
|
57 | |
} |
58 | 0 | else if (src instanceof byte[]) |
59 | |
{ |
60 | 0 | feed = feedInput.build(new XmlReader(new ByteArrayInputStream((byte[]) src))); |
61 | |
|
62 | |
} |
63 | 0 | else if (src instanceof Document) |
64 | |
{ |
65 | 0 | feed = feedInput.build((Document) src); |
66 | |
|
67 | |
} |
68 | 0 | else if (src instanceof InputSource) |
69 | |
{ |
70 | 0 | feed = feedInput.build((InputSource) src); |
71 | |
|
72 | |
} |
73 | 0 | else if (src instanceof File) |
74 | |
{ |
75 | 0 | feed = feedInput.build((File) src); |
76 | |
|
77 | |
} |
78 | 0 | return feed; |
79 | |
} |
80 | 0 | catch (Exception e) |
81 | |
{ |
82 | 0 | throw new TransformerException(this, e); |
83 | |
} |
84 | |
} |
85 | |
} |