Exemplo n.º 1
0
  private Sequence processQuery(String select) throws SAXException {
    XQueryContext context = null;
    try {
      context = new XQueryContext(broker.getBrokerPool(), accessCtx);
      context.setStaticallyKnownDocuments(documentSet);
      Map.Entry<String, String> namespaceEntry;
      for (final Iterator<Map.Entry<String, String>> i = namespaces.entrySet().iterator();
          i.hasNext(); ) {
        namespaceEntry = (Map.Entry<String, String>) i.next();
        context.declareNamespace(namespaceEntry.getKey(), namespaceEntry.getValue());
      }
      Map.Entry<String, Object> entry;
      for (final Iterator<Map.Entry<String, Object>> i = variables.entrySet().iterator();
          i.hasNext(); ) {
        entry = (Map.Entry<String, Object>) i.next();
        context.declareVariable(entry.getKey().toString(), entry.getValue());
      }
      // TODO(pkaminsk2): why replicate XQuery.compile here?
      final XQueryLexer lexer = new XQueryLexer(context, new StringReader(select));
      final XQueryParser parser = new XQueryParser(lexer);
      final XQueryTreeParser treeParser = new XQueryTreeParser(context);
      parser.xpath();
      if (parser.foundErrors()) {
        throw new SAXException(parser.getErrorMessage());
      }

      final AST ast = parser.getAST();

      if (LOG.isDebugEnabled()) {
        LOG.debug("generated AST: " + ast.toStringTree());
      }

      final PathExpr expr = new PathExpr(context);
      treeParser.xpath(ast, expr);
      if (treeParser.foundErrors()) {
        throw new SAXException(treeParser.getErrorMessage());
      }
      expr.analyze(new AnalyzeContextInfo());
      final Sequence seq = expr.eval(null, null);
      return seq;
    } catch (final RecognitionException e) {
      LOG.warn("error while creating variable", e);
      throw new SAXException(e);
    } catch (final TokenStreamException e) {
      LOG.warn("error while creating variable", e);
      throw new SAXException(e);
    } catch (final XPathException e) {
      throw new SAXException(e);
    } finally {
      if (context != null) {
        context.reset(false);
      }
    }
  }
Exemplo n.º 2
0
  public static void parse(XQueryContext context, String pattern, XSLPathExpr content)
      throws XPathException {
    boolean xpointer = false;

    // TODO: rewrite RootNode?
    if (pattern.equals("//")) {
      content.add(new LocationStep(context, Constants.SELF_AXIS, new AnyNodeTest()));
      return;
    }
    if (pattern.equals("/")) {
      content.add(new LocationStep(context, Constants.SELF_AXIS, new AnyNodeTest()));
      return;
    }

    Source source = new StringSource(pattern);
    Reader reader;
    try {
      reader = source.getReader();
    } catch (IOException e1) {
      return; // TODO: report error???
    }

    long start = System.currentTimeMillis();
    XQueryLexer lexer = new XQueryLexer(context, reader);
    XQueryParser parser = new XQueryParser(lexer);
    XQueryTreeParser treeParser = new XQueryTreeParser(context);
    try {
      if (xpointer) parser.xpointer();
      else parser.xpath();
      if (parser.foundErrors()) {
        LOG.debug(parser.getErrorMessage());
        throw new StaticXQueryException(parser.getErrorMessage());
      }

      AST ast = parser.getAST();
      if (ast == null)
        throw new XPathException(
            "Unknown XQuery parser error: the parser returned an empty syntax tree.");

      PathExpr expr = new PathExpr(context);
      if (xpointer) treeParser.xpointer(ast, expr);
      else treeParser.xpath(ast, expr);
      if (treeParser.foundErrors()) {
        throw new StaticXQueryException(
            treeParser.getErrorMessage(), treeParser.getLastException());
      }

      expr.analyze(new AnalyzeContextInfo(context));

      //            if (context.optimizationsEnabled()) {
      //                Optimizer optimizer = new Optimizer(context);
      //                expr.accept(optimizer);
      //                if (optimizer.hasOptimized()) {
      //                    context.reset(true);
      //                    expr.resetState(true);
      //                    expr.analyze(new AnalyzeContextInfo());
      //                }
      //            }

      // Log the query if it is not too large, but avoid
      // dumping huge queries to the log
      if (context.getExpressionCount() < 150) {
        LOG.debug("Query diagnostics:\n" + ExpressionDumper.dump(expr));
      } else {
        LOG.debug("Query diagnostics:\n" + "[skipped: more than 150 expressions]");
      }
      if (LOG.isDebugEnabled()) {
        NumberFormat nf = NumberFormat.getNumberInstance();
        LOG.debug("Compilation took " + nf.format(System.currentTimeMillis() - start) + " ms");
      }
      // return

      content.add(expr);

    } catch (RecognitionException e) {
      LOG.debug("Error compiling query: " + e.getMessage(), e);
      String msg = e.getMessage();
      if (msg.endsWith(", found 'null'"))
        msg = msg.substring(0, msg.length() - ", found 'null'".length());
      throw new StaticXQueryException(e.getLine(), e.getColumn(), msg);
    } catch (TokenStreamException e) {
      LOG.debug("Error compiling query: " + e.getMessage(), e);
      throw new StaticXQueryException(e.getMessage(), e);
    }
  }