// Adding call to validation method inside try catch statement
 public void WriteIfElseStructureInWebmethodTry(CSharpFile file, DocumentScript script)
 {
     foreach (var expr in file.IndexOfTryCatchStmt)
     {
         var copy         = (TryCatchStatement)expr.Clone();
         var parentMethod = expr.GetParent <MethodDeclaration>();
         var methodName   = "Valid" + parentMethod.Name;
         var chldOfTypPar = parentMethod.GetChildByRole(Roles.Parameter);
         if (chldOfTypPar != null)
         {
             bool foundIfElseDecl = false;
             var  trySecondChild  = expr.FirstChild.NextSibling.FirstChild.NextSibling;
             foreach (var expression in trySecondChild.Parent.Children.OfType <IfElseStatement>())
             {
                 if (expression.GetText().Contains(methodName))
                 {
                     foundIfElseDecl = true;
                 }
             }
             if (!foundIfElseDecl)
             {
                 script.InsertBefore(trySecondChild, allPatterns.IfElseTryCall(methodName));
             }
         }
     }
 }
        //Add Using ORP; Decl in file
        public void AddUsingAPIDecl(CSharpFile file, DocumentScript script)
        {
            var  firstUsingExpr = file.IndexOfUsingDecl.First();
            var  copy           = (UsingDeclaration)firstUsingExpr.Clone();
            var  parentDecl     = firstUsingExpr.Parent;
            bool foundWebMethod = false;
            bool founORPDecl    = false;

            foreach (var method in parentDecl.Descendants.OfType <MethodDeclaration>())
            {
                if (method.FirstChild.GetText().Contains("WebMethod"))
                {
                    foundWebMethod = true;
                    break;
                }
            }
            foreach (var otherUsingExpr in firstUsingExpr.Parent.Children.OfType <UsingDeclaration>())
            {
                if (otherUsingExpr.Match(allPatterns.ORPUsingDecl()).Success)
                {
                    founORPDecl = true;
                    break;
                }
            }
            if (foundWebMethod && !founORPDecl)
            {
                script.InsertBefore(file.IndexOfUsingDecl.Last(), allPatterns.ORPUsingDecl());
            }
            //            if (!(Path.GetDirectoryName(file.fileName).EndsWith("ORP")) && foundWebMethod && !founORPDecl)
            //                script.InsertBefore(file.IndexOfUsingDecl.Last().NextSibling, allPatterns.ORPUsingDecl());
        }
 private void DummyTextForTryCallValidation(CSharpFile file, DocumentScript script)
 {
     foreach (var expr in file.IndexOfIfElStmtValidation)
     {
         script.InsertText(script.GetCurrentOffset(expr.StartLocation), "DummyText ");
     }
 }
 public void RemoveAccessControlCheckAccess(CSharpFile file, DocumentScript script, string checkAccessMethodName)
 {
     foreach (ExpressionStatement accessControlExpr in file.IndexofExprStmtCheckAccess)
     {
         script.Remove(accessControlExpr, true);
     }
 }
 public void FindUsingAPIDecl(AstNode invocation, CSharpFile file)
 {
     if (invocation.GetType().Name == "UsingDeclaration")
     {
         file.IndexOfUsingDecl.Add((UsingDeclaration)invocation);
     }
 }
        // Writing validation Methhod strucutre for webmethod
        public void WriteValidationMethodStructure(CSharpFile file, DocumentScript script)
        {
            foreach (MethodDeclaration expr in file.IndexOfWebMthdDecl)
            {
                // for adding method before the webmethod
                var copy         = (MethodDeclaration)expr.Clone();
                var chldOfTypPar = expr.GetChildByRole(Roles.Parameter);
                var mtdhName     = expr.Name;
                var chdMtdhName  = "Valid" + mtdhName;

                var  validationMthd     = allPatterns.ValidationMthd(chdMtdhName);
                bool validMethodPresent = false;

                if (chldOfTypPar != null)
                {
                    if (expr.PrevSibling != null && expr.PrevSibling.GetType().Name == "MethodDeclaration")
                    {
                        if (expr.PrevSibling.GetText().Contains(chdMtdhName))
                        {
                            validMethodPresent = true;
                        }
                    }
                    if (!validMethodPresent)
                    {
                        script.InsertBefore(expr, validationMthd);
                    }
                }
            }
        }
예제 #7
0
 public void BuildSyntTree()
 {
     foreach (var item in msbuildProject.GetItems("Compile"))
     {
         var file = new CSharpFile(this, Path.Combine(msbuildProject.DirectoryPath, item.EvaluatedInclude));
         Files.Add(file);
     }
 }
 public void FindWebMethod(AstNode invocation, CSharpFile file)
 {
     if (invocation.GetType().Name == "MethodDeclaration" &&
         FindWebMethod(invocation))
     {
         file.IndexOfWebMthdDecl.Add((MethodDeclaration)invocation);
     }
 }
 public void FindCheckAccess(AstNode invocation, CSharpFile file, string checkAccessMethodName)
 {
     if (allPatterns.AccessControlExpression(checkAccessMethodName).Match(invocation).Success &&
         FindWebMethod(invocation.GetParent <MethodDeclaration>()))
     {
         file.IndexofExprStmtCheckAccess.Add((ExpressionStatement)invocation);
     }
 }
        public void WriteValidationMethodBody(CSharpFile file, DocumentScript script)
        {
            foreach (var expr in file.IndexOfWebMthdDecl)
            {
                // if parameters are present, go to next iteration without doing anything.
                if (expr.Parameters.Count != 0)
                {
                    continue;
                }

                if (expr.Name != "Valid" + (expr.NextSibling.GetChildByRole(Roles.Identifier).GetText()))
                {
                    continue;
                }

                // logic to insert parameters to validation Method.
                var    copy = (MethodDeclaration)expr.Clone();
                string str  = string.Join(", ", from parameter
                                          in expr.NextSibling.Descendants.OfType <ParameterDeclaration>()
                                          select parameter.GetText());
                var chldOfTypPar = expr.GetChildByRole(Roles.RPar);
                int offset       = script.GetCurrentOffset(chldOfTypPar.StartLocation);
                script.InsertText(offset, str);

                // Insert Static keyword to validation method.
                script.InsertText(script.GetCurrentOffset(expr.ReturnType.StartLocation), "static ");

                // logic to insert if else statements inside validation Method body
                int offset1 = script.GetCurrentOffset(expr.LastChild.FirstChild.EndLocation);

                var locationToInsert = expr.LastChild.LastChild.PrevSibling;
                script.InsertBefore(locationToInsert, allPatterns.ValidationFieldDecl());
                foreach (var inv in expr.NextSibling.Children.OfType <ParameterDeclaration>())
                {
                    string dataType = inv.FirstChild.GetText();
                    string varName  = inv.LastChild.GetText();
                    if (dataType.Contains("int"))
                    {
                        script.InsertBefore(locationToInsert, allPatterns.IfElStmtInt(varName));
                    }
                    else if (dataType.Contains("string") || dataType.Contains("String"))
                    {
                        script.InsertBefore(locationToInsert, allPatterns.IfElStmtStr(varName));
                    }
                    else if (dataType.Contains("float") || dataType.Contains("decimal") || dataType.Contains("Decimal"))
                    {
                        script.InsertBefore(locationToInsert, allPatterns.IfElseFloatDecimal(varName));
                    }
                    else
                    {
                        script.InsertText(script.GetCurrentOffset(locationToInsert.StartLocation), "DummyText_DatatypeIsDifferent ");
                    }
                }
            }
        }
 // Checking Whether Try Catch is Present in WebMethod
 public void CheckTryCatchInWebMethodBody(CSharpFile file, DocumentScript script)
 {
     foreach (MethodDeclaration expr in file.IndexOfWebMthdDecl)
     {
         var copy = (MethodDeclaration)expr.NextSibling.Clone();
         if (expr.NextSibling.FirstChild.GetText().Contains("WebMethod") &&
             !expr.NextSibling.Descendants.OfType <TryCatchStatement>().Any())
         {
             script.InsertText(script.GetCurrentOffset(expr.NextSibling.StartLocation), "DummyText_TryCatchNotPresent");
         }
     }
 }
        public void FindFirstTryCatchInWebMethod(AstNode invocation, CSharpFile file)
        {
            var methodName = invocation.GetParent <MethodDeclaration>();

            if (invocation.GetType().Name == "MethodDeclaration" && FindWebMethod(invocation))
            {
                try
                {
                    var tryCatchStmt = invocation.Descendants.OfType <TryCatchStatement>().First();
                    file.IndexOfTryCatchStmt.Add((TryCatchStatement)tryCatchStmt);
                }
                catch (Exception) { }
            }
        }
 public void FindValidationMethod(AstNode invocation, CSharpFile file)
 {
     if (invocation.NextSibling != null)
     {
         var next = invocation.NextSibling;
         if (invocation.GetType().Name == "MethodDeclaration" &&
             next.GetType().Name == "MethodDeclaration")
         {
             if (FindWebMethod(next))
             {
                 file.IndexOfWebMthdDecl.Add((MethodDeclaration)invocation);
             }
         }
     }
 }
 public void FindClassOfWebMethods(AstNode invocation, CSharpFile file)
 {
     if (invocation.GetType().Name == "TypeDeclaration")
     {
         bool WebMethodPresent = false;
         foreach (var inv in invocation.Descendants.OfType <MethodDeclaration>())
         {
             if (inv.FirstChild.GetText().Contains("WebMethod"))
             {
                 WebMethodPresent = true;
                 break;
             }
         }
         if (WebMethodPresent)
         {
             file.IndexOfClassDecl.Add((TypeDeclaration)invocation);
         }
     }
 }
        public void InsertParametersIfElseInWebmethodTry(CSharpFile file, DocumentScript script)
        {
            foreach (var expr in file.IndexOfIfElStmt)
            {
                //check if parameters are passed before, go to next iteration
                if (!(expr.Descendants.OfType <InvocationExpression>().First().GetText().Split("()".ToCharArray())[1] == ""))
                {
                    continue;
                }

                // logic to add parameters in if block (in try catch of webmethod) to Validation Method.
                var    copy       = (IfElseStatement)expr.Clone();
                var    ParentExpr = expr.GetParent <MethodDeclaration>();
                string str        = string.Join(", ", from parameter
                                                in ParentExpr.Descendants.OfType <ParameterDeclaration>()
                                                select parameter.Name);

                int parameterOffset = script.GetCurrentOffset(expr.GetChildByRole(Roles.RPar).StartLocation) - 1;
                script.InsertText(parameterOffset, str);

                // putting Return Vriable Name in If Else Block.
                if (expr.GetParent <MethodDeclaration>().ReturnType.GetText() != "void")
                {
                    var returnVar      = expr.GetParent <MethodDeclaration>().Body.LastChild.PrevSibling.FirstChild;
                    int retValueOffset = script.GetCurrentOffset(expr.LastChild.LastChild.StartLocation);
                    if (returnVar.GetText() == "return")
                    {
                        string retString = returnVar.NextSibling.GetText();
                        script.InsertText(retValueOffset, " " + retString);
                    }
                    else
                    {
                        script.InsertText(retValueOffset, " DummyText");
                    }
                }
            }
        }
 // Add pageName parameter in clas global declaration
 public void AddPageNameGlobalinClass(CSharpFile file, DocumentScript script)
 {
     foreach (var expr in file.IndexOfClassDecl)
     {
         bool foundPageNameGlobalInClass = false;
         var  copy = (TypeDeclaration)expr.Clone();
         foreach (var TypeMember in expr.Members.OfType <FieldDeclaration>())
         {
             if (TypeMember.Match(allPatterns.PageNameGlobalFieldDecl(expr.Name + ".aspx")).Success)
             {
                 foundPageNameGlobalInClass = true;
                 //break;
             }
             if (TypeMember.Match(allPatterns.PageNameGlobalFieldDecl1(expr.Name + ".aspx")).Success)
             {
                 script.Remove(TypeMember, true);
             }
         }
         if (!foundPageNameGlobalInClass)
         {
             script.InsertBefore(expr.Members.First(), allPatterns.PageNameGlobalFieldDecl(expr.Name + ".aspx"));
         }
     }
 }
 // To write access control statement in try block of webmethod
 public void WriteAccessControlStmtInTryCatch(CSharpFile file, DocumentScript script, string checkAccessMethodName)
 {
     foreach (var expr in file.IndexOfTryCatchStmt)
     {
         bool foundCheckAccessControl = false;
         var  copy = (TryCatchStatement)expr.Clone();
         var  AccessControlPattern = allPatterns.AccessControlExpression(checkAccessMethodName);
         foreach (var expression in expr.FirstChild.NextSibling.Children.OfType <ExpressionStatement>())
         {
             if (expression.Match(AccessControlPattern).Success)
             {
                 foundCheckAccessControl = true;
             }
         }
         if (!foundCheckAccessControl)
         {
             string className = (expr.GetParent <TypeDeclaration>().Name.ToString());
             try
             {
                 script.InsertBefore(expr.FirstChild.NextSibling.FirstChild.NextSibling.NextSibling, AccessControlPattern);
                 if (!(AccessControlPageNames.Contains(className)))
                 {
                     AccessControlPageNames.Add(className);
                 }
             }
             catch (Exception)
             {
                 script.InsertBefore(expr.FirstChild.NextSibling.FirstChild.NextSibling, AccessControlPattern);
                 if (!(AccessControlPageNames.Contains(className)))
                 {
                     AccessControlPageNames.Add(className);
                 }
             }
         }
     }
 }
        public void FindIfElseInWebMethod(AstNode invocation, CSharpFile file)
        {
            if (invocation.GetType().Name == "IfElseStatement" &&
                FindWebMethod(invocation.GetParent <MethodDeclaration>()))
            {
                Expression childOfTypeRoleCondition = invocation.GetChildByRole(Roles.Condition);
                if (childOfTypeRoleCondition.GetType().Name == "UnaryOperatorExpression")
                {
                    string strToCheck = "Valid" + invocation.GetParent <MethodDeclaration>().Name;

                    if (allPatterns.IfElseValidMethodUnary(strToCheck).Match(childOfTypeRoleCondition).Success)
                    {
                        file.IndexOfIfElStmt.Add((IfElseStatement)invocation);
                    }
                    else if (allPatterns.IfElseValidMethodUnaryOld().Match(childOfTypeRoleCondition).Success)
                    {
                        string strToCheckforAlreadyDeclared = childOfTypeRoleCondition.Descendants.OfType <IdentifierExpression>().First().GetText();
                        if (strToCheckforAlreadyDeclared.IndexOf(("valid"), StringComparison.OrdinalIgnoreCase) > 0)
                        {
                            file.IndexOfIfElStmtValidation.Add((IfElseStatement)invocation);
                        }
                    }
                    else if (allPatterns.IfElseValidMethodUnaryMemberRef().Match(childOfTypeRoleCondition).Success)
                    {
                        string strToCheckAlreadyDecare = childOfTypeRoleCondition.Descendants.OfType <MemberReferenceExpression>().First().LastChild.GetText();
                        if (strToCheckAlreadyDecare.IndexOf(("valid"), StringComparison.OrdinalIgnoreCase) > 0)
                        {
                            file.IndexOfIfElStmtValidation.Add((IfElseStatement)invocation);
                        }
                    }
                }
                else if (childOfTypeRoleCondition.GetType().Name == "BinaryOperatorExpression")
                {
                    if (allPatterns.IfElseValidMethodBinary().Match(childOfTypeRoleCondition).Success)
                    {
                        string strToCheck = childOfTypeRoleCondition.Descendants.OfType <IdentifierExpression>().First().GetText();
                        if (strToCheck.IndexOf("Valid", StringComparison.OrdinalIgnoreCase) > 0)
                        {
                            file.IndexOfIfElStmtValidation.Add((IfElseStatement)invocation);
                        }
                    }
                    else if (allPatterns.IfElseValidMethodBinaryMemberRef().Match(childOfTypeRoleCondition).Success)
                    {
                        string strToCheck = childOfTypeRoleCondition.Descendants.OfType <IdentifierExpression>().First().NextSibling.GetText();
                        if (strToCheck.IndexOf("Valid", StringComparison.OrdinalIgnoreCase) > 0)
                        {
                            file.IndexOfIfElStmtValidation.Add((IfElseStatement)invocation);
                        }
                    }
                }
                //else if ()
                //   if(childRole == "UnaryOperatorExpression" || childRole == "BinaryOperatorExpression")
                //     file.IndexOfIfElStmt.Add((IfElseStatement)invocation);

                /*                file.IndexOfIfElStmt.Add((IfElseStatement)invocation);
                 *              string strToCheck = null;
                 *              try{
                 *                  //strToCheck = invocation.FirstChild.NextSibling.NextSibling.FirstChild.NextSibling.FirstChild.GetText();
                 *                  strToCheck = invocation.GetChildByRole(Roles.Condition).DescendantsAndSelf.OfType<InvocationExpression>().First().Children.OfType<IdentifierExpression>().First().GetText();
                 *              }
                 *              catch(Exception) {}
                 *              if (strToCheck != null)
                 *              {
                 *                  if (strToCheck.IndexOf("Valid", StringComparison.OrdinalIgnoreCase) >= 0  &&
                 *                      FoundWebMethodAttribute(invocation.GetParent<MethodDeclaration>()))
                 *                  {
                 *                      if (strToCheck == "Valid" + invocation.GetParent<MethodDeclaration>().Name)
                 *                      {
                 *                          file.IndexOfIfElStmt.Add((IfElseStatement)invocation);
                 *                      }
                 *                      else
                 *                      {
                 *                          invocation.Descendants.OfType<InvocationExpression>().First().GetNodeAt(invocation.StartLocation, null);
                 *                          //foreach(var expr)
                 *                      }
                 * //                        else if()
                 *                  }
                 *              }
                 */
            }
        }