Ejemplo n.º 1
0
        private static void EvaluateKey(XPathNavigator?node, Query matchExpr, string matchStr, Query useExpr, Hashtable keyTable)
        {
            try
            {
                if (matchExpr.MatchNode(node) == null)
                {
                    return;
                }
            }
            catch (XPathException)
            {
                throw XsltException.Create(SR.Xslt_InvalidPattern, matchStr);
            }
            object            result = useExpr.Evaluate(new XPathSingletonIterator(node !, /*moved:*/ true));
            XPathNodeIterator?it     = result as XPathNodeIterator;

            if (it != null)
            {
                bool checkDuplicates = false;
                while (it.MoveNext())
                {
                    AddKeyValue(keyTable, /*key:*/ it.Current !.Value !, /*value:*/ node !, checkDuplicates);
                    checkDuplicates = true;
                }
            }
            else
            {
                string key = XmlConvert.ToXPathString(result) !;
                AddKeyValue(keyTable, key, /*value:*/ node !, /*checkDuplicates:*/ false);
            }
        }
Ejemplo n.º 2
0
        internal void SortNewNodeSet(Processor proc, ArrayList sortarray)
        {
            Debug.Assert(0 < sortarray.Count);
            int numSorts = sortarray.Count;
            XPathSortComparer comparer = new XPathSortComparer(numSorts);

            for (int i = 0; i < numSorts; i++)
            {
                Sort  sort = (Sort)sortarray[i] !;
                Query expr = proc.GetCompiledQuery(sort.select);

                comparer.AddSort(expr, new XPathComparerHelper(sort.order, sort.caseOrder, sort.lang, sort.dataType));
            }
            List <SortKey> results = new List <SortKey>();

            Debug.Assert(proc.ActionStack.Peek() == this, "the trick we are doing with proc.Current will work only if this is topmost frame");

            while (NewNextNode(proc))
            {
                XPathNodeIterator?savedNodeset = _nodeSet;
                _nodeSet = _newNodeSet;              // trick proc.Current node

                SortKey key = new SortKey(numSorts, /*originalPosition:*/ results.Count, _newNodeSet !.Current !.Clone());

                for (int j = 0; j < numSorts; j++)
                {
                    key[j] = comparer.Expression(j).Evaluate(_newNodeSet);
                }
                results.Add(key);

                _nodeSet = savedNodeset;                 // restore proc.Current node
            }
            results.Sort(comparer);
            _newNodeSet = new XPathSortArrayIterator(results);
        }
Ejemplo n.º 3
0
        private XPathNodeIterator Document(object arg0, string?baseUri)
        {
            XPathNodeIterator?it = arg0 as XPathNodeIterator;

            if (it != null)
            {
                ArrayList list      = new ArrayList();
                Hashtable documents = new Hashtable();
                while (it.MoveNext())
                {
                    Uri uri = ComposeUri(it.Current !.Value, baseUri ?? it.Current.BaseURI);
                    if (!documents.ContainsKey(uri))
                    {
                        documents.Add(uri, null);
                        list.Add(_processor !.GetNavigator(uri));
                    }
                }
                return(new XPathArrayIterator(list));
            }
            else
            {
                return(new XPathSingletonIterator(
                           _processor !.GetNavigator(
                               ComposeUri(XmlConvert.ToXPathString(arg0) !, baseUri ?? _manager !.Navigator.BaseURI)
                               )
                           ));
            }
        }
Ejemplo n.º 4
0
        public override XPathNavigator?Advance()
        {
            if (_iterator == null)
            {
                _input = qyInput.Advance();
                if (_input == null)
                {
                    return(null);
                }

                XPathNavigator prev;
                do
                {
                    prev   = _input.Clone();
                    _input = qyInput.Advance();
                } while (prev.IsDescendant(_input));
                _input = prev;

                _iterator = XPathEmptyIterator.Instance;
            }

            while (!_iterator.MoveNext())
            {
                bool matchSelf;
                if (_input !.NodeType == XPathNodeType.Attribute || _input.NodeType == XPathNodeType.Namespace)
                {
                    _input.MoveToParent();
                    matchSelf = false;
                }
Ejemplo n.º 5
0
 private static object SimplifyValue(object value)
 {
     // If result of xsl:number is not in correct range it should be returned as is.
     // so we need intermidiate string value.
     // If it's already a double we would like to keep it as double.
     // So this function converts to string only if result is nodeset or RTF
     Debug.Assert(!(value is int));
     if (Type.GetTypeCode(value.GetType()) == TypeCode.Object)
     {
         XPathNodeIterator?nodeset = value as XPathNodeIterator;
         if (nodeset != null)
         {
             if (nodeset.MoveNext())
             {
                 return(nodeset.Current !.Value);
             }
             return(string.Empty);
         }
         XPathNavigator?nav = value as XPathNavigator;
         if (nav != null)
         {
             return(nav.Value);
         }
     }
     return(value);
 }
Ejemplo n.º 6
0
            public override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
            {
                XsltCompileContext xsltCompileContext = (XsltCompileContext)xsltContext;

                string local, prefix;

                PrefixQName.ParseQualifiedName(ToString(args[0]), out prefix, out local);
                string?          ns      = xsltContext.LookupNamespace(prefix);
                XmlQualifiedName keyName = new XmlQualifiedName(local, ns);

                XPathNavigator root = docContext.Clone();

                root.MoveToRoot();

                ArrayList?resultCollection = null;

                foreach (Key key in xsltCompileContext._processor !.KeyList !)
                {
                    if (key.Name == keyName)
                    {
                        Hashtable?keyTable = key.GetKeys(root);
                        if (keyTable == null)
                        {
                            keyTable = xsltCompileContext.BuildKeyTable(key, root);
                            key.AddKey(root, keyTable);
                        }

                        XPathNodeIterator?it = args[1] as XPathNodeIterator;
                        if (it != null)
                        {
                            it = it.Clone();
                            while (it.MoveNext())
                            {
                                resultCollection = AddToList(resultCollection, (ArrayList?)keyTable[it.Current !.Value]);
Ejemplo n.º 7
0
 protected static XPathNodeIterator?Clone(XPathNodeIterator?input)
 {
     if (input != null)
     {
         return(input.Clone());
     }
     return(null);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// This method sets the inherent XPathNodeIterator instance.
 /// </summary>
 /// <param name="xPath">A compiled XPath expression</param>
 private void FindXPath(XPathExpression?xPath)
 {
     xPath?.SetContext(_paramContext);
     if (xPath != null)
     {
         _xIter = _xNav?.Select(xPath);
     }
     InitializeCustomContext(_paramContext);
 }
Ejemplo n.º 9
0
 private void Init(Action?action, ActionFrame?container, XPathNodeIterator?nodeSet)
 {
     _state         = Action.Initialized;
     _action        = action;
     _container     = container;
     _currentAction = 0;
     _nodeSet       = nodeSet;
     _newNodeSet    = null;
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="XPathQueryManager"/> class.
 /// </summary>
 /// <param name="xPathDoc">An XmlDocument instance</param>
 /// <param name="xIter">An XPathNodeIterator instance</param>
 /// <param name="paramContext">A <see cref="XPathQueryManager.CustomQueryContext"/> instance for parameterized XPath expressions</param>
 private XPathQueryManager(XPathDocument?xPathDoc, XPathNodeIterator?xIter, CustomQueryContext?paramContext)
     : this(xPathDoc)
 {
     if (xIter != null)
     {
         _xNav = xIter.Current;
     }
     InitializeCustomContext(paramContext);
 }
Ejemplo n.º 11
0
        public override XPathNavigator?Advance()
        {
            if (_workIterator == null)
            {
                XPathNavigator last;
                {
                    XPathNavigator?input = qyInput.Advance();
                    if (input == null)
                    {
                        return(null);
                    }
                    last = input.Clone();
                    do
                    {
                        last.MoveTo(input);
                    } while ((input = qyInput.Advance()) != null);

                    if (last.NodeType == XPathNodeType.Attribute || last.NodeType == XPathNodeType.Namespace)
                    {
                        last.MoveToParent();
                    }
                }
                // Fill ancestorStk :
                do
                {
                    _ancestorStk.Push(last.Clone());
                } while (last.MoveToParent());
                // Create workIterator :
                // last.MoveToRoot(); We are on root already
                _workIterator = last.SelectDescendants(XPathNodeType.All, true);
            }

            while (_workIterator.MoveNext())
            {
                currentNode = _workIterator.Current !;
                if (currentNode.IsSamePosition(_ancestorStk.Peek()))
                {
                    _ancestorStk.Pop();
                    if (_ancestorStk.Count == 0)
                    {
                        currentNode   = null;
                        _workIterator = null;
                        Debug.Assert(qyInput.Advance() == null, "we read all qyInput.Advance() already");
                        return(null);
                    }
                    continue;
                }
                if (matches(currentNode))
                {
                    position++;
                    return(currentNode);
                }
            }
            Debug.Fail("Algorithm error: we missed the sentinel node");
            return(null);
        }
Ejemplo n.º 12
0
            // static helper methods:
            public static XPathNodeIterator ToIterator(object argument)
            {
                XPathNodeIterator?it = argument as XPathNodeIterator;

                if (it == null)
                {
                    throw XsltException.Create(SR.Xslt_NoNodeSetConversion);
                }
                return(it);
            }
Ejemplo n.º 13
0
    /// <summary>
    ///     Selects a list of XmlNode matching an XPath expression.
    /// </summary>
    /// <param name="source">A source XmlNode.</param>
    /// <param name="expression">An XPath expression.</param>
    /// <param name="variables">A set of XPathVariables.</param>
    /// <returns>The list of XmlNode matching the XPath expression.</returns>
    /// <remarks>
    ///     <para>
    ///         If
    ///         <param name="variables" />
    ///         is <c>null</c>, or is empty, or contains only one single
    ///         value which itself is <c>null</c>, then variables are ignored.
    ///     </para>
    ///     <para>The XPath expression should reference variables as <c>$var</c>.</para>
    /// </remarks>
    public static XmlNodeList?SelectNodes(this XmlNode source, string?expression, params XPathVariable[]?variables)
    {
        if (variables == null || variables.Length == 0 || variables[0] == null)
        {
            return(source.SelectNodes(expression ?? string.Empty));
        }

        XPathNodeIterator?iterator = source.CreateNavigator()?.Select(expression ?? string.Empty, variables);

        return(XmlNodeListFactory.CreateNodeList(iterator));
    }
Ejemplo n.º 14
0
            public static string?ToString(object argument)
            {
                XPathNodeIterator?it = argument as XPathNodeIterator;

                if (it != null)
                {
                    return(IteratorToString(it));
                }
                else
                {
                    return(XmlConvert.ToXPathString(argument) !);
                }
            }
Ejemplo n.º 15
0
        internal object RunQuery(ActionFrame context, int key)
        {
            Query             query = GetCompiledQuery(key);
            object            value = query.Evaluate(context.NodeSet !);
            XPathNodeIterator?it    = value as XPathNodeIterator;

            if (it != null)
            {
                return(new XPathArrayIterator(it));
            }

            return(value);
        }
Ejemplo n.º 16
0
            public static double ToNumber(object argument)
            {
                XPathNodeIterator?it = argument as XPathNodeIterator;

                if (it != null)
                {
                    return(XmlConvert.ToXPathDouble(IteratorToString(it)));
                }
                XPathNavigator?nav = argument as XPathNavigator;

                if (nav != null)
                {
                    return(XmlConvert.ToXPathDouble(nav.ToString()));
                }
                return(XmlConvert.ToXPathDouble(argument));
            }
Ejemplo n.º 17
0
        public object Evaluate <T>(XNode node, string expression, IXmlNamespaceResolver?resolver) where T : class
        {
            XPathNavigator    navigator = node.CreateNavigator();
            object            result    = navigator.Evaluate(expression, resolver);
            XPathNodeIterator?iterator  = result as XPathNodeIterator;

            if (iterator != null)
            {
                return(EvaluateIterator <T>(iterator));
            }
            if (!(result is T))
            {
                throw new InvalidOperationException(SR.Format(SR.InvalidOperation_UnexpectedEvaluation, result.GetType()));
            }
            return((T)result);
        }
Ejemplo n.º 18
0
            public static bool ToBoolean(object argument)
            {
                XPathNodeIterator?it = argument as XPathNodeIterator;

                if (it != null)
                {
                    return(Convert.ToBoolean(IteratorToString(it), CultureInfo.InvariantCulture));
                }
                XPathNavigator?nav = argument as XPathNavigator;

                if (nav != null)
                {
                    return(Convert.ToBoolean(nav.ToString(), CultureInfo.InvariantCulture));
                }
                return(Convert.ToBoolean(argument, CultureInfo.InvariantCulture));
            }
Ejemplo n.º 19
0
            public virtual bool MoveNext()
            {
                // Delegate to XPathNodeIterator
                if (!_iterationStarted)
                {
                    // Reset iteration to original position
                    _current          = _original.Clone();
                    _iterationStarted = true;
                }

                if (_current == null || !_current.MoveNext())
                {
                    // Iteration complete
                    _current = null;
                    return(false);
                }
                return(true);
            }
Ejemplo n.º 20
0
        public override XPathNavigator?Advance()
        {
            while (true)
            {
                if (_nodeIterator == null)
                {
                    position = 0;
                    XPathNavigator?nav = qyInput.Advance();
                    if (nav == null)
                    {
                        return(null);
                    }
                    if (NameTest)
                    {
                        if (TypeTest == XPathNodeType.ProcessingInstruction)
                        {
                            _nodeIterator = new IteratorFilter(nav.SelectDescendants(TypeTest, matchSelf), Name);
                        }
                        else
                        {
                            _nodeIterator = nav.SelectDescendants(Name, Namespace !, matchSelf);
                        }
                    }
                    else
                    {
                        _nodeIterator = nav.SelectDescendants(TypeTest, matchSelf);
                    }
                }

                if (_nodeIterator.MoveNext())
                {
                    position++;
                    currentNode = _nodeIterator.Current;
                    return(currentNode);
                }
                else
                {
                    _nodeIterator = null;
                }
            }
        }
Ejemplo n.º 21
0
 internal void Init(ActionFrame containerFrame, XPathNodeIterator?nodeSet)
 {
     Init(containerFrame.GetAction(0), containerFrame, nodeSet);
 }
Ejemplo n.º 22
0
 public override void Reset()
 {
     _workIterator = null;
     _ancestorStk.Clear();
     base.Reset();
 }
Ejemplo n.º 23
0
 internal void Init(Action action, XPathNodeIterator?nodeSet)
 {
     Init(action, null, nodeSet);
 }
Ejemplo n.º 24
0
 internal void InitNewNodeSet(XPathNodeIterator nodeSet)
 {
     Debug.Assert(nodeSet != null);
     _newNodeSet = nodeSet;
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Creates an instance of a <see cref="XmlNodeList"/> that allows
 /// enumerating <see cref="XmlNode"/> elements in the iterator.
 /// </summary>
 /// <param name="iterator">The result of a previous node selection
 /// through an <see cref="XPathNavigator"/> query.</param>
 /// <returns>An initialized list ready to be enumerated.</returns>
 /// <remarks>The underlying XML store used to issue the query must be
 /// an object inheriting <see cref="XmlNode"/>, such as
 /// <see cref="XmlDocument"/>.</remarks>
 public static XmlNodeList CreateNodeList(XPathNodeIterator?iterator)
 {
     return(new XmlNodeListIterator(iterator));
 }
Ejemplo n.º 26
0
        internal void PushActionFrame(Action action, XPathNodeIterator?nodeSet)
        {
            ActionFrame frame = PushNewFrame();

            frame.Init(action, nodeSet);
        }
Ejemplo n.º 27
0
        internal void PushActionFrame(ActionFrame container, XPathNodeIterator?nodeSet)
        {
            ActionFrame frame = PushNewFrame();

            frame.Init(container, nodeSet);
        }
Ejemplo n.º 28
0
 public XmlNodeListIterator(XPathNodeIterator?iterator) => _iterator = iterator?.Clone();
Ejemplo n.º 29
0
 /// <summary>
 ///     Creates an instance of a <see cref="XmlNodeList" /> that allows
 ///     enumerating <see cref="XmlNode" /> elements in the iterator.
 /// </summary>
 /// <param name="iterator">
 ///     The result of a previous node selection
 ///     through an <see cref="XPathNavigator" /> query.
 /// </param>
 /// <returns>An initialized list ready to be enumerated.</returns>
 /// <remarks>
 ///     The underlying XML store used to issue the query must be
 ///     an object inheriting <see cref="XmlNode" />, such as
 ///     <see cref="XmlDocument" />.
 /// </remarks>
 public static XmlNodeList CreateNodeList(XPathNodeIterator?iterator) => new XmlNodeListIterator(iterator);
Ejemplo n.º 30
0
 internal void PushTemplateLookup(XPathNodeIterator?nodeSet, XmlQualifiedName?mode, Stylesheet?importsOf)
 {
     Debug.Assert(_templateLookup != null);
     _templateLookup.Initialize(mode, importsOf);
     PushActionFrame(_templateLookup, nodeSet);
 }