1
2
3
4
5
6
7
8
9
10 package org.mule.transformers.jaxb;
11
12 import org.mule.api.annotations.ContainsTransformerMethods;
13 import org.mule.api.annotations.Transformer;
14 import org.mule.api.annotations.param.InboundHeaders;
15 import org.mule.api.annotations.param.Payload;
16 import org.mule.jaxb.model.EmailAddress;
17 import org.mule.jaxb.model.Person;
18 import org.mule.module.xml.util.XMLUtils;
19
20 import java.io.InputStream;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24
25 import javax.xml.bind.JAXBContext;
26 import javax.xml.bind.JAXBException;
27 import javax.xml.xpath.XPathExpressionException;
28
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Node;
31
32
33
34
35
36
37 @ContainsTransformerMethods
38 public class JAXBTestTransformers
39 {
40
41 @Transformer(sourceTypes = {String.class})
42 public Person toPerson(Document doc, JAXBContext context) throws JAXBException
43 {
44 return (Person) context.createUnmarshaller().unmarshal(doc);
45 }
46
47
48
49 @Transformer(sourceTypes = {String.class, InputStream.class})
50 public List<EmailAddress> toEmailAddresses(@Payload Document doc, @InboundHeaders("*") Map headers, JAXBContext context) throws JAXBException, XPathExpressionException
51 {
52
53
54 if(!headers.get("foo").equals("fooValue"))
55 {
56 throw new IllegalArgumentException("Header foo was not set to the correct value 'fooValue'");
57 }
58
59 List<Node> nodes = XMLUtils.select("/person/emailAddresses/emailAddress", doc);
60 List<EmailAddress> addrs = new ArrayList<EmailAddress>(nodes.size());
61 for (Node node : nodes)
62 {
63 addrs.add(context.createUnmarshaller().unmarshal(node, EmailAddress.class).getValue());
64 }
65 return addrs;
66 }
67
68 }