SEARCH
TOOLBOX
LANGUAGES
modified on 22 July 2010 at 08:54 ••• 1,452 views

Requested API changes/30-May-2010

From

Jump to: navigation, search

30-May-2010, by Dinis Cruz

file:Extra_ExtensionMethods.cs

// This file is part of the OWASP O2 Platform (http://www.owasp.org/index.php/OWASP_O2_Platform) and is released under the Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0)
using System;
using System.IO;
using System.Drawing;
using System.Threading;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Reflection;
using System.Text;
using O2.Interfaces.O2Core;
using O2.Kernel;
using O2.Kernel.ExtensionMethods;
using O2.DotNetWrappers.ExtensionMethods;
using O2.DotNetWrappers.Windows;
using O2.DotNetWrappers.DotNet;
using O2.Views.ASCX;
using O2.Views.ASCX.DataViewers; 
using O2.Views.ASCX.ExtensionMethods; 
using O2.External.SharpDevelop.AST;
using O2.External.SharpDevelop.ExtensionMethods;
using O2.External.IE.Wrapper;				
using O2.External.IE.ExtensionMethods;
using O2.XRules.Database._Rules._Interfaces;
 
 
 
using WatiN.Core;
using WatiN.Core.Interfaces;
using SHDocVw;
using mshtml;
//O2Ref:WatiN.Core.1x.dll
//O2Ref:Microsoft.mshtml.dll
//O2Ref:Interop.SHDocVw.dll
//_O2File:C:\O2\O2Scripts_Database\_Scripts\APIs\WatiN\WatiN_IE.cs
//_O2File:C:\O2\O2Scripts_Database\_Scripts\APIs\WatiN\WatiN_IE_ExtensionMethods.cs
//O2Ref:C:\O2\_tempDir\5-27-2010\tmp4479.tmp.dll
 
namespace O2.Script
{		
    public static class ExtraMethods2
    {    
 
    	//Elements
 
    	public static IHTMLElement htmlElement(this Element element)
    	{
    		return (IHTMLElement) element.HTMLElement;
    	}    	   	
 
    	public static void remove(this Element element)
    	{
    		element.outerHtml("");	
    	}
 
    	public static void remove(this List<Element> elements)
    	{
    		foreach(var element in elements)
    			element.outerHtml("");	
    	}
 
    	public static List<T> outerHtml<T>(this List<T> elements, string outerHtml)
    		where T : Element
    	{
    		foreach(var element in elements)
    			element.outerHtml(outerHtml);	
    		return elements;
    	}
 
    	public static List<T> innerHtml<T>(this List<T> elements, string innerHtml)
    		where T : Element
    	{
    		foreach(var element in elements)
    			element.innerHtml(innerHtml);	
    		return elements;
    	}
 
    	public static T outerHtml<T>(this T element, string outerHtml)
    		where T : Element
    	{
    		if (element!= null)
    		{
    			var htmlElement = element.htmlElement();
    			if (htmlElement != null)    			
    				htmlElement.outerHTML = outerHtml;    				   			
    		}
    		return element;
    	}
 
    	public static T innerHtml<T>(this T element, string innerHtml)
    		where T : Element
    	{
    		if (element!= null)
    		{
    			var htmlElement = element.htmlElement();
    			if (htmlElement != null)    			
    				htmlElement.innerHTML= innerHtml;    				   			
    		}
    		return element;
    	}	
 
    	public static Element @class(this List<Element> elements, string className)
    	{
    		foreach(var element in elements)
				if (element.ClassName == className)
					return element;
			return null;
    	}
    	public static List<Element> @classes(this List<Element> elements, string className)
    	{
    		return (from element in elements
					where (element.ClassName == className)
					select element).toList();
    	}
 
    	public static List<string> classes(this List<Element> elements)
		{
			return (from element in elements
					where (element.ClassName != null)
					select element.ClassName).toList();
		}
 
    	public static List<Element> elements(this IElementsContainer elementsContainer)
    	{
    		return (from element in elementsContainer.Elements
    				select element).toList();
    	}
 
    	public static List<T> elements<T>(this WatiN_IE watinIe)
    		where T : Element
		{
			return (from element in watinIe.elements()
					where element is T
					select (T)element).toList();
		}
 
		public static List<string> ids(this List<Element> elements)
		{
			return (from element in elements
					where (element.Id != null)
					select element.Id).toList();
		}
 
		public static Dictionary<string,Element> byId(this List<Element> elements)
		{		
			var result = new Dictionary<string,Element>();
			foreach(var element in elements)
				if (element.Id != null)
					result.add(element.Id, element);
			return result;
		}
 
 
		public static T element<T>(this WatiN_IE watinIe, string id)
			where T : Element
		{
			return watinIe.elements().id<T>(id);
		}
 
		public static Element element(this WatiN_IE watinIe, string id)
		{
			return watinIe.elements().id(id);
		}
 
		public static Element id(this List<Element> elements, string id)
		{
			foreach(var element in elements)
				if (element.Id != null && element.Id == id)
					return element;
			return null;
		}
 
		public static List<Element> texts(this List<Element> elements, string text)
		{
			return elements.texts(text,false);
		}
 
		public static List<Element> texts(this List<Element> elements, string text, bool useRegEx)
		{
			if (useRegEx)
				return (from element in elements
						where element.text().regEx(text)
						select element).toList();
			else
				return (from element in elements
						where element.text() == text
						select element).toList();
		}
 
		public static Element text(this List<Element> elements, string text)
		{
			foreach(var element in elements)
				if (element.Id != null && element.text() == text)
					return element;
			return null;
		}
 
		public static T id<T>(this List<Element> elements, string id)
			where T : Element
		{
			var element = elements.id(id);
			if (element is T)
				return (T)element;
			return null;			
		}
 
		// TextField Extension methods
 
		public static List<string> texts(this List<TextField> textFields)
		{
			return (from textField in textFields
					select textField.text()).toList();
		}
 
		public static List<TextField> texts(this List<TextField> textFields, string text)
		{
			return (from textField in textFields
					where textField.text() == text
					select textField).toList();
		}
		public static TextField appendLine(this TextField textField, string textToAppend)
		{
			return textField.appendText(textToAppend.line());
		}
 
		public static TextField appendText(this TextField textField, string textToAppend)
		{
			if (textField!= null)
			{
				textField.value(textField.value() + textToAppend);
			}
			return textField; 
		}
 
		public static WatiN_IE set_Value(this WatiN_IE watinIe, string textFieldId, string text)
		{
			return watinIe.value(textFieldId, text);
		}
 
 
		public static WatiN_IE value(this WatiN_IE watinIe, string textFieldId, string text)
		{
			var textField = watinIe.textField(textFieldId);
			if (textField != null)
				textField.value(text);
			else
				"in WatiN_IE value, could not find textField with id: {0}".error(text);
			return watinIe;
 
		}
 
		// Link
 
		public static List<string> ids(this List<Link> links)
		{
			return (from link in links
					where (link.Id != null)
					select link.Id).toList();
		}
 
		public static bool hasLink(this WatiN_IE watinIe, string nameOrId)
		{			
			foreach(var link in watinIe.links())
				if (link.id() == nameOrId || link.text() == nameOrId)
					return true;
			return false;
			//return watinIe.links().ids().Contains(id);
		}
 
		// Button
 
		public static bool hasButton(this WatiN_IE watinIe, string nameOrId)
		{	
			foreach(var button in watinIe.buttons())
				if (button.id() == nameOrId || button.value() == nameOrId)
					return true;
			return false;
			//return watinIe.buttons().ids().Contains(id);						
		}
 
		public static WatiN_IE click(this WatiN_IE watinIe, string id)
		{
			if (watinIe.hasButton(id))
			{
				var button = watinIe.button(id);			
				button.click();
			}
			else if (watinIe.hasLink(id))
			{
				var link = watinIe.link(id);			
				link.click();
			}
			else
				"in WatiN_IE click, could not find button or link with id: {0}".error(id);
			return watinIe;
 
		}
 
		// list<string> extension methods
 
		public static bool contains(this List<String> list, string text)
		{
			if (list!=null)
				return list.Contains(text);
			return false;
		}
 
		// Panel extension methods
		public static object tag(this Panel panel)
		{
			return (object)panel.invokeOnThread(()=> panel.Tag);
		}
 
		public static T tag<T>(this Panel panel)
		{
			return (T)panel.invokeOnThread(
				()=>{
						var tag = panel.Tag;
						if (tag is T)
							return (T)tag;
						return default(T);
					});
		}
 
		public static Panel tag(this Panel panel, object tag)
		{
			panel.invokeOnThread(()=> panel.Tag = tag);
			return panel;
		}
 
		// misc
		public static bool url(this WatiN_IE watinIe, string url)
		{
			return  (watinIe.url() == url);
		}
 
		// Flashing and Hightlight
 
		public static T flash<T>(this T element)
		where T : Element
		{
			return element.flash(2);
		}
 
		public static T flash<T>(this T element, int timesToFlash)
			where T : Element
		{
			element.Flash(timesToFlash);
			return element;
		}
 
		public static T select<T>(this T element)
			where T : Element
		{
			return element.highlight();			
		}
 
		public static T highlight<T>(this T element)
			where T : Element
		{
			element.Highlight(true);
			return element;
 
		}   	
 
	  	public static O2BrowserIE editMode(this O2BrowserIE o2BrowserIE)
	  	{
	  		return (O2BrowserIE)o2BrowserIE.invokeOnThread(
	  			()=>{
	  					o2BrowserIE.HtmlEditMode = true;
	  					return o2BrowserIE;
	  				});
	  	}
 
	  	//when adding to O2 code base, make onEditedHtmlChange thread safe
	  	public static O2BrowserIE onTextChange(this O2BrowserIE o2BrowserIE, Action<string> callback)
	  	{
	  		return (O2BrowserIE)o2BrowserIE.invokeOnThread(
	  			()=>{
	  					o2BrowserIE.onEditedHtmlChange(callback);			
	  					return o2BrowserIE;
	  				});
	  	}
 
 
	  	// Reflection
 
		public static void infoTypeName(this object _object)
		{
			if (_object.notNull())
				_object.typeName().info();
			else
				"in infoTypeName _object was null".error();
		}
 
		// Logging ExtensionMethods
 
		public static void info(this bool value)
		{
			value.str().info();
		}
 
		public static string debug(this bool value)
		{
			return value.str().debug();
		}
 
 
 
	}
}

file:Extra_AST_ExtensionMethods.cs

// This file is part of the OWASP O2 Platform (http://www.owasp.org/index.php/OWASP_O2_Platform) and is released under the Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0)
using System;
using System.IO;
using System.Drawing;
using System.Threading;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Reflection;
using System.Text;
using O2.Interfaces.O2Core;
using O2.Kernel;
using O2.Kernel.ExtensionMethods;
using O2.DotNetWrappers.ExtensionMethods;
using O2.DotNetWrappers.Windows;
using O2.DotNetWrappers.DotNet;
using O2.Views.ASCX;
using O2.Views.ASCX.DataViewers; 
using O2.Views.ASCX.ExtensionMethods; 
using O2.External.SharpDevelop.AST;
using O2.External.SharpDevelop.ExtensionMethods;
using O2.API.AST.ExtensionMethods;
 
using ICSharpCode.NRefactory;
using Ast = ICSharpCode.NRefactory.Ast;
 
namespace O2.Script
{
    public static class Extra_Ast_ExtensionMethods
    {    
    	//in O2_API_AST ExtensionMethods.CSharp	
 
    	//MethodDeclaration 
 
    	public static Ast.MethodDeclaration returnType(this Ast.MethodDeclaration methodDeclaration, string returnType)
    	{
    		methodDeclaration.TypeReference = new Ast.TypeReference(returnType);
    		return methodDeclaration;
    	}
 
 
	  	// Attribute_ExtensionMethods
 
	  	public static T  add_Attribute<T>(this T attributedNode, string attributeName)
	  		where T : Ast.AttributedNode
	  	{
	  		var attribute = new Ast.Attribute(attributeName,null,null);	  		
	  		return attributedNode.add_Attribute(attribute);	  		
	  	}
 
	  	public static T add_Attribute<T>(this T attributedNode, Ast.Attribute attribute)
	  		where T : Ast.AttributedNode
	  	{	  	
	  		var attributeSection = new Ast.AttributeSection();	  			
	  		attributeSection.Attributes.Add(attribute);
			return attributedNode.add_Attribute(attributeSection);	
	  	}
 
	  	public static T add_Attribute<T>(this T attributedNode, Ast.AttributeSection attributeSection)
	  		where T : Ast.AttributedNode
		{			
	  		attributedNode.Attributes.Add(attributeSection);
	  		return attributedNode;
		}
 
		// MethodDeclaration_ExtensionMethods
		public static Ast.BlockStatement add_Body(this Ast.MethodDeclaration methodDeclaration)
		{
			var blockDeclaration = new Ast.BlockStatement();
			methodDeclaration.Body = blockDeclaration;
			return blockDeclaration;
		}
 
		public static T append<T>(this T iNode, Ast.INode iNodeToAppend)
			where T : Ast.INode
		{			
			if (iNode.hash() != iNodeToAppend.hash())
				iNode.Children.Add(iNodeToAppend);
			return iNode;
		}
 
		// BlockStatement
		public static Ast.BlockStatement body(this Ast.INode iNode)
		{ 
			if (iNode is Ast.MethodDeclaration)
				return (iNode as Ast.MethodDeclaration).Body;
			var methodDeclaration = iNode.methodDeclaration();
			if (methodDeclaration.notNull())
				return methodDeclaration.Body; 
			"method declaration for iNode: {0} was null".error(iNode);
			return null;
		}
 
		public static Ast.BlockStatement parentBlock(this Ast.INode iNode)
		{ 
			return iNode.parent<Ast.BlockStatement>();
		}
 
		// variableDeclaration 
 
		public static Ast.VariableDeclaration add_Variable(this Ast.BlockStatement blockDeclaration, string name, object value)
		{
			var primitiveValue = new Ast.PrimitiveExpression(value,value.str());
			var typeReference = new Ast.TypeReference(value.typeName());
			return blockDeclaration.add_Variable(name, primitiveValue,typeReference);
		}
		//new TypeReference("String");
		public static Ast.VariableDeclaration add_Variable(this Ast.BlockStatement blockDeclaration, string name, Ast.Expression expression)
		{
			return blockDeclaration.add_Variable(name, expression, Ast.TypeReference.Null);
		}
 
		public static Ast.VariableDeclaration add_Variable(this Ast.BlockStatement blockDeclaration, string name, Ast.Expression expression, string typeReference)
		{
			return blockDeclaration.add_Variable(name, expression, new Ast.TypeReference(typeReference));
		}
 
		public static Ast.VariableDeclaration add_Variable(this Ast.BlockStatement blockDeclaration, string name, Ast.Expression expression, Ast.TypeReference typeReference)
		{
			var variableDeclaration = new Ast.VariableDeclaration(name, expression);						
			variableDeclaration.TypeReference = typeReference;
			var localVariableDeclaration = new Ast.LocalVariableDeclaration(variableDeclaration);
			blockDeclaration.append(localVariableDeclaration);
			return variableDeclaration;
		}
 
		public static Ast.BlockStatement add_Return(this Ast.BlockStatement blockStatement, object returnData)
		{
			if (returnData.notNull())
			{
				Ast.Expression returnStatement;
				//if (returnData is Ast.ExpressionStatement)
				//returnStatement = returnData as Ast.ExpressionStatement;
				if (returnData is Ast.Expression)
					returnStatement = (returnData as Ast.Expression);
				else
					returnStatement = new Ast.PrimitiveExpression(returnData,returnData.str());
				blockStatement.append(new Ast.ReturnStatement(returnStatement));
			}
			return blockStatement;
		}
 
		//Expression 
		public static Ast.ExpressionStatement expressionStatement(this Ast.Expression expression)
		{
			return new Ast.ExpressionStatement(expression);
		}
 
		// Object
 
		public static int hash(this object _object)
		{
			if (_object != null)
				return _object.GetHashCode();
			return default(int);
		}
 
		public static bool isNull(this object _object)
		{
			return _object == null;
		}
 
		public static bool notNull(this object _object)
		{
			return _object != null;
		}
 
 
		//Method Invocation
 
		public static Ast.MemberReferenceExpression add_MemberReference(this Ast.BlockStatement blockStatement, string memberName) //, AstExpression expression)
		{
			var identifier = new Ast.IdentifierExpression(memberName);
			var memberReference = new Ast.MemberReferenceExpression(identifier,"data");
			blockStatement.append(memberReference.expressionStatement());
			return memberReference;
		}
 
		public static Ast.InvocationExpression add_Invocation(this Ast.BlockStatement blockStatement, string methodName)
		{
			return blockStatement.add_Invocation("",methodName);
		}
 
		public static Ast.InvocationExpression add_Invocation(this Ast.BlockStatement blockStatement, string typeName, string methodName, params object[] parameters) //, AstExpression expression)
		{
			if (methodName.valid().isFalse())
				return null;
 
			Ast.Expression memberExpression = null;
			if (typeName.valid())
				memberExpression = new Ast.MemberReferenceExpression(new Ast.IdentifierExpression(typeName),methodName); 
			else
				memberExpression = new Ast.IdentifierExpression(methodName);
 
			var memberReference = new Ast.InvocationExpression(memberExpression);
			if (parameters!= null)
			{
				var arguments = new List<Ast.Expression>();
				foreach(var parameter in parameters)
					if (parameter is Ast.Expression)
						arguments.add(parameter as Ast.Expression);
					else
						arguments.add(new Ast.PrimitiveExpression(parameter,parameter.str()));
 
				memberReference.Arguments = arguments;
			}
 
			blockStatement.append(memberReference.expressionStatement());
 
			return memberReference;
		}
 
 
 
 
 
     }
}
MediaWiki Appliance - Powered by TurnKey Linux