Example #1
0
        /// <summary>This helper function constructs XmlReader object
        /// using provided XML source code.</summary>
        /// <param name="xmlSource">Source XML data.</param>
        /// <returns>XmlReader object.</returns>
        public static XmlReader GetXmlReader(string xmlSource)
        {
            if (xmlSource.Contains("<!DOCTYPE html>"))
            {
                const string NewValue = "<!DOCTYPE html PUBLIC " +
                                        "\"-//W3C//DTD XHTML 1.0 Transitional//EN\" " +
                                        "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
                xmlSource = xmlSource.Replace("<!DOCTYPE html>", NewValue);
            }

            if (!xmlSource.Contains("<html xmlns="))
            {
                xmlSource = xmlSource.Replace("<html", "<html xmlns=\"http://www.w3.org/1999/xhtml\"");
            }

            var strReader = new StringReader(xmlSource);
            var settings  = new XmlReaderSettings
            {
                XmlResolver     = new XmlUrlResolverWithCache(),
                CheckCharacters = false,
                IgnoreComments  = true,
                IgnoreProcessingInstructions = true,
                IgnoreWhitespace             = true
            };

            // For .NET 4.0 and higher DtdProcessing property should be used instead of ProhibitDtd
            if (settings.GetType().GetProperty("DtdProcessing") != null)
            {
                Type t = typeof(XmlReaderSettings).GetProperty("DtdProcessing").PropertyType;
                settings.GetType().InvokeMember(
                    "DtdProcessing",
                    BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty,
                    null,
                    settings,
                    new[] { Enum.Parse(t, "2") });    // 2 is a value of DtdProcessing.Parse
            }
            else if (settings.GetType().GetProperty("ProhibitDtd") != null)
            {
                settings.GetType().InvokeMember(
                    "ProhibitDtd",
                    BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty,
                    null,
                    settings,
                    new object[] { false });
            }

            return(XmlReader.Create(strReader, settings));
        }
Example #2
0
        bool LoadFromStream(Stream stream, string filename,
                            ICollection <NatvisVisualizerScanner.FileInfo> typeVisualizers)
        {
            var serializer = new XmlSerializer(typeof(AutoVisualizer));
            var settings   = new XmlReaderSettings
            {
                IgnoreComments = true,
                IgnoreProcessingInstructions = true,
                IgnoreWhitespace             = true
            };

            // set XmlResolver via reflection, if it exists. This is required for desktop CLR, as
            // otherwise the XML reader may attempt to hit untrusted external resources.
            PropertyInfo xmlResolverProperty = settings.GetType().GetProperty("XmlResolver",
                                                                              BindingFlags.Public |
                                                                              BindingFlags
                                                                              .Instance);

            xmlResolverProperty?.SetValue(settings, null);

            using (var reader = XmlReader.Create(stream, settings))
            {
                var autoVis = serializer.Deserialize(reader) as AutoVisualizer;
                if (autoVis?.Items == null)
                {
                    return(false);
                }

                var f = new NatvisVisualizerScanner.FileInfo(autoVis, filename);
                foreach (object o in autoVis.Items)
                {
                    if (o is VisualizerType)
                    {
                        var      v = (VisualizerType)o;
                        TypeName t = TypeName.Parse(v.Name);
                        if (t != null)
                        {
                            f.Visualizers.Add(new NatvisVisualizerScanner.TypeInfo(t, v));
                        }

                        // add an entry for each alternative name too
                        if (v.AlternativeType != null)
                        {
                            foreach (AlternativeTypeType a in v.AlternativeType)
                            {
                                t = TypeName.Parse(a.Name);
                                if (t != null)
                                {
                                    f.Visualizers.Add(new NatvisVisualizerScanner.TypeInfo(t, v));
                                }
                            }
                        }
                    }
                }

                typeVisualizers.Add(f);
                return(true);
            }
        }
Example #3
0
        private bool LoadFile(string path)
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(AutoVisualizer));
                if (!File.Exists(path))
                {
                    _process.WriteOutput(String.Format(CultureInfo.CurrentCulture, ResourceStrings.FileNotFound, path));
                    return(false);
                }
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                settings.IgnoreProcessingInstructions = true;
                settings.IgnoreWhitespace             = true;

                // set XmlResolver via reflection, if it exists. This is required for desktop CLR, as otherwise the XML reader may
                // attempt to hit untrusted external resources.
                var xmlResolverProperty = settings.GetType().GetProperty("XmlResolver", BindingFlags.Public | BindingFlags.Instance);
                xmlResolverProperty?.SetValue(settings, null);

                using (var stream = new System.IO.FileStream(path, FileMode.Open, FileAccess.Read))
                    using (var reader = XmlReader.Create(stream, settings))
                    {
                        AutoVisualizer autoVis = null;
                        autoVis = serializer.Deserialize(reader) as AutoVisualizer;
                        if (autoVis != null)
                        {
                            FileInfo f = new FileInfo(autoVis);
                            if (autoVis.Items == null)
                            {
                                return(false);
                            }
                            foreach (var o in autoVis.Items)
                            {
                                if (o is VisualizerType)
                                {
                                    VisualizerType v = (VisualizerType)o;
                                    TypeName       t = TypeName.Parse(v.Name, _process.Logger);
                                    if (t != null)
                                    {
                                        lock (_typeVisualizers)
                                        {
                                            f.Visualizers.Add(new TypeInfo(t, v));
                                        }
                                    }
                                    // add an entry for each alternative name too
                                    if (v.AlternativeType != null)
                                    {
                                        foreach (var a in v.AlternativeType)
                                        {
                                            t = TypeName.Parse(a.Name, _process.Logger);
                                            if (t != null)
                                            {
                                                lock (_typeVisualizers)
                                                {
                                                    f.Visualizers.Add(new TypeInfo(t, v));
                                                }
                                            }
                                        }
                                    }
                                }
                                else if (o is AliasType)
                                {
                                    AliasType a = (AliasType)o;
                                    TypeName  t = TypeName.Parse(a.Name, _process.Logger);
                                    if (t != null)
                                    {
                                        lock (_typeVisualizers)
                                        {
                                            f.Aliases.Add(new AliasInfo(t, a));
                                        }
                                    }
                                }
                            }
                            _typeVisualizers.Add(f);
                        }
                        return(autoVis != null);
                    }
            }
            catch (Exception exception)
            {
                // don't allow natvis failures to stop debugging
                _process.WriteOutput(String.Format(CultureInfo.CurrentCulture, ResourceStrings.ErrorReadingFile, exception.Message, path));
                return(false);
            }
        }
Example #4
0
 public int v4()
 {
     XmlReaderSettings rs = new XmlReaderSettings();
     XmlReaderSettings crs = rs.Clone();
     CError.Compare(rs.CheckCharacters, crs.CheckCharacters, "CheckCharacters");
     CError.Compare(rs.CloseInput, crs.CloseInput, "CloseInput");
     CError.Compare(rs.DtdProcessing, crs.DtdProcessing, "ProhibitDtd");
     CError.Compare(rs.IgnoreComments, crs.IgnoreComments, "IgnoreComments");
     CError.Compare(rs.IgnoreProcessingInstructions, crs.IgnoreProcessingInstructions, "IgnorePI");
     CError.Compare(rs.IgnoreWhitespace, crs.IgnoreWhitespace, "IgnoreWhitespace");
     CError.Compare(rs.LineNumberOffset, crs.LineNumberOffset, "LineNumberOffset");
     CError.Compare(rs.LinePositionOffset, crs.LinePositionOffset, "LinePositionOffset");
     CError.Compare(rs.MaxCharactersInDocument, crs.MaxCharactersInDocument, "maxcharsindoc");
     CError.Compare(rs.NameTable, crs.NameTable, "NameTable");
     CError.Compare(rs.ConformanceLevel, crs.ConformanceLevel, "ConformanceLevel");
     CError.Compare(rs.GetType(), crs.GetType(), "GetType");
     return TEST_PASS;
 }
Example #5
0
 public int v3()
 {
     string readerType = (string)this.Param;
     XmlReaderSettings rs = new XmlReaderSettings();
     using (XmlReader r = ReaderHelper.CreateReader(readerType, new StringReader("<a/>"), false, null, rs))
     {
         bool cc = r.Settings.CheckCharacters;
         bool closeinput = r.Settings.CloseInput;
         DtdProcessing dtd = r.Settings.DtdProcessing;
         bool ignorecomm = r.Settings.IgnoreComments;
         bool ignorepi = r.Settings.IgnoreProcessingInstructions;
         bool ignorewhtsp = r.Settings.IgnoreWhitespace;
         int lineNumberOffset = r.Settings.LineNumberOffset;
         int linePositionOffset = r.Settings.LinePositionOffset;
         long maxcharsindoc = r.Settings.MaxCharactersInDocument;
         XmlNameTable nameTable = r.Settings.NameTable;
         ConformanceLevel cl = r.Settings.ConformanceLevel;
         Type t = r.Settings.GetType();
         rs.Reset();
         CError.Compare(cc, rs.CheckCharacters, "cc");
         CError.Compare(closeinput, rs.CloseInput, "closeinput");
         CError.Compare(dtd, rs.DtdProcessing, "dtd");
         CError.Compare(ignorecomm, rs.IgnoreComments, "ignorecomm");
         CError.Compare(ignorepi, rs.IgnoreProcessingInstructions, "ignorepi");
         CError.Compare(ignorewhtsp, rs.IgnoreWhitespace, "ignorewhtsp");
         CError.Compare(lineNumberOffset, rs.LineNumberOffset, "lineNumberOffset");
         CError.Compare(linePositionOffset, rs.LinePositionOffset, "linePositionOffset");
         CError.Compare(maxcharsindoc, rs.MaxCharactersInDocument, "maxcharsindoc");
         CError.Compare(nameTable, rs.NameTable, "nameTable");
         CError.Compare(cl, rs.ConformanceLevel, "cl");
         CError.Compare(t, rs.GetType(), "t");
         return TEST_PASS;
     }
 }
Example #6
0
        public static XmlReader OpenXml(string content)
        {
            var settings = new XmlReaderSettings();

            settings.CloseInput     = true;
            settings.IgnoreComments = true;
            settings.IgnoreProcessingInstructions = true;
            settings.IgnoreWhitespace             = true;
            settings.NameTable = new NameTable();

            // set XmlResolver via reflection, if it exists. This is required for desktop CLR, as otherwise the XML reader may
            // attempt to hit untrusted external resources.
            var xmlResolverProperty = settings.GetType().GetProperty("XmlResolver", BindingFlags.Public | BindingFlags.Instance);

            xmlResolverProperty?.SetValue(settings, null);

            // Create our own namespace manager so that we can set the default namespace
            // We need this because the XML serializer requires correct namespaces,
            // but project systems may not provide it.
            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(settings.NameTable);

            namespaceManager.AddNamespace(string.Empty, XmlNamespace);
            XmlParserContext context = new XmlParserContext(settings.NameTable, namespaceManager, string.Empty, XmlSpace.None);

            StringReader stringReader = null;
            XmlReader    reader       = null;
            bool         success      = false;

            try
            {
                stringReader = new StringReader(content);
                reader       = XmlReader.Create(stringReader, settings, context);

                // Read to the top level element
                while (reader.NodeType != XmlNodeType.Element)
                {
                    reader.Read();
                }

                if (reader.NamespaceURI != XmlNamespace)
                {
                    throw new XmlException(string.Format(CultureInfo.CurrentCulture, MICoreResources.Error_UnknownXmlElement, reader.Name));
                }

                success = true;
                return(reader);
            }
            finally
            {
                if (!success)
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                    else if (stringReader != null)
                    {
                        // NOTE: the reader will close the input, so we only want to do this
                        // if we failed to create the reader.
                        stringReader.Dispose();
                    }
                }
            }
        }