Ejemplo n.º 1
0
        /// <summary>
        /// Method to obtain all the warnings as a result of the validation of the html
        /// </summary>
        /// <param name="urlDocument">Xml document that represents the result of the validation</param>
        /// <param name="mNamespace">Namespace used to obtain some data such as line, colum, etc.</param>
        private void HTMLWarnings(XDocument urlDocument, XNamespace mNamespace)
        {
            warnings = new Warnings();

            //Obtaining the descendants of the elements labeled "warnings". With this we obtain all the warnings
            var warningsElements = from e in urlDocument.Descendants(mNamespace + "warnings")
                                   select e;
            //Obtaining the descendants of the elements labeled "warningcount". With this we can obtain the number of warnings.
            var warningCountElement = from e in warningsElements.Descendants(mNamespace + "warningcount")
                                      select e;
            //Obtaining the descendants of the elements labeled "warning". With this we can obtain information from each of the warnings. 
            var warningListElements = from e in warningsElements.Descendants(mNamespace + "warning")
                                      select e;

            //Iterate over the 'warningaccount' variable to obtain the number of warnings
            foreach (var element in warningCountElement)
            {
                //Store the value of the count
                warnings.warningCount = element.Value;

                //Iterate over the 'warningListElements' variable to obtain each error
                foreach (var warningElement in warningListElements)
                {
                    //Create an instance of a Warning
                    Warning warning = new Warning();

                    //If there is a number of line
                    if (warningElement.Descendants(mNamespace + "line").Count() > 0)
                        //Store all the información of the warning.
                        warning.line = warningElement.Descendants(mNamespace + "line").First().Value;
                    //If there is a number of column
                    if (warningElement.Descendants(mNamespace + "col").Count() > 0)
                        //Store all the información of the warning.
                        warning.col = warningElement.Descendants(mNamespace + "col").First().Value;
                    //If there is an explnation
                    if (warningElement.Descendants(mNamespace + "explanation").Count() > 0)
                        //Store all the información of the warning.
                        warning.explanation = warningElement.Descendants(mNamespace + "explanation").First().Value;
                    //If there is a source
                    if (warningElement.Descendants(mNamespace + "source").Count() > 0)
                        //Store all the información of the warning.
                        warning.source = warningElement.Descendants(mNamespace + "source").First().Value;
                    //If there is a messageid
                    if (warningElement.Descendants(mNamespace + "messageid").Count() > 0)
                    {
                        //If the messageid stars with a 'W' it means that the warning is a PotentialIssue
                        if (warningElement.Descendants(mNamespace + "messageid").First().Value.StartsWith("W"))
                        {
                            //Create an instance of a WarningPotentialIssue
                            WarningPotentialIssue warningPotentialIssue = new WarningPotentialIssue();

                            //Store the messageid in the warningPotentialIssue object
                            warningPotentialIssue.messageid = warningElement.Descendants(mNamespace + "messageid").First().Value;
                            //If there is a message
                            if (warningElement.Descendants(mNamespace + "message").Count() > 0)
                                //Store the message in the warningPotentialIssue object
                                warningPotentialIssue.message = warningElement.Descendants(mNamespace + "message").First().Value;
                            ////Add the warningPotentialIssue to the list of warningPotentialIssues.
                            warningPotentialIssues.Add(warningPotentialIssue);
                        }
                        //If the messageid not stars with a 'W'
                        else
                        {
                            //Store the messageid
                            warning.messageid = warningElement.Descendants(mNamespace + "messageid").First().Value;
                            //If there is a message
                            if (warningElement.Descendants(mNamespace + "message").Count() > 0)
                                //Store the message
                                warning.message = warningElement.Descendants(mNamespace + "message").First().Value;

                            //Add the warning to the list of warnings
                            warnings.Add(warning);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Method to obtain all the warnings as a result of the validation of the html
        /// </summary>
        /// <param name="urlDocument">Xml document that represents the result of the validation</param>
        /// <param name="mNamespace">Namespace used to obtain some data such as line, colum, etc.</param>
        private void HTMLWarnings(XDocument urlDocument, XNamespace mNamespace)
        {
            warnings = new Warnings();

            //Obtaining the descendants of the elements labeled "warnings". With this we obtain all the warnings
            var warningsElements = from e in urlDocument.Descendants(mNamespace + "warnings")
                                   select e;
            //Obtaining the descendants of the elements labeled "warningcount". With this we can obtain the number of warnings.
            var warningCountElement = from e in warningsElements.Descendants(mNamespace + "warningcount")
                                      select e;
            //Obtaining the descendants of the elements labeled "warning". With this we can obtain information from each of the warnings.
            var warningListElements = from e in warningsElements.Descendants(mNamespace + "warning")
                                      select e;

            //Iterate over the 'warningaccount' variable to obtain the number of warnings
            foreach (var element in warningCountElement)
            {
                //Store the value of the count
                warnings.warningCount = element.Value;

                //Iterate over the 'warningListElements' variable to obtain each error
                foreach (var warningElement in warningListElements)
                {
                    //Create an instance of a Warning
                    Warning warning = new Warning();

                    //If there is a number of line
                    if (warningElement.Descendants(mNamespace + "line").Count() > 0)
                    {
                        //Store all the información of the warning.
                        warning.line = warningElement.Descendants(mNamespace + "line").First().Value;
                    }
                    //If there is a number of column
                    if (warningElement.Descendants(mNamespace + "col").Count() > 0)
                    {
                        //Store all the información of the warning.
                        warning.col = warningElement.Descendants(mNamespace + "col").First().Value;
                    }
                    //If there is an explnation
                    if (warningElement.Descendants(mNamespace + "explanation").Count() > 0)
                    {
                        //Store all the información of the warning.
                        warning.explanation = warningElement.Descendants(mNamespace + "explanation").First().Value;
                    }
                    //If there is a source
                    if (warningElement.Descendants(mNamespace + "source").Count() > 0)
                    {
                        //Store all the información of the warning.
                        warning.source = warningElement.Descendants(mNamespace + "source").First().Value;
                    }
                    //If there is a messageid
                    if (warningElement.Descendants(mNamespace + "messageid").Count() > 0)
                    {
                        //If the messageid stars with a 'W' it means that the warning is a PotentialIssue
                        if (warningElement.Descendants(mNamespace + "messageid").First().Value.StartsWith("W"))
                        {
                            //Create an instance of a WarningPotentialIssue
                            WarningPotentialIssue warningPotentialIssue = new WarningPotentialIssue();

                            //Store the messageid in the warningPotentialIssue object
                            warningPotentialIssue.messageid = warningElement.Descendants(mNamespace + "messageid").First().Value;
                            //If there is a message
                            if (warningElement.Descendants(mNamespace + "message").Count() > 0)
                            {
                                //Store the message in the warningPotentialIssue object
                                warningPotentialIssue.message = warningElement.Descendants(mNamespace + "message").First().Value;
                            }
                            ////Add the warningPotentialIssue to the list of warningPotentialIssues.
                            warningPotentialIssues.Add(warningPotentialIssue);
                        }
                        //If the messageid not stars with a 'W'
                        else
                        {
                            //Store the messageid
                            warning.messageid = warningElement.Descendants(mNamespace + "messageid").First().Value;
                            //If there is a message
                            if (warningElement.Descendants(mNamespace + "message").Count() > 0)
                            {
                                //Store the message
                                warning.message = warningElement.Descendants(mNamespace + "message").First().Value;
                            }

                            //Add the warning to the list of warnings
                            warnings.Add(warning);
                        }
                    }
                }
            }
        }
        public static validationResults ValidateAccessibility(string htmlSourceCode)
        {
            var diz = new Dictionary<string, object>();


            diz.Add("MAX_FILE_SIZE", "52428800");
            diz.Add("pastehtml", htmlSourceCode);
            diz.Add("validate_paste", "Check it");
            diz.Add("enable_html_validation", "1");
            diz.Add("radio_gid[]", "7");
            diz.Add("checkbox_gid[]", "8");
            diz.Add("rpt_format", "1");

            //diz.Add("enable_html_validation", "1");

            using (var resp = FormUpload.MultipartFormDataPost(
                "http://achecker.ca/checker/index.php",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)",
                diz
                ))
            {
                using (var str = resp.GetResponseStream())
                {

                    HtmlDocument doc = new HtmlDocument();
                    doc.Load(str);
                    var errorsHtml = doc.DocumentNode.SelectSingleNode(@"//*[@id='AC_errors']");
                    var errorNodes = errorsHtml.SelectNodes(@"child::*[@class='gd_one_check']");

                    var errorList = new List<Error>();
                    if (errorNodes != null)
                    {
                        foreach (var itm in errorNodes)
                        {
                            try
                            {
                                var err = new Error();
                                err.message = itm.SelectSingleNode(@"child::*[@class='gd_msg']/a").InnerText;
                                err.explanation = itm.SelectSingleNode(@"child::*[@class='gd_question_section']").InnerText;
                                err.explanation = Regex.Replace(err.explanation, @"[\s]+", " ");

                                var location = itm.SelectSingleNode(@"child::table/tr/td/em").InnerText;
                                var m = Regex.Match(location, @"Line\s(?:(?<line>[0-9]+)),\sColumn\s(?:(?<col>[0-9]+))");
                                err.col = int.Parse(m.Groups["col"].Value);
                                err.line = int.Parse(m.Groups["line"].Value);
                                errorList.Add(err);
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }

                        }
                    }

                    var potentialHtml = doc.DocumentNode.SelectSingleNode(@"//*[@id='AC_potential_problems']");

                    var potenzialNodes = potentialHtml.SelectNodes(@"child::*[@class='gd_one_check']");

                    var potentialList = new List<Warning>();
                    if (potenzialNodes != null)
                    {
                        foreach (var itm in potenzialNodes)
                        {
                            try
                            {
                                var message = itm.SelectSingleNode(@"child::*[@class='gd_msg']/a").InnerText;
                                var explanation = itm.SelectSingleNode(@"child::*[@class='gd_question_section']").InnerText;
                                explanation = Regex.Replace(explanation, @"[\s]+", " ");
                                var locations = itm.SelectNodes(@"child::table/tr");
                                foreach (var l in locations)
                                {
                                    var warn = new Warning();
                                    warn.message = message;
                                    warn.explanation = explanation;
                                    var location = l.SelectSingleNode(@"child::td/em").InnerHtml;
                                    var m = Regex.Match(location, @"Line\s(?:(?<line>[0-9]+)),\sColumn\s(?:(?<col>[0-9]+))");
                                    warn.col = m.Groups["col"].Value;
                                    warn.line = m.Groups["line"].Value;
                                    potentialList.Add(warn);
                                }
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                    }
                    var result = new validationResults();
                    result.Errors = errorList;
                    result.Potentials = new List<WarningPotentialIssue>();
                    result.Warnings = potentialList;
                    return result;

                }
            }
        }