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