/**
   * Try to precompile the arguments to the function. This method is shared by the implementations
   * of the three XPath functions matches(), replace(), and tokenize().
   *
   * @param args the supplied arguments to the function, as an array
   * @param patternArg the position of the argument containing the regular expression
   * @param flagsArg the position of the argument containing the flags
   * @return the compiled regular expression, or null indicating that the information is not
   *     available statically so it cannot be precompiled
   * @throws XPathException if any failure occurs, in particular, if the regular expression is
   *     invalid
   */
  public static RegularExpression tryToCompile(
      Expression[] args, int patternArg, int flagsArg, StaticContext env) throws XPathException {
    if (patternArg > args.length - 1) {
      // too few arguments were supplied; the error will be reported in due course
      return null;
    }
    CharSequence flagstr = null;
    if (args.length - 1 < flagsArg) {
      flagstr = "";
    } else if (args[flagsArg] instanceof StringValue) {
      flagstr = ((StringValue) args[flagsArg]).getStringValueCS();
    }

    if (args[patternArg] instanceof StringValue && flagstr != null) {
      try {
        Platform platform = env.getConfiguration().getPlatform();
        CharSequence in = ((StringValue) args[patternArg]).getStringValueCS();
        RegularExpression regexp = platform.compileRegularExpression(in, true, flagstr);
        return regexp;
      } catch (XPathException err) {
        StaticError e2 = new StaticError(err.getMessage());
        e2.setErrorCode("FORX0002");
        throw e2;
      }
    } else {
      return null;
    }
  }
 public void checkArguments(StaticContext env) throws XPathException {
   if (checked) return;
   checked = true;
   super.checkArguments(env);
   Optimizer opt = env.getConfiguration().getOptimizer();
   argument[1] = ExpressionTool.unsorted(opt, argument[1], false);
   if (argument[0] instanceof StringValue) {
     // common case, key name is supplied as a constant
     try {
       keyFingerprint =
           ((ExpressionContext) env)
               .getFingerprint(((StringValue) argument[0]).getStringValue(), false);
     } catch (XPathException e) {
       StaticError err =
           new StaticError(
               "Error in key name "
                   + ((StringValue) argument[0]).getStringValue()
                   + ": "
                   + e.getMessage());
       err.setLocator(this);
       err.setErrorCode("XTDE1260");
       throw err;
     }
     if (keyFingerprint == -1) {
       StaticError err =
           new StaticError(
               "Key " + ((StringValue) argument[0]).getStringValue() + " has not been defined");
       err.setLocator(this);
       err.setErrorCode("XTDE1260");
       throw err;
     }
   } else {
     // we need to save the namespace context
     nsContext = env.getNamespaceResolver();
   }
 }