Пример #1
0
    protected void Tester_Load(object sender, EventArgs e)
    {
        // set up some validators that depend on server-side constants
        string lengthExpression = "^(.|\\s){0," + MAX_INPUT_LENGTH.ToString() + "}$";

        revPatternLength.ValidationExpression     = lengthExpression;
        revPatternLength.ErrorMessage             = string.Format(revPatternLength.ErrorMessage, MAX_INPUT_LENGTH);
        revHaystackLength.ValidationExpression    = lengthExpression;
        revHaystackLength.ErrorMessage            = string.Format(revHaystackLength.ErrorMessage, MAX_INPUT_LENGTH);
        revReplacementLength.ValidationExpression = lengthExpression;
        revReplacementLength.ErrorMessage         = string.Format(revReplacementLength.ErrorMessage, MAX_INPUT_LENGTH);
        rvMatchesLimit.MaximumValue = MAX_MATCHES.ToString();
        rvMatchesLimit.ErrorMessage = string.Format(rvMatchesLimit.ErrorMessage, MAX_MATCHES);

        if (!Page.IsPostBack)
        {
            // there may be stuff on the query string to load into the form
            this.LoadFromQueryString();

            if (txtPattern.Text.Length > 0 && txtHaystack.Text.Length > 0)
            {
                // we already have enough stuff from the query string to run the regex
                Page.Validate();
                RunRegex(true, DetailsPanels.All);
            }
            else
            {
                // starting off with empty pattern and haystack inputs
                lblRegexInfoBlurb.Text = EMPTY_START_MESSAGE;
            }
        }
        else if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
        {
            // get boolean for whether to update haystack backdrop
            bool updateHaystackBackdrop = (hfUpdateHaystackBackdrop.Value == "true");

            // translate detals tab id to details panel enum
            DetailsPanels detailsPanel = DetailsPanels.None;
            if (hfDetailsLink.Value == hlDetailsInfo.ClientID)
            {
                detailsPanel = DetailsPanels.RegexInfo;
            }
            else if (hfDetailsLink.Value == hlDetailsTable.ClientID)
            {
                detailsPanel = DetailsPanels.Table;
            }
            else if (hfDetailsLink.Value == hlDetailsContext.ClientID)
            {
                detailsPanel = DetailsPanels.Context;
            }
            else if (hfDetailsLink.Value == hlDetailsSplit.ClientID)
            {
                detailsPanel = DetailsPanels.SplitList;
            }

            // validate and run
            Page.Validate();
            RunRegex(updateHaystackBackdrop, detailsPanel);
        }
    }
Пример #2
0
    protected void Update_Click(object sender, EventArgs e)
    {
        DetailsPanels.DataBind();
        DetailsPanels.Visible = true;
        UpdateDetails.Visible = false;

        //set up object to use the web.config file
        string connectionString = WebConfigurationManager.ConnectionStrings["photocatconnection"].ConnectionString;

        //set up connection object called 'myConnection'
        SqlConnection myConnection = new SqlConnection(connectionString);

        //open database communication
        myConnection.Open();

        string fupdate = fnameedit.Text;
        string supdate = snameedit.Text;
        string eupdate = emailedit.Text;
        string pupdate = passedit.Text;

        int row = int.Parse(Request.QueryString["UserID"]);

        string query = "UPDATE Users SET Forename = @newf, Surname = @news, Email = @newe, Password = @newp WHERE UserID = @id";

        SqlCommand myCommand = new SqlCommand(query, myConnection);

        //parameterised object
        myCommand.Parameters.AddWithValue("@newf", fupdate);
        myCommand.Parameters.AddWithValue("@news", supdate);
        myCommand.Parameters.AddWithValue("@newe", eupdate);
        myCommand.Parameters.AddWithValue("@newp", pupdate);
        myCommand.Parameters.AddWithValue("@id", row);

        myCommand.ExecuteNonQuery();

        Details.DataBind();

        myConnection.Close();
    }
Пример #3
0
    private void RunRegex(bool updateHaystackBackdrop, DetailsPanels detailsPanel)
    {
        bool isPageValid         = false; // flag that keeps track of validation success
        long elapsedMilliseconds = 0;     // total time it took for the regex to run

        // begin series of setup and validation steps
        if (Page.IsValid)
        {
            // at this point, we know these are numerical values
            startPosition = Convert.ToInt32(txtStartPosition.Text);
            matchesLimit  = Convert.ToInt32(txtMatchesLimit.Text);

            // check numerical value of start position
            if (startPosition > txtHaystack.Text.Length)
            {
                cvStartPositionMaxValue.IsValid = false;
            }

            if (Page.IsValid)
            {
                // browsers other than IE do not include \r in inputs during async postback
                // this normalizes inputs to always have \r\n for newlines instead of just \n
                txtPattern.Text     = Regex.Replace(txtPattern.Text, "\r?\n", "\r\n");
                txtHaystack.Text    = Regex.Replace(txtHaystack.Text, "\r?\n", "\r\n");
                txtReplacement.Text = Regex.Replace(txtReplacement.Text, "\r?\n", "\r\n");

                // collect options
                RegexOptions regexOptions = 0;
                if (cbIgnoreCase.Checked)
                {
                    regexOptions = regexOptions | RegexOptions.IgnoreCase;
                }
                if (cbIgnorePatternWhitespace.Checked)
                {
                    regexOptions = regexOptions | RegexOptions.IgnorePatternWhitespace;
                }
                if (cbExplicitCapture.Checked)
                {
                    regexOptions = regexOptions | RegexOptions.ExplicitCapture;
                }
                if (cbCultureInvariant.Checked)
                {
                    regexOptions = regexOptions | RegexOptions.CultureInvariant;
                }
                if (cbSingleline.Checked)
                {
                    regexOptions = regexOptions | RegexOptions.Singleline;
                }
                if (cbMultiline.Checked)
                {
                    regexOptions = regexOptions | RegexOptions.Multiline;
                }
                if (cbRightToLeft.Checked)
                {
                    regexOptions = regexOptions | RegexOptions.RightToLeft;
                }
                if (cbEcmaScript.Checked)
                {
                    regexOptions = regexOptions | RegexOptions.ECMAScript;
                }

                // initialize new regex object, careful to catch any parsing exception from
                // the pattern to show to the user as a validation error
                try
                {
                    testRegex = new Regex(txtPattern.Text, regexOptions);
                }
                catch (ArgumentOutOfRangeException)
                {
                    // ECMA script option throws exception if used with certain other options
                    cvInvalidOptionsCombo.IsValid = false;
                }
                catch (ArgumentException ex)
                {
                    // catch parsing exceptions, double check message to make sure it's
                    // the type of exception we're expecting
                    if (ex.Message.StartsWith("parsing") && ex.Message.Contains(" - "))
                    {
                        // display part of the exception message string as a validation error
                        cvPatternParsing.ErrorMessage = ex.Message.Substring(ex.Message.LastIndexOf(" - ") + 3).TrimEnd('.');
                        cvPatternParsing.IsValid      = false;
                    }
                }

                if (Page.IsValid)
                {
                    // the actual regex match execution happens within
                    elapsedMilliseconds = TimeLimitedRegexMatch();

                    if (Page.IsValid)
                    {
                        // winner!
                        isPageValid = true;
                    }
                }
            }
        }

        if (!isPageValid)
        {
            // page is invalid, will fall back to regex info details tab to show errors
            hfHasMatches.Value        = "false";
            lblRegexInfoBlurb.Visible = false;
            pnlPermalinkWrap.Visible  = false;

            hfDetailsLink.Value = hlDetailsInfo.ClientID;
            upDetailsInfo.Update();
        }
        else
        {
            // page is valid
            if (txtPattern.Text.Length == 0 || txtHaystack.Text.Length == 0)
            {
                // not enough input
                hfHasMatches.Value     = "false";
                lblRegexInfoBlurb.Text = EMPTY_START_MESSAGE;

                if (updateHaystackBackdrop)
                {
                    // clear haystack backdrop (panel updated later)
                    hgcHaystackBackdrop.InnerHtml = "";
                }

                hfDetailsLink.Value = hlDetailsInfo.ClientID;
                upDetailsInfo.Update();
            }
            else
            {
                // count matches
                int matchesCount = Math.Min(matches.Count, matchesLimit);

                // send this flag back to the page
                hfHasMatches.Value = (matchesCount > 0) ? "true" : "false";

                // regex info details panel doesn't require matches to be found
                // furthermore, if no matches are found, active panel is reverted to regex info on client-side
                if (matchesCount == 0 || detailsPanel == DetailsPanels.All || detailsPanel == DetailsPanels.RegexInfo)
                {
                    // display matches count and timing info in the regex info blurb
                    lblRegexInfoBlurb.Text = "<strong>" + matchesCount.ToString() + "</strong>" +
                                             (matchesCount == 1 ? " match" : " matches") +
                                             " found in about " +
                                             "<strong>" + elapsedMilliseconds.ToString() + "</strong>" +
                                             (elapsedMilliseconds == 1 ? " millisecond" : " milliseconds") +
                                             ".";

                    SetPermalink();

                    hfDetailsLink.Value = hlDetailsInfo.ClientID;
                    upDetailsInfo.Update();
                }

                // all other details panels (and the haystack backdrop) require that matches were found
                if (matchesCount > 0)
                {
                    if (updateHaystackBackdrop)
                    {
                        string matchesInContext = testRegex.Replace(txtHaystack.Text,
                                                                    new MatchEvaluator(MatchHighlightNoReplacements), matchesLimit, startPosition);
                        matchesInContext = HttpUtility.HtmlEncode(matchesInContext);
                        matchesInContext = matchesInContext.Replace(MATCH_HIGHLIGHT_LEFT_MARKER, "<em>");
                        matchesInContext = matchesInContext.Replace(MATCH_HIGHLIGHT_RIGHT_MARKER, "</em>");

                        // fix newline weirdness
                        matchesInContext = matchesInContext.Replace("\r", "");

                        // update haystack backdrop
                        hgcHaystackBackdrop.InnerHtml = '\n' + matchesInContext + '\n';
                    }

                    if (detailsPanel == DetailsPanels.All || detailsPanel == DetailsPanels.Context)
                    {
                        // both haystack backdrop and the context details panel need the string for matches in context
                        string matchesInContext = testRegex.Replace(txtHaystack.Text,
                                                                    new MatchEvaluator(MatchHighlight), matchesLimit, startPosition);
                        matchesInContext = HttpUtility.HtmlEncode(matchesInContext);
                        matchesInContext = matchesInContext.Replace(MATCH_HIGHLIGHT_LEFT_MARKER, "<em>");
                        matchesInContext = matchesInContext.Replace(MATCH_HIGHLIGHT_RIGHT_MARKER, "</em>");

                        // fix newline weirdness
                        matchesInContext = matchesInContext.Replace("\r", "");

                        // update context details panel, adding zero-width spaces after newlines to
                        // ensure proper highlighting around line breaks
                        hgcDetailsContext.InnerHtml = "\n" + matchesInContext.Replace("\n", "\n&#8203;") + '\n';

                        upDetailsContext.Update();
                    }

                    if (detailsPanel == DetailsPanels.All || detailsPanel == DetailsPanels.Table)
                    {
                        // matches table
                        dlDetailsTable.DataSource = matches;
                        dlDetailsTable.DataBind();

                        upDetailsTable.Update();
                    }

                    if (detailsPanel == DetailsPanels.All || detailsPanel == DetailsPanels.SplitList)
                    {
                        // split list
                        hgcSplitList.Attributes.Add("start", "0");
                        rptSplitList.DataSource = testRegex.Split(txtHaystack.Text, matchesLimit, startPosition);
                        rptSplitList.DataBind();

                        upDetailsSplit.Update();
                    }
                }
            }
        }

        // this is just for users that don't have javascript on
        if (hfHasMatches.Value == "true")
        {
            pnlDetailsTable.Style["display"]   = "block";
            pnlDetailsContext.Style["display"] = "block";
            pnlDetailsSplit.Style["display"]   = "block";
        }
        else
        {
            pnlDetailsTable.Style["display"]   = "none";
            pnlDetailsContext.Style["display"] = "none";
            pnlDetailsSplit.Style["display"]   = "none";
        }

        if (updateHaystackBackdrop)
        {
            upHaystackBackdrop.Update();
        }
    }