Example #1
0
        /// <summary>
        /// Parses the element on which an <see cref="XmlReader"/> is positioned as a CppCheck error node.
        /// </summary>
        /// <param name="reader">The reader from which a CppCheck error shall be read.</param>
        /// <param name="strings">Strings used to parse the CppCheck log.</param>
        /// <returns>
        /// A <see cref="CppCheckError"/> parsed from the XML on which <paramref name="reader"/> is
        /// positioned.
        /// </returns>
        /// <exception cref="XmlException">The xml on which <paramref name="reader"/> is placed is
        /// in an incorrect format.</exception>
        public static CppCheckError Parse(XmlReader reader, CppCheckStrings strings)
        {
            if (!reader.IsStartElement(strings.Error))
            {
                throw reader.CreateException(ConverterResources.CppCheckElementNotError);
            }

            string id = null;
            string message = null;
            string verboseMessage = null;
            string severity = null;
            while (reader.MoveToNextAttribute())
            {
                string attributeName = reader.LocalName;
                if (Ref.Equal(attributeName, strings.Id))
                {
                    id = reader.Value;
                }
                else if (Ref.Equal(attributeName, strings.Msg))
                {
                    message = reader.Value;
                }
                else if (Ref.Equal(attributeName, strings.Verbose))
                {
                    verboseMessage = reader.Value;
                }
                else if (Ref.Equal(attributeName, strings.Severity))
                {
                    severity = reader.Value;
                }
            }

            reader.MoveToElement();
            ImmutableArray<CppCheckLocation> locations = ParseLocationsSubtree(reader, strings);

            reader.Read(); // Consumes the end element or self closing element and positions the reader on the next node

            try
            {
                return new CppCheckError(
                    id,
                    message,
                    verboseMessage,
                    severity,
                    locations
                    );
            }
            catch (ArgumentException ex)
            {
                throw reader.CreateException(ex.Message);
            }
        }
        /// <summary>Parses an element as a Fortify PathElement node, consuming the node.</summary>
        /// <param name="xmlReader">The <see cref="XmlReader"/> from which a node shall be parsed. When
        /// this function returns, this reader is placed directly after the element on which it is
        /// currently placed.</param>
        /// <param name="strings">Strings used in processing Fortify logs.</param>
        /// <returns>A <see cref="FortifyPathElement"/> parsed from the element on which
        /// <paramref name="xmlReader"/> is positioned when this method is called.</returns>
        public static FortifyPathElement Parse(XmlReader xmlReader, FortifyStrings strings)
        {
            //<xs:complexType name="PathElement">
            //    <xs:sequence>
            //        <xs:element name="FileName" type="xs:string" minOccurs="1" maxOccurs="1"/>
            //        <xs:element name="FilePath" type="xs:string" minOccurs="1" maxOccurs="1"/>
            //        <xs:element name="LineStart" type="xs:string" minOccurs="1" maxOccurs="1"/>
            //        <xs:element name="Snippet" type="xs:string" minOccurs="0" maxOccurs="1"/>
            //        <xs:element name="SnippetLine" type="xs:int" minOccurs="0" maxOccurs="1"/>
            //        <xs:element name="TargetFunction" type="xs:string" minOccurs="0" maxOccurs="1"/>
            //    </xs:sequence>
            //</xs:complexType>

            if (xmlReader.NodeType != XmlNodeType.Element || xmlReader.IsEmptyElement)
            {
                throw xmlReader.CreateException(ConverterResources.FortifyNotValidPathElement);
            }

            xmlReader.Read(); // Always true because !IsEmptyElement
            xmlReader.IgnoreElement(strings.FileName, IgnoreOptions.Required);
            string filePath = xmlReader.ReadElementContentAsString(strings.FilePath, String.Empty);
            int lineStart = xmlReader.ReadElementContentAsInt(strings.LineStart, String.Empty);
            xmlReader.IgnoreElement(strings.Snippet, IgnoreOptions.Optional);
            xmlReader.IgnoreElement(strings.SnippetLine, IgnoreOptions.Optional);
            string targetFunction = xmlReader.ReadOptionalElementContentAsString(strings.TargetFunction);
            xmlReader.ReadEndElement();

            try
            {
                return new FortifyPathElement(filePath, lineStart, targetFunction);
            }
            catch (ArgumentException ex)
            {
                throw xmlReader.CreateException(ex.Message);
            }
        }
Example #3
0
        /// <summary>
        /// Parses a Fortify Result element from an <see cref="XmlReader"/>.
        /// </summary>
        /// <param name="xmlReader">The <see cref="XmlReader"/> from which an element containing a Fortify result shall be
        /// consumed. When this method returns, this <see cref="XmlReader"/> is positioned on the following element.</param>
        /// <param name="strings">Strings used in processing a Fortify report.</param>
        /// <returns>A <see cref="FortifyIssue"/> containing data from the node on which <paramref name="xmlReader"/> was
        /// placed when this method was called.</returns>
        public static FortifyIssue Parse(XmlReader xmlReader, FortifyStrings strings)
        {
            //<xs:element name="Result">
            //    <xs:complexType>
            //        <xs:sequence>
            //            <!-- Result Description -->
            //            <xs:element name="Category" type="xs:string" minOccurs="1" maxOccurs="1"/>
            //            <xs:element name="Folder" type="xs:string" minOccurs="1" maxOccurs="1"/>
            //            <xs:element name="Kingdom" type="xs:string" minOccurs="1" maxOccurs="1"/>
            //            <xs:element name="Abstract" type="xs:string" minOccurs="0" maxOccurs="1"/>
            //            <xs:element name="AbstractCustom" type="xs:string" minOccurs="0" maxOccurs="1"/>
            //            <xs:element name="Friority" type="xs:string" minOccurs="0" maxOccurs="1"/>
            //            <!-- custom tags including Analysis -->
            //            <xs:element name="Tag" minOccurs="0" maxOccurs="unbounded">
            //                <xs:complexType>
            //                    <xs:sequence>
            //                        <xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1"/>
            //                        <xs:element name="Value" type="xs:string" minOccurs="1" maxOccurs="1"/>
            //                    </xs:sequence>
            //                </xs:complexType>
            //            </xs:element>
            //            <xs:element name="Comment" minOccurs="0" maxOccurs="unbounded">
            //                <xs:complexType>
            //                    <xs:sequence>
            //                        <xs:element name="UserInfo" type="xs:string" minOccurs="1" maxOccurs="1"/>
            //                        <xs:element name="Comment" type="xs:string" minOccurs="1" maxOccurs="1"/>
            //                    </xs:sequence>
            //                </xs:complexType>
            //            </xs:element>
            //            <!-- primary or sink -->
            //            <xs:element name="Primary" type="PathElement" minOccurs="1" maxOccurs="1"/>
            //            <!-- source -->
            //            <xs:element name="Source" type="PathElement" minOccurs="0" maxOccurs="1"/>
            //            <xs:element name="TraceDiagramPath" type="xs:string" minOccurs="0" maxOccurs="1"/>
            //            <!-- optional external category (i.e. STIG) -->
            //            <xs:element name="ExternalCategory" minOccurs="0" maxOccurs="1">
            //                <xs:complexType>
            //                    <xs:simpleContent>
            //                        <xs:extension base="xs:string">
            //                            <xs:attribute name="type" type="xs:string" use="required"/>
            //                        </xs:extension>
            //                    </xs:simpleContent>
            //                </xs:complexType>
            //            </xs:element>
            //        </xs:sequence>
            //        <xs:attribute name="iid" type="xs:string" use="optional"/>
            //        <xs:attribute name="ruleID" type="xs:string" use="optional"/>
            //    </xs:complexType>
            //</xs:element>
            if (!xmlReader.IsStartElement(strings.Issue))
            {
                throw xmlReader.CreateException(ConverterResources.FortifyNotValidResult);
            }

            string iid = null;
            string ruleId = null;
            while (xmlReader.MoveToNextAttribute())
            {
                string name = xmlReader.LocalName;
                if (Ref.Equal(name, strings.Iid))
                {
                    iid = xmlReader.Value;
                }
                else if (Ref.Equal(name, strings.RuleId))
                {
                    ruleId = xmlReader.Value;
                }
            }

            xmlReader.MoveToElement();
            xmlReader.Read(); // reads start element

            string category = xmlReader.ReadElementContentAsString(strings.Category, String.Empty);
            xmlReader.IgnoreElement(strings.Folder, IgnoreOptions.Required);
            string kingdom = xmlReader.ReadElementContentAsString(strings.Kingdom, String.Empty);
            string abstract_ = xmlReader.ReadOptionalElementContentAsString(strings.Abstract);
            string abstractCustom = xmlReader.ReadOptionalElementContentAsString(strings.AbstractCustom);
            string friority = xmlReader.ReadOptionalElementContentAsString(strings.Friority);
            xmlReader.IgnoreElement(strings.Tag, IgnoreOptions.Optional | IgnoreOptions.Multiple);
            xmlReader.IgnoreElement(strings.Comment, IgnoreOptions.Optional | IgnoreOptions.Multiple);
            FortifyPathElement primary = FortifyPathElement.Parse(xmlReader, strings);
            FortifyPathElement source;
            if (xmlReader.NodeType == XmlNodeType.Element && Ref.Equal(xmlReader.LocalName, strings.Source))
            {
                source = FortifyPathElement.Parse(xmlReader, strings);
            }
            else
            {
                source = null;
            }

            xmlReader.IgnoreElement(strings.TraceDiagramPath, IgnoreOptions.Optional);
            ImmutableArray<int> cweIds = ImmutableArray<int>.Empty;
            if (xmlReader.NodeType == XmlNodeType.Element && Ref.Equal(xmlReader.LocalName, strings.ExternalCategory))
            {
                if (xmlReader.GetAttribute(strings.Type) == "CWE")
                {
                    cweIds = ParseCweIds(xmlReader.ReadElementContentAsString());
                }
                else
                {
                    xmlReader.Skip();
                }
            }

            xmlReader.ReadEndElement(); // </Result>

            return new FortifyIssue(ruleId, iid, category, kingdom, abstract_, abstractCustom, friority, primary, source, cweIds);
        }
        /// <summary>Reads a "hints" element, consuming the element.</summary>
        /// <exception cref="XmlException">Thrown when the Android Studio file is incorrect.</exception>
        /// <param name="reader">The reader from which the hints shall be parsed.</param>
        /// <param name="strings">NameTable strings used to parse Android Studio files.</param>
        /// <returns>The set of hint values from <paramref name="reader"/>.</returns>
        public static ImmutableArray<string> ReadHints(XmlReader reader, AndroidStudioStrings strings)
        {
            Debug.Assert(Ref.Equal(reader.LocalName, strings.Hints), "ReadHints didn't have hints");

            var result = ImmutableArray.CreateBuilder<string>();
            if (!reader.IsEmptyElement)
            {
                int hintsDepth = reader.Depth;
                reader.Read(); // Consume start element
                while (reader.Depth > hintsDepth)
                {
                    if (!Ref.Equal(reader.LocalName, strings.Hint))
                    {
                        throw reader.CreateException(ConverterResources.AndroidStudioHintsElementContainedNonHint);
                    }

                    string hintContent = reader.GetAttribute(strings.Value);
                    if (hintContent == null)
                    {
                        throw reader.CreateException(ConverterResources.AndroidStudioHintElementMissingValue);
                    }

                    if (hintContent.Length != 0)
                    {
                        result.Add(hintContent);
                    }

                    reader.Skip();
                }
            }

            reader.Read(); // Consume the end / empty element
            return result.ToImmutable();
        }
        /// <summary>Parses a "problem" node from an Android Studio log and consumes that node.</summary>
        /// <exception cref="XmlException">Thrown when the Android Studio file is incorrect.</exception>
        /// <param name="reader">The reader from which the problem shall be parsed.</param>
        /// <param name="strings">NameTable strings used to parse Android Studio files.</param>
        /// <returns>
        /// If the problem node is not empty, an instance of <see cref="AndroidStudioProblem" />;
        /// otherwise, null.
        /// </returns>
        public static AndroidStudioProblem Parse(XmlReader reader, AndroidStudioStrings strings)
        {
            if (!reader.IsStartElement(strings.Problem))
            {
                throw reader.CreateException(ConverterResources.AndroidStudioNotProblemElement);
            }

            Builder b = new Builder();
            if (!reader.IsEmptyElement)
            {
                int problemDepth = reader.Depth;
                reader.Read(); // Get to children
                while (reader.Depth > problemDepth)
                {
                    string nodeName = reader.LocalName;
                    if (Ref.Equal(nodeName, strings.File))
                    {
                        b.File = reader.ReadElementContentAsString();
                    }
                    else if (Ref.Equal(nodeName, strings.Line))
                    {
                        b.Line = Math.Max(1, reader.ReadElementContentAsInt());
                    }
                    else if (Ref.Equal(nodeName, strings.Module))
                    {
                        b.Module = reader.ReadElementContentAsString();
                    }
                    else if (Ref.Equal(nodeName, strings.Package))
                    {
                        b.Package = reader.ReadElementContentAsString();
                    }
                    else if (Ref.Equal(nodeName, strings.EntryPoint))
                    {
                        ReadEntryPointElement(ref b, reader, strings);
                    }
                    else if (Ref.Equal(nodeName, strings.ProblemClass))
                    {
                        ReadProblemClassElement(ref b, reader, strings);
                    }
                    else if (Ref.Equal(nodeName, strings.Hints))
                    {
                        b.Hints = ReadHints(reader, strings);
                    }
                    else if (Ref.Equal(nodeName, strings.Description))
                    {
                        b.Description = reader.ReadElementContentAsString();
                    }
                    else
                    {
                        reader.Skip();
                    }
                }
            }

            reader.Read(); // Consume the empty / end element

            if (b.IsEmpty)
            {
                return null;
            }

            try
            {
                return new AndroidStudioProblem(b);
            }
            catch (ArgumentException invalidData)
            {
                throw reader.CreateException(invalidData.Message);
            }
        }