Package net.sf.saxon.query

Examples of net.sf.saxon.query.StaticQueryContext


  private PathMapRoot contextRoot;

    public SaxonXQueryExpression(String xQueryString, XMLNamespaces namespaces, List<DerivedColumn> passing, List<XMLTable.XMLColumn> columns)
    throws QueryResolverException {
        config.setErrorListener(ERROR_LISTENER);
        StaticQueryContext context = new StaticQueryContext(config);
        IndependentContext ic = new IndependentContext(config);
       
        if (namespaces != null) {
          for (NamespaceItem item : namespaces.getNamespaceItems()) {
            if (item.getPrefix() == null) {
              if (item.getUri() == null) {
                context.setDefaultElementNamespace(""); //$NON-NLS-1$
                ic.setDefaultElementNamespace(""); //$NON-NLS-1$
              } else {
                context.setDefaultElementNamespace(item.getUri());
                ic.setDefaultElementNamespace(item.getUri());
              }
            } else {
            context.declareNamespace(item.getPrefix(), item.getUri());
            ic.declareNamespace(item.getPrefix(), item.getUri());
            }
      }
        }
        for (DerivedColumn derivedColumn : passing) {
          if (derivedColumn.getAlias() == null) {
            continue;
          }
          try {
        context.declareGlobalVariable(StructuredQName.fromClarkName(derivedColumn.getAlias()), SequenceType.ANY_SEQUENCE, null, true);
      } catch (XPathException e) {
        //this is always expected to work
        throw new TeiidRuntimeException(e, "Could not define global variable"); //$NON-NLS-1$
      }
    }
       
      processColumns(columns, ic);       
   
        try {
      this.xQuery = context.compileQuery(xQueryString);
    } catch (XPathException e) {
      throw new QueryResolverException(e, QueryPlugin.Util.getString("SaxonXQueryExpression.compile_failed")); //$NON-NLS-1$
    }
    }
View Full Code Here


        stdbuf.append(" - free(init): " + StringUtils.displayBytesSize(free));
        final StopWatch sw = new StopWatch("[Saxon] " + queryFile);
        queryFile = XMARK_HOME + '/' + queryFile;
        final Configuration config = new Configuration();
        config.setHostLanguage(Configuration.XQUERY);
        final StaticQueryContext staticContext = new StaticQueryContext(config);
        staticContext.setBaseURI(new File(queryFile).toURI().toString());
        XQueryExpression exp = staticContext.compileQuery(IOUtils.toString(new FileInputStream(queryFile)));
        final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
        StringWriter res_sw = new StringWriter();
        Properties props = new Properties();
        //props.setProperty(OutputKeys.METHOD, "text");
        props.setProperty(SaxonOutputKeys.WRAP, "no");
View Full Code Here

    }

    private static String invokeQueryBySaxon(String query) throws XPathException {
        Configuration config = new Configuration();
        config.setHostLanguage(Configuration.XQUERY);
        StaticQueryContext staticContext = new StaticQueryContext(config);
        XQueryExpression exp = staticContext.compileQuery(query);
        DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
        StringWriter res_sw = new StringWriter();
        Properties props = new Properties();
        props.setProperty(SaxonOutputKeys.WRAP, "no");
        props.setProperty(OutputKeys.INDENT, "no");
View Full Code Here

   
    // saxon
    context = null;
    saxonSerializer = null;
    if (mode.equals("saxon")) {
      context = new StaticQueryContext(new Configuration());
      saxonSerializer = createIdentityTransform(
          new String[] {"net.sf.saxon.TransformerFactoryImpl"});
    }
   
    // DOM
View Full Code Here

      if (mode.equals("xom-NNF")) {
        builder = new Builder(XOMUtil.getNullNodeFactory());
      }
     
      // saxon
      StaticQueryContext context = null;
      Transformer saxonSerializer = null;
      if (mode.equals("saxon")) {
        context = new StaticQueryContext(new Configuration());
        String clazz = "net.sf.saxon.TransformerFactoryImpl";
        System.setProperty("javax.xml.transform.TransformerFactory", clazz);
        saxonSerializer = TransformerFactory.newInstance().newTransformer();
      }
     
      // DOM
      DocumentBuilder domBuilder = null;
      Transformer domSerializer = null;
      if (mode.equals("dom")) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion", Boolean.FALSE);
        domBuilder = factory.newDocumentBuilder();
        String clazz = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
//        String clazz = "org.apache.xalan.processor.TransformerFactoryImpl";
        System.setProperty("javax.xml.transform.TransformerFactory", clazz);
        domSerializer = TransformerFactory.newInstance().newTransformer();
        System.err.println(domSerializer.getClass().getName());
      }
     
      // FastInfoSet
      Object fiSerializer = null;
//      XMLStreamWriter fiStaxSerializer = null;
      Builder fiBuilder = null
      Method fiMethod = null;
     
      XMLStreamReader fistaxReader = null;
      Method fistaxMethod = null;
      if (mode.startsWith("fi")) {
        NodeFactory factory = null;
        if (mode.indexOf("NNF") >= 0) factory = XOMUtil.getNullNodeFactory();
        XMLReader parser = (XMLReader) Class.forName("com.sun.xml.fastinfoset.sax.SAXDocumentParser").newInstance();
        fiBuilder = new Builder(parser, false, factory);
       
        if (mode.indexOf("stax") >= 0) {
          fiSerializer = (XMLStreamWriter) Class.forName("com.sun.xml.fastinfoset.stax.StAXDocumentSerializer").newInstance();       
        } else {
          fiSerializer = (ContentHandler) Class.forName("com.sun.xml.fastinfoset.sax.SAXDocumentSerializer").newInstance();
        }
       
        if (mode.startsWith("fi1")) { // enable "full indexing"
          Method method;
          method = fiSerializer.getClass().getMethod(
              "setAttributeValueSizeLimit", new Class[] {Integer.TYPE});
          method.invoke(fiSerializer, new Object[] {new Integer(Integer.MAX_VALUE)});
          method = fiSerializer.getClass().getMethod(
              "setCharacterContentChunkSizeLimit", new Class[] {Integer.TYPE});
          method.invoke(fiSerializer, new Object[] {new Integer(Integer.MAX_VALUE)});
        }
       
        fiMethod = fiSerializer.getClass().getMethod(
            "setOutputStream", new Class[] { OutputStream.class});
       
        if (mode.indexOf("stax") >= 0) {
          fistaxReader = (XMLStreamReader) Class.forName("com.sun.xml.fastinfoset.stax.StAXDocumentParser").newInstance();
          fistaxMethod = fistaxReader.getClass().getMethod(
              "setInputStream", new Class[] { InputStream.class});
        }
      }
     
      // StAX
      Builder staxBuilder = null;
      if (mode.indexOf("stax") >= 0) {
        NodeFactory factory = null;
        if (mode.indexOf("NNF") >= 0) factory = XOMUtil.getNullNodeFactory();
        staxBuilder = StaxUtil.createBuilder(staxInputFactory, factory);
      }
     
      for (int j=5; j < args.length; j++) {
        try {
          File[] files = IOTestUtil.listXMLFiles(args[j]);
          for (int k=0; k < files.length; k++) {
            File file = files[k];
            if (bogus(file) || ignore(file) || file.isDirectory()) {
              System.out.println("\n" + ": IGNORING " + file + " ...");
              continue;
            }
           
            System.out.println("now processing " + file);
  //          System.out.print(".");
           
            // prepare
            // file:/path/to/file --> file:///path/to/file
            String baseURI = file.toURI().toASCIIString();
            if (baseURI.startsWith("file:/")) {
              baseURI = baseURI.substring("file:/".length());
              if (!baseURI.startsWith("//")) baseURI = "//" + baseURI;
              baseURI = "file:/" + baseURI;
            }
  //          System.out.println("baseURI: " + baseURI);
            Document doc = new Builder().build(file);
           
            byte[] data = codec.serialize(doc, compressionLevel);
            if (!cmd.equals("test")) doc = new BinaryXMLCodec().deserialize(data); // use "interned" strings
            byte[] fileData = FileUtil.toByteArray(new FileInputStream(file));
            long fileLength = file.length();
            int encodedSize = 0;
           
            org.w3c.dom.Document domDoc = null;
            if (mode.equals("dom")) {
              domDoc = domBuilder.parse(file);
            }
           
            NodeInfo saxonDoc = null;
            if (mode.equals("saxon")) {
              saxonDoc = context.buildDocument(new StreamSource(new ByteArrayInputStream(fileData)));
            }
   
            if (mode.startsWith("fi")) {
              if (mode.indexOf("stax") >= 0) {
//                data = serializeWithStax(doc, staxOutputFactory);
                data = serializeWithFastInfosetStax(doc, (XMLStreamWriter)fiSerializer, fiMethod, new ByteArrayOutputStream());
              } else {
                data = serializeWithFastInfoset(doc, (ContentHandler)fiSerializer, fiMethod, new ByteArrayOutputStream());
              }
            }
   
            // run the benchmark
            long start = System.currentTimeMillis();
            for (int i = 0; i < iterations; i++) {
              try {
                // serialize
                if (cmd.equals("ser") || cmd.equals("serdeser") || cmd.equals("test")) {
                  ByteArrayOutputStream out = createOutputStream(cmd.equals("ser"));
                  if (mode.startsWith("bnux")) {
                    if (mode.indexOf("stream") < 0) {
                      codec.serialize(doc, compressionLevel, out);
                      data = out.toByteArray();
                    } else {
                      data = serializeWithStreamingBnux(doc, compressionLevel, out);
                    }
                  } else if (mode.startsWith("xom")) {
                    if (mode.indexOf("stax") >= 0) {
                      data = serializeWithStax(doc, staxOutputFactory, out);
                    } else if (mode.indexOf("stream") < 0) {
                      data = serializeWithXOM(doc, out);
                    } else {
                      data = serializeWithStreamingXOM(doc, out);
                    }
                  } else if (mode.equals("saxon")) {
                    saxonSerializer.transform(saxonDoc, new StreamResult(out));
                    data = out.toByteArray();
                  } else if (mode.equals("dom")) {
                    domSerializer.transform(new DOMSource(domDoc), new StreamResult(out));
                    data = out.toByteArray();
                  } else if (mode.startsWith("fi")) {
                    if (mode.indexOf("stax") >= 0) {
//                      data = serializeWithStax(doc, staxOutputFactory);
                      data = serializeWithFastInfosetStax(doc, (XMLStreamWriter)fiSerializer, fiMethod, out);
                    } else {
                      data = serializeWithFastInfoset(doc, (ContentHandler)fiSerializer, fiMethod, out);
                    }
                  } else {
                    throw new IllegalArgumentException("illegal mode");
                  }
                  checksum += data.length;
                }
                encodedSize = data.length;
                doneEncoded += encodedSize;
               
                // deserialize
                Document doc2 = null;
                if (cmd.equals("deser") || cmd.equals("serdeser") || cmd.equals("test")) {
                  if (mode.startsWith("bnux")) {
                    doc2 = codec.deserialize(new ByteArrayInputStream(data), bnuxFactory);
                  } else if (mode.startsWith("xom") && mode.indexOf("stax") >= 0) {
                    doc2 = staxBuilder.build(new ByteArrayInputStream(fileData));         
                  } else if (mode.startsWith("xom")) {
                    if (mode.indexOf("stream") < 0) {
                      doc2 = builder.build(new ByteArrayInputStream(fileData), baseURI);
                    } else {
                      doc2 = builder.build(new ByteArrayInputStream(data), baseURI);
                    }
                  } else if (mode.equals("saxon")) { // just for deser comparison
                    context.buildDocument(new StreamSource(new ByteArrayInputStream(fileData)));
                  } else if (mode.equals("dom")) {
                    domDoc = domBuilder.parse(new ByteArrayInputStream(fileData));
  //                  System.err.println(domDoc.getClass().getName());
                  } else if (mode.startsWith("fi") && mode.indexOf("stax") >=0 ) {
                    fistaxMethod.invoke(fistaxReader, new Object[] {new ByteArrayInputStream(data)});
View Full Code Here

                LOG.debug("Initializing XQueryBuilder " + this);
            }
            configuration = new Configuration();
            configuration.setHostLanguage(Configuration.XQUERY);

            staticQueryContext = new StaticQueryContext(getConfiguration());
            Set<Map.Entry<String, String>> entries = namespacePrefixes.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                String prefix = entry.getKey();
                String uri = entry.getValue();
                staticQueryContext.declareNamespace(prefix, uri);
View Full Code Here

  public XQuery(String query, URI baseURI, StaticQueryContext staticContext,
        DocumentURIResolver resolver) throws XQueryException {
   
    if (query == null) throw new IllegalArgumentException("query must not be null");
    if (staticContext == null) {
      staticContext = new StaticQueryContext(createConfiguration());
    } else {
      staticContext = staticContext.copy();
    }
   
    this.staticContext = staticContext;
View Full Code Here

     * @param runtimeConfig
     * @return Instance of ListVariable that contains results.
     * @throws XPathException
     */
    public static ListVariable evaluateXPath(String xpath, String xml, RuntimeConfig runtimeConfig) throws XPathException {
        StaticQueryContext sqc = runtimeConfig.getStaticQueryContext();
        Configuration config = sqc.getConfiguration();

        XQueryExpression exp = runtimeConfig.getXQueryExpressionPool().getCompiledExpression(xpath);
        DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
        StringReader reader = new StringReader(xml);

        dynamicContext.setContextItem(sqc.buildDocument(new StreamSource(reader)));

        return createListOfXmlNodes(exp, dynamicContext);
    }
View Full Code Here

    private XQueryExpressionPool xQueryExpressionPool;

    public synchronized StaticQueryContext getStaticQueryContext() {
        if (staticQueryContext == null) {
            Configuration config = new Configuration();
            staticQueryContext = new StaticQueryContext(config);
        }
        return staticQueryContext;
    }
View Full Code Here

        String xqExpression = xq.toString().trim();
        XQueryExternalParamDef[] externalParamDefs = xqueryDef.getExternalParamDefs();

        RuntimeConfig runtimeConfig = scraper.getRuntimeConfig();
        final StaticQueryContext sqc = runtimeConfig.getStaticQueryContext();
        final Configuration config = sqc.getConfiguration();

      try {
          final XQueryExpression exp = runtimeConfig.getXQueryExpressionPool().getCompiledExpression(xqExpression);
        final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
View Full Code Here

TOP

Related Classes of net.sf.saxon.query.StaticQueryContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.