Beispiel #1
0
  public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
      context.getProfiler().start(this);
      context
          .getProfiler()
          .message(
              this,
              Profiler.DEPENDENCIES,
              "DEPENDENCIES",
              Dependency.getDependenciesName(this.getDependencies()));
      if (contextSequence != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
      if (contextItem != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
    }

    Sequence seq;
    Sequence result;

    if (contextItem != null) contextSequence = contextItem.toSequence();

    /*
    if (contextSequence == null || contextSequence.isEmpty())
    	result = Sequence.EMPTY_SEQUENCE;
    */

    // If we have one argument, we take it into account
    if (getSignature().getArgumentCount() > 0)
      seq = getArgument(0).eval(contextSequence, contextItem);
    // Otherwise, we take the context sequence and we iterate over it
    else seq = contextSequence;

    if (seq == null) throw new XPathException(this, "XPDY0002: Undefined context item");

    if (seq.isEmpty())
      // Bloody specs !
      result = StringValue.EMPTY_STRING;
    else {
      Item item = seq.itemAt(0);
      if (!Type.subTypeOf(item.getType(), Type.NODE))
        throw new XPathException(
            this, "XPTY0004: item is not a node; got '" + Type.getTypeName(item.getType()) + "'");
      // TODO : how to improve performance ?
      Node n = ((NodeValue) item).getNode();
      if (n instanceof QNameable)
        result = new StringValue(((QNameable) n).getQName().getStringValue());
      else result = StringValue.EMPTY_STRING;
    }

    if (context.getProfiler().isEnabled()) context.getProfiler().end(this, "", result);

    return result;
  }
  public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (context.getProfiler().isEnabled()) {
      context.getProfiler().start(this);
      context
          .getProfiler()
          .message(
              this,
              Profiler.DEPENDENCIES,
              "DEPENDENCIES",
              Dependency.getDependenciesName(this.getDependencies()));
      if (contextSequence != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
    }

    Sequence result;
    if (args.length == 0 || args[0].isEmpty()) {
      result = Sequence.EMPTY_SEQUENCE;
    } else {
      Sequence arg = args[0];
      AbstractDateTimeValue date = (AbstractDateTimeValue) arg.itemAt(0);
      if (isCalledAs("day-from-dateTime") || isCalledAs("day-from-date")) {
        result = new IntegerValue(date.getPart(DateValue.DAY), Type.INTEGER);
      } else if (isCalledAs("month-from-dateTime") || isCalledAs("month-from-date")) {
        result = new IntegerValue(date.getPart(DateValue.MONTH), Type.INTEGER);
      } else if (isCalledAs("year-from-dateTime") || isCalledAs("year-from-date")) {
        result = new IntegerValue(date.getPart(DateValue.YEAR), Type.INTEGER);
      } else if (isCalledAs("hours-from-dateTime") || isCalledAs("hours-from-time")) {
        result = new IntegerValue(date.getPart(DateValue.HOUR), Type.INTEGER);
      } else if (isCalledAs("minutes-from-dateTime") || isCalledAs("minutes-from-time")) {
        result = new IntegerValue(date.getPart(DateValue.MINUTE), Type.INTEGER);
      } else if (isCalledAs("seconds-from-dateTime") || isCalledAs("seconds-from-time")) {
        result = new IntegerValue(date.calendar.getSecond()).convertTo(Type.DECIMAL);
        if (date.calendar.getFractionalSecond() != null)
          result =
              ((DecimalValue) result).plus(new DecimalValue(date.calendar.getFractionalSecond()));
      } else if (isCalledAs("timezone-from-dateTime")
          || isCalledAs("timezone-from-date")
          || isCalledAs("timezone-from-time")) {
        result = date.getTimezone();
      } else {
        throw new Error("can't handle function " + mySignature.getName().getLocalName());
      }
    }

    if (context.getProfiler().isEnabled()) context.getProfiler().end(this, "", result);

    return result;
  }
Beispiel #3
0
  /* (non-Javadoc)
   * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
   */
  public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (context.getProfiler().isEnabled()) {
      context.getProfiler().start(this);
      context
          .getProfiler()
          .message(
              this,
              Profiler.DEPENDENCIES,
              "DEPENDENCIES",
              Dependency.getDependenciesName(this.getDependencies()));
      if (contextSequence != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
    }

    Sequence result;
    // return 0 if the argument sequence is empty
    // TODO : return empty sequence ?
    if (args[0].isEmpty()) result = IntegerValue.ZERO;
    else {
      int count = 0;
      for (SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
        Item next = i.nextItem();
        if (Type.subTypeOf(next.getType(), Type.NODE)) {
          NodeValue nv = (NodeValue) next;
          if (nv.getImplementationType() != NodeValue.PERSISTENT_NODE)
            throw new XPathException(
                getASTNode(), getName() + " cannot be applied to in-memory nodes.");
          NodeProxy np = (NodeProxy) nv;
          Match match = np.getMatches();
          while (match != null) {
            if (match.getNodeId().isDescendantOrSelfOf(np.getNodeId())) {
              count += match.getFrequency();
            }
            match = match.getNextMatch();
          }
        }
      }
      result = new IntegerValue(count);
    }

    if (context.getProfiler().isEnabled()) context.getProfiler().end(this, "", result);

    return result;
  }
Beispiel #4
0
  public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
      context.getProfiler().start(this);
      context
          .getProfiler()
          .message(
              this,
              Profiler.DEPENDENCIES,
              "DEPENDENCIES",
              Dependency.getDependenciesName(this.getDependencies()));
      if (contextSequence != null) {
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
      }
      if (contextItem != null) {
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
      }
    }

    if (contextItem != null) {
      contextSequence = contextItem.toSequence();
    }

    Sequence result;
    final String s1 = getArgument(0).eval(contextSequence).getStringValue();
    final String s2 = getArgument(1).eval(contextSequence).getStringValue();
    if (s1.length() == 0 || s2.length() == 0) {
      result = Sequence.EMPTY_SEQUENCE;
    } else {
      final Collator collator = getCollator(contextSequence, contextItem, 3);
      if (Collations.startsWith(collator, s1, s2)) {
        result = BooleanValue.TRUE;
      } else {
        result = BooleanValue.FALSE;
      }
    }

    if (context.getProfiler().isEnabled()) {
      context.getProfiler().end(this, "", result);
    }

    return result;
  }
  /* (non-Javadoc)
   * @see org.exist.xquery.Expression#eval(org.exist.dom.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
   */
  public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
      context.getProfiler().start(this);
      context
          .getProfiler()
          .message(
              this,
              Profiler.DEPENDENCIES,
              "DEPENDENCIES",
              Dependency.getDependenciesName(this.getDependencies()));
      if (contextSequence != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
      if (contextItem != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
    }

    ResponseModule myModule = (ResponseModule) context.getModule(ResponseModule.NAMESPACE_URI);

    // response object is read from global variable $response
    Variable var = myModule.resolveVariable(ResponseModule.RESPONSE_VAR);
    if (var == null || var.getValue() == null) throw new XPathException("Response not set");
    if (var.getValue().getItemType() != Type.JAVA_OBJECT)
      throw new XPathException("Variable $response is not bound to a Java object.");
    JavaObjectValue response = (JavaObjectValue) var.getValue().itemAt(0);

    // get parameter
    int code =
        ((IntegerValue) getArgument(0).eval(contextSequence, contextItem).convertTo(Type.INTEGER))
            .getInt();

    // set response status code
    if (response.getObject() instanceof ResponseWrapper)
      ((ResponseWrapper) response.getObject()).setStatusCode(code);
    else
      throw new XPathException("Type error: variable $response is not bound to a response object");

    return Sequence.EMPTY_SEQUENCE;
  }
Beispiel #6
0
  public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
      context.getProfiler().start(this);
      context
          .getProfiler()
          .message(
              this,
              Profiler.DEPENDENCIES,
              "DEPENDENCIES",
              Dependency.getDependenciesName(this.getDependencies()));
      if (contextSequence != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
      if (contextItem != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
    }

    if (contextItem != null) contextSequence = contextItem.toSequence();

    Sequence result;
    Sequence arg = null;
    if (getSignature().getArgumentCount() == 1) arg = getArgument(0).eval(contextSequence);
    else arg = contextSequence;

    if (arg == null) throw new XPathException(getASTNode(), "XPDY0002: Undefined context item");

    if (arg.isEmpty()) result = DoubleValue.NaN;
    else {
      try {
        result = arg.convertTo(Type.DOUBLE);
      } catch (XPathException e) {
        result = DoubleValue.NaN;
      }
    }

    if (context.getProfiler().isEnabled()) context.getProfiler().end(this, "", result);

    return result;
  }
Beispiel #7
0
  /* (non-Javadoc)
   * @see org.exist.xquery.Expression#eval(org.exist.dom.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
   */
  public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    logger.error(
        "Use of deprecated function fn:doctype(). "
            + "It will be removed soon. Please "
            + "use util:doctype() instead.");
    if (context.getProfiler().isEnabled()) {
      context.getProfiler().start(this);
      context
          .getProfiler()
          .message(
              this,
              Profiler.DEPENDENCIES,
              "DEPENDENCIES",
              Dependency.getDependenciesName(this.getDependencies()));
      if (contextSequence != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
      if (contextItem != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
    }

    MutableDocumentSet docs = new DefaultDocumentSet();
    for (int i = 0; i < getArgumentCount(); i++) {
      Sequence seq = getArgument(i).eval(contextSequence, contextItem);
      for (SequenceIterator j = seq.iterate(); j.hasNext(); ) {
        String next = j.nextItem().getStringValue();
        context.getBroker().getXMLResourcesByDoctype(next, docs);
      }
    }

    NodeSet result = new ExtArrayNodeSet(1);
    for (Iterator i = docs.getDocumentIterator(); i.hasNext(); ) {
      result.add(new NodeProxy((DocumentImpl) i.next(), NodeId.DOCUMENT_NODE));
    }

    if (context.getProfiler().isEnabled()) context.getProfiler().end(this, "", result);

    return result;
  }
Beispiel #8
0
  public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
      context.getProfiler().start(this);
      context
          .getProfiler()
          .message(
              this,
              Profiler.DEPENDENCIES,
              "DEPENDENCIES",
              Dependency.getDependenciesName(this.getDependencies()));
      if (contextSequence != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
      if (contextItem != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
    }

    Sequence result;
    Sequence[] args = getArguments(contextSequence, contextItem);
    Collator collator = getCollator(contextSequence, contextItem, 3);
    int length = args[0].getItemCount();
    if (length != args[1].getItemCount()) result = BooleanValue.FALSE;
    else {
      result = BooleanValue.TRUE;
      for (int i = 0; i < length; i++) {
        if (!deepEquals(args[0].itemAt(i), args[1].itemAt(i), collator)) {
          result = BooleanValue.FALSE;
          break;
        }
      }
    }

    if (context.getProfiler().isEnabled()) context.getProfiler().end(this, "", result);

    return result;
  }
  /** @see org.exist.xquery.Expression#eval(Sequence, Item) */
  public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
      context.getProfiler().start(this);
      context
          .getProfiler()
          .message(
              this,
              Profiler.DEPENDENCIES,
              "DEPENDENCIES",
              Dependency.getDependenciesName(this.getDependencies()));
      if (contextSequence != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
      if (contextItem != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
    }

    Sequence result;
    Sequence arg = getArgument(0).eval(contextSequence, contextItem);
    if (arg.isEmpty()) result = BooleanValue.FALSE;
    else {
      String path = arg.itemAt(0).getStringValue();
      try {
        result = BooleanValue.valueOf(DocUtils.isDocumentAvailable(this.context, path));
      } catch (Exception e) {
        throw new XPathException(getASTNode(), e.getMessage());
      }
    }

    if (context.getProfiler().isEnabled()) context.getProfiler().end(this, "", result);

    return result;
  }
Beispiel #10
0
  /*
   * (non-Javadoc)
   *
   * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[],
   *      org.exist.xquery.value.Sequence)
   */
  public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {

    if (context.getProfiler().isEnabled()) {
      context.getProfiler().start(this);
      context
          .getProfiler()
          .message(
              this,
              Profiler.DEPENDENCIES,
              "DEPENDENCIES",
              Dependency.getDependenciesName(this.getDependencies()));
      if (contextSequence != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
      if (contextItem != null)
        context
            .getProfiler()
            .message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
    }

    Expression arg1 = getArgument(0);
    Sequence s1 = arg1.eval(contextSequence, contextItem);

    Expression arg2 = getArgument(1);
    context.pushDocumentContext();
    Sequence s2 = arg2.eval(contextSequence, contextItem);
    context.popDocumentContext();

    if (s1.isEmpty()) {
      return BooleanValue.valueOf(s2.isEmpty());
    } else if (s2.isEmpty()) {
      return BooleanValue.valueOf(s1.isEmpty());
    }

    Sequence result = null;
    StringBuffer v1 = new StringBuffer();
    StringBuffer v2 = new StringBuffer();
    try {
      if (s1.hasMany()) {
        for (int i = 0; i < s1.getItemCount(); i++) {
          v1.append(serialize((NodeValue) s1.itemAt(i)));
        }
      } else {
        v1.append(serialize((NodeValue) s1.itemAt(0)));
      }
      if (s2.hasMany()) {
        for (int i = 0; i < s2.getItemCount(); i++) {
          v2.append(serialize((NodeValue) s2.itemAt(i)));
        }
      } else {
        v2.append(serialize((NodeValue) s2.itemAt(0)));
      }
      Diff d = new Diff(v1.toString(), v2.toString());
      boolean identical = d.identical();
      if (!identical) {
        logger.warn("Diff result: " + d.toString());
      }
      result = new BooleanValue(identical);
    } catch (Exception e) {
      throw new XPathException(
          this,
          "An exception occurred while serializing node " + "for comparison: " + e.getMessage(),
          e);
    }

    if (context.getProfiler().isEnabled()) context.getProfiler().end(this, "", result);

    return result;
  }