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