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 "); } } } }
//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()); }
// 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)); } } } }
// 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); } } } }
// 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); } } } } }
// 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")); } } }