Example #1
0
        /**
         * Static method called as an external function call to evaluate a literal when running in "unfolded" mode.
         * The function simply returns the value of its argument - but the optimizer doesn't know that, so it
         * can't pre-evaluate the call at compile time.
         *
         * @param value the value to be returned
         * @return the supplied value, unchanged
         */

        /*public static Sequence lazyLiteral(Sequence value) {
         *  return value;
         * } */

        /**
         * Collect together information about all the dependencies of tests that use a given environment
         * @param processor the Saxon processor
         * @param env the environment for which dependency information is to be gathered
         * @
         */

        private void buildDependencyDictionary(Processor processor, Environment env)
        {
            XQueryCompiler xqCompiler = processor.NewXQueryCompiler();

            xqCompiler.XQueryLanguageVersion = "3.0";
            xqCompiler.BaseUri = testSuiteDir;
            XdmValue result = xqCompiler.Compile(
                "        declare namespace fots = \"http://www.w3.org/2010/09/qt-fots-catalog\";\n" +
                "        let $testsets := doc('" + testSuiteDir + "/catalog.xml')//fots:test-set/@file/doc(resolve-uri(., exactly-one(base-uri(.))))\n" +
                "        for $dependencyTS in $testsets//fots:dependency\n" +
                "        let $name := $dependencyTS/@type\n" +
                "        let $value := $dependencyTS/@value\n" +
                "        group by $name, $value\n" +
                "        order by $name, $value\n" +
                "        return <dependency type='{$name}' value='{$value}' />").Load().Evaluate();


            foreach (XdmItem item in result)
            {
                XdmNode node  = (XdmNode)item;
                string  type  = node.GetAttributeValue(new QName("type"));
                string  value = node.GetAttributeValue(new QName("value"));
                addDependency(type, value, dependencyIsSatisfied(node, env));
            }
        }
Example #2
0
    /**
     * Show a query producing a sequence as its result and returning the sequence
     * to the Java application in the form of an iterator. The sequence is then
     * output by serializing each item individually, with each item on a new line.
     */

    public static void ExampleToSerializedSequence(String inputFileName)
    {
        Processor processor = new Processor();

        XmlTextReader reader = new XmlTextReader(inputFileName,
                                                 new FileStream(inputFileName, FileMode.Open, FileAccess.Read));

        reader.Normalization = true;

        // add a validating reader - not to perform validation, but to expand entity references
        XmlValidatingReader validator = new XmlValidatingReader(reader);

        validator.ValidationType = ValidationType.None;

        XdmNode doc = processor.NewDocumentBuilder().Build(reader);

        XQueryCompiler   compiler = processor.NewXQueryCompiler();
        XQueryExecutable exp      = compiler.Compile("//ISBN");
        XQueryEvaluator  eval     = exp.Load();

        eval.ContextItem = doc;

        foreach (XdmNode node in eval)
        {
            Console.WriteLine(node.OuterXml);
        }
    }
Example #3
0
    /**
     * Show a query reading an input document using an XmlReader (the .NET XML parser)
     */

    public static void ExampleFromXmlReader(String inputFileName)
    {
        Processor processor = new Processor();

        XmlTextReader reader = new XmlTextReader(inputFileName,
                                                 new FileStream(inputFileName, FileMode.Open, FileAccess.Read));

        reader.Normalization = true;

        // add a validating reader - not to perform validation, but to expand entity references
        XmlValidatingReader validator = new XmlValidatingReader(reader);

        validator.ValidationType = ValidationType.None;

        XdmNode doc = processor.NewDocumentBuilder().Build(validator);

        XQueryCompiler   compiler = processor.NewXQueryCompiler();
        XQueryExecutable exp      = compiler.Compile("/");
        XQueryEvaluator  eval     = exp.Load();

        eval.ContextItem = doc;
        Serializer qout = new Serializer();

        qout.SetOutputProperty(Serializer.METHOD, "xml");
        qout.SetOutputProperty(Serializer.INDENT, "yes");
        qout.SetOutputStream(new FileStream("testoutput2.xml", FileMode.Create, FileAccess.Write));
        eval.Run(qout);
    }
// https://www.nuget.org/packages/Saxon-HE/

        public string XQuery(string xml, string query)
        {
            Processor processor = new Processor();

            XmlDocument input = new XmlDocument();

            input.LoadXml(xml);

            XdmNode        indoc = processor.NewDocumentBuilder().Build(new XmlNodeReader(input));
            DomDestination qout  = new DomDestination();

            XQueryCompiler   compiler = processor.NewXQueryCompiler();
            XQueryExecutable exp      = compiler.Compile(query);
            XQueryEvaluator  eval     = exp.Load();

            eval.ContextItem = indoc;
            eval.Run(qout);
            XmlDocument outdoc = qout.XmlDocument;

            using (var stringWriter = new StringWriter())
                using (var xmlTextWriter = XmlWriter.Create(stringWriter))
                {
                    outdoc.WriteTo(xmlTextWriter);
                    xmlTextWriter.Flush();
                    return(stringWriter.GetStringBuilder().ToString());
                }
        }
Example #5
0
        private void writeModuleReportXQuery(string paperName, string className, StreamWriter sw)
        {
            Processor      pr  = new Processor();
            XQueryCompiler xqc = pr.NewXQueryCompiler();

            string      errPath = "Papers\\" + paperName + "\\" + className + "_err.xml";
            XmlDocument errdoc  = new XmlDocument();

            errdoc.Load(errPath);
            XmlElement  root      = errdoc.DocumentElement;
            XmlNodeList ruleNodes = root.GetElementsByTagName("err-rule");

            foreach (XmlNode xrule in ruleNodes)
            {
                string rule = xrule.InnerText;
                //Console.WriteLine(rule);

                XQueryExecutable xqe    = xqc.Compile(rule);
                XQueryEvaluator  xqev   = xqe.Load();
                XdmValue         result = xqev.Evaluate(); //对比结果


                foreach (XdmItem rs in result)
                {
                    sw.WriteLine(count + ". " + rs.ToString().Trim());
                    count++;
                }
            }
        }
Example #6
0
    public void GenXML()
    {
        String sourceUri = Server.MapPath("5648.xml");
        String xqUri     = Server.MapPath("graph.xq");

        using (FileStream sXml = File.OpenRead(sourceUri))
        {
            using (FileStream sXq = File.OpenRead(xqUri))
            {
                Processor      processor = new Processor();
                XQueryCompiler compiler  = processor.NewXQueryCompiler();
                compiler.BaseUri = sourceUri;
                XQueryExecutable exp  = compiler.Compile(sXq);
                XQueryEvaluator  eval = exp.Load();

                DocumentBuilder loader = processor.NewDocumentBuilder();
                loader.BaseUri = new Uri(sourceUri);
                XdmNode indoc = loader.Build(new FileStream(sourceUri, FileMode.Open, FileAccess.Read));

                eval.ContextItem = indoc;
                Serializer qout = new Serializer();
                qout.SetOutputProperty(Serializer.METHOD, "xml");
                qout.SetOutputProperty(Serializer.INDENT, "yes");
                qout.SetOutputProperty(Serializer.SAXON_INDENT_SPACES, "1");
                qout.SetOutputWriter(Response.Output);
                eval.Run(qout);
            }
        }
    }
Example #7
0
        public XQueryExecutable Compile(TextReader module, XQueryCompileOptions options)
        {
            XQueryCompiler compiler = CreateCompiler(options);

            try {
                return(WrapExecutable(compiler.Compile(module.ReadToEnd()), options));
            } catch (Exception ex) {
                throw WrapCompileException(ex, compiler);
            }
        }
Example #8
0
    /**
     * Show a query producing a single atomic value as its result and returning the value
     * to the Java application
     */

    public static void ExampleToSingleton()
    {
        Processor        processor = new Processor();
        XQueryCompiler   compiler  = processor.NewXQueryCompiler();
        XQueryExecutable exp       = compiler.Compile("avg(for $i in 1 to 10 return $i * $i)");
        XQueryEvaluator  eval      = exp.Load();
        XdmAtomicValue   result    = (XdmAtomicValue)eval.EvaluateSingle();

        Console.WriteLine("Result type: " + result.Value.GetType());
        Console.WriteLine("Result value: " + (decimal)result.Value);
    }
Example #9
0
    /**
     * Show how to run two queries in tandem. The second query is applied to the
     * results of the first.
     */

    public static void ExampleExtra()
    {
        // place-holder to add user-defined examples for testing
        Processor        processor = new Processor();
        XQueryCompiler   compiler  = processor.NewXQueryCompiler();
        XQueryExecutable exp       = compiler.Compile(@"<out>{matches('ABC', '\p{IsBasicLatin}*')}</out>");
        XQueryEvaluator  eval      = exp.Load();
        DomDestination   dest      = new DomDestination();

        eval.Run(dest);
        Console.WriteLine(dest.XmlDocument.OuterXml);
    }
Example #10
0
        private XQueryExecutable CreateXQueryExecutable(string currentNamespaceURI = "", Processor processor = null)
        {
            XQueryExecutable xQueryExecutable = null;

            if (processor != null && !string.IsNullOrEmpty(XQuery))
            {
                XQueryCompiler xQueryCompiler = processor.NewXQueryCompiler();
                xQueryCompiler.DeclareNamespace(_evaluator.RuleNamespace, currentNamespaceURI);
                xQueryExecutable = xQueryCompiler.Compile(XQuery);
            }
            return(xQueryExecutable);
        }
Example #11
0
        /// <summary>
        /// Perform XQuery <paramref name="query"/> on XML document at <paramref name="input"/> using Saxon API.
        /// </summary>
        /// <param name="input">A valid XML document.</param>
        /// <param name="query">An XQuery query.</param>
        /// <returns>Returns the results of the <paramref name="query"/> as an XmlDocument.</returns>
        public static XmlDocument PerformQuery(XmlDocument input, string query)
        {
            var              processor  = new Processor();
            XdmNode          inputXml   = processor.NewDocumentBuilder().Build(new XmlNodeReader(input));
            XQueryCompiler   compiler   = processor.NewXQueryCompiler();
            XQueryExecutable executable = compiler.Compile(query);
            XQueryEvaluator  evaluator  = executable.Load();

            evaluator.ContextItem = inputXml;
            var domOut = new DomDestination();

            evaluator.Run(domOut);
            return(domOut.XmlDocument);
        }
Example #12
0
        XQueryCompiler CreateCompiler(XQueryCompileOptions options)
        {
            XQueryCompiler compiler = this.processor.NewXQueryCompiler();

            compiler.ErrorList = new ArrayList();
            compiler.BaseUri   = options.BaseUri.AbsoluteUri;

            foreach (XPathModuleInfo item in XPathModules.Modules.Where(m => m.Predeclare))
            {
                compiler.DeclareNamespace(item.PredeclarePrefix, item.Namespace);
            }

            return(compiler);
        }
Example #13
0
    /**
     * Show a query producing a sequence as its result and returning the sequence
     * to the Java application in the form of an iterator. For each item in the
     * result, its string value is output.
     */

    public static void ExampleToSequence()
    {
        Processor        processor = new Processor();
        XQueryCompiler   compiler  = processor.NewXQueryCompiler();
        XQueryExecutable exp       = compiler.Compile("for $i in 1 to 10 return $i * $i");
        XQueryEvaluator  eval      = exp.Load();
        XdmValue         value     = eval.Evaluate();
        IEnumerator      e         = value.GetEnumerator();

        while (e.MoveNext())
        {
            XdmItem item = (XdmItem)e.Current;
            Console.WriteLine(item.ToString());
        }
    }
        public void ExecuteQuery(string query, string destination, string dimension, string granularity)
        {
            Processor      processor = new Processor();
            XQueryCompiler compiler  = processor.NewXQueryCompiler();

            compiler.BaseUri = BaseUri;
            XQueryExecutable exp  = compiler.Compile(query);
            XQueryEvaluator  eval = exp.Load();

            if (dimension.Equals("Geo", StringComparison.InvariantCultureIgnoreCase))
            {
                int id  = GetMetaID("Country");
                int id1 = GetMetaID("Region");
                int id2 = GetMetaID("City");
                int id3 = GetMetaID("Street");
                eval.SetExternalVariable(new QName("Country"), new XdmAtomicValue(id));
                eval.SetExternalVariable(new QName("Region"), new XdmAtomicValue(id1));
                eval.SetExternalVariable(new QName("City"), new XdmAtomicValue(id2));
                eval.SetExternalVariable(new QName("Street"), new XdmAtomicValue(id3));
                //expr.bindInt(new QName("Country"), id, null);
                //expr.bindInt(new QName("Region"), id1, null);
                //expr.bindInt(new QName("City"), id2, null);
                //expr.bindInt(new QName("Street"), id3, null);
            }
            else if (dimension.Equals("Time", StringComparison.InvariantCultureIgnoreCase))
            {
                int id = GetMetaID("Date/Time Original");
                eval.SetExternalVariable(new QName("id"), new XdmAtomicValue(id));
            }
            else if (dimension.Equals("Social", StringComparison.InvariantCultureIgnoreCase))
            {
                int id = GetMetaID("Artist");
                eval.SetExternalVariable(new QName("id"), new XdmAtomicValue(id));
            }

            Serializer qout = new Serializer();

            qout.SetOutputProperty(Serializer.METHOD, "xml");
            qout.SetOutputProperty(Serializer.INDENT, "yes");
            FileStream outStream = new FileStream(BaseUri + destination, FileMode.Create, FileAccess.Write);

            qout.SetOutputStream(outStream);
            Console.WriteLine("Output written to " + destination);
            eval.Run(qout);
            outStream.Dispose();
            outStream.Close();
            qout.Close();
        }
Example #15
0
    /**
     * Show a query that takes a parameter (external variable) as input.
     * The query produces a single atomic value as its result and returns the value
     * to the Java application. For the types of value that may be returned, and
     * their mapping to XPath data types, see {@link XPathEvaluator#Evaluate}
     */

    public static void ExampleWithParam()
    {
        Processor      processor = new Processor();
        XQueryCompiler compiler  = processor.NewXQueryCompiler();

        compiler.DeclareNamespace("p", "http://saxon.sf.net/ns/p");
        XQueryExecutable exp = compiler.Compile(
            "declare variable $p:in as xs:integer external; $p:in * $p:in");
        XQueryEvaluator eval = exp.Load();

        eval.SetExternalVariable(new QName("http://saxon.sf.net/ns/p", "p:in"), new XdmAtomicValue(12));
        XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();

        Console.WriteLine("Result type: " + result.Value.GetType());
        Console.WriteLine("Result value: " + (long)result.Value);
    }
        public List <MetadataAttributes> GetGeo(string granularity, bool distinct = true)
        {
            string query = "";

            if (granularity.Equals("Country", StringComparison.InvariantCultureIgnoreCase))
            {
                query = String.Format(distinct ? XQueries.ExtractGeoArray : XQueries.ExtractGeoArray2, "Country");
            }
            else if (granularity.Equals("Region", StringComparison.InvariantCultureIgnoreCase))
            {
                query = String.Format(distinct ? XQueries.ExtractGeoArray : XQueries.ExtractGeoArray2, "Region");
            }
            else if (granularity.Equals("City", StringComparison.InvariantCultureIgnoreCase))
            {
                query = String.Format(distinct ? XQueries.ExtractGeoArray : XQueries.ExtractGeoArray2, "City");
            }
            else
            {
                query = String.Format(distinct ? XQueries.ExtractGeoArray : XQueries.ExtractGeoArray2, "Street");
            };

            List <MetadataAttributes> ts = new List <MetadataAttributes>();

            Processor      processor = new Processor();
            XQueryCompiler compiler  = processor.NewXQueryCompiler();

            compiler.BaseUri = BaseUri;
            XQueryExecutable exp   = compiler.Compile(query);
            XQueryEvaluator  eval  = exp.Load();
            XdmValue         value = eval.Evaluate();
            IEnumerator      e     = value.GetEnumerator();

            while (e.MoveNext())
            {
                Interval interval = new Interval();
                var      current  = e.Current.ToString();
                if (!current.Equals("")) // a revoir avec elio
                {
                    MetadataAttributes geo = new MetadataAttributes("Geo", "Nominal", current, interval);
                    ts.Add(geo);
                }
            }
            //ts.Reverse();

            return(ts);
        }
Example #17
0
    /**
     * Show a query producing a document as its result and serializing this
     * to a FileStream
     */

    public static void ExampleToStreamResult()
    {
        Processor      processor = new Processor();
        XQueryCompiler compiler  = processor.NewXQueryCompiler();

        compiler.BaseUri = "http://www.saxonica.com/";
        compiler.DeclareNamespace("saxon", "http://saxon.sf.net/");
        XQueryExecutable exp  = compiler.Compile("<saxon:example>{static-base-uri()}</saxon:example>");
        XQueryEvaluator  eval = exp.Load();
        Serializer       qout = new Serializer();

        qout.SetOutputProperty(Serializer.METHOD, "xml");
        qout.SetOutputProperty(Serializer.INDENT, "yes");
        qout.SetOutputProperty(Serializer.SAXON_INDENT_SPACES, "1");
        qout.SetOutputStream(new FileStream("testoutput.xml", FileMode.Create, FileAccess.Write));
        eval.Run(qout);
    }
Example #18
0
        /**
         * Saxon: Safe when Whitelisting on XQuery Expression Example
         * Proves that Saxon is safe from injection when whitelisting the XQuery expression
         */
        protected void Page_Load(object sender, EventArgs e)
        {
            bool expectedSafe = false;

            try
            {
                // parse the XML
                Processor       processor = new Processor(false);
                DocumentBuilder doc       = processor.newDocumentBuilder();
                XdmNode         node      = doc.build(new StreamSource(appPath + "/resources/students.xml"));

                // query the XML
                string query;
                if (Request.QueryString["payload"].Contains("\"") || Request.QueryString["payload"].Contains(";"))
                {
                    PrintResults(expectedSafe, new List <string>());
                    throw new InvalidParameterException("First Name parameter must not contain quotes or semicolons");
                }
                else
                {
                    query = "for $s in //Students/Student " +
                            "where $s/FirstName = \"" + Request.QueryString["payload"] + "\" " +
                            "return $s";    // safe in here!
                }
                XQueryCompiler   xqComp = processor.newXQueryCompiler();
                XQueryExecutable xqExec = xqComp.compile(query);
                XQueryEvaluator  xqEval = xqExec.load();
                xqEval.setContextItem(node);
                xqEval.evaluate();

                // interpret the result of the query
                List <string> resultList = new List <string>();
                foreach (XdmValue value in xqEval)
                {
                    resultList.Add(value.ToString());
                }

                // print the results on the query
                PrintResults(expectedSafe, resultList);
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
Example #19
0
    ///<summary>
    /// Demonstrate extensibility using user-written extension functions
    ///</summary>

    public static void ExampleExtensibility()
    {
        String query =
            "declare namespace ext = \"clitype:SampleExtensions.SampleExtensions?asm=SampleExtensions\";" +
            "<out>" +
            "  <addition>{ext:add(2,2)}</addition>" +
            "  <average>{ext:average((1,2,3,4,5,6))}</average>" +
            "  <language>{ext:hostLanguage()}</language>" +
            "</out>";

        Processor        processor = new Processor();
        XQueryCompiler   compiler  = processor.NewXQueryCompiler();
        XQueryExecutable exp       = compiler.Compile(query);
        XQueryEvaluator  eval      = exp.Load();
        Serializer       qout      = new Serializer();

        eval.Run(qout);
    }
        public List <string> GetObjects(string query)
        {
            List <string> objects = new List <string>();

            Processor      processor = new Processor();
            XQueryCompiler compiler  = processor.NewXQueryCompiler();

            compiler.BaseUri = BaseUri;
            XQueryExecutable exp   = compiler.Compile(query);
            XQueryEvaluator  eval  = exp.Load();
            XdmValue         value = eval.Evaluate();
            IEnumerator      e     = value.GetEnumerator();

            while (e.MoveNext())
            {
                objects.Add(e.Current.ToString());
            }
            return(objects);
        }
Example #21
0
    /**
     * Show a query taking a DOM as its input and producing a DOM as its output.
     */

    public static void ExampleToDOM(String inputFileName)
    {
        Processor processor = new Processor();

        XmlDocument input = new XmlDocument();

        input.Load(inputFileName);
        XdmNode indoc = processor.NewDocumentBuilder().Build(new XmlNodeReader(input));

        XQueryCompiler   compiler = processor.NewXQueryCompiler();
        XQueryExecutable exp      = compiler.Compile("<doc>{reverse(/*/*)}</doc>");
        XQueryEvaluator  eval     = exp.Load();

        eval.ContextItem = indoc;
        DomDestination qout = new DomDestination();

        eval.Run(qout);
        XmlDocument outdoc = qout.XmlDocument;

        Console.WriteLine(outdoc.OuterXml);
    }
Example #22
0
    ///<summary>
    /// ExampleExtra is where you can place your own code for testing
    ///</summary>

    public static void ExampleExtra()
    {
        // place-holder to add user-defined examples for testing
        string xhtml = "<html><head><title>Hello World</title></head></html>";
        string query = "data(/html/head/title)";

        Processor   processor = new Processor();
        XmlDocument input     = new XmlDocument();

        input.LoadXml(xhtml);
        XQueryCompiler   compiler = processor.NewXQueryCompiler();
        XQueryExecutable exp      = compiler.Compile(query);
        XQueryEvaluator  eval     = exp.Load();

        eval.ContextItem = processor.NewDocumentBuilder().Build(new XmlNodeReader(input));
        Serializer   qout = new Serializer();
        StringWriter sw   = new StringWriter();

        qout.SetOutputProperty(Serializer.OMIT_XML_DECLARATION, "yes"); qout.SetOutputWriter(sw); eval.Run(qout);

        Console.WriteLine(sw);
    }
        public List <MetadataAttributes> GetSocial(string query)
        {
            List <MetadataAttributes> ts = new List <MetadataAttributes>();

            Processor      processor = new Processor();
            XQueryCompiler compiler  = processor.NewXQueryCompiler();

            compiler.BaseUri = BaseUri;
            XQueryExecutable exp   = compiler.Compile(query);
            XQueryEvaluator  eval  = exp.Load();
            XdmValue         value = eval.Evaluate();
            IEnumerator      e     = value.GetEnumerator();

            while (e.MoveNext())
            {
                Interval           interval = new Interval();
                MetadataAttributes social   = new MetadataAttributes("Social", "Nominal", e.Current.ToString(), interval);
                ts.Add(social);
            }

            return(ts);
        }
Example #24
0
    /**
     * Show a query taking a Saxon tree as its input and producing a Saxon tree as its output.
     */

    public static void ExampleToXDM(String inputFileName)
    {
        Processor processor = new Processor();

        DocumentBuilder loader = processor.NewDocumentBuilder();

        loader.BaseUri = new Uri(inputFileName);
        XdmNode indoc = loader.Build(
            new FileStream(inputFileName, FileMode.Open, FileAccess.Read));

        XQueryCompiler   compiler = processor.NewXQueryCompiler();
        XQueryExecutable exp      = compiler.Compile("<doc>{reverse(/*/*)}</doc>");
        XQueryEvaluator  eval     = exp.Load();

        eval.ContextItem = indoc;
        XdmDestination qout = new XdmDestination();

        eval.Run(qout);
        XdmNode outdoc = qout.XdmNode;

        Console.WriteLine(outdoc.OuterXml);
    }
Example #25
0
        /**
         * Saxon: Safe when Using Bind Variables on XQuery Expression Example
         * Proves that Saxon is safe from injection when using bind variables on the XQuery expression
         */
        protected void Page_Load(object sender, EventArgs e)
        {
            bool expectedSafe = true;

            try
            {
                // parse the XML
                Processor       processor = new Processor(false);
                DocumentBuilder doc       = processor.newDocumentBuilder();
                XdmNode         node      = doc.build(new StreamSource(appPath + "/resources/students.xml"));

                // query the XML
                string query = "declare variable $name as xs:string external; " +
                               "for $s in //Students/Student " +
                               "where $s/FirstName = $name " +
                               "return $s";   // safe!
                XQueryCompiler   xqComp = processor.newXQueryCompiler();
                XQueryExecutable xqExec = xqComp.compile(query);
                XQueryEvaluator  xqEval = xqExec.load();
                xqEval.setContextItem(node);
                xqEval.setExternalVariable(new QName("name"), new XdmAtomicValue(Request.QueryString["payload"]));
                xqEval.evaluate();

                // interpret the result of the query
                List <string> resultList = new List <string>();
                foreach (XdmValue value in xqEval)
                {
                    resultList.Add(value.ToString());
                }

                // print the results on the query
                PrintResults(expectedSafe, resultList);
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
        public int GetMetaID(String meta)
        {
            Processor      processor = new Processor();
            XQueryCompiler compiler  = processor.NewXQueryCompiler();

            //compiler.DeclareNamespace("fn", "http://www.w3.org/2005/xpath-functions");
            compiler.BaseUri = BaseUri;
            XQueryExecutable exp  = compiler.Compile(XQueries.GetMetaID);
            XQueryEvaluator  eval = exp.Load();

            eval.SetExternalVariable(new QName("meta"), new XdmAtomicValue(meta));
            //XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();
            XdmItem     value = eval.EvaluateSingle();
            IEnumerator e     = value.GetEnumerator();

            while (e.MoveNext())
            {
                //XdmItem item = (XdmItem)e.Current;
                //Console.WriteLine(item.ToString());
                return(Convert.ToInt32(e.Current.ToString()));
            }
            return(-1);
        }
Example #27
0
    /**
     * Show a query consisting of two modules, using a QueryResolver to resolve
     * the "import module" declaration
     */

    public static void ExampleMultiModule()
    {
        String mod1 = "import module namespace m2 = 'http://www.example.com/module2';" +
                      "m2:square(3)";

        String mod2 = "module namespace m2 = 'http://www.example.com/module2';" +
                      "declare function m2:square($p) { $p * $p };";

        Processor      processor = new Processor();
        XQueryCompiler compiler  = processor.NewXQueryCompiler();

        InlineModuleResolver resolver = new InlineModuleResolver();

        resolver.AddModule(new Uri("http://www.example.com/module2"), mod2);
        compiler.QueryResolver = resolver;
        XQueryExecutable exp  = compiler.Compile(mod1);
        XQueryEvaluator  eval = exp.Load();

        XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();

        Console.WriteLine("Result type: " + result.Value.GetType());
        Console.WriteLine("Result value: " + (long)result.Value);
    }
Example #28
0
 static ProcessorException WrapCompileException(Exception ex, XQueryCompiler compiler)
 {
     return(WrapCompileException(ex, compiler.ErrorList));
 }
Example #29
0
    public void go(String[] args)
    {
        Console.WriteLine("Testing Saxon " + processor.ProductVersion);
        testSuiteDir = args[0];
        if (testSuiteDir.EndsWith("/"))
        {
            testSuiteDir = testSuiteDir.Substring(0, testSuiteDir.Length - 1);
        }
        saxonResultsDir = args[1];
        if (saxonResultsDir.EndsWith("/"))
        {
            saxonResultsDir = saxonResultsDir.Substring(0, testSuiteDir.Length - 1);
        }
        Hashtable exceptions = new Hashtable();

        if (args.Length > 1)
        {
            testPattern = (args[2]); // TODO: allow a regex
        }

        for (int i = 0; i < args.Length; i++)
        {
            if (args[i].Equals("-w"))
            {
                //showWarnings = true;
            }
            else if (args[i].Equals("-debug"))
            {
                debug = true;
            }
        }

        fileComparer = new FileComparer(processor, testSuiteDir);

        XPathCompiler xpc = processor.NewXPathCompiler();

        xpc.DeclareNamespace("t", testURI);
        xpc.DeclareVariable(new QName("", "param"));
        findSourcePath = xpc.Compile("//t:test-suite/t:sources/t:source[@ID=$param]");

        findCollection = xpc.Compile("//t:test-suite/t:sources/t:collection[@ID=$param]");

        xpc = processor.NewXPathCompiler();
        xpc.DeclareNamespace("t", testURI);
        xpc.DeclareVariable(new QName("", "testcase"));
        xpc.DeclareVariable(new QName("", "moduleuri"));
        findModule = xpc.Compile("for $m in $testcase/t:module[@namespace=$moduleuri] " +
                                 "return concat('file:///" + testSuiteDir +
                                 "/', root($testcase)/t:test-suite/t:sources/t:module[@ID=string($m)]/@FileName, '.xq')");

        //xpc = processor.NewXPathCompiler();
        //xpc.DeclareNamespace("saxon", "http://saxon.sf.net/");
        //xpc.DeclareVariable(new QName("", "actual"));
        //xpc.DeclareVariable(new QName("", "gold"));
        //xpc.DeclareVariable(new QName("", "debug"));
        //compareDocuments = xpc.Compile("saxon:deep-equal($actual, $gold, (), if ($debug) then 'JNCPS?!' else 'JNCPS')");

        QName testCaseNT             = new QName(testURI, "test-case");
        QName nameNT                 = new QName(testURI, "name");
        QName queryNT                = new QName(testURI, "query");
        QName inputNT                = new QName(testURI, "input");
        QName inputFileNT            = new QName(testURI, "input-file");
        QName inputUriNT             = new QName(testURI, "input-URI");
        QName defaultCollectionNT    = new QName(testURI, "defaultCollection");
        QName outputFileNT           = new QName(testURI, "output-file");
        QName expectedErrorNT        = new QName(testURI, "expected-error");
        QName schemaNT               = new QName(testURI, "schema");
        QName contextItemNT          = new QName(testURI, "contextItem");
        QName inputQueryNT           = new QName(testURI, "input-query");
        QName sourceDocumentNT       = new QName(testURI, "source-document");
        QName errorNT                = new QName(testURI, "error");
        QName validationNT           = new QName(testURI, "validation");
        QName discretionaryItemsNT   = new QName(testURI, "discretionary-items");
        QName discretionaryFeatureNT = new QName(testURI, "discretionary-feature");
        QName discretionaryChoiceNT  = new QName(testURI, "discretionary-choice");
        QName initialContextNodeNT   = new QName(testURI, "initial-context-node");


        QName fileAtt      = new QName("", "file");
        QName filePathAtt  = new QName("", "FilePath");
        QName fileNameAtt  = new QName("", "FileName");
        QName errorIdAtt   = new QName("", "error-id");
        QName compareAtt   = new QName("", "compare");
        QName nameAtt      = new QName("", "name");
        QName behaviorAtt  = new QName("", "behavior");
        QName qnameAtt     = new QName("", "qname");
        QName modeAtt      = new QName("", "mode");
        QName validatesAtt = new QName("", "validates");
        QName variableAtt  = new QName("", "variable");
        QName roleAtt      = new QName("", "role");

        DocumentBuilder builder       = processor.NewDocumentBuilder();
        XdmNode         exceptionsDoc = builder.Build(new Uri(saxonResultsDir + "/exceptions.xml"));

        // The exceptions.xml file contains details of tests that aren't to be run, for example
        // because they have known bugs or require special configuration

        IEnumerator exceptionTestCases = exceptionsDoc.EnumerateAxis(XdmAxis.Descendant, new QName("", "exception"));

        while (exceptionTestCases.MoveNext())
        {
            XdmNode  n          = (XdmNode)exceptionTestCases.Current;
            String   nameAttVal = n.StringValue;
            char[]   seps       = { ' ', '\n', '\t' };
            String[] parts      = nameAttVal.Split(seps);
            foreach (string p in parts)
            {
                if (!exceptions.ContainsKey(p))
                {
                    exceptions.Add(p, "Exception");
                }
            }
        }

        // Hash table containing all source documents. The key is the document name in the
        // catalog, the value is the corresponding document node

        Hashtable sourceDocs = new Hashtable(50);

        // Load the catalog

        XdmNode catalog = builder.Build(new Uri(testSuiteDir + "/XQTScatalog.xml"));

        // Add all Static Typing test cases to the exceptions list

        xpc = processor.NewXPathCompiler();
        xpc.DeclareNamespace("t", testURI);
        XPathSelector st = xpc.Compile("//t:test-group[@name='StaticTyping']//t:test-case").Load();

        st.ContextItem = catalog;
        IEnumerator ste = st.GetEnumerator();

        while (ste.MoveNext())
        {
            XdmNode testCase = (XdmNode)ste.Current;
            exceptions.Add(testCase.GetAttributeValue(nameAtt), "StaticTypingException");
        }

        // Create the results file

        results = new StreamWriter(saxonResultsDir + "/results"
                                   + processor.ProductVersion + "n.xml");

        results.WriteLine("<test-suite-result xmlns='http://www.w3.org/2005/02/query-test-XQTSResult'>");

        // Pre-load all the schemas

        SchemaManager mgr = processor.SchemaManager;
        IEnumerator   se  = catalog.EnumerateAxis(XdmAxis.Descendant, schemaNT);

        while (se.MoveNext())
        {
            XdmNode schemaNode = (XdmNode)se.Current;
            Console.WriteLine("Loading schema " + schemaNode.GetAttributeValue(fileNameAtt));
            Uri location = new Uri(testSuiteDir + "/" + schemaNode.GetAttributeValue(fileNameAtt));
            mgr.Compile(location);
        }

        total = 0;
        IEnumerator testCases = catalog.EnumerateAxis(XdmAxis.Descendant, testCaseNT);

        while (testCases.MoveNext())
        {
            total++;
        }

        // Process the test cases in turn

        testCases = catalog.EnumerateAxis(XdmAxis.Descendant, testCaseNT);
        while (testCases.MoveNext())
        {
            XdmNode testCase = (XdmNode)testCases.Current;

            String testName = testCase.GetAttributeValue(nameAtt);
            if (testPattern != null && !testName.StartsWith(testPattern))
            {
                continue;
            }
            if (exceptions.ContainsKey(testName))
            {
                continue;
            }

            Console.WriteLine("Test " + testName);


            // Compile the query

            String errorCode = null;

            String  filePath  = testCase.GetAttributeValue(filePathAtt);
            XdmNode query     = getChildElement(testCase, queryNT);
            String  queryName = query.GetAttributeValue(nameAtt);
            String  queryPath = testSuiteDir + "/Queries/XQuery/" + filePath + queryName + ".xq";

            XQueryCompiler compiler = processor.NewXQueryCompiler();
            compiler.BaseUri       = new Uri(queryPath).ToString();
            compiler.QueryResolver = new XqtsModuleResolver(testCase, findModule);

            ArrayList errors = new ArrayList();
            compiler.ErrorList = errors;
            XQueryEvaluator xqe    = null;
            FileStream      stream = null;
            try
            {
                stream = new FileStream(queryPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                xqe    = compiler.Compile(stream).Load();
            }
            catch (Exception e)
            {
                if (errors.Count > 0 && ((StaticError)errors[0]).ErrorCode != null)
                {
                    errorCode = ((StaticError)errors[0]).ErrorCode.LocalName;
                }
                else if (e is StaticError && ((StaticError)e).ErrorCode != null)
                {
                    Console.WriteLine(e.Message);
                    errorCode = ((StaticError)e).ErrorCode.LocalName;
                }
                else
                {
                    Console.WriteLine(e.Message);
                    errorCode = "ErrorXXX";
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }

            // if the query compiled successfully, try to run it

            String outputPath = null;
            if (errorCode == null && xqe != null)
            {
                // Supply any input documents

                IEnumerator en = testCase.EnumerateAxis(XdmAxis.Child, inputFileNT);
                while (en.MoveNext())
                {
                    XdmNode file = (XdmNode)en.Current;
                    String  var  = file.GetAttributeValue(variableAtt);
                    if (var != null)
                    {
                        String  sourceName = file.StringValue;
                        XdmNode sourceDoc;
                        if (sourceDocs.ContainsKey(sourceName))
                        {
                            sourceDoc = (XdmNode)sourceDocs[sourceName];
                        }
                        else
                        {
                            sourceDoc = buildSource(catalog, builder, sourceName);
                            sourceDocs.Add(sourceName, sourceDoc);
                        }
                        xqe.SetExternalVariable(new QName("", var), sourceDoc);
                    }
                }

                // Supply any input URIs

                IEnumerator eu = testCase.EnumerateAxis(XdmAxis.Child, inputUriNT);
                while (eu.MoveNext())
                {
                    XdmNode file = (XdmNode)eu.Current;
                    String  var  = file.GetAttributeValue(variableAtt);
                    if (var != null)
                    {
                        String sourceName = file.StringValue;
                        if (sourceName.StartsWith("collection"))
                        {
                            // Supply a collection URI.
                            // This seems to be the only way to distinguish a document URI
                            // from a collection URI.
                            String        uri = "collection:" + sourceName;
                            XPathSelector xpe = findCollection.Load();
                            xpe.SetVariable(new QName("", "param"), new XdmAtomicValue(sourceName));
                            xpe.ContextItem = catalog;
                            XdmNode collectionNode = (XdmNode)xpe.EvaluateSingle();
                            if (collectionNode == null)
                            {
                                Console.WriteLine("*** Collection " + sourceName + " not found");
                            }
                            processor.RegisterCollection(new Uri(uri), getCollection(collectionNode));
                            xqe.SetExternalVariable(new QName("", var), new XdmAtomicValue(uri));
                        }
                        else
                        {
                            // Supply a document URI.
                            // We exploit the fact that the short name of the document is
                            // always the same as the file name in these tests
                            String uri = "file:///" + testSuiteDir + "/TestSources/" + sourceName + ".xml";
                            xqe.SetExternalVariable(new QName("", var), new XdmAtomicValue(uri));
                        }
                    }
                }

                // Supply the default collection if required

                XdmNode defaultCollection = getChildElement(testCase, defaultCollectionNT);
                if (defaultCollection != null)
                {
                    String        sourceName = defaultCollection.StringValue;
                    XPathSelector xpe        = findCollection.Load();
                    xpe.SetVariable(new QName("", "param"), new XdmAtomicValue(sourceName));
                    xpe.ContextItem = catalog;
                    XdmNode collectionNode = (XdmNode)xpe.EvaluateSingle();
                    if (collectionNode == null)
                    {
                        Console.WriteLine("*** Collection " + sourceName + " not found");
                    }
                    processor.RegisterCollection(null, getCollection(collectionNode));
                }

                // Supply any external variables defined as the result of a separate query

                IEnumerator ev = testCase.EnumerateAxis(XdmAxis.Child, inputQueryNT);
                while (ev.MoveNext())
                {
                    XdmNode inputQuery = (XdmNode)ev.Current;

                    String         fileName     = inputQuery.GetAttributeValue(nameAtt);
                    String         subQueryPath = testSuiteDir + "/Queries/XQuery/" + filePath + fileName + ".xq";
                    XQueryCompiler subCompiler  = processor.NewXQueryCompiler();
                    compiler.BaseUri = new Uri(subQueryPath).ToString();
                    FileStream subStream = new FileStream(subQueryPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    XdmValue   value     = subCompiler.Compile(subStream).Load().Evaluate();
                    String     var       = inputQuery.GetAttributeValue(variableAtt);
                    xqe.SetExternalVariable(new QName("", var), value);
                }

                // Supply the context item if required

                IEnumerator ci = testCase.EnumerateAxis(XdmAxis.Child, contextItemNT);
                while (ci.MoveNext())
                {
                    XdmNode file = (XdmNode)ci.Current;

                    String sourceName = file.StringValue;
                    if (!sourceDocs.ContainsKey(sourceName))
                    {
                        XdmNode doc = buildSource(catalog, builder, sourceName);
                        sourceDocs.Add(sourceName, doc);
                    }
                    XdmNode sourceDoc = (XdmNode)sourceDocs[sourceName];
                    xqe.ContextItem = sourceDoc;
                }

                // Create a serializer for the output


                outputPath = saxonResultsDir + "/results.net/" + filePath + queryName + ".out";
                Serializer sr = new Serializer();
                try
                {
                    sr.SetOutputFile(outputPath);
                    sr.SetOutputProperty(new QName("", "method"), "xml");
                    sr.SetOutputProperty(new QName("", "omit-xml-declaration"), "yes");
                    sr.SetOutputProperty(new QName("", "indent"), "no");
                }
                catch (DynamicError)
                {
                    // probably means that no output directory exists, which is probably because
                    // an error is expected
                    outputPath = saxonResultsDir + "/results.net/" + queryName + ".out";
                    sr.SetOutputFile(outputPath);
                }

                // Finally, run the query

                try
                {
                    xqe.Run(sr);
                }
                catch (DynamicError e)
                {
                    Console.WriteLine(e.Message);
                    QName code = e.ErrorCode;
                    if (code != null && code.LocalName != null)
                    {
                        errorCode = code.LocalName;
                    }
                    else
                    {
                        errorCode = "ErrYYYYY";
                    }
                }
                catch (Exception e2)
                {
                    Console.WriteLine("Unexpected exception: " + e2.Message);
                    Console.WriteLine(e2.StackTrace);
                    errorCode = "CRASH!!!";
                }
            }

            // Compare actual results with expected results

            if (errorCode != null)
            {
                // query returned an error at compile time or run-time, check this was expected

                string      expectedError = "";
                bool        matched       = false;
                IEnumerator en            = testCase.EnumerateAxis(XdmAxis.Child, expectedErrorNT);
                while (en.MoveNext())
                {
                    XdmNode error             = (XdmNode)en.Current;
                    String  expectedErrorCode = error.StringValue;
                    expectedError += (expectedErrorCode + " ");
                    if (expectedErrorCode.Equals(errorCode))
                    {
                        matched = true;
                        feedback.Feedback(passed++, failed, total);
                        Console.WriteLine("Error " + errorCode + " as expected");
                        results.WriteLine("<test-case name='" + testName + "' result='pass'/>");
                        break;
                    }
                }
                if (!matched)
                {
                    if (expectedError.Equals(""))
                    {
                        feedback.Feedback(passed, failed++, total);
                        Console.WriteLine("Error " + errorCode + ", expected success");
                        results.WriteLine("<test-case name='" + testName + "' result='fail' comment='error " + errorCode + ", expected success'/>");
                    }
                    else
                    {
                        feedback.Feedback(passed++, failed, total);
                        Console.WriteLine("Error " + errorCode + ", expected " + expectedError);
                        results.WriteLine("<test-case name='" + testName + "' result='pass' comment='error " + errorCode + ", expected " + expectedError + "'/>");
                    }
                }
            }
            else
            {
                // query returned no error

                bool        matched = false;
                String      diag    = "";
                IEnumerator en      = testCase.EnumerateAxis(XdmAxis.Child, outputFileNT);
                while (en.MoveNext())
                {
                    XdmNode outputFile = (XdmNode)en.Current;
                    String  fileName   = testSuiteDir + "/ExpectedTestResults/" + filePath + outputFile.StringValue;
                    String  comparator = outputFile.GetAttributeValue(compareAtt);
                    if (comparator.Equals("Inspect"))
                    {
                        matched = true;
                        feedback.Feedback(passed++, failed, total);
                        results.WriteLine("<test-case name='" + testName + "' result='inspect'/>");
                        break;
                    }
                    else
                    {
                        String comparison = fileComparer.compare(outputPath, fileName, comparator);
                        matched = (comparison == "OK" || comparison.StartsWith("#"));
                        if (matched)
                        {
                            feedback.Feedback(passed++, failed, total);
                            results.WriteLine("<test-case name='" + testName + "' result='pass'/>");
                            diag = diag + ("<!-- " + comparison + " -->\n");
                            break;
                        }
                    }
                }

                if (!matched)
                {
                    string      expectedError = "";
                    IEnumerator ee            = testCase.EnumerateAxis(XdmAxis.Child, expectedErrorNT);
                    while (ee.MoveNext())
                    {
                        XdmNode error             = (XdmNode)ee.Current;
                        String  expectedErrorCode = error.StringValue;
                        expectedError += (expectedErrorCode + " ");
                    }

                    if (expectedError.Equals(""))
                    {
                        feedback.Feedback(passed, failed++, total);
                        Console.WriteLine("Results differ from expected results");
                        results.WriteLine("<test-case name='" + testName + "' result='fail'/>");
                    }
                    else
                    {
                        feedback.Feedback(passed, failed++, total);
                        Console.WriteLine("Error " + expectedError + "expected but not reported");
                        results.WriteLine("<test-case name='" + testName + "' result='fail' comment='expected error " + expectedError + "not reported'/>");
                    }
                }
            }
        }

        results.WriteLine("</test-suite-result>");
        results.Close();
    }
        public ViewModel(MainWindow v, Model model)
        {
            this.view  = v;
            this.model = model;

            // Register the types for hightlighting. C#, VB.NET and TSQL are defined
            // internally by the editor, so they do not need to be registered.
            HighlightingManager.Instance.RegisterHighlighting("Go", new[] { ".go" }, LoadHighlightDefinition("Go.xshd"));

            Properties.Settings.Default.PropertyChanged += (object sender, PropertyChangedEventArgs e) =>
            {
                // Save all the user's settings
                Properties.Settings.Default.Save();
            };

            ExitCommand = new RelayCommand(
                p => { Application.Current.Shutdown(); });

            OpenSourceCommand = new RelayCommand(
                p =>
            {
                var(extension, l) = this.Language;

                OpenFileDialog dlg = new OpenFileDialog();
                dlg.DefaultExt     = extension;                    // Default file extension
                dlg.Filter         = l + "  Files |*" + extension; // Filter files by extension

                // Show open file dialog box
                Nullable <bool> result = dlg.ShowDialog();

                // Process save file dialog box results
                if (result.Value)
                {
                    view.SourceEditor.Text = File.ReadAllText(dlg.FileName);
                }
            }
                );
            SaveSourceCommand = new RelayCommand(
                p =>
            {
                var(e, l) = this.Language;

                SaveFileDialog dlg = new SaveFileDialog
                {
                    FileName   = "document",          // Default file name
                    DefaultExt = e,                   // Default file extension
                    Filter     = l + "  Files |*" + e // Filter files by extension
                };

                // Show save file dialog box
                Nullable <bool> result = dlg.ShowDialog();

                // Process save file dialog box results
                if (result.Value)
                {
                    // Save document
                    File.WriteAllText(dlg.FileName, view.SourceEditor.Text);
                }
            }
                );

            OpenAstCommand = new RelayCommand(
                p =>
            {
                OpenFileDialog dlg = new OpenFileDialog
                {
                    DefaultExt = ".xml",            // Default file extension
                    Filter     = "XML Files |*.xml" // Filter files by extension
                };

                // Show open file dialog box
                Nullable <bool> result = dlg.ShowDialog();

                // Process save file dialog box results
                if (result.Value)
                {
                    view.ResultsEditor.Text = File.ReadAllText(dlg.FileName);
                }
            }
                );
            SaveAstCommand = new RelayCommand(
                p =>
            {
                SaveFileDialog dlg = new SaveFileDialog
                {
                    FileName   = "document",        // Default file name
                    DefaultExt = ".xml",            // Default file extension
                    Filter     = "XML Files |*.xml" // Filter files by extension
                };

                // Show save file dialog box
                Nullable <bool> result = dlg.ShowDialog();

                // Process save file dialog box results
                if (result.Value)
                {
                    // Save document
                    File.WriteAllText(dlg.FileName, view.ResultsEditor.Text);
                }
            }
                );
            OpenQueryCommand = new RelayCommand(
                p =>
            {
                OpenFileDialog dlg = new OpenFileDialog
                {
                    DefaultExt = ".xq",           // Default file extension
                    Filter     = "xq Files |*.xq" // Filter files by extension
                };

                // Show open file dialog box
                Nullable <bool> result = dlg.ShowDialog();

                // Process save file dialog box results
                if (result.Value)
                {
                    view.QueryEditor.Text = File.ReadAllText(dlg.FileName);
                }
            }
                );
            SaveQueryCommand = new RelayCommand(
                p =>
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.FileName       = "document";       // Default file name
                dlg.DefaultExt     = ".xq";            // Default file extension
                dlg.Filter         = "xq Files |*.xq"; // Filter files by extension

                // Show save file dialog box
                Nullable <bool> result = dlg.ShowDialog();

                // Process save file dialog box results
                if (result.Value)
                {
                    // Save document
                    File.WriteAllText(dlg.FileName, view.QueryEditor.Text);
                }
            }
                );

            SaveQueryResultsCommand = new RelayCommand(
                p =>
            {
                SaveFileDialog dlg = new SaveFileDialog
                {
                    FileName   = "document",        // Default file name
                    DefaultExt = ".xml",            // Default file extension
                    Filter     = "XML Files |*.xml" // Filter files by extension
                };

                // Show save file dialog box
                Nullable <bool> result = dlg.ShowDialog();

                // Process save file dialog box results
                if (result.Value)
                {
                    // Save document
                    File.WriteAllText(dlg.FileName, view.QueryEditor.Text);
                }
            }
                );

            ExecuteExtractionCommand = new RelayCommand(
                p =>
            {
                this.Status = "";
                this.Result = "";

                // Get the current language extractor from the model
                ILanguageExtractor extractor = this.Languages
                                               .Where(l => l.Metadata.Name == Properties.Settings.Default.CurrentLanguage)
                                               .Select(l => l.Value).FirstOrDefault();

                XDocument doc = null;;
                IEnumerable <IDiagnosticItem> diagnostics = null;
                try
                {
                    (doc, diagnostics) = extractor.Extract(this.view.SourceEditor.Text);
                }
                catch (Exception e)
                {
                    // Nothing.
                }
                this.Result = doc != null ? doc.ToString() : "";

                // Remove all the entries in the error list and the squigglies
                this.DiagnosticItems.Clear();
                this.view.SourceEditorTextMarkerService.RemoveAll(m => true);

                if (diagnostics != null)
                {
                    foreach (var d in diagnostics)
                    {
                        this.DiagnosticItems.Add(d);
                        int startOffset    = this.view.SourceEditor.Document.GetOffset(d.Line, d.Column);
                        int endOffset      = this.view.SourceEditor.Document.GetOffset(d.EndLine, d.EndColumn);
                        int length         = endOffset - startOffset;
                        ITextMarker marker = this.view.SourceEditorTextMarkerService.Create(startOffset, length);
                        marker.MarkerTypes = TextMarkerTypes.SquigglyUnderline;
                        marker.MarkerColor = d.Severity == "Error" ? Colors.Red : Colors.Green;
                    }
                }
            }
                );

            view.InputBindings.Add(new InputBinding(OpenSourceCommand, new KeyGesture(Key.O, ModifierKeys.Control)));
            view.InputBindings.Add(new InputBinding(SaveSourceCommand, new KeyGesture(Key.S, ModifierKeys.Control)));
            view.InputBindings.Add(new InputBinding(ExecuteExtractionCommand, new KeyGesture(Key.E, ModifierKeys.Control)));

            this.executeQueryCommand = new RelayCommand(
                p =>
            {
                Processor processor = new Processor();
                StringReader sr     = new StringReader(this.view.ResultsEditor.Text);
                XmlReader reader    = XmlReader.Create(sr, new XmlReaderSettings()
                {
                    ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags.None
                });

                XdmNode doc = null;
                try
                {
                    doc = processor.NewDocumentBuilder().Build(reader);
                }
                catch (Exception)
                {
                }

                XQueryCompiler compiler = processor.NewXQueryCompiler();

                // Add any namespaces needed...
                // compiler.DeclareNamespace("saxon", "http://saxon.sf.net/");

                try
                {
                    XQueryExecutable exp = compiler.Compile(this.view.QueryEditor.Text);
                    XQueryEvaluator eval = exp.Load();

                    // The context node is always the root document.
                    if (doc != null)
                    {
                        eval.ContextItem = doc;
                    }

                    Serializer qout = processor.NewSerializer();
                    qout.SetOutputProperty(Serializer.METHOD, "xml");
                    qout.SetOutputProperty(Serializer.OMIT_XML_DECLARATION, "yes");
                    // Not available: qout.SetOutputProperty(Serializer.SAXON_INDENT_SPACES, "2");
                    // Do not put attributes on separate lines:
                    qout.SetOutputProperty(Serializer.INDENT, "no");

                    // Put the result of the XQuery query into a memory stream:
                    var output = new MemoryStream();
                    qout.SetOutputStream(output);

                    // Run the query:
                    eval.Run(qout);

                    // Harvest the result
                    output.Position           = 0;
                    StreamReader resultReader = new StreamReader(output);
                    string result             = resultReader.ReadToEnd();

                    // Normalize the strange looking output generated by the serializer
                    // if it is XML
                    try
                    {
                        var d = XDocument.Parse(result);
                        this.view.QueryResultsEditor.Text = d.ToString(SaveOptions.None);
                    }
                    catch
                    {
                        this.view.QueryResultsEditor.Text = result;
                    }
                }
                catch (Exception e)
                {
                    this.view.QueryResultsEditor.Text = e.Message;
                }
            },
                p => { return(true); });
        }