View Javadoc

1   /*
2    * $Id: Dom4jPropertyExtractor.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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  
11  package org.mule.util.properties;
12  
13  import org.mule.umo.UMOMessage;
14  
15  import org.dom4j.Node;
16  
17  /**
18   * Will select the text of a single node based on the property name
19   */
20  public class Dom4jPropertyExtractor implements PropertyExtractor
21  {
22      public Object getProperty(String name, Object message)
23      {
24          Object payload = message;
25          if (message instanceof UMOMessage)
26          {
27              payload = ((UMOMessage)message).getPayload();
28          }
29          if (payload instanceof org.dom4j.Document)
30          {
31              org.dom4j.Document dom4jDoc = (org.dom4j.Document)payload;
32              try
33              {
34                  Node node = dom4jDoc.selectSingleNode(name);
35                  if (node != null)
36                  {
37                      return node.getText();
38                  }
39              }
40              catch (Exception ignored)
41              {
42                  // ignore
43              }
44          }
45          else if (payload instanceof org.dom4j.Node)
46          {
47              org.dom4j.Node dom4jNode = (org.dom4j.Node)payload;
48              try
49              {
50                  Node node = dom4jNode.selectSingleNode(name);
51                  if (node != null)
52                  {
53                      return node.getText();
54                  }
55              }
56              catch (Exception ignored)
57              {
58                  // ignore
59              }
60          }
61          return null;
62      }
63  }