View Javadoc

1   package fr.in2p3.jsaga.helpers.xpath;
2   
3   import org.ogf.saga.error.NoSuccessException;
4   import org.w3c.dom.*;
5   
6   import javax.xml.xpath.*;
7   
8   /* ***************************************************
9   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
10  * ***             http://cc.in2p3.fr/             ***
11  * ***************************************************
12  * File:   XJSDLXPathSelector
13  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
14  * Date:   9 mai 2008
15  * ***************************************************
16  * Description:                                      */
17  /**
18   *
19   */
20  public class XJSDLXPathSelector {
21      private XPath m_selector;
22      private Document m_jcDesc;
23  
24      public XJSDLXPathSelector(Document jcDesc) {
25          m_selector = XPathFactory.newInstance().newXPath();
26          m_selector.setNamespaceContext(new XJSDLNamespaceContext());
27          m_jcDesc = jcDesc;
28      }
29  
30      public boolean exists(String xpath) throws NoSuccessException {
31          return this.exists(m_jcDesc, xpath);
32      }
33      public boolean exists(Node domTree, String xpath) throws NoSuccessException {
34          try {
35              Boolean result = (Boolean) m_selector.evaluate(xpath, domTree, XPathConstants.BOOLEAN);
36              return result.booleanValue();
37          } catch (XPathExpressionException e) {
38              throw new NoSuccessException(e);
39          }
40      }
41  
42      public String getString(String xpath) throws NoSuccessException {
43          return this.getString(m_jcDesc, xpath);
44      }
45      public String getString(Node domTree, String xpath) throws NoSuccessException {
46          try {
47              String result = (String) m_selector.evaluate(xpath, domTree, XPathConstants.STRING);
48              if (!result.equals("")) {
49                  return result;
50              } else {
51                  return null;
52              }
53          } catch (XPathExpressionException e) {
54              throw new NoSuccessException(e);
55          }
56      }
57  
58      public Node getNode(String xpath) throws NoSuccessException {
59          return this.getNode(m_jcDesc, xpath);
60      }
61      public Node getNode(Node domTree, String xpath) throws NoSuccessException {
62          try {
63              return (Node) m_selector.evaluate(xpath, domTree, XPathConstants.NODE);
64          } catch (XPathExpressionException e) {
65              throw new NoSuccessException(e);
66          }
67      }
68  
69      public NodeList getNodes(String xpath) throws NoSuccessException {
70          return this.getNodes(m_jcDesc, xpath);
71      }
72      public NodeList getNodes(Node domTree, String xpath) throws NoSuccessException {
73          try {
74              return (NodeList) m_selector.evaluate(xpath, domTree, XPathConstants.NODESET);
75          } catch (XPathExpressionException e) {
76              throw new NoSuccessException(e);
77          }
78      }
79  }