Ejemplo n.º 1
0
        /// <summary>
        /// Set the value of a variable
        /// </summary>
        /// <param name="name">The name of the variable. This must match the name of a variable
        /// that was declared to the XPathCompiler. No error occurs if the expression does not
        /// actually reference a variable with this name.</param>
        /// <param name="value">The value to be given to the variable.</param>


        public void SetVariable(QName name, XdmValue value)
        {
            JXPathVariable var   = null;
            String         uri   = (name.Uri == null ? "" : name.Uri);
            String         local = name.LocalName;

            foreach (JXPathVariable v in declaredVariables)
            {
                String vuri = v.getVariableQName().getURI();
                if (vuri == null)
                {
                    vuri = "";
                }
                if (vuri == uri && v.getVariableQName().getLocalPart() == local)
                {
                    var = v;
                    break;
                }
            }
            if (var == null)
            {
                // TODO: this seems to conflict with the documentation of the method
                throw new ArgumentException("Variable has not been declared: " + name);
            }
            dynamicContext.setVariable(var, value.Unwrap());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Set the value of a variable
        /// <param name="name">The name of the variable. This must match the name of a variable
        /// that was declared to the XPathCompiler. No error occurs if the expression does not
        /// actually reference a variable with this name.</param>
        /// <param name="value">The value to be given to the variable.</param>
        /// </summary>

        public void SetVariable(QName name, XdmValue value)
        {
            JXPathVariable var   = null;
            String         uri   = (name.Uri == null ? "" : name.Uri);
            String         local = name.LocalName;

            foreach (JXPathVariable v in declaredVariables)
            {
                String vuri = v.getVariableQName().getNamespaceURI();
                if (vuri == null)
                {
                    vuri = "";
                }
                if (vuri == uri && v.getVariableQName().getLocalName() == local)
                {
                    var = v;
                    break;
                }
            }
            if (var == null)
            {
                throw new ArgumentException("Variable has not been declared: " + name);
            }
            dynamicContext.setVariable(var, value.Unwrap());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Declare a variable for use by the XPath expression. If the expression
        /// refers to any variables, then they must be declared here, unless the
        /// <c>AllowUndeclaredVariables</c> property has been set to true.
        /// </summary>
        /// <param name="name">The name of the variable, as a <c>QName</c></param>


        public void DeclareVariable(QName name)
        {
            if (cache != null)
            {
                cache.Clear();
            }
            JXPathVariable var = env.declareVariable(name.ToQNameValue());
            //declaredVariables.Add(var);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Load the compiled XPath expression to prepare it for execution.
        /// </summary>
        /// <returns>
        /// An <c>XPathSelector</c>. The returned <c>XPathSelector</c> can be used to
        /// set up the dynamic context, and then to evaluate the expression.
        /// </returns>

        public XPathSelector Load()
        {
            ArrayList declaredVariables = new ArrayList();
            JIterator iter = env.iterateExternalVariables();

            while (iter.hasNext())
            {
                JXPathVariable var = (JXPathVariable)iter.next();
                declaredVariables.Add(var);
            }
            return(new XPathSelector(exp, config, declaredVariables));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Get the required cardinality of a declared variable in the static context of the expression.
        /// The occurrence indicator, one of '?' (zero-or-one),
        /// '*' (zero-or-more), '+' (one-or-more), ' ' (a single space) (exactly one),
        /// or 'º' (masculine ordinal indicator, xBA) (exactly zero). The type empty-sequence()
        /// can be represented by an occurrence indicator of 'º' with any item type.
        /// If the variable was explicitly declared, this will be the occurrence indicator that was set when the
        /// variable was declared. If no item type was set, it will be <see cref="net.sf.saxon.s9api.OccurrenceIndicator#ZERO_OR_MORE"/>.
        /// If the variable was implicitly declared by reference (which can happen only when the
        /// allowUndeclaredVariables option is set), the returned type will be
        /// <see cref="net.sf.saxon.s9api.OccurrenceIndicator#ZERO_OR_MORE"/>.
        /// If no variable with the specified QName has been declared either explicitly or implicitly,
        /// the method returns 0.
        /// </summary>
        /// <param name="variableName">the name of a declared variable</param>
        /// <returns>the required cardinality.</returns>


        public char GetRequiredCardinalityForVariable(QName variableName)
        {
            JXPathVariable var = env.getExternalVariable(variableName.ToStructuredQName());

            if (var == null)
            {
                return('0');
            }
            else
            {
                return(GetOccurrenceIndicator(var.getRequiredType().getCardinality()));
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Get a list of external variables used by the expression. This will include both variables that were explicitly
        /// declared to the <c>XPathCompiler</c>, and (if the <c>AllowUndeclaredVariables</c> option was set) variables that
        /// are referenced within the expression but not explicitly declared.
        /// </summary>
        /// <returns>
        /// An IEnumerator over the names of the external variables, as instances of <c>QName</c>.</returns>

        public IEnumerator EnumerateExternalVariables()
        {
            ArrayList list = new ArrayList();
            JIterator iter = env.iterateExternalVariables();

            while (iter.hasNext())
            {
                JXPathVariable   var = (JXPathVariable)iter.next();
                JStructuredQName q   = var.getVariableQName();
                list.Add(new QName(q.getPrefix(), q.getURI(), q.getLocalPart()));
            }
            return(list.GetEnumerator());
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Get the required cardinality of a declared variable in the static context of the expression.
        /// </summary>
        /// <remarks>
        /// <para>The result is given as an occurrence indicator, one of:</para>
        /// <list>
        /// <item>'?' (zero-or-one)</item>
        /// <item>'*' (zero-or-more)</item>
        /// <item>'+' (one-or-more)</item>
        /// <item>' ' (a single space) (exactly one)</item>
        /// <item>'º' (masculine ordinal indicator, xBA) (exactly zero)</item>
        /// </list>
        /// <para>The type <c>empty-sequence()</c> can be represented by an occurrence indicator of 'º' with
        /// any item type.</para>
        /// <para>If the variable was explicitly declared, this will be the occurrence indicator that was set when the
        /// variable was declared. If no item type was set, it will be
        /// <see cref="net.sf.saxon.s9api.OccurrenceIndicator#ZERO_OR_MORE"/>.</para>
        /// <para>If the variable was implicitly declared by reference (which can happen only when the
        /// <c>allowUndeclaredVariables</c> option is set), the returned type will be
        /// <see cref="net.sf.saxon.s9api.OccurrenceIndicator#ZERO_OR_MORE"/>.</para>
        /// <para>If no variable with the specified <c>QName</c> has been declared either explicitly or implicitly,
        /// the method returns '0'.</para>
        /// </remarks>
        /// <param name="variableName">the name of a declared variable</param>
        /// <returns>The required cardinality, in the form of an occurrence indicator.</returns>


        public char GetRequiredCardinalityForVariable(QName variableName)
        {
            JXPathVariable var = ((JIndependentContext)executable.getUnderlyingStaticContext()).getExternalVariable(variableName.ToStructuredQName());

            if (var == null)
            {
                return('0');
            }
            else
            {
                return(GetOccurrenceIndicator(var.getRequiredType().getCardinality()));
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Compile an expression supplied as a String.
        /// </summary>
        /// <example>
        /// <code>
        /// XPathExecutable q = compiler.Compile("distinct-values(//*/node-name()");
        /// </code>
        /// </example>
        /// <param name="source">A string containing the source text of the XPath expression</param>
        /// <returns>An <c>XPathExecutable</c> which represents the compiled xpath expression object.
        /// The XPathExecutable may be run as many times as required, in the same or a different
        /// thread. The <c>XPathExecutable</c> is not affected by any changes made to the <c>XPathCompiler</c>
        /// once it has been compiled.</returns>
        /// <exception cref="StaticError">
        /// Throws a <c>Saxon.Api.StaticError</c> if there is any static error in the XPath expression.
        /// This includes both syntax errors, semantic errors such as references to undeclared functions or
        /// variables, and statically-detected type errors.
        /// </exception>

        public XPathExecutable Compile(String source)
        {
            if (cache != null)
            {
                XPathExecutable exec = (XPathExecutable)cache[source];
                if (exec != null)
                {
                    return(exec);
                }
            }
            try
            {
                JIndependentContext ic = env;
                if (ic.isAllowUndeclaredVariables())
                {
                    // self-declaring variables modify the static context. The XPathCompiler must not change state
                    // as the result of compiling an expression, so we need to copy the static context.
                    ic = new JIndependentContext(env);
                    for (JIterator iter = env.iterateExternalVariables(); iter.hasNext();)
                    {
                        JXPathVariable var  = (JXPathVariable)iter.next();
                        JXPathVariable var2 = ic.declareVariable(var.getVariableQName());
                    }
                }
                JXPathEvaluator eval = new JXPathEvaluator(config);
                eval.setStaticContext(ic);
                JXPathExpression cexp = eval.createExpression(source);
                XPathExecutable  exec = new XPathExecutable(cexp, config, ic);
                if (cache != null)
                {
                    cache[source] = exec;
                }
                return(exec);
            }
            catch (net.sf.saxon.trans.XPathException err)
            {
                throw new StaticError(err);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Compile an expression supplied as a String.
        /// </summary>
        /// <example>
        /// <code>
        /// XPathExecutable q = compiler.Compile("distinct-values(//*/node-name()");
        /// </code>
        /// </example>
        /// <param name="source">A string containing the source text of the XPath expression</param>
        /// <returns>An <c>XPathExecutable</c> which represents the compiled xpath expression object.
        /// The XPathExecutable may be run as many times as required, in the same or a different
        /// thread. The <c>XPathExecutable</c> is not affected by any changes made to the <c>XPathCompiler</c>
        /// once it has been compiled.</returns>

        public XPathExecutable Compile(String source)
        {
            JIndependentContext ic = env;

            if (ic.isAllowUndeclaredVariables())
            {
                // self-declaring variables modify the static context. The XPathCompiler must not change state
                // as the result of compiling an expression, so we need to copy the static context.
                ic = env.copy();
                for (JIterator iter = env.iterateExternalVariables(); iter.hasNext();)
                {
                    JXPathVariable var  = (JXPathVariable)iter.next();
                    JXPathVariable var2 = ic.declareVariable(var.getVariableQName());
                }
            }
            JXPathEvaluator eval = new JXPathEvaluator(config);

            eval.setStaticContext(ic);
            JXPathExpression cexp = eval.createExpression(source);

            return(new XPathExecutable(cexp, config, ic));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Declare a variable for use by the XPath expression. If the expression
        /// refers to any variables, then they must be declared here.
        /// </summary>
        /// <param name="name">The name of the variable, as a <c>QName</c></param>


        public void DeclareVariable(QName name)
        {
            JXPathVariable var = env.declareVariable(name.ToQNameValue());

            declaredVariables.Add(var);
        }