View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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   * Used to parse Bean parameter annotations. Note this annotation only supports the Jaxp API and w3c Dom.  There is
25   * not Dom4J support.
26   *
27   * @see org.mule.api.annotations.expression.XPath
28   * @see org.mule.module.xml.expression.XPathExpressionEvaluator
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  }