/// <summary>
 ///   Adds the contents of another <see cref='QualifiedNameCollection'/> to the end of the collection.
 /// </summary>
 /// <param name='val'>
 ///    A <see cref='QualifiedNameCollection'/> containing the objects to add to the collection.
 /// </param>
 /// <seealso cref='QualifiedNameCollection.Add'/>
 public void AddRange(QualifiedNameCollection val)
 {
     for (int i = 0; i < val.Count; i++)
     {
         this.Add(val[i]);
     }
 }
Пример #2
0
        /// <summary>
        /// Gets the parent element path based on the index position.
        /// </summary>
        public static XmlElementPath GetParentElementPath(string xml)
        {
            QualifiedNameCollection namespaces = new QualifiedNameCollection();
            XmlElementPath          path       = GetFullParentElementPath(xml, namespaces);

            path.Compact();
            return(path);
        }
Пример #3
0
 /// <summary>
 /// Finds the namespace for the specified prefix.
 /// </summary>
 private static string GetNamespaceForPrefix(QualifiedNameCollection namespaces, string prefix)
 {
     foreach (QualifiedName name in namespaces)
     {
         if (name.Prefix == prefix)
         {
             return(name.Namespace);
         }
     }
     return(String.Empty);
 }
Пример #4
0
        /// <summary>
        /// Gets the name of the attribute and its prefix at the specified index. The index
        /// can be anywhere inside the attribute name or in the attribute value.
        /// The namespace for the element containing the attribute will also be determined
        /// if the includeNamespace flag is set to true.
        /// </summary>
        public static QualifiedName GetQualifiedAttributeNameAtIndex(string xml, int index, bool includeNamespace)
        {
            string        name          = GetAttributeNameAtIndex(xml, index);
            QualifiedName qualifiedName = GetQualifiedName(name);

            if (qualifiedName != null && String.IsNullOrEmpty(qualifiedName.Namespace) && includeNamespace)
            {
                QualifiedNameCollection namespaces = new QualifiedNameCollection();
                XmlElementPath          path       = GetActiveElementStartPathAtIndex(xml, index, namespaces);
                qualifiedName.Namespace = GetNamespaceForPrefix(namespaces, path.Elements.LastPrefix);
            }
            return(qualifiedName);
        }
Пример #5
0
        /// <summary>
        /// Gets the parent element path based on the index position. This
        /// method does not compact the path so it will include all elements
        /// including those in another namespace in the path.
        /// </summary>
        private static XmlElementPath GetFullParentElementPath(string xml, QualifiedNameCollection namespaces)
        {
            XmlElementPath path = new XmlElementPath();
            IDictionary <string, string> namespacesInScope = null;

            using (StringReader reader = new StringReader(xml))
            {
                using (XmlTextReader xmlReader = new XmlTextReader(reader))
                {
                    try
                    {
                        xmlReader.XmlResolver = null; // prevent XmlTextReader from loading external DTDs
                        while (xmlReader.Read())
                        {
                            switch (xmlReader.NodeType)
                            {
                            case XmlNodeType.Element:
                                if (!xmlReader.IsEmptyElement)
                                {
                                    QualifiedName elementName = new QualifiedName(xmlReader.LocalName, xmlReader.NamespaceURI, xmlReader.Prefix);
                                    path.Elements.Add(elementName);
                                }
                                break;

                            case XmlNodeType.EndElement:
                                path.Elements.RemoveLast();
                                break;
                            }
                        }
                    }
                    catch (XmlException)
                    {
                        namespacesInScope = xmlReader.GetNamespacesInScope(XmlNamespaceScope.All);
                    }
                }
            }

            // Add namespaces in scope for the last element read.
            if (namespacesInScope != null)
            {
                foreach (KeyValuePair <string, string> ns in namespacesInScope)
                {
                    namespaces.Add(new QualifiedName(String.Empty, ns.Value, ns.Key));
                }
            }

            return(path);
        }
Пример #6
0
        /// <summary>
        /// Gets the active element path given the element text.
        /// </summary>
        private static XmlElementPath GetActiveElementStartPath(string xml, int index, string elementText, QualifiedNameCollection namespaces)
        {
            QualifiedName elementName = GetElementName(elementText);

            if (elementName == null)
            {
                return(new XmlElementPath());
            }

            NamespaceURI elementNamespace = GetElementNamespace(elementText);

            XmlElementPath path = GetFullParentElementPath(xml.Substring(0, index), namespaces);

            // Try to get a namespace for the active element's prefix.
            if (elementName.Prefix.Length > 0 && elementNamespace.Namespace.Length == 0)
            {
                elementName.Namespace      = GetNamespaceForPrefix(namespaces, elementName.Prefix);
                elementNamespace.Namespace = elementName.Namespace;
                elementNamespace.Prefix    = elementName.Prefix;
            }

            if (elementNamespace.Namespace.Length == 0)
            {
                if (path.Elements.Count > 0)
                {
                    QualifiedName parentName = path.Elements[path.Elements.Count - 1];
                    elementNamespace.Namespace = parentName.Namespace;
                    elementNamespace.Prefix    = parentName.Prefix;
                }
            }
            path.Elements.Add(new QualifiedName(elementName.Name, elementNamespace.Namespace, elementNamespace.Prefix));
            path.Compact();
            return(path);
        }
Пример #7
0
 /// <summary>
 /// Gets the name of the attribute and its prefix at the specified index. The index
 /// can be anywhere inside the attribute name or in the attribute value.
 /// The namespace for the element containing the attribute will also be determined
 /// if the includeNamespace flag is set to true.
 /// </summary>
 public static QualifiedName GetQualifiedAttributeNameAtIndex(string xml, int index, bool includeNamespace)
 {
     string name = GetAttributeNameAtIndex(xml, index);
     QualifiedName qualifiedName = GetQualifiedName(name);
     if (qualifiedName != null && String.IsNullOrEmpty(qualifiedName.Namespace) && includeNamespace)
     {
         QualifiedNameCollection namespaces = new QualifiedNameCollection();
         XmlElementPath path = GetActiveElementStartPathAtIndex(xml, index, namespaces);
         qualifiedName.Namespace = GetNamespaceForPrefix(namespaces, path.Elements.LastPrefix);
     }
     return qualifiedName;
 }
Пример #8
0
 /// <summary>
 ///   Initializes a new instance of <see cref='QualifiedNameEnumerator'/>.
 /// </summary>
 public QualifiedNameEnumerator(QualifiedNameCollection mappings)
 {
     this.temp = ((IEnumerable)(mappings));
     this.baseEnumerator = temp.GetEnumerator();
 }
Пример #9
0
        /// <summary>
        /// Gets path of the xml element start tag that the specified
        /// <paramref name="index"/> is currently located. This is different to the
        /// GetActiveElementStartPath method since the index can be inside the element
        /// name.
        /// </summary>
        /// <remarks>If the index outside the start tag then an empty path
        /// is returned.</remarks>
        private static XmlElementPath GetActiveElementStartPathAtIndex(string xml, int index, QualifiedNameCollection namespaces)
        {
            // Find first non xml element name character to the right of the index.
            index = GetCorrectedIndex(xml.Length, index);
            if (index < 0) // can happen when xml.Length==0
            {
                return(new XmlElementPath());
            }
            int currentIndex = index;

            for (; currentIndex < xml.Length; ++currentIndex)
            {
                char ch = xml[currentIndex];
                if (!IsXmlNameChar(ch))
                {
                    break;
                }
            }

            string elementText = GetElementNameAtIndex(xml, currentIndex);

            if (elementText != null)
            {
                return(GetActiveElementStartPath(xml, currentIndex, elementText, namespaces));
            }
            return(new XmlElementPath());
        }
Пример #10
0
 /// <summary>
 /// Gets path of the xml element start tag that the specified
 /// <paramref name="index"/> is currently located. This is different to the
 /// GetActiveElementStartPath method since the index can be inside the element
 /// name.
 /// </summary>
 /// <remarks>If the index outside the start tag then an empty path
 /// is returned.</remarks>
 public static XmlElementPath GetActiveElementStartPathAtIndex(string xml, int index)
 {
     QualifiedNameCollection namespaces = new QualifiedNameCollection();
     return GetActiveElementStartPathAtIndex(xml, index, namespaces);
 }
Пример #11
0
 /// <summary>
 /// Finds the namespace for the specified prefix.
 /// </summary>
 static string GetNamespaceForPrefix(QualifiedNameCollection namespaces, string prefix)
 {
     foreach (QualifiedName name in namespaces)
     {
         if (name.Prefix == prefix)
         {
             return name.Namespace;
         }
     }
     return String.Empty;
 }
Пример #12
0
        /// <summary>
        /// Gets the parent element path based on the index position. This
        /// method does not compact the path so it will include all elements
        /// including those in another namespace in the path.
        /// </summary>
        static XmlElementPath GetFullParentElementPath(string xml, QualifiedNameCollection namespaces)
        {
            XmlElementPath path = new XmlElementPath();
            IDictionary<string, string> namespacesInScope = null;
            using (StringReader reader = new StringReader(xml))
            {
                using (XmlTextReader xmlReader = new XmlTextReader(reader))
                {
                    try
                    {
                        xmlReader.XmlResolver = null; // prevent XmlTextReader from loading external DTDs
                        while (xmlReader.Read())
                        {
                            switch (xmlReader.NodeType)
                            {
                                case XmlNodeType.Element:
                                    if (!xmlReader.IsEmptyElement)
                                    {
                                        QualifiedName elementName = new QualifiedName(xmlReader.LocalName, xmlReader.NamespaceURI, xmlReader.Prefix);
                                        path.Elements.Add(elementName);
                                    }
                                    break;
                                case XmlNodeType.EndElement:
                                    path.Elements.RemoveLast();
                                    break;
                            }
                        }
                    }
                    catch (XmlException)
                    {
                        namespacesInScope = xmlReader.GetNamespacesInScope(XmlNamespaceScope.All);
                    }
                }
            }

            // Add namespaces in scope for the last element read.
            if (namespacesInScope != null)
            {
                foreach (KeyValuePair<string, string> ns in namespacesInScope)
                {
                    namespaces.Add(new QualifiedName(String.Empty, ns.Value, ns.Key));
                }
            }

            return path;
        }
Пример #13
0
        /// <summary>
        /// Gets path of the xml element start tag that the specified
        /// <paramref name="index"/> is currently located. This is different to the
        /// GetActiveElementStartPath method since the index can be inside the element
        /// name.
        /// </summary>
        /// <remarks>If the index outside the start tag then an empty path
        /// is returned.</remarks>
        static XmlElementPath GetActiveElementStartPathAtIndex(string xml, int index, QualifiedNameCollection namespaces)
        {
            // Find first non xml element name character to the right of the index.
            index = GetCorrectedIndex(xml.Length, index);
            if (index < 0) // can happen when xml.Length==0
                return new XmlElementPath();
            int currentIndex = index;
            for (; currentIndex < xml.Length; ++currentIndex)
            {
                char ch = xml[currentIndex];
                if (!IsXmlNameChar(ch))
                {
                    break;
                }
            }

            string elementText = GetElementNameAtIndex(xml, currentIndex);
            if (elementText != null)
            {
                return GetActiveElementStartPath(xml, currentIndex, elementText, namespaces);
            }
            return new XmlElementPath();
        }
Пример #14
0
 /// <summary>
 /// Gets path of the xml element start tag that the specified
 /// <paramref name="index"/> is currently inside.
 /// </summary>
 /// <remarks>If the index outside the start tag then an empty path
 /// is returned.</remarks>
 /// <param name="index"></param>
 /// <param name="xml"></param>
 /// <param name="namespaces">Returns the namespaces that are
 /// exist in the xml.</param>
 static XmlElementPath GetActiveElementStartPath(string xml, int index, QualifiedNameCollection namespaces)
 {
     XmlElementPath path = new XmlElementPath();
     string elementText = GetActiveElementStartText(xml, index);
     if (elementText != null)
     {
         path = GetActiveElementStartPath(xml, index, elementText, namespaces);
     }
     return path;
 }
Пример #15
0
        /// <summary>
        /// Gets the active element path given the element text.
        /// </summary>
        static XmlElementPath GetActiveElementStartPath(string xml, int index, string elementText, QualifiedNameCollection namespaces)
        {
            QualifiedName elementName = GetElementName(elementText);
            if (elementName == null)
            {
                return new XmlElementPath();
            }

            NamespaceURI elementNamespace = GetElementNamespace(elementText);

            XmlElementPath path = GetFullParentElementPath(xml.Substring(0, index), namespaces);

            // Try to get a namespace for the active element's prefix.
            if (elementName.Prefix.Length > 0 && elementNamespace.Namespace.Length == 0)
            {
                elementName.Namespace = GetNamespaceForPrefix(namespaces, elementName.Prefix);
                elementNamespace.Namespace = elementName.Namespace;
                elementNamespace.Prefix = elementName.Prefix;
            }

            if (elementNamespace.Namespace.Length == 0)
            {
                if (path.Elements.Count > 0)
                {
                    QualifiedName parentName = path.Elements[path.Elements.Count - 1];
                    elementNamespace.Namespace = parentName.Namespace;
                    elementNamespace.Prefix = parentName.Prefix;
                }
            }
            path.Elements.Add(new QualifiedName(elementName.Name, elementNamespace.Namespace, elementNamespace.Prefix));
            path.Compact();
            return path;
        }
 /// <summary>
 ///   Initializes a new instance of <see cref='QualifiedNameEnumerator'/>.
 /// </summary>
 public QualifiedNameEnumerator(QualifiedNameCollection mappings)
 {
     this.temp           = ((IEnumerable)(mappings));
     this.baseEnumerator = temp.GetEnumerator();
 }
Пример #17
0
        /// <summary>
        /// Gets path of the xml element start tag that the specified
        /// <paramref name="index"/> is currently inside.
        /// </summary>
        /// <remarks>If the index outside the start tag then an empty path
        /// is returned.</remarks>
        /// <param name="index"></param>
        /// <param name="xml"></param>
        /// <param name="namespaces">Returns the namespaces that are
        /// exist in the xml.</param>
        private static XmlElementPath GetActiveElementStartPath(string xml, int index, QualifiedNameCollection namespaces)
        {
            XmlElementPath path        = new XmlElementPath();
            string         elementText = GetActiveElementStartText(xml, index);

            if (elementText != null)
            {
                path = GetActiveElementStartPath(xml, index, elementText, namespaces);
            }
            return(path);
        }
 /// <summary>
 ///   Initializes a new instance of <see cref='QualifiedNameCollection'/> based on another <see cref='QualifiedNameCollection'/>.
 /// </summary>
 /// <param name='val'>
 ///   A <see cref='QualifiedNameCollection'/> from which the contents are copied
 /// </param>
 public QualifiedNameCollection(QualifiedNameCollection val)
 {
     this.AddRange(val);
 }
Пример #19
0
 /// <summary>
 ///   Adds the contents of another <see cref='QualifiedNameCollection'/> to the end of the collection.
 /// </summary>
 /// <param name='val'>
 ///    A <see cref='QualifiedNameCollection'/> containing the objects to add to the collection.
 /// </param>
 /// <seealso cref='QualifiedNameCollection.Add'/>
 public void AddRange(QualifiedNameCollection val)
 {
     for (int i = 0; i < val.Count; i++)
     {
         this.Add(val[i]);
     }
 }
Пример #20
0
        /// <summary>
        /// Gets path of the xml element start tag that the specified
        /// <paramref name="index"/> is currently located. This is different to the
        /// GetActiveElementStartPath method since the index can be inside the element
        /// name.
        /// </summary>
        /// <remarks>If the index outside the start tag then an empty path
        /// is returned.</remarks>
        public static XmlElementPath GetActiveElementStartPathAtIndex(string xml, int index)
        {
            QualifiedNameCollection namespaces = new QualifiedNameCollection();

            return(GetActiveElementStartPathAtIndex(xml, index, namespaces));
        }
Пример #21
0
 /// <summary>
 ///   Initializes a new instance of <see cref='QualifiedNameCollection'/> based on another <see cref='QualifiedNameCollection'/>.
 /// </summary>
 /// <param name='val'>
 ///   A <see cref='QualifiedNameCollection'/> from which the contents are copied
 /// </param>
 public QualifiedNameCollection(QualifiedNameCollection val)
 {
     this.AddRange(val);
 }
Пример #22
0
 /// <summary>
 /// Gets the parent element path based on the index position.
 /// </summary>
 public static XmlElementPath GetParentElementPath(string xml)
 {
     QualifiedNameCollection namespaces = new QualifiedNameCollection();
     XmlElementPath path = GetFullParentElementPath(xml, namespaces);
     path.Compact();
     return path;
 }