1
2
3
4
5
6
7 package org.mule.module.xml.config;
8
9 import org.mule.api.annotations.expression.XPath;
10 import org.mule.api.annotations.meta.Evaluator;
11 import org.mule.api.expression.ExpressionAnnotationParser;
12 import org.mule.expression.ExpressionConfig;
13 import org.mule.expression.transformers.ExpressionArgument;
14 import org.mule.module.xml.i18n.XmlMessages;
15
16 import java.lang.annotation.Annotation;
17
18 import org.w3c.dom.Document;
19 import org.w3c.dom.Element;
20 import org.w3c.dom.Node;
21 import org.w3c.dom.NodeList;
22
23
24
25
26
27
28
29
30 public class XPathAnnotationParser implements ExpressionAnnotationParser
31 {
32 public ExpressionArgument parse(Annotation annotation, Class<?> parameterType)
33 {
34 Evaluator evaluator = annotation.annotationType().getAnnotation(Evaluator.class);
35 String eval = "xpath2";
36 String type;
37 if (evaluator != null)
38 {
39 if (parameterType.equals(Node.class) || parameterType.equals(org.dom4j.Node.class) ||
40 parameterType.equals(Element.class) || parameterType.equals(org.dom4j.Element.class) ||
41 parameterType.equals(Document.class) || parameterType.equals(org.dom4j.Document.class))
42 {
43 type = "[node]";
44 }
45 else if(NodeList.class.isAssignableFrom(parameterType))
46 {
47 type = "[nodeset]";
48 }
49 else if(Boolean.class.isAssignableFrom(parameterType))
50 {
51 type = "[boolean]";
52 }
53 else if(Double.class.isAssignableFrom(parameterType))
54 {
55 type = "[number]";
56 }
57 else if(String.class.isAssignableFrom(parameterType))
58 {
59 type = "[string]";
60 }
61 else
62 {
63 throw new IllegalArgumentException(XmlMessages.xpathResultTypeNotSupported(parameterType).getMessage());
64 }
65 return new ExpressionArgument(null, new ExpressionConfig(type + ((XPath) annotation).value(),
66 eval, null), ((XPath) annotation).optional(), parameterType);
67 }
68 else
69 {
70 throw new IllegalArgumentException("The @Evaluator annotation must be set on an Expression Annotation");
71 }
72 }
73
74 public boolean supports(Annotation annotation)
75 {
76 return annotation instanceof XPath;
77 }
78 }