예제 #1
0
  /**
   * Get an iterator over the results of the expression. This returns results without any conversion
   * of the returned items to "native" Java classes. The iterator will deliver a sequence of Item
   * objects, each item being either a NodeInfo (representing a node) or an AtomicValue
   * (representing an atomic value).
   *
   * <p>
   *
   * <p>To get the results of the query in the form of an XML document in which each item is wrapped
   * by an element indicating its type, use:
   *
   * <p>
   *
   * <p><code>QueryResult.wrap(iterator(env))</code>
   *
   * <p>
   *
   * <p>To serialize the results to a file, use the QueryResult.serialize() method.
   *
   * @param env Provides the dynamic query evaluation context
   * @return an iterator over the results of the query. The class SequenceIterator is modeled on the
   *     standard Java Iterator class, but has extra functionality and can throw exceptions when
   *     errors occur.
   * @throws XPathException if a dynamic error occurs in evaluating the query. Some dynamic errors
   *     will not be reported by this method, but will only be reported when the individual items of
   *     the result are accessed using the returned iterator.
   */
  public SequenceIterator iterator(DynamicQueryContext env) throws XPathException {
    if (isUpdating) {
      throw new XPathException("Cannot call iterator() on an updating query");
    }
    Controller controller = newController();
    initializeController(env, controller);

    try {
      Item contextItem = env.getContextItem();

      // Bindery bindery = controller.getBindery();
      // bindery.openStackFrame();
      controller.defineGlobalParameters();
      XPathContextMajor context = controller.newXPathContext();

      if (contextItem != null) {
        if (!staticContext
            .getUserQueryContext()
            .getRequiredContextItemType()
            .matchesItem(contextItem, false, env.getConfiguration())) {
          throw new XPathException(
              "The supplied context item does not match the required context item type");
        }
        UnfailingIterator single = SingletonIterator.makeIterator(contextItem);
        single.next();
        context.setCurrentIterator(single);
        controller.setInitialContextItem(contextItem);
      }

      // In tracing/debugging mode, evaluate all the global variables first
      if (controller.getTraceListener() != null) {
        controller.preEvaluateGlobals(context);
      }

      context.openStackFrame(stackFrameMap);

      SequenceIterator iterator = expression.iterate(context);
      return new ErrorReportingIterator(iterator, controller.getErrorListener());
    } catch (XPathException err) {
      TransformerException terr = err;
      while (terr.getException() instanceof TransformerException) {
        terr = (TransformerException) terr.getException();
      }
      XPathException de = XPathException.makeXPathException(terr);
      controller.reportFatalError(de);
      throw de;
    }
  }
예제 #2
0
  /**
   * Run the query in pull mode.
   *
   * <p>
   *
   * <p>For maximum effect this method should be used when lazyConstructionMode has been set in the
   * Configuration.
   *
   * <p>
   *
   * <p><b>Note: this method usually has very similar performance to the {@link
   * #run(DynamicQueryContext,javax.xml.transform.Result,java.util.Properties)} method (which does
   * the same thing), but sometimes it is significantly slower. Therefore, the run() method is
   * preferred.</b>
   *
   * @param dynamicEnv the dynamic context for query evaluation
   * @param destination the destination of the query results
   * @param outputProperties the serialization parameters
   * @see Configuration#setLazyConstructionMode(boolean)
   */
  public void pull(DynamicQueryContext dynamicEnv, Result destination, Properties outputProperties)
      throws XPathException {
    if (isUpdating) {
      throw new XPathException("Cannot call pull() on an updating query");
    }
    Configuration config = dynamicEnv.getConfiguration();
    try {
      Controller controller = newController();
      // initializeController(dynamicEnv, controller);
      EventIterator iter = iterateEvents(controller, dynamicEnv);
      // iter = new Decomposer(iter, config);

      Properties actualProperties = validateOutputProperties(controller, outputProperties);
      SerializerFactory sf = config.getSerializerFactory();
      PipelineConfiguration pipe = config.makePipelineConfiguration();
      pipe.setSerializing(true);
      Receiver receiver = sf.getReceiver(destination, pipe, actualProperties);

      receiver = new NamespaceReducer(receiver);
      if ("yes".equals(actualProperties.getProperty(SaxonOutputKeys.WRAP))) {
        receiver = new SequenceWrapper(receiver);
        // receiver = new TracingFilter(receiver);
      } else {
        receiver = new TreeReceiver(receiver);
      }
      EventIteratorToReceiver.copy(iter, (SequenceReceiver) receiver);
    } catch (XPathException err) {
      config.reportFatalError(err);
      throw err;
    }
  }
예제 #3
0
  private XPathContextMajor initialContext(DynamicQueryContext dynamicEnv, Controller controller)
      throws XPathException {
    Item contextItem = dynamicEnv.getContextItem();
    controller.defineGlobalParameters();
    XPathContextMajor context = controller.newXPathContext();

    if (contextItem != null) {
      if (!staticContext
          .getUserQueryContext()
          .getRequiredContextItemType()
          .matchesItem(contextItem, false, dynamicEnv.getConfiguration())) {
        throw new XPathException(
            "The supplied context item does not match the required context item type");
      }
      UnfailingIterator single = SingletonIterator.makeIterator(contextItem);
      single.next();
      context.setCurrentIterator(single);
      controller.setInitialContextItem(contextItem);
    }
    return context;
  }
예제 #4
0
  private void initializeController(DynamicQueryContext env, Controller controller) {
    HashMap parameters = env.getParameters();
    if (parameters != null) {
      Iterator iter = parameters.keySet().iterator();
      while (iter.hasNext()) {
        String paramName = (String) iter.next();
        Object paramValue = parameters.get(paramName);
        controller.setParameter(paramName, paramValue);
      }
    }

    controller.setURIResolver(env.getURIResolver());
    controller.setErrorListener(env.getErrorListener());
    controller.addTraceListener(env.getTraceListener());
    controller.setTraceFunctionDestination(env.getTraceFunctionDestination());
    DateTimeValue currentDateTime = env.getCurrentDateTime();
    if (currentDateTime != null) {
      try {
        controller.setCurrentDateTime(currentDateTime);
      } catch (XPathException e) {
        throw new AssertionError(e); // the value should already have been checked
      }
    }
  }