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 | String argName = arg.getName(); |
58 | 0 | if (argName.equals("title")) |
59 | |
{ |
60 | 0 | entry.setTitle(StringUtils.trimToEmpty((String) arg.evaluate(message))); |
61 | |
} |
62 | 0 | else if (argName.equals("id")) |
63 | |
{ |
64 | 0 | entry.setId(StringUtils.trimToEmpty((String) arg.evaluate(message))); |
65 | |
} |
66 | 0 | else if (argName.equals("summary")) |
67 | |
{ |
68 | 0 | entry.setSummary(StringUtils.trimToEmpty((String) arg.evaluate(message))); |
69 | |
} |
70 | 0 | else if (argName.equals("content")) |
71 | |
{ |
72 | 0 | Object content = arg.evaluate(message); |
73 | 0 | if (content instanceof DataHandler) |
74 | |
{ |
75 | 0 | entry.setContent((DataHandler) content); |
76 | |
} |
77 | 0 | if (content instanceof Element) |
78 | |
{ |
79 | 0 | entry.setContent((Element) content); |
80 | |
} |
81 | 0 | if (content instanceof String) |
82 | |
{ |
83 | 0 | entry.setContent((String) content); |
84 | |
} |
85 | 0 | if (content instanceof InputStream) |
86 | |
{ |
87 | 0 | entry.setContent((InputStream) content); |
88 | |
} |
89 | 0 | } |
90 | 0 | else if (argName.equals("updated")) |
91 | |
{ |
92 | 0 | Object date = arg.evaluate(message); |
93 | 0 | if (date instanceof Date) |
94 | |
{ |
95 | 0 | entry.setUpdated((Date) date); |
96 | |
} |
97 | |
else |
98 | |
{ |
99 | 0 | entry.setUpdated(date.toString()); |
100 | |
} |
101 | 0 | } |
102 | 0 | else if (argName.equals("edited")) |
103 | |
{ |
104 | 0 | Object date = arg.evaluate(message); |
105 | 0 | if (date instanceof Date) |
106 | |
{ |
107 | 0 | entry.setEdited((Date) date); |
108 | |
} |
109 | |
else |
110 | |
{ |
111 | 0 | entry.setEdited(date.toString()); |
112 | |
} |
113 | 0 | } |
114 | 0 | else if (argName.equals("published")) |
115 | |
{ |
116 | 0 | Object date = arg.evaluate(message); |
117 | 0 | if (date instanceof Date) |
118 | |
{ |
119 | 0 | entry.setPublished((Date) date); |
120 | |
} |
121 | |
else |
122 | |
{ |
123 | 0 | entry.setPublished(date.toString()); |
124 | |
} |
125 | 0 | } |
126 | 0 | else if (argName.equals("rights")) |
127 | |
{ |
128 | 0 | entry.setRights((String) arg.evaluate(message)); |
129 | |
} |
130 | 0 | else if (argName.equals("draft")) |
131 | |
{ |
132 | 0 | entry.setDraft((Boolean) arg.evaluate(message)); |
133 | |
} |
134 | 0 | else if (argName.equals("author")) |
135 | |
{ |
136 | 0 | Object author = arg.evaluate(message); |
137 | 0 | if (author instanceof Person) |
138 | |
{ |
139 | 0 | entry.addAuthor((Person) author); |
140 | |
} |
141 | |
else |
142 | |
{ |
143 | 0 | entry.addAuthor(author.toString()); |
144 | |
} |
145 | 0 | } |
146 | 0 | else if (argName.equals("category")) |
147 | |
{ |
148 | 0 | Object category = arg.evaluate(message); |
149 | 0 | if (category instanceof Category) |
150 | |
{ |
151 | 0 | entry.addCategory((Category) category); |
152 | |
} |
153 | |
else |
154 | |
{ |
155 | 0 | entry.addCategory(category.toString()); |
156 | |
} |
157 | 0 | } |
158 | 0 | else if (argName.equals("contributor")) |
159 | |
{ |
160 | 0 | Object author = arg.evaluate(message); |
161 | 0 | if (author instanceof Person) |
162 | |
{ |
163 | 0 | entry.addContributor((Person) author); |
164 | |
} |
165 | |
else |
166 | |
{ |
167 | 0 | entry.addContributor(author.toString()); |
168 | |
} |
169 | 0 | } |
170 | 0 | else if (argName.equals("link")) |
171 | |
{ |
172 | 0 | Object link = arg.evaluate(message); |
173 | 0 | if (link instanceof Link) |
174 | |
{ |
175 | 0 | entry.addLink((Link) link); |
176 | |
} |
177 | |
else |
178 | |
{ |
179 | 0 | entry.addLink(link.toString()); |
180 | |
} |
181 | 0 | } |
182 | |
else |
183 | |
{ |
184 | 0 | throw new TransformerException(CoreMessages.propertyHasInvalidValue("entry-property.name", argName), this); |
185 | |
} |
186 | |
|
187 | 0 | } |
188 | |
|
189 | 0 | if (TYPE_ENTRY.equals(getReturnDataType())) |
190 | |
{ |
191 | 0 | return entry; |
192 | |
} |
193 | 0 | else if (TYPE_OUTPUT_HANDLER.equals(getReturnDataType())) |
194 | |
{ |
195 | 0 | final Entry e = entry; |
196 | 0 | return new OutputHandler() |
197 | 0 | { |
198 | |
public void write(MuleEvent event, OutputStream out) throws IOException |
199 | |
{ |
200 | 0 | FOMWriterOptions opts = new FOMWriterOptions(); |
201 | 0 | opts.setCharset(event.getEncoding()); |
202 | 0 | e.writeTo(out, opts); |
203 | 0 | } |
204 | |
}; |
205 | |
} |
206 | |
else |
207 | |
{ |
208 | 0 | return entry.toString(); |
209 | |
} |
210 | |
} |
211 | |
} |