@BeforeMethod
 public void setUp() throws SaxonApiException {
   MockitoAnnotations.initMocks(this);
   given(processorFactory.createProcessor()).willReturn(processor);
   given(processor.newXQueryCompiler()).willReturn(xQueryCompiler);
   given(xQueryCompiler.compile(QUERY)).willReturn(xQueryExecutable);
   given(xQueryExecutable.load()).willReturn(xQueryEvaluator);
   given(inputSourceFactory.createInputSource(INPUT)).willReturn(inputSource);
   given(saxSourceFactory.createSAXSource(inputSource)).willReturn(saxSource);
   given(byteArrayOutputStreamFactory.createByteArrayOutputStream()).willReturn(outputStream);
 }
Пример #2
0
  /**
   * Run an XQuery statement against the input specified by pSourceDOM. Ouput is written to
   * pDestinationDOM. If pDestinationDOM has any existing children they are removed beforehand.
   * :{contexts} are supported in the XQuery String.
   *
   * @param pSourceDOM The Source data for the XQuery.
   * @param pAttachPoint The initial context node (".") of the XQuery expression.
   * @param pDestinationDOM The Destination for the output.
   * @param pXQuery The XQuery String.
   * @param pContextUElem Used for resolving any :{contexts} which appear in the XQuery.
   */
  public static void runXQuery(
      DOM pSourceDOM,
      DOM pAttachPoint,
      DOM pDestinationDOM,
      String pXQuery,
      ContextUElem pContextUElem) {

    XQueryCompiler lCompiler = SaxonEnvironment.getXQueryCompiler();

    // Rewrite :{context}s and the like
    pXQuery = SaxonEnvironment.replaceFoxMarkup(pXQuery, null);

    XQueryExecutable lExecutable;
    try {
      Track.pushDebug("XQueryCompile");
      lExecutable = lCompiler.compile(pXQuery);
    } catch (SaxonApiException e) {
      throw new ExInternal("Error compiling XQuery", e);
    } finally {
      Track.pop("XQueryCompile");
    }

    // TODO implement compiled XQuery cache
    XQueryEvaluator lEvaluator = lExecutable.load();

    pDestinationDOM.removeAllChildren();
    DOMContentHandler lContentHandler = new DOMContentHandler(pDestinationDOM);

    try {
      Track.pushDebug("XQueryPrepare");
      lEvaluator.setSource(pSourceDOM.wrap());
      lEvaluator.setContextItem(new XdmNode(pAttachPoint.wrap()));
      lEvaluator.setDestination(new SAXDestination(lContentHandler));
    } catch (SaxonApiException e) {
      throw new ExInternal("Error preparing XQuery", e);
    } finally {
      Track.pop("XQueryPrepare");
    }

    try {
      Track.pushDebug("XQueryExecute");
      SaxonEnvironment.setThreadLocalContextUElem(pContextUElem);
      lEvaluator.run();
    } catch (SaxonApiException e) {
      throw new ExInternal("Error running XQuery", e);
    } finally {
      Track.pop("XQueryExecute");
      SaxonEnvironment.clearThreadLocalContextUElem();
    }
  }
Пример #3
0
 private <T> T execute(XQueryExecutable executable, ExecutionContext context) {
   try {
     XQueryEvaluator evaluator = executable.load();
     for (ExternalVariable externalVariable : context.getExternalVariables()) {
       evaluator.setExternalVariable(
           externalVariable.getName(), new XdmAtomicValue(externalVariable.getValue()));
     }
     evaluator.setTraceListener(traceExtension.getTraceListener());
     XdmValue result = evaluator.evaluate();
     return xdmValueConverter.convert(result);
   } catch (SaxonApiException e) {
     throw new RuntimeException(e);
   }
 }