Ejemplo n.º 1
0
 public TagReader(AXmlParser tagSoupParser, ITextSource input, bool collapseProperlyNestedElements)
     : base(input)
 {
     this.tagSoupParser = tagSoupParser;
     if (collapseProperlyNestedElements)
         elementNameStack = new Stack<string>();
 }
Ejemplo n.º 2
0
 public TagReader(AXmlParser tagSoupParser, ITextSource input, bool collapseProperlyNestedElements) : base(input)
 {
     this.tagSoupParser = tagSoupParser;
     if (collapseProperlyNestedElements)
     {
         elementNameStack = new Stack <string>();
     }
 }
Ejemplo n.º 3
0
 public static void Run(string fileName)
 {
     var textSource = new StringTextSource(File.ReadAllText(fileName));
     using (var textReader = textSource.CreateReader()) {
         using (var xmlReader = new XmlTextReader(textReader)) {
             Run(xmlReader);
         }
     }
     var doc = new AXmlParser().Parse(textSource);
     using (var xmlReader = doc.CreateReader()) {
         Run(xmlReader);
     }
     var xmlDocument = new XmlDocument();
     xmlDocument.Load(doc.CreateReader());
     xmlDocument.Save(Path.Combine(Program.TempPath, "savedXmlDocument.xml"));
     var xDocument = XDocument.Load(doc.CreateReader());
     xDocument.Save(Path.Combine(Program.TempPath, "savedXDocument.xml"));
 }
Ejemplo n.º 4
0
		public static void Run(string fileName)
		{
			bool includeAttributes = true;
			var textSource = new StringTextSource(File.ReadAllText(fileName));
			using (var textReader = textSource.CreateReader()) {
				using (var xmlReader = new XmlTextReader(textReader)) {
					Run(xmlReader, includeAttributes);
				}
			}
			var doc = new AXmlParser().Parse(textSource);
			using (var xmlReader = doc.CreateReader()) {
				Run(xmlReader, includeAttributes);
			}
			var xmlDocument = new XmlDocument();
			xmlDocument.Load(doc.CreateReader());
			xmlDocument.Save(Path.Combine(Program.TempPath, "savedXmlDocument.xml"));
			var xDocument = XDocument.Load(doc.CreateReader());
			xDocument.Save(Path.Combine(Program.TempPath, "savedXDocument.xml"));
			File.WriteAllText(Path.Combine(Program.TempPath, "inputDocument.xml"), textSource.Text);
		}
Ejemplo n.º 5
0
		public ParseInformation Parse(FileName fileName, ITextSource fileContent, bool fullParseInformationRequested,
		                              IProject parentProject, CancellationToken cancellationToken)
		{
			AXmlParser parser = new AXmlParser();
			AXmlDocument document;
			IncrementalParserState newParserState;
			if (fileContent.Version is OnDiskTextSourceVersion) {
				document = parser.Parse(fileContent, cancellationToken);
				newParserState = null;
			} else {
				document = parser.ParseIncremental(parserState, fileContent, out newParserState, cancellationToken);
			}
			parserState = newParserState;
			XamlUnresolvedFile unresolvedFile = XamlUnresolvedFile.Create(fileName, fileContent, document);
			ParseInformation parseInfo;
			if (fullParseInformationRequested)
				parseInfo = new XamlFullParseInformation(unresolvedFile, document, fileContent.CreateSnapshot());
			else
				parseInfo = new ParseInformation(unresolvedFile, fileContent.Version, false);
			AddTagComments(document, parseInfo, fileContent);
			return parseInfo;
		}
Ejemplo n.º 6
0
        static List<XmlDocumentationElement> CreateElements(IEnumerable<AXmlObject> childObjects, IEntity declaringEntity, Func<string, IEntity> crefResolver, int nestingLevel)
        {
            List<XmlDocumentationElement> list = new List<XmlDocumentationElement>();
            foreach (var child in childObjects) {
                var childText = child as AXmlText;
                var childElement = child as AXmlElement;
                if (childText != null) {
                    list.Add(new XmlDocumentationElement(childText.Value, declaringEntity));
                } else if (childElement != null) {
                    if (nestingLevel < 5 && childElement.Name == "inheritdoc") {
                        string cref = childElement.GetAttributeValue("cref");
                        IEntity inheritedFrom = null;
                        DocumentationComment inheritedDocumentation = null;
                        if (cref != null) {
                            inheritedFrom = crefResolver(cref);
                            if (inheritedFrom != null)
                                inheritedDocumentation = inheritedFrom.Documentation;
                        } else {
                            foreach (IMember baseMember in InheritanceHelper.GetBaseMembers((IMember)declaringEntity, includeImplementedInterfaces: true)) {
                                inheritedDocumentation = baseMember.Documentation;
                                if (inheritedDocumentation != null) {
                                    inheritedFrom = baseMember;
                                    break;
                                }
                            }
                        }

                        if (inheritedDocumentation != null) {
                            var doc = new AXmlParser().Parse(inheritedDocumentation.Xml);

                            // XPath filter not yet implemented
                            if (childElement.Parent is AXmlDocument && childElement.GetAttributeValue("select") == null) {
                                // Inheriting documentation at the root level
                                List<string> doNotInherit = new List<string>();
                                doNotInherit.Add("overloads");
                                doNotInherit.AddRange(childObjects.OfType<AXmlElement>().Select(e => e.Name).Intersect(
                                    doNotInheritIfAlreadyPresent));

                                var inheritedChildren = doc.Children.Where(
                                    inheritedObject => {
                                        AXmlElement inheritedElement = inheritedObject as AXmlElement;
                                        return !(inheritedElement != null && doNotInherit.Contains(inheritedElement.Name));
                                    });

                                list.AddRange(CreateElements(inheritedChildren, inheritedFrom, inheritedDocumentation.ResolveCref, nestingLevel + 1));
                            }
                        }
                    } else {
                        list.Add(new XmlDocumentationElement(childElement, declaringEntity, crefResolver) { nestingLevel = nestingLevel });
                    }
                }
            }
            if (list.Count > 0 && list[0].IsTextNode) {
                if (string.IsNullOrWhiteSpace(list[0].textContent))
                    list.RemoveAt(0);
                else
                    list[0].textContent = list[0].textContent.TrimStart();
            }
            if (list.Count > 0 && list[list.Count - 1].IsTextNode) {
                if (string.IsNullOrWhiteSpace(list[list.Count - 1].textContent))
                    list.RemoveAt(list.Count - 1);
                else
                    list[list.Count - 1].textContent = list[list.Count - 1].textContent.TrimEnd();
            }
            return list;
        }
Ejemplo n.º 7
0
 static XmlDocumentationElement Create(DocumentationComment documentationComment, IEntity declaringEntity)
 {
     var doc = new AXmlParser().Parse(documentationComment.Xml);
     return new XmlDocumentationElement(doc, declaringEntity, documentationComment.ResolveCref);
 }
Ejemplo n.º 8
0
        static XmlDocumentationElement Create(DocumentationComment documentationComment, IEntity declaringEntity)
        {
            var doc = new AXmlParser().Parse(documentationComment.Xml);

            return(new XmlDocumentationElement(doc, declaringEntity, documentationComment.ResolveCref));
        }
Ejemplo n.º 9
0
        static List <XmlDocumentationElement> CreateElements(IEnumerable <AXmlObject> childObjects, IEntity declaringEntity, Func <string, IEntity> crefResolver, int nestingLevel)
        {
            List <XmlDocumentationElement> list = new List <XmlDocumentationElement>();

            foreach (var child in childObjects)
            {
                var childText    = child as AXmlText;
                var childTag     = child as AXmlTag;
                var childElement = child as AXmlElement;
                if (childText != null)
                {
                    list.Add(new XmlDocumentationElement(childText.Value, declaringEntity));
                }
                else if (childTag != null && childTag.IsCData)
                {
                    foreach (var text in childTag.Children.OfType <AXmlText>())
                    {
                        list.Add(new XmlDocumentationElement(text.Value, declaringEntity));
                    }
                }
                else if (childElement != null)
                {
                    if (nestingLevel < 5 && childElement.Name == "inheritdoc")
                    {
                        string  cref          = childElement.GetAttributeValue("cref");
                        IEntity inheritedFrom = null;
                        DocumentationComment inheritedDocumentation = null;
                        if (cref != null)
                        {
                            inheritedFrom = crefResolver(cref);
                            if (inheritedFrom != null)
                            {
                                inheritedDocumentation = inheritedFrom.Documentation;
                            }
                        }
                        else
                        {
                            foreach (IMember baseMember in InheritanceHelper.GetBaseMembers((IMember)declaringEntity, includeImplementedInterfaces: true))
                            {
                                inheritedDocumentation = baseMember.Documentation;
                                if (inheritedDocumentation != null)
                                {
                                    inheritedFrom = baseMember;
                                    break;
                                }
                            }
                        }

                        if (inheritedDocumentation != null)
                        {
                            var doc = new AXmlParser().Parse(inheritedDocumentation.Xml);

                            // XPath filter not yet implemented
                            if (childElement.Parent is AXmlDocument && childElement.GetAttributeValue("select") == null)
                            {
                                // Inheriting documentation at the root level
                                List <string> doNotInherit = new List <string>();
                                doNotInherit.Add("overloads");
                                doNotInherit.AddRange(childObjects.OfType <AXmlElement>().Select(e => e.Name).Intersect(
                                                          doNotInheritIfAlreadyPresent));

                                var inheritedChildren = doc.Children.Where(
                                    inheritedObject => {
                                    AXmlElement inheritedElement = inheritedObject as AXmlElement;
                                    return(!(inheritedElement != null && doNotInherit.Contains(inheritedElement.Name)));
                                });

                                list.AddRange(CreateElements(inheritedChildren, inheritedFrom, inheritedDocumentation.ResolveCref, nestingLevel + 1));
                            }
                        }
                    }
                    else
                    {
                        list.Add(new XmlDocumentationElement(childElement, declaringEntity, crefResolver)
                        {
                            nestingLevel = nestingLevel
                        });
                    }
                }
            }
            if (list.Count > 0 && list[0].IsTextNode)
            {
                if (string.IsNullOrWhiteSpace(list[0].textContent))
                {
                    list.RemoveAt(0);
                }
                else
                {
                    list[0].textContent = list[0].textContent.TrimStart();
                }
            }
            if (list.Count > 0 && list[list.Count - 1].IsTextNode)
            {
                if (string.IsNullOrWhiteSpace(list[list.Count - 1].textContent))
                {
                    list.RemoveAt(list.Count - 1);
                }
                else
                {
                    list[list.Count - 1].textContent = list[list.Count - 1].textContent.TrimEnd();
                }
            }
            return(list);
        }
Ejemplo n.º 10
0
        public static void Run(ITextSource originalXmlFile)
        {
            int seed;
            lock (sharedRnd) {
                seed = sharedRnd.Next();
            }
            Console.WriteLine(seed);
            Random rnd = new Random(seed);

            AXmlParser parser = new AXmlParser();
            StringBuilder b = new StringBuilder(originalXmlFile.Text);
            IncrementalParserState parserState = null;
            var versionProvider = new TextSourceVersionProvider();
            int totalCharactersParsed = 0;
            int totalCharactersChanged = originalXmlFile.TextLength;
            TimeSpan incrementalParseTime = TimeSpan.Zero;
            TimeSpan nonIncrementalParseTime = TimeSpan.Zero;
            Stopwatch w = new Stopwatch();
            for (int iteration = 0; iteration < 100; iteration++) {
                totalCharactersParsed += b.Length;
                var textSource = new StringTextSource(b.ToString(), versionProvider.CurrentVersion);
                w.Restart();
                var incrementalResult = parser.ParseIncremental(parserState, textSource, out parserState);
                w.Stop();
                incrementalParseTime += w.Elapsed;
                w.Restart();
                var nonIncrementalResult = parser.Parse(textSource);
                w.Stop();
                nonIncrementalParseTime += w.Elapsed;
                CompareResults(incrementalResult, nonIncrementalResult);

                incrementalResult.AcceptVisitor(new ValidationVisitor(textSource));

                // Randomly mutate the file:

                List<TextChangeEventArgs> changes = new List<TextChangeEventArgs>();
                int modifications = rnd.Next(0, 25);
                int offset = 0;
                for (int i = 0; i < modifications; i++) {
                    if (i == 0 || rnd.Next(0, 10) == 0)
                        offset = rnd.Next(0, b.Length);
                    else
                        offset += rnd.Next(0, Math.Min(10, b.Length - offset));
                    int originalOffset = rnd.Next(0, originalXmlFile.TextLength);
                    int insertionLength;
                    int removalLength;
                    switch (rnd.Next(0, 21) / 10) {
                        case 0:
                            removalLength = 0;
                            insertionLength = rnd.Next(0, Math.Min(50, originalXmlFile.TextLength - originalOffset));
                            break;
                        case 1:
                            removalLength = rnd.Next(0, Math.Min(20, b.Length - offset));
                            insertionLength = rnd.Next(0, Math.Min(20, originalXmlFile.TextLength - originalOffset));
                            break;
                        default:
                            removalLength = rnd.Next(0, b.Length - offset);
                            insertionLength = rnd.Next(0, originalXmlFile.TextLength - originalOffset);
                            break;
                    }
                    string removedText = b.ToString(offset, removalLength);
                    b.Remove(offset, removalLength);
                    string insertedText = originalXmlFile.GetText(originalOffset, insertionLength);
                    b.Insert(offset, insertedText);
                    versionProvider.AppendChange(new TextChangeEventArgs(offset, removedText, insertedText));
                    totalCharactersChanged += insertionLength;
                }
            }
            Console.WriteLine("Incremental parse time:     " + incrementalParseTime + " for " + totalCharactersChanged + " characters changed");
            Console.WriteLine("Non-Incremental parse time: " + nonIncrementalParseTime + " for " + totalCharactersParsed + " characters");
        }