예제 #1
0
  /**
   * Append an argument to the list of query operator arguments.
   *
   * @param q The query argument (query operator) to append.
   * @throws IllegalArgumentException q is an invalid argument
   */
  public void appendArg(Qry q) throws IllegalArgumentException {

    //  The query parser and query operator type system are too simple
    //  to detect some kinds of query syntax errors.  appendArg does
    //  additional syntax checking while creating the query tree.  It
    //  also inserts SCORE operators between QrySop operators and QryIop
    //  arguments, and propagates field information from QryIop
    //  children to parents.  Basically, it creates a well-formed
    //  query tree.

    if (this instanceof QryIopTerm) {
      throw new IllegalArgumentException("The TERM operator has no arguments.");
    }

    //  SCORE operators can have only a single argument of type QryIop.

    if (this instanceof QrySopScore) {
      if (this.args.size() > 0) {
        throw new IllegalArgumentException("Score operators can have only one argument");
      } else if (!(q instanceof QryIop)) {
        throw new IllegalArgumentException(
            "The argument to a SCORE operator must be of type QryIop.");
      } else {
        this.args.add(q);
        return;
      }
    }

    //  Check whether it is necessary to insert an implied SCORE
    //  operator between a QrySop operator and a QryIop argument.

    if ((this instanceof QrySop) && (q instanceof QryIop)) {
      Qry impliedOp = new QrySopScore();
      impliedOp.setDisplayName("#SCORE");
      impliedOp.appendArg(q);
      this.args.add(impliedOp);
      return;
    }

    //  QryIop operators must have QryIop arguments in the same field.

    if ((this instanceof QryIop) && (q instanceof QryIop)) {

      if (this.args.size() == 0) {
        ((QryIop) this).field = new String(((QryIop) q).getField());
      } else {
        if (!((QryIop) this).field.equals(((QryIop) q).getField())) {
          throw new IllegalArgumentException(
              "Arguments to QryIop operators must be in the same field.");
        }
      }

      this.args.add(q);
      return;
    }

    //  QrySop operators and their arguments must be of the same type.

    if ((this instanceof QrySop) && (q instanceof QrySop)) {
      this.args.add(q);
      return;
    }

    throw new IllegalArgumentException(
        "Objects of type "
            + q.getClass().getName()
            + " cannot be an argument to a query operator of type "
            + this.getClass().getName());
  }