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