1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.atom.transformers; |
11 | |
|
12 | |
import org.mule.api.MuleEvent; |
13 | |
import org.mule.api.MuleMessage; |
14 | |
import org.mule.api.transformer.DataType; |
15 | |
import org.mule.api.transformer.TransformerException; |
16 | |
import org.mule.api.transport.OutputHandler; |
17 | |
import org.mule.config.i18n.CoreMessages; |
18 | |
import org.mule.expression.transformers.AbstractExpressionTransformer; |
19 | |
import org.mule.expression.transformers.ExpressionArgument; |
20 | |
import org.mule.transformer.types.DataTypeFactory; |
21 | |
import org.mule.util.StringUtils; |
22 | |
|
23 | |
import java.io.IOException; |
24 | |
import java.io.InputStream; |
25 | |
import java.io.OutputStream; |
26 | |
import java.util.Date; |
27 | |
|
28 | |
import javax.activation.DataHandler; |
29 | |
|
30 | |
import org.apache.abdera.Abdera; |
31 | |
import org.apache.abdera.factory.Factory; |
32 | |
import org.apache.abdera.model.Category; |
33 | |
import org.apache.abdera.model.Element; |
34 | |
import org.apache.abdera.model.Entry; |
35 | |
import org.apache.abdera.model.Link; |
36 | |
import org.apache.abdera.model.Person; |
37 | |
import org.apache.abdera.parser.stax.FOMWriterOptions; |
38 | |
|
39 | |
public class AtomEntryBuilderTransformer extends AbstractExpressionTransformer |
40 | |
{ |
41 | 0 | private static final DataType<Entry> TYPE_ENTRY = DataTypeFactory.create(Entry.class); |
42 | 0 | private static final DataType<OutputHandler> TYPE_OUTPUT_HANDLER = DataTypeFactory.create(OutputHandler.class); |
43 | |
|
44 | |
public AtomEntryBuilderTransformer() |
45 | 0 | { |
46 | 0 | setReturnDataType(TYPE_OUTPUT_HANDLER); |
47 | 0 | } |
48 | |
|
49 | |
@Override |
50 | |
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException |
51 | |
{ |
52 | 0 | Factory factory = Abdera.getInstance().getFactory(); |
53 | 0 | Entry entry = factory.newEntry(); |
54 | |
|
55 | 0 | for (ExpressionArgument arg: arguments) |
56 | |
{ |
57 | 0 | if (arg.getName().equals("title")) |
58 | |
{ |
59 | 0 | entry.setTitle(StringUtils.trimToEmpty((String) arg.evaluate(message))); |
60 | |
} |
61 | 0 | else if (arg.getName().equals("id")) |
62 | |
{ |
63 | 0 | entry.setId(StringUtils.trimToEmpty((String) arg.evaluate(message))); |
64 | |
} |
65 | 0 | else if (arg.getName().equals("summary")) |
66 | |
{ |
67 | 0 | entry.setSummary(StringUtils.trimToEmpty((String) arg.evaluate(message))); |
68 | |
} |
69 | 0 | else if (arg.getName().equals("content")) |
70 | |
{ |
71 | 0 | Object content = arg.evaluate(message); |
72 | 0 | if (content instanceof DataHandler) |
73 | |
{ |
74 | 0 | entry.setContent((DataHandler) content); |
75 | |
} |
76 | 0 | if (content instanceof Element) |
77 | |
{ |
78 | 0 | entry.setContent((Element) content); |
79 | |
} |
80 | 0 | if (content instanceof String) |
81 | |
{ |
82 | 0 | entry.setContent((String) content); |
83 | |
} |
84 | 0 | if (content instanceof InputStream) |
85 | |
{ |
86 | 0 | entry.setContent((InputStream) content); |
87 | |
} |
88 | 0 | } |
89 | 0 | else if (arg.getName().equals("updated")) |
90 | |
{ |
91 | 0 | Object date = arg.evaluate(message); |
92 | 0 | if (date instanceof Date) |
93 | |
{ |
94 | 0 | entry.setUpdated((Date) date); |
95 | |
} |
96 | |
else |
97 | |
{ |
98 | 0 | entry.setUpdated(date.toString()); |
99 | |
} |
100 | 0 | } |
101 | 0 | else if (arg.getName().equals("edited")) |
102 | |
{ |
103 | 0 | Object date = arg.evaluate(message); |
104 | 0 | if (date instanceof Date) |
105 | |
{ |
106 | 0 | entry.setEdited((Date) date); |
107 | |
} |
108 | |
else |
109 | |
{ |
110 | 0 | entry.setEdited(date.toString()); |
111 | |
} |
112 | 0 | } |
113 | 0 | else if (arg.getName().equals("published")) |
114 | |
{ |
115 | 0 | Object date = arg.evaluate(message); |
116 | 0 | if (date instanceof Date) |
117 | |
{ |
118 | 0 | entry.setPublished((Date) date); |
119 | |
} |
120 | |
else |
121 | |
{ |
122 | 0 | entry.setPublished(date.toString()); |
123 | |
} |
124 | 0 | } |
125 | 0 | else if (arg.getName().equals("rights")) |
126 | |
{ |
127 | 0 | entry.setRights((String) arg.evaluate(message)); |
128 | |
} |
129 | 0 | else if (arg.getName().equals("draft")) |
130 | |
{ |
131 | 0 | entry.setDraft((Boolean) arg.evaluate(message)); |
132 | |
} |
133 | 0 | else if (arg.getName().equals("author")) |
134 | |
{ |
135 | 0 | Object author = arg.evaluate(message); |
136 | 0 | if (author instanceof Person) |
137 | |
{ |
138 | 0 | entry.addAuthor((Person) author); |
139 | |
} |
140 | |
else |
141 | |
{ |
142 | 0 | entry.addAuthor(author.toString()); |
143 | |
} |
144 | 0 | } |
145 | 0 | else if (arg.getName().equals("category")) |
146 | |
{ |
147 | 0 | Object category = arg.evaluate(message); |
148 | 0 | if (category instanceof Category) |
149 | |
{ |
150 | 0 | entry.addCategory((Category) category); |
151 | |
} |
152 | |
else |
153 | |
{ |
154 | 0 | entry.addCategory(category.toString()); |
155 | |
} |
156 | 0 | } |
157 | 0 | else if (arg.getName().equals("contributor")) |
158 | |
{ |
159 | 0 | Object author = arg.evaluate(message); |
160 | 0 | if (author instanceof Person) |
161 | |
{ |
162 | 0 | entry.addContributor((Person) author); |
163 | |
} |
164 | |
else |
165 | |
{ |
166 | 0 | entry.addContributor(author.toString()); |
167 | |
} |
168 | 0 | } |
169 | 0 | else if (arg.getName().equals("link")) |
170 | |
{ |
171 | 0 | Object link = arg.evaluate(message); |
172 | 0 | if (link instanceof Link) |
173 | |
{ |
174 | 0 | entry.addLink((Link) link); |
175 | |
} |
176 | |
else |
177 | |
{ |
178 | 0 | entry.addLink(link.toString()); |
179 | |
} |
180 | 0 | } |
181 | |
else |
182 | |
{ |
183 | 0 | throw new TransformerException(CoreMessages.propertyHasInvalidValue("entry-property.name", arg.getName()), this); |
184 | |
} |
185 | |
|
186 | |
} |
187 | |
|
188 | 0 | if (TYPE_ENTRY.equals(getReturnDataType())) |
189 | |
{ |
190 | 0 | return entry; |
191 | |
} |
192 | 0 | else if (TYPE_OUTPUT_HANDLER.equals(getReturnDataType())) |
193 | |
{ |
194 | 0 | final Entry e = entry; |
195 | 0 | return new OutputHandler() |
196 | 0 | { |
197 | |
public void write(MuleEvent event, OutputStream out) throws IOException |
198 | |
{ |
199 | 0 | FOMWriterOptions opts = new FOMWriterOptions(); |
200 | 0 | opts.setCharset(event.getEncoding()); |
201 | 0 | e.writeTo(out, opts); |
202 | 0 | } |
203 | |
}; |
204 | |
} |
205 | |
else |
206 | |
{ |
207 | 0 | return entry.toString(); |
208 | |
} |
209 | |
} |
210 | |
} |