View Javadoc

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