/**
  * Creates a new XPath instance and initializes it with the WorkflowNamespaceContext and the
  * WorkflowFunctionResolver.
  */
 public static XPath newXPath() {
   XPath xPath = XPathFactory.newInstance().newXPath();
   xPath.setNamespaceContext(new WorkflowNamespaceContext());
   WorkflowFunctionResolver resolver = new WorkflowFunctionResolver();
   xPath.setXPathFunctionResolver(resolver);
   resolver.setXpath(xPath);
   return xPath;
 }
 @JRubyMethod
 public IRubyObject evaluate(ThreadContext context, IRubyObject expr, IRubyObject handler) {
   String src = expr.convertToString().asJavaString();
   try {
     if (!handler.isNil()) {
       xpath.setXPathFunctionResolver(new NokogiriXPathFunctionResolver(handler));
     }
     XPathExpression xpathExpression = xpath.compile(src);
     return new XmlXpath(
         context.getRuntime(),
         (RubyClass) context.getRuntime().getClassFromPath("Nokogiri::XML::XPath"),
         xpathExpression,
         this.context);
   } catch (XPathExpressionException xpee) {
     throw new RaiseException(XmlSyntaxError.getXPathSyntaxError(context, xpee));
   }
 }
Exemple #3
0
 /**
  * Test for XPath.setXPathFunctionResolver(XPathFunctionResolver resolver). set resolver as null,
  * should throw NPE.
  */
 @Test(expectedExceptions = NullPointerException.class)
 public void testCheckXPath61() {
   xpath.setXPathFunctionResolver(null);
 }
Exemple #4
0
 /**
  * Test for XPath.setXPathFunctionResolver(XPathFunctionResolver resolver). Set a valid resolver
  * and retrieve it using getXPathFunctionResolver(), should return the same.
  */
 @Test
 public void testCheckXPath60() {
   xpath.setXPathFunctionResolver((functionName, arity) -> null);
   assertNotNull(xpath.getXPathFunctionResolver());
 }
  private Object evaluate(
      QName returnType,
      String code,
      ExpressionVariables variables,
      ObjectResolver objectResolver,
      Collection<FunctionLibrary> functions,
      String contextDescription,
      OperationResult result)
      throws ExpressionEvaluationException, ObjectNotFoundException, ExpressionSyntaxException {

    XPathExpressionCodeHolder codeHolder = new XPathExpressionCodeHolder(code);
    // System.out.println("code " + code);
    XPath xpath = factory.newXPath();
    XPathVariableResolver variableResolver =
        new LazyXPathVariableResolver(
            variables, objectResolver, contextDescription, prismContext, result);
    xpath.setXPathVariableResolver(variableResolver);
    xpath.setNamespaceContext(new MidPointNamespaceContext(codeHolder.getNamespaceMap()));
    xpath.setXPathFunctionResolver(getFunctionResolver(functions));

    XPathExpression expr;
    try {

      expr = xpath.compile(codeHolder.getExpressionAsString());

    } catch (Exception e) {
      Throwable originalException = ExceptionUtil.lookForTunneledException(e);
      if (originalException != null && originalException instanceof ObjectNotFoundException) {
        throw (ObjectNotFoundException) originalException;
      }
      if (originalException != null && originalException instanceof ExpressionSyntaxException) {
        throw (ExpressionSyntaxException) originalException;
      }
      if (e instanceof XPathExpressionException) {
        throw createExpressionEvaluationException(e, contextDescription);
      }
      if (e instanceof RuntimeException) {
        throw (RuntimeException) e;
      }
      throw new SystemException(e.getMessage(), e);
    }

    Object rootNode;
    try {
      rootNode = determineRootNode(variableResolver, contextDescription);
    } catch (SchemaException e) {
      throw new ExpressionSyntaxException(e.getMessage(), e);
    }
    Object evaluatedExpression;

    try {

      evaluatedExpression = expr.evaluate(rootNode, returnType);

    } catch (Exception e) {
      Throwable originalException = ExceptionUtil.lookForTunneledException(e);
      if (originalException != null && originalException instanceof ObjectNotFoundException) {
        throw (ObjectNotFoundException) originalException;
      }
      if (originalException != null && originalException instanceof ExpressionSyntaxException) {
        throw (ExpressionSyntaxException) originalException;
      }
      if (e instanceof XPathExpressionException) {
        throw createExpressionEvaluationException(e, contextDescription);
      }
      if (e instanceof RuntimeException) {
        throw (RuntimeException) e;
      }
      throw new SystemException(e.getMessage(), e);
    }

    if (evaluatedExpression == null) {
      return null;
    }

    return evaluatedExpression;
  }