net.sf.saxon.xpath.XPathEvaluator Java Examples

The following examples show how to use net.sf.saxon.xpath.XPathEvaluator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SaxonEngine.java    From jlibs with Apache License 2.0 6 votes vote down vote up
@Override
public List<Object> evaluate(TestCase testCase, String file) throws Exception{
    XPathFactoryImpl xpf = new XPathFactoryImpl();
    XPathEvaluator xpe = (XPathEvaluator)xpf.newXPath();
    xpe.getConfiguration().setVersionWarning(false);
    ((StandardErrorListener)xpe.getConfiguration().getErrorListener()).setRecoveryPolicy(Configuration.RECOVER_SILENTLY);
    xpe.getStaticContext().setBackwardsCompatibilityMode(true);
    xpe.setXPathVariableResolver(testCase.variableResolver);
    xpe.setXPathFunctionResolver(testCase.functionResolver);
    xpe.setNamespaceContext(testCase.nsContext);
    NodeInfo doc = xpe.getConfiguration().buildDocument(new SAXSource(new InputSource(file)));

    List<Object> results = new ArrayList<Object>(testCase.xpaths.size());
    for(XPathInfo xpathInfo: testCase.xpaths){
        if(xpathInfo.forEach==null)
            results.add(xpe.evaluate(xpathInfo.xpath, doc, xpathInfo.resultType));
        else{
            List<Object> list = new ArrayList<Object>();
            List nodeList = (List)xpe.evaluate(xpathInfo.forEach, doc, XPathConstants.NODESET);
            for(Object context: nodeList)
                list.add(xpe.evaluate(xpathInfo.xpath, context, xpathInfo.resultType));
            results.add(list);
        }
    }
    return results;
}
 
Example #2
Source File: JDKEngine.java    From jlibs with Apache License 2.0 5 votes vote down vote up
@Override
public List<Object> evaluate(TestCase testCase, String file) throws Exception{
    Document doc = toDOM(file);
    List<Object> results = new ArrayList<Object>(testCase.xpaths.size());
    XPath xpathObj = factory.newXPath();
    if(xpathObj instanceof XPathEvaluator){
        XPathEvaluator xpe = (XPathEvaluator)xpathObj;
        xpe.getConfiguration().setVersionWarning(false);
        ((StandardErrorListener)xpe.getConfiguration().getErrorListener()).setRecoveryPolicy(Configuration.RECOVER_SILENTLY);
        xpe.getStaticContext().setBackwardsCompatibilityMode(true);
    }
    for(XPathInfo xpathInfo: testCase.xpaths){
        xpathObj.setXPathVariableResolver(testCase.variableResolver);
        xpathObj.setXPathFunctionResolver(testCase.functionResolver);
        xpathObj.setNamespaceContext(testCase.nsContext);

        if(xpathInfo.forEach==null)
            results.add(xpathObj.evaluate(xpathInfo.xpath, doc, xpathInfo.resultType));
        else{
            List<Object> list = new ArrayList<Object>();
            NodeList nodeList = (NodeList)xpathObj.evaluate(xpathInfo.forEach, doc, XPathConstants.NODESET);
            for(int i=0; i<nodeList.getLength(); i++){
                Object context = nodeList.item(i);
                list.add(xpathObj.evaluate(xpathInfo.xpath, context, xpathInfo.resultType));
            }
            results.add(list);
        }
    }
    return results;
}
 
Example #3
Source File: PSXPathBoundSchema.java    From ph-schematron with Apache License 2.0 4 votes vote down vote up
@Nonnull
private XPath _createXPathContext ()
{
  final MapBasedNamespaceContext aNamespaceContext = getNamespaceContext ();
  final XPath aXPathContext = XPathHelper.createNewXPath (m_aXPathConfig.getXPathFactory (),
                                                          m_aXPathConfig.getXPathVariableResolver (),
                                                          m_aXPathConfig.getXPathFunctionResolver (),
                                                          aNamespaceContext);

  if ("net.sf.saxon.xpath.XPathEvaluator".equals (aXPathContext.getClass ().getName ()))
  {
    // Saxon implementation special handling
    final XPathEvaluator aSaxonXPath = (XPathEvaluator) aXPathContext;

    // Since 9.7.0-4 it must implement NamespaceResolver
    aSaxonXPath.setNamespaceContext (new SaxonNamespaceContext (aNamespaceContext));

    // Wrap the PSErrorHandler to a ErrorListener
    final Function <Configuration, ? extends ErrorReporter> factory = cfg -> {
      final IPSErrorHandler aErrHdl = getErrorHandler ();
      return (final XmlProcessingError error) -> {
        final ILocation aLocation = error.getLocation () == null ? null
                                                                 : new SimpleLocation (error.getLocation ()
                                                                                            .getSystemId (),
                                                                                       error.getLocation ()
                                                                                            .getLineNumber (),
                                                                                       error.getLocation ()
                                                                                            .getColumnNumber ());
        aErrHdl.handleError (SingleError.builder ()
                                        .setErrorLevel (error.isWarning () ? EErrorLevel.WARN : EErrorLevel.ERROR)
                                        .setErrorID (error.getErrorCode () != null ? error.getErrorCode ().toString ()
                                                                                   : null)
                                        .setErrorLocation (aLocation)
                                        .setErrorText (error.getMessage ())
                                        .setLinkedException (error.getCause ())
                                        .build ());
      };
    };
    aSaxonXPath.getConfiguration ().setErrorReporterFactory (factory);
  }
  return aXPathContext;
}
 
Example #4
Source File: Xpath2Selector.java    From webmagic with Apache License 2.0 4 votes vote down vote up
private void init() throws XPathExpressionException {
    XPathEvaluator xPathEvaluator = new XPathEvaluator();
    xPathEvaluator.setNamespaceContext(XPath2NamespaceContext.INSTANCE);
    xPathExpression = xPathEvaluator.compile(xpathStr);
}