Package net.sf.saxon.s9api

Examples of net.sf.saxon.s9api.XQueryEvaluator


            Processor qtproc = runtime.getProcessor();
            DocumentBuilder builder = qtproc.newDocumentBuilder();
            builder.setBaseURI(new URI("http://example.com/"));
            XQueryCompiler xqcomp = qtproc.newXQueryCompiler();
            XQueryExecutable xqexec = xqcomp.compile(".");
            XQueryEvaluator xqeval = xqexec.load();
            xqeval.setContextItem(doc);

            serializer = new Serializer();

            serializer.setOutputProperty(Serializer.Property.BYTE_ORDER_MARK, serial.getByteOrderMark() ? "yes" : "no");
            // FIXME: support CDATA_SECTION_ELEMENTS
            if (serial.getDoctypePublic() != null) {
                serializer.setOutputProperty(Serializer.Property.DOCTYPE_PUBLIC, serial.getDoctypePublic());
            }
            if (serial.getDoctypeSystem() != null) {
                serializer.setOutputProperty(Serializer.Property.DOCTYPE_SYSTEM, serial.getDoctypeSystem());
            }
            if (serial.getEncoding() != null) {
                serializer.setOutputProperty(Serializer.Property.ENCODING, serial.getEncoding());
            }
            serializer.setOutputProperty(Serializer.Property.ESCAPE_URI_ATTRIBUTES, serial.getEscapeURIAttributes() ? "yes" : "no");
            serializer.setOutputProperty(Serializer.Property.INCLUDE_CONTENT_TYPE, serial.getIncludeContentType() ? "yes" : "no");
            serializer.setOutputProperty(Serializer.Property.INDENT, serial.getIndent() ? "yes" : "no");
            if (serial.getMediaType() != null) {
                serializer.setOutputProperty(Serializer.Property.MEDIA_TYPE, serial.getMediaType());
            }
            if (serial.getMethod() != null) {
                serializer.setOutputProperty(Serializer.Property.METHOD, serial.getMethod().getLocalName());
            }
            if (serial.getNormalizationForm() != null) {
                serializer.setOutputProperty(Serializer.Property.NORMALIZATION_FORM, serial.getNormalizationForm());
            }
            serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, serial.getOmitXMLDeclaration() ? "yes" : "no");
            if (serial.getStandalone() != null) {
                String standalone = serial.getStandalone();
                if ("true".equals(standalone)) {
                    serializer.setOutputProperty(Serializer.Property.STANDALONE, "yes");
                } else if ("false".equals(standalone)) {
                    serializer.setOutputProperty(Serializer.Property.STANDALONE, "no");
                }
                // What about omit?
            }
            serializer.setOutputProperty(Serializer.Property.UNDECLARE_PREFIXES, serial.getUndeclarePrefixes() ? "yes" : "no");
            if (serial.getVersion() != null) {
                serializer.setOutputProperty(Serializer.Property.VERSION, serial.getVersion());
            }

            if (uri == null) {
                serializer.setOutputStream(out);
            } else {
                File file = URIUtils.getFile(uri);
                runtime.finest("com.xmlcalabash.io", null, "Attempt to write file: " + uri);
                //FileOutputStream fos = new FileOutputStream(file);
                //serializer.setOutputStream(fos);
            }

            xqeval.setDestination(serializer);
            xqeval.run();

            if (uri == null && runtime.getDebug()) {
                System.out.println("\n--<document boundary>--------------------------------------------------------------------------");
            }
        } catch (URISyntaxException use) {
View Full Code Here


    //System.out.print(System.currentTimeMillis()-time_stamp+", ");
   
    //System.out.println("Receiving key: " + key);
    //System.out.println("Receiving value: " + value);
   
    XQueryEvaluator eval = exec.load();
   
    //System.out.print(System.currentTimeMillis()-time_stamp+", ");
   
    XdmNode keyDoc;
    XdmNode valDoc;

    try {
      keyDoc = doc.build(new StreamSource(new StringReader("<Key>"+key.toString()+"</Key>")));
      valDoc = doc.build(new StreamSource(new StringReader(value.toString())));
   
      eval.setExternalVariable(new QName("key"), keyDoc);
      eval.setExternalVariable(new QName("value"), valDoc);

    } catch (SaxonApiException e1) {
      e1.printStackTrace();
    }
   
    //System.out.print(System.currentTimeMillis()-time_stamp+", ");
   

   
    k = new Text();
    v = new Text();
    String result_string;
   
    try {
      XdmValue res = eval.evaluate();
      for (XdmItem i : res) {
        result_string = i.toString();
       
        System.out.println("Emitting: " + result_string);
       
View Full Code Here

   
    //System.out.println(System.getProperty("user.dir"));
   
    //System.out.println("Receiving key: " + key);
   
    XQueryEvaluator eval = exec.load();
   
    //System.out.print(System.currentTimeMillis()-time_stamp+", ");
   
   
    int counter = 0;
   
   
    //if streaming is enabled we need to write the input to a file
    //in order to allow the use of the doc tag, which is required by
    //saxon:stream()
    if (conf.get(XQueryMRSaxonConfFile.PROPNAME_STREAM_ENABLED).equals("true")) {
      BufferedWriter valWriter;
     
      try {
        FileWriter fVal = new FileWriter("value.txt");
        valWriter = new BufferedWriter(fVal);
       
        valWriter.write("<root>\n");
        for (Text inputValue : inputValues) {
          valWriter.write(inputValue.toString());
          counter++;
        }
        valWriter.write("</root>");
       
        valWriter.flush();
        fVal.close();
        valWriter.close();
        System.out.println("Length of value-input " + counter);
      } catch (IOException e1) {
        e1.printStackTrace();
      }
     
      //System.out.print(System.currentTimeMillis()-time_stamp+", ");
     
      XdmNode keyDoc;
     
      try {
        keyDoc = doc.build(new StreamSource(new StringReader(key.toString())));
               
        eval.setExternalVariable(new QName("key"), keyDoc);
        eval.setExternalVariable(new QName("uri"), new XdmAtomicValue("value.txt"));
      } catch (SaxonApiException e) {
        e.printStackTrace();
      }
   
      //System.out.print(System.currentTimeMillis()-time_stamp+", ");
     
    //if streaming is not enabled (no need to write input to file)
    } else {
     
      StringBuffer values = new StringBuffer();
      values.append("<root>");
      for (Text inputValue : inputValues) {
        values.append(inputValue.toString().trim().replaceAll("\n", "").replaceAll("\t", ""));
        //counter++;
      }
      values.append("</root>");
     
      //System.out.println("Length of input: " + counter);
     
      //System.out.println("###VALUES###:\n" + values);
     
      //System.out.print(System.currentTimeMillis()-time_stamp+", ");
     
      XdmNode keyDoc;
      XdmNode valDoc;
     
      try {
        keyDoc = doc.build(new StreamSource(new StringReader(key.toString())));
        valDoc = doc.build(new StreamSource(new StringReader(values.toString())));
       
        eval.setExternalVariable(new QName("key"), keyDoc);
        eval.setExternalVariable(new QName("value"), valDoc);
      } catch (SaxonApiException e) {
        e.printStackTrace();
      }
    }
   
    //System.out.print(System.currentTimeMillis()-time_stamp+", ");
   
   
   
    try {
      Iterator<XdmItem> iter = eval.iterator();
      while (iter.hasNext()) {
        context.write(new Text(iter.next().toString()), new Text(iter.next().toString()));
      }
    } catch (IOException e) {
      e.printStackTrace();
View Full Code Here

        xqcomp.getUnderlyingStaticContext().setSchemaAware(
                xqcomp.getProcessor().getUnderlyingConfiguration().isLicensedFeature(
                        Configuration.LicenseFeature.ENTERPRISE_XQUERY));

        XQueryExecutable xqexec = xqcomp.compile(".");
        XQueryEvaluator xqeval = xqexec.load();
        if (xproc.getHtmlSerializer() && "html".equals(serializer.getOutputProperty(Serializer.Property.METHOD))) {
            ContentHandler ch = null;
            Object outputDest = serializer.getOutputDestination();

            if (outputDest == null) {
                //???
                xqeval.setDestination(serializer);
            } else if (outputDest instanceof OutputStream) {
                ch = new HtmlSerializer((OutputStream) outputDest);
                xqeval.setDestination(new SAXDestination(ch));
            } else if (outputDest instanceof Writer) {
                ch = new HtmlSerializer((Writer) outputDest);
                xqeval.setDestination(new SAXDestination(ch));
            } else if (outputDest instanceof File) {
                try {
                    FileOutputStream fos = new FileOutputStream((File) outputDest);
                    ch = new HtmlSerializer(fos);
                    xqeval.setDestination(new SAXDestination(ch));
                } catch (FileNotFoundException fnfe) {
                    xqeval.setDestination(serializer);
                }
            } else {
                //???
                xqeval.setDestination(serializer);
            }
        } else {
            xqeval.setDestination(serializer);
        }

        for (XdmNode node : nodes) {
            xqeval.setContextItem(node);
            xqeval.run();
            // Even if we output an XML decl before the first node, we must not do it before any others!
            serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes");
        }
    }
View Full Code Here

                    Processor qtproc = runtime.getProcessor();
                    XQueryCompiler xqcomp = qtproc.newXQueryCompiler();
                    xqcomp.setModuleURIResolver(runtime.getResolver());
                    XQueryExecutable xqexec = xqcomp.compile(queryexpr);
                    XQueryEvaluator xqeval = xqexec.load();
                    xqeval.setContextItem(srcDoc);

                    serializer.setOutputStream(os);
                    xqeval.setDestination(serializer);
                    xqeval.run();
                } finally {
                    os.close();
                }
            } else {
                OutputStream os = process.getOutputStream();
View Full Code Here

        }
        if (query != null && queryFile != null) {
          throw new MorphlineCompilationException("Must not define both query and queryFile at the same time", config);
        }
       
        XQueryEvaluator evaluator = executable.load();
        Config variables = getConfigs().getConfig(fragment, "externalVariables", ConfigFactory.empty());
        for (Map.Entry<String, Object> entry : new Configs().getEntrySet(variables)) {
          XdmValue xdmValue = XdmNode.wrap(new UntypedAtomicValue(entry.getValue().toString()));
          evaluator.setExternalVariable(new QName(entry.getKey()), xdmValue);
        }
        Config fileVariables = getConfigs().getConfig(fragment, "externalFileVariables", ConfigFactory.empty());
        for (Map.Entry<String, Object> entry : new Configs().getEntrySet(fileVariables)) {
          File file = new File(entry.getValue().toString());
          XdmValue doc = parseXmlDocument(file);
          evaluator.setExternalVariable(new QName(entry.getKey()), doc);
        }
        if (isTracing) {
          evaluator.setTraceListener(new XQueryTraceListener()); // TODO redirect from stderr to SLF4J
        }

        fragments.add(new Fragment(fragmentPath, evaluator));
     
      validateArguments();
View Full Code Here

      for (Fragment fragment : fragments) {
        Record template = inputRecord.copy();
        removeAttachments(template);
        XdmNode document = parseXmlDocument(stream);
        LOG.trace("XQuery input document: {}", document);
        XQueryEvaluator evaluator = fragment.xQueryEvaluator;
        evaluator.setContextItem(document);
       
        int i = 0;
        for (XdmItem item : evaluator) {
          i++;
          if (LOG.isTraceEnabled()) {
View Full Code Here

        }
        if (query != null && queryFile != null) {
          throw new MorphlineCompilationException("Must not define both query and queryFile at the same time", config);
        }
       
        XQueryEvaluator evaluator = executable.load();
        Config variables = getConfigs().getConfig(fragment, "externalVariables", ConfigFactory.empty());
        for (Map.Entry<String, Object> entry : new Configs().getEntrySet(variables)) {
          XdmValue xdmValue = XdmNode.wrap(new UntypedAtomicValue(entry.getValue().toString()));
          evaluator.setExternalVariable(new QName(entry.getKey()), xdmValue);
        }
        Config fileVariables = getConfigs().getConfig(fragment, "externalFileVariables", ConfigFactory.empty());
        for (Map.Entry<String, Object> entry : new Configs().getEntrySet(fileVariables)) {
          File file = new File(entry.getValue().toString());
          XdmValue doc = parseXmlDocument(file);
          evaluator.setExternalVariable(new QName(entry.getKey()), doc);
        }
        if (isTracing) {
          evaluator.setTraceListener(new XQueryTraceListener()); // TODO redirect from stderr to SLF4J
        }

        fragments.add(new Fragment(fragmentPath, evaluator));
     
      validateArguments();
View Full Code Here

      for (Fragment fragment : fragments) {
        Record template = inputRecord.copy();
        removeAttachments(template);
        XdmNode document = parseXmlDocument(stream);
        LOG.trace("XQuery input document: {}", document);
        XQueryEvaluator evaluator = fragment.xQueryEvaluator;
        evaluator.setContextItem(document);
       
        int i = 0;
        for (XdmItem item : evaluator) {
          i++;
          if (LOG.isTraceEnabled()) {
View Full Code Here

TOP

Related Classes of net.sf.saxon.s9api.XQueryEvaluator

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.