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