corpus-services  1.0
XPathEvaluator.java
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 
7 package de.uni_hamburg.corpora.utilities;
8 
9 import javax.xml.xpath.XPath;
10 import javax.xml.xpath.XPathExpression;
11 import javax.xml.xpath.XPathFactory;
12 import javax.xml.xpath.XPathFactoryConfigurationException;
13 import net.sf.saxon.lib.NamespaceConstant;
14 import org.w3c.dom.Document;
15 import java.util.logging.Level;
16 import java.util.logging.Logger;
17 import javax.xml.xpath.XPathExpressionException;
18 
23 public class XPathEvaluator {
24 
25  private static Document document;
26 
27 
28  public XPathEvaluator(Document doc){
29  document = doc;
30  }
31 
32 
33  public Object evaluate(String xpe){
34 
35  Object result = null;
36 
37  try{
38 
39  System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_SAXON, "net.sf.saxon.xpath.XPathFactoryImpl");
40  XPathFactory factory = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON);
41  XPath xpath = factory.newXPath();
42  XPathExpression expr = xpath.compile(xpe);
43  result = expr.evaluate(document);
44 
45  } catch (XPathFactoryConfigurationException ex) {
46  Logger.getLogger(XPathEvaluator.class.getName()).log(Level.SEVERE, null, ex);
47  } catch (XPathExpressionException ex) {
48  Logger.getLogger(XPathEvaluator.class.getName()).log(Level.SEVERE, null, ex);
49  }
50 
51  return result;
52 
53  }
54 
55  public Object evaluate(String xpe, Document doc){
56  document = doc;
57  return evaluate(xpe);
58  }
59 
60 }
Object evaluate(String xpe, Document doc)