public IDictionary <string, string> GetNamespacesInScope(
            XmlNamespaceScope scope)
        {
            IXmlNamespaceResolver nsr = reader as IXmlNamespaceResolver;

            return(nsr.GetNamespacesInScope(scope));
        }
        public XPath2Context(IXmlNamespaceResolver nsManager)
        {
            NameTable        = new NameTable();
            NamespaceManager = new XmlNamespaceManager(NameTable);
            SchemaSet        = new XmlSchemaSet(NameTable);

            if (nsManager != null)
            {
                foreach (KeyValuePair <string, string> ns in nsManager.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml))
                {
                    NamespaceManager.AddNamespace(ns.Key, ns.Value);
                }
            }

            if (NamespaceManager.LookupNamespace("xs") == null)
            {
                NamespaceManager.AddNamespace("xs", XmlReservedNs.NsXs);
            }
            if (NamespaceManager.LookupNamespace("xsi") == null)
            {
                NamespaceManager.AddNamespace("xsi", XmlReservedNs.NsXsi);
            }
            if (NamespaceManager.LookupNamespace("fn") == null)
            {
                NamespaceManager.AddNamespace("fn", XmlReservedNs.NsXQueryFunc);
            }
            if (NamespaceManager.LookupNamespace("local") == null)
            {
                NamespaceManager.AddNamespace("local", XmlReservedNs.NsXQueryLocalFunc);
            }
            if (NamespaceManager.LookupNamespace("wmh") == null)
            {
                NamespaceManager.AddNamespace("wmh", XmlReservedNs.NsWmhExt);
            }
        }
 public IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
 {
     IXmlNamespaceResolver resolver = reader as IXmlNamespaceResolver;
     if (resolver == null)
         throw new NotSupportedException ("The input XmlReader does not implement IXmlNamespaceResolver and thus this validating reader cannot collect in-scope namespaces.");
     return resolver.GetNamespacesInScope (scope);
 }
Esempio n. 4
0
 public IEnumerable <NamespaceDeclaration> GetNamespacePrefixes()
 {
     foreach (var p in source.GetNamespacesInScope(XmlNamespaceScope.All))
     {
         yield return(new NamespaceDeclaration(p.Value, p.Key));
     }
 }
Esempio n. 5
0
        public bool FindStylesheetElement()
        {
            if (!_topLevelReader)
            {
                if (_reader.ReadState != ReadState.Interactive)
                {
                    return(false);
                }
            }

            // The stylesheet may be an embedded stylesheet. If this is the case the reader will be in Interactive state and should be
            // positioned on xsl:stylesheet element (or any preceding whitespace) but there also can be namespaces defined on one
            // of the ancestor nodes. These namespace definitions have to be copied to the xsl:stylesheet element scope. Otherwise it
            // will not be possible to resolve them later and loading the stylesheet will end up with throwing an exception.
            IDictionary <string, string> namespacesInScope = null;

            if (_reader.ReadState == ReadState.Interactive)
            {
                // This may be an embedded stylesheet - store namespaces in scope
                IXmlNamespaceResolver nsResolver = _reader as IXmlNamespaceResolver;
                if (nsResolver != null)
                {
                    namespacesInScope = nsResolver.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);
                }
            }

            while (MoveToNextSibling() && _nodeType == XmlNodeType.Whitespace)
            {
                ;
            }

            // An Element node was reached. Potentially this is xsl:stylesheet instruction.
            if (_nodeType == XmlNodeType.Element)
            {
                // If namespacesInScope is not null then the stylesheet being read is an embedded stylesheet that can have namespaces
                // defined outside of xsl:stylesheet instruction. In this case the namespace definitions collected above have to be added
                // to the element scope.
                if (namespacesInScope != null)
                {
                    foreach (KeyValuePair <string, string> prefixNamespacePair in namespacesInScope)
                    {
                        // The namespace could be redefined on the element we just read. If this is the case scopeManager already has
                        // namespace definition for this prefix and the old definition must not be added to the scope.
                        if (_scopeManager.LookupNamespace(prefixNamespacePair.Key) == null)
                        {
                            string nsAtomizedValue = _atoms.NameTable.Add(prefixNamespacePair.Value);
                            _scopeManager.AddNsDeclaration(prefixNamespacePair.Key, nsAtomizedValue);
                            _ctxInfo.AddNamespace(prefixNamespacePair.Key, nsAtomizedValue);
                        }
                    }
                }

                // return true to indicate that we reached XmlNodeType.Element node - potentially xsl:stylesheet element.
                return(true);
            }

            // return false to indicate that we did not reach XmlNodeType.Element node so it is not a valid stylesheet.
            return(false);
        }
Esempio n. 6
0
        /// <summary>
        /// The resolve namespaces for serializer.
        /// </summary>
        /// <param name="resolver">
        /// The resolver.
        /// </param>
        /// <param name="includeDefaultNamespace">
        /// The include default namespace.
        /// </param>
        /// <returns>
        /// The <see cref="XmlSerializerNamespaces"/>.
        /// </returns>
        public static XmlSerializerNamespaces ResolveNamespacesForSerializer(
            IXmlNamespaceResolver resolver,
            bool includeDefaultNamespace = false)
        {
            if (resolver == null)
            {
                return(null);
            }

            var container = new XmlSerializerNamespaces();

            // копируем словарь сочетаний
            var dictionary = resolver.GetNamespacesInScope(XmlNamespaceScope.All);

            if (dictionary != null)
            {
                // определяем дефолтовый нэймспейс, если его нужно игнорировать
                string skipDefaultNamespace;
                if (includeDefaultNamespace)
                {
                    skipDefaultNamespace = null;
                }
                else
                {
                    if (!dictionary.TryGetValue(string.Empty, out skipDefaultNamespace))
                    {
                        skipDefaultNamespace = null;
                    }
                }

                // копируем словарь
                foreach (var ns in dictionary)
                {
                    // пропускаем стандартные префиксы
                    string prefix = ns.Key;
                    if (string.Compare(prefix, "xml", StringComparison.Ordinal) == 0 || string.Compare(prefix, "xmlns", StringComparison.Ordinal) == 0)
                    {
                        continue;
                    }

                    // пропускаем дефолтовый нэймспейс, если его нужно игнорировать
                    string name = ns.Value;
                    if (skipDefaultNamespace != null && string.Compare(skipDefaultNamespace, name) == 0)
                    {
                        continue;
                    }

                    // добавляем текущее сочетание
                    container.Add(prefix, ns.Value);
                }
            }

            return(container);
        }
        private static void WriteNamespaces(TextWriter writer, IXmlNamespaceResolver nav, string leadIn)
        {
            var namespaces = nav.GetNamespacesInScope(XmlNamespaceScope.Local);

            foreach (var n in namespaces)
            {
                writer.Write(leadIn);
                writer.Write(Constants.Xmlns);
                if (string.IsNullOrWhiteSpace(n.Key) == false)
                {
                    writer.Write(Constants.Colon);
                    writer.Write(n.Key);
                }
                writer.Write(Constants.Space);
                writer.WriteLine(n.Value);
            }
        }
Esempio n. 8
0
        /// <summary>
        /// The retrieve default namespace prefix.
        /// </summary>
        /// <param name="resolver">
        /// The resolver.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string RetrieveDefaultNamespacePrefix(IXmlNamespaceResolver resolver)
        {
            if (resolver == null)
            {
                return(null);
            }

            var str = resolver.LookupNamespace(string.Empty);

            if (string.IsNullOrEmpty(str))
            {
                return(string.Empty);
            }

            var prefix = resolver.LookupPrefix(str);

            if (string.IsNullOrEmpty(prefix))
            {
                var namespacesInScope = resolver.GetNamespacesInScope(XmlNamespaceScope.All);
                if (namespacesInScope == null)
                {
                    return(prefix);
                }

                foreach (var pair in namespacesInScope)
                {
                    var key = pair.Key;
                    if (!string.IsNullOrEmpty(key) && (string.Compare(pair.Value, str, StringComparison.Ordinal) == 0))
                    {
                        return(key);
                    }
                }
            }

            return(prefix);
        }
Esempio n. 9
0
 //
 // IXmlNamespaceResolver
 //
 IDictionary <string, string> IXmlNamespaceResolver.GetNamespacesInScope(XmlNamespaceScope scope)
 {
     return(readerAsNSResolver.GetNamespacesInScope(scope));
 }
 internal static XmlNamespaceManager GetNamespaces(IXmlNamespaceResolver resolver)
 {
     XmlNamespaceManager manager = new XmlNamespaceManager(new System.Xml.NameTable());
     foreach (KeyValuePair<string, string> pair in resolver.GetNamespacesInScope(XmlNamespaceScope.All))
     {
         if (pair.Key != "xmlns")
         {
             manager.AddNamespace(pair.Key, pair.Value);
         }
     }
     return manager;
 }
Esempio n. 11
0
        private void Initialize(XmlReader givenXmlReader, XamlSchemaContext schemaContext, XamlXmlReaderSettings settings)
        {
            XmlReader myXmlReader;

            _mergedSettings = (settings == null) ? new XamlXmlReaderSettings() : new XamlXmlReaderSettings(settings);
            //Wrap the xmlreader with a XmlCompatReader instance to apply MarkupCompat rules.
            if (!_mergedSettings.SkipXmlCompatibilityProcessing)
            {
                XmlCompatibilityReader mcReader =
                    new XmlCompatibilityReader(givenXmlReader,
                                               new IsXmlNamespaceSupportedCallback(IsXmlNamespaceSupported)
                                               );
                mcReader.Normalization = true;
                myXmlReader            = mcReader;
            }
            else
            {   //Don't wrap the xmlreader with XmlCompatReader.
                // Useful for uses where users want to keep mc: content in the XamlNode stream.
                // Or have already processed the markup compat and want that extra perf.
                myXmlReader = givenXmlReader;
            }
            // Pick up the XmlReader settings to override the "settings" defaults.
            if (!String.IsNullOrEmpty(myXmlReader.BaseURI))
            {
                _mergedSettings.BaseUri = new Uri(myXmlReader.BaseURI);
            }
            if (myXmlReader.XmlSpace == XmlSpace.Preserve)
            {
                _mergedSettings.XmlSpacePreserve = true;
            }
            if (!String.IsNullOrEmpty(myXmlReader.XmlLang))
            {
                _mergedSettings.XmlLang = myXmlReader.XmlLang;
            }
            IXmlNamespaceResolver       myXmlReaderNS   = myXmlReader as IXmlNamespaceResolver;
            Dictionary <string, string> xmlnsDictionary = null;

            if (myXmlReaderNS != null)
            {
                IDictionary <string, string> rootNamespaces = myXmlReaderNS.GetNamespacesInScope(XmlNamespaceScope.Local);
                if (rootNamespaces != null)
                {
                    foreach (KeyValuePair <string, string> ns in rootNamespaces)
                    {
                        if (xmlnsDictionary == null)
                        {
                            xmlnsDictionary = new Dictionary <string, string>();
                        }
                        xmlnsDictionary[ns.Key] = ns.Value;
                    }
                }
            }

            if (schemaContext == null)
            {
                schemaContext = new XamlSchemaContext();
            }

            _endOfStreamNode = new XamlNode(XamlNode.InternalNodeType.EndOfStream);

            _context = new XamlParserContext(schemaContext, _mergedSettings.LocalAssembly);
            _context.AllowProtectedMembersOnRoot = _mergedSettings.AllowProtectedMembersOnRoot;
            _context.AddNamespacePrefix(KnownStrings.XmlPrefix, XamlLanguage.Xml1998Namespace);

            Func <string, string> namespaceResolver = myXmlReader.LookupNamespace;

            _context.XmlNamespaceResolver = namespaceResolver;

            XamlScanner    xamlScanner = new XamlScanner(_context, myXmlReader, _mergedSettings);
            XamlPullParser parser      = new XamlPullParser(_context, xamlScanner, _mergedSettings);

            _nodeStream      = new NodeStreamSorter(_context, parser, _mergedSettings, xmlnsDictionary);
            _current         = new XamlNode(XamlNode.InternalNodeType.StartOfStream); // user must call Read() before using properties.
            _currentLineInfo = new LineInfo(0, 0);
        }
Esempio n. 12
0
 IDictionary <string, string> IXmlNamespaceResolver.GetNamespacesInScope(XmlNamespaceScope scope)
 {
     return(nsResolver != null?nsResolver.GetNamespacesInScope(scope) :
                new Dictionary <string, string> ());
 }
Esempio n. 13
0
 IDictionary <string, string> IXmlNamespaceResolver.GetNamespacesInScope(XmlNamespaceScope scope)
 {
     return((_readerAsResolver == null) ? null : _readerAsResolver.GetNamespacesInScope(scope));
 }
Esempio n. 14
0
 public IDictionary <string, string> GetNamespacesInScope(XmlNamespaceScope scope) =>
 _baseNamespaceResolver?.GetNamespacesInScope(scope);
        private static void WriteNamespaces(TextWriter writer, IXmlNamespaceResolver nav, string leadIn)
        {
            var namespaces = nav.GetNamespacesInScope(XmlNamespaceScope.Local);

            foreach (var n in namespaces)
            {
                writer.Write(leadIn);
                writer.Write(Constants.Xmlns);
                if (string.IsNullOrWhiteSpace(n.Key) == false)
                {
                    writer.Write(Constants.Colon);
                    writer.Write(n.Key);
                }
                writer.Write(Constants.Space);
                writer.WriteLine(n.Value);
            }
        }
 // Internal IXmlNamespaceResolver methods
 internal IDictionary <string, string> GetNamespacesInScope(XmlNamespaceScope scope)
 {
     return(coreReaderNSResolver.GetNamespacesInScope(scope));
 }
Esempio n. 17
0
 IDictionary IXmlNamespaceResolver.GetNamespacesInScope(XmlNamespaceScope scope)
 {
     return(nsResolver != null?nsResolver.GetNamespacesInScope(scope) : new Hashtable());
 }
Esempio n. 18
0
 internal static XmlNamespaceManager GetNamespaces( IXmlNamespaceResolver resolver ) {
     XmlNamespaceManager mngr = new XmlNamespaceManager(new NameTable());
     IDictionary<string,string> dictionary = resolver.GetNamespacesInScope( XmlNamespaceScope.All );
     foreach ( KeyValuePair<string,string> pair in dictionary ) {
         //"xmlns " is always in the namespace manager so adding it would throw an exception
         if( pair.Key != "xmlns" )
             mngr.AddNamespace( pair.Key, pair.Value );
     }
     return mngr;
 }
Esempio n. 19
0
 public IDictionary <string, string> GetNamespacesInScope(XmlNamespaceScope scope) => _other.GetNamespacesInScope(scope);
        private void Initialize(XmlReader givenXmlReader, XamlSchemaContext schemaContext, XamlXmlReaderSettings settings)
        {
            XmlReader reader;

            this._mergedSettings = (settings == null) ? new XamlXmlReaderSettings() : new XamlXmlReaderSettings(settings);
            if (!this._mergedSettings.SkipXmlCompatibilityProcessing)
            {
                XmlCompatibilityReader reader2 = new XmlCompatibilityReader(givenXmlReader, new IsXmlNamespaceSupportedCallback(this.IsXmlNamespaceSupported))
                {
                    Normalization = true
                };
                reader = reader2;
            }
            else
            {
                reader = givenXmlReader;
            }
            if (!string.IsNullOrEmpty(reader.BaseURI))
            {
                this._mergedSettings.BaseUri = new Uri(reader.BaseURI);
            }
            if (reader.XmlSpace == XmlSpace.Preserve)
            {
                this._mergedSettings.XmlSpacePreserve = true;
            }
            if (!string.IsNullOrEmpty(reader.XmlLang))
            {
                this._mergedSettings.XmlLang = reader.XmlLang;
            }
            IXmlNamespaceResolver       resolver        = reader as IXmlNamespaceResolver;
            Dictionary <string, string> xmlnsDictionary = null;

            if (resolver != null)
            {
                IDictionary <string, string> namespacesInScope = resolver.GetNamespacesInScope(XmlNamespaceScope.Local);
                if (namespacesInScope != null)
                {
                    foreach (KeyValuePair <string, string> pair in namespacesInScope)
                    {
                        if (xmlnsDictionary == null)
                        {
                            xmlnsDictionary = new Dictionary <string, string>();
                        }
                        xmlnsDictionary[pair.Key] = pair.Value;
                    }
                }
            }
            if (schemaContext == null)
            {
                schemaContext = new XamlSchemaContext();
            }
            this._endOfStreamNode = new XamlNode(XamlNode.InternalNodeType.EndOfStream);
            this._context         = new XamlParserContext(schemaContext, this._mergedSettings.LocalAssembly);
            this._context.AllowProtectedMembersOnRoot = this._mergedSettings.AllowProtectedMembersOnRoot;
            this._context.AddNamespacePrefix("xml", "http://www.w3.org/XML/1998/namespace");
            Func <string, string> func = new Func <string, string>(reader.LookupNamespace);

            this._context.XmlNamespaceResolver = func;
            XamlScanner    scanner = new XamlScanner(this._context, reader, this._mergedSettings);
            XamlPullParser parser  = new XamlPullParser(this._context, scanner, this._mergedSettings);

            this._nodeStream      = new NodeStreamSorter(this._context, parser, this._mergedSettings, xmlnsDictionary);
            this._current         = new XamlNode(XamlNode.InternalNodeType.StartOfStream);
            this._currentLineInfo = new System.Xaml.LineInfo(0, 0);
        }