예제 #1
0
        private static HtmlFormDefinition PopulateForm(Match formMatch, string pageHtml)
        {
            var parsedForm = new HtmlFormDefinition {
                PageHtml = pageHtml
            };

            foreach (var attribute in Utility.ParseAttributes(formMatch.Groups[RegexLibrary.ParseFormsAttributesGroup].Value))
            {
                parsedForm.attributes.Add(attribute.Key, attribute.Value);
            }

            // TODO: need to remove comments from form HTML before parsing out controls; commented out controls will be found!

            // Populate controls
            var controlMatches = RegexCache.Instance.Regex(RegexLibrary.ParseFormControls, RegexLibrary.ParseFormControlsOptions).Matches(formMatch.Groups[RegexLibrary.ParseFormsBodyGroup].Value);

            foreach (Match controlMatch in controlMatches)
            {
                HtmlFormControl control;

                if (controlMatch.Groups[RegexLibrary.ParseFormControlsInputGroup].Value.Length > 0)
                {
                    var inputControl = new InputHtmlFormControl(controlMatch.Value);

                    if (inputControl.ControlType == InputHtmlFormControlType.Radio)
                    {
                        control = new InputRadioHtmlFormControl(controlMatch.Value);
                    }
                    else if (inputControl.ControlType == InputHtmlFormControlType.CheckBox)
                    {
                        control = new InputCheckBoxHtmlFormControl(controlMatch.Value);
                    }
                    else
                    {
                        // Generic control
                        control = inputControl;
                    }
                }
                else if (controlMatch.Groups[RegexLibrary.ParseFormControlsSelectGroup].Value.Length > 0)
                {
                    control = new SelectHtmlFormControl(controlMatch.Value);
                }
                else if (controlMatch.Groups[RegexLibrary.ParseFormControlsTextAreaGroup].Value.Length > 0)
                {
                    control = new TextAreaHtmlFormControl(controlMatch.Value);
                }
                else
                {
                    throw new System.Net.WebException(string.Format(CultureInfo.CurrentCulture, NScrapeResources.UnsupportedHtmlControl, controlMatch.Value));
                }

                if (control.Name != null)
                {
                    parsedForm.controls.Add(control);
                }
            }

            return(parsedForm);
        }
예제 #2
0
        /// <summary>
        /// Loads the form specified by the provided form definition.
        /// </summary>
        /// <param name="formUrl">Contains the URL where the page containing the form resides.</param>
        /// <param name="formDefinition">Contains the form definition.</param>
        public void Load(Uri formUrl, HtmlFormDefinition formDefinition)
        {
            FormUrl = formUrl;

            Html = formDefinition.PageHtml;

            PopulateForm(formDefinition);
        }
예제 #3
0
        private void Initialize(int formOrdinal)
        {
            var formDefinitions = HtmlFormDefinition.Parse(Html).ToList();

            if (formOrdinal < 0 || formOrdinal >= formDefinitions.Count)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, NScrapeResources.InvalidFormOrdinal, formOrdinal));
            }

            PopulateForm(formDefinitions.ElementAt(formOrdinal));
        }
예제 #4
0
        private void PopulateForm(HtmlFormDefinition formDefinition)
        {
            foreach (var attribute in formDefinition.Attributes)
            {
                Attributes.Add(attribute.Key, attribute.Value);
            }

            foreach (var control in formDefinition.Controls)
            {
                Controls.Add(control);
            }
        }
예제 #5
0
        private void Initialize(KeyValuePair <string, string> identifyingAttribute)
        {
            var formDefinitions = HtmlFormDefinition.Parse(Html).ToList();

            var formDefinition = formDefinitions.FirstOrDefault(d => d.Attributes.ContainsKey(identifyingAttribute.Key) && d.Attributes[identifyingAttribute.Key] == identifyingAttribute.Value);

            if (formDefinition == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, NScrapeResources.InvalidFormId, identifyingAttribute.Key.ToUpperInvariant(), identifyingAttribute.Value));
            }

            PopulateForm(formDefinition);
        }
예제 #6
0
        private void PopulateForm( HtmlFormDefinition formDefinition )
        {
            foreach ( var attribute in formDefinition.Attributes ) {
                Attributes.Add( attribute.Key, attribute.Value );
            }

            foreach ( var control in formDefinition.Controls ) {
                Controls.Add( control );
            }
        }
예제 #7
0
        /// <summary>
        /// Loads the form specified by the provided form definition.
        /// </summary>
        /// <param name="formUrl">Contains the URL where the page containing the form resides.</param>
        /// <param name="formDefinition">Contains the form definition.</param>
        public void Load( Uri formUrl, HtmlFormDefinition formDefinition )
        {
            FormUrl = formUrl;

            Html = formDefinition.PageHtml;

            PopulateForm( formDefinition );
        }
예제 #8
0
        private static HtmlFormDefinition PopulateForm( Match formMatch, string pageHtml )
        {
            var parsedForm = new HtmlFormDefinition {
                PageHtml = pageHtml
            };

            foreach ( var attribute in Utility.ParseAttributes( formMatch.Groups[RegexLibrary.ParseFormsAttributesGroup].Value ) ) {
                parsedForm.attributes.Add( attribute.Key, attribute.Value );
            }

            // TODO: need to remove comments from form HTML before parsing out controls; commented out controls will be found!

            // Populate controls
            var controlMatches = RegexCache.Instance.Regex( RegexLibrary.ParseFormControls, RegexLibrary.ParseFormControlsOptions ).Matches( formMatch.Groups[RegexLibrary.ParseFormsBodyGroup].Value );

            foreach ( Match controlMatch in controlMatches ) {
                HtmlFormControl control;

                if ( controlMatch.Groups[RegexLibrary.ParseFormControlsInputGroup].Value.Length > 0 ) {
                    var inputControl = new InputHtmlFormControl( controlMatch.Value );

                    if ( inputControl.ControlType == InputHtmlFormControlType.Radio ) {
                        control = new InputRadioHtmlFormControl( controlMatch.Value );
                    }
                    else if ( inputControl.ControlType == InputHtmlFormControlType.CheckBox ) {
                        control = new InputCheckBoxHtmlFormControl( controlMatch.Value );
                    }
                    else {
                        // Generic control
                        control = inputControl;
                    }
                }
                else if ( controlMatch.Groups[RegexLibrary.ParseFormControlsSelectGroup].Value.Length > 0 ) {
                    control = new SelectHtmlFormControl( controlMatch.Value );
                }
                else if ( controlMatch.Groups[RegexLibrary.ParseFormControlsTextAreaGroup].Value.Length > 0 ) {
                    control = new TextAreaHtmlFormControl( controlMatch.Value );
                }
                else {
                    throw new System.Net.WebException( string.Format( CultureInfo.CurrentCulture, NScrapeResources.UnsupportedHtmlControl, controlMatch.Value ) );
                }

                if ( control.Name != null ) {
                    parsedForm.controls.Add( control );
                }
            }

            return parsedForm;
        }