View Javadoc

1   /*
2    * $Id: AbstractXPathExpressionEvaluator.java 11231 2008-03-06 21:17:37Z rossmason $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.expression;
11  
12  import org.mule.api.MuleMessage;
13  import org.mule.api.MuleRuntimeException;
14  import org.mule.api.lifecycle.Disposable;
15  import org.mule.module.xml.i18n.XmlMessages;
16  import org.mule.util.expression.ExpressionEvaluator;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.WeakHashMap;
23  
24  import org.jaxen.JaxenException;
25  import org.jaxen.XPath;
26  
27  /**
28   * Provides a base class for XPath property extractors. The XPath engine used is jaxen (http://jaxen.org) which supports
29   * XPath queries on other object models such as JavaBeans as well as Xml
30   */
31  public abstract class AbstractXPathExpressionEvaluator implements ExpressionEvaluator, Disposable
32  {
33      private Map cache = new WeakHashMap(8);
34  
35      /** {@inheritDoc} */
36      public Object evaluate(String expression, Object message)
37      {
38          try
39          {
40              if(message instanceof MuleMessage)
41              {
42                  message = ((MuleMessage)message).getPayload();
43              }
44              XPath xpath = getXPath(expression, message);
45  
46              List result = xpath.selectNodes(message);
47              result = extractResultsFromNodes(result);
48              if(result.size()==1)
49              {
50                  return result.get(0);
51              }
52              else if(result.size()==0)
53              {
54                  return null;
55              }
56              else
57              {
58                  return result;
59              }
60          }
61          catch (JaxenException e)
62          {
63              throw new MuleRuntimeException(XmlMessages.failedToProcessXPath(expression), e);
64          }
65      }
66  
67      /** {@inheritDoc} */
68      public final void setName(String name)
69      {
70          throw new UnsupportedOperationException("setName");
71      }
72  
73      protected XPath getXPath(String expression, Object object) throws JaxenException
74      {
75          XPath xpath = (XPath)cache.get(expression + getClass().getName());
76          if(xpath==null)
77          {
78              xpath = createXPath(expression, object);
79              cache.put(expression + getClass().getName(), xpath);
80          }
81          return xpath;
82      }
83  
84      protected abstract XPath createXPath(String expression, Object object) throws JaxenException;
85  
86      protected List extractResultsFromNodes(List results)
87      {
88          if(results==null)
89          {
90              return null;
91          }
92          List newResults = new ArrayList(results.size());
93          for (Iterator iterator = results.iterator(); iterator.hasNext();)
94          {
95              Object o = iterator.next();
96              newResults.add(extractResultFromNode(o));
97          }
98          return newResults;
99      }
100 
101     /**
102      * A lifecycle method where implementor should free up any resources. If an
103      * exception is thrown it should just be logged and processing should continue.
104      * This method should not throw Runtime exceptions.
105      */
106     public void dispose()
107     {
108         cache.clear();
109     }
110 
111     protected abstract Object extractResultFromNode(Object result);
112 }