public static WADLParam[] GetWADLParams(MethodInfo method, WADLParameterAddedHandler onParameterAdded) { return((from paramater in method.GetParameters() select paramater) .Aggregate <ParameterInfo, List <WADLParam> >(new List <WADLParam>(), delegate(List <WADLParam> list, ParameterInfo parameter) { XmlSchemaSet schemas = null; WADLParam[] parameters = WADLParam.GetWADLParams(parameter); if (parameters.Length == 0) { WADLParam param = CreateWADLParam(parameter, ref schemas); list.Add(param); onParameterAdded(param, schemas); } else { Array.ForEach <WADLParam>(parameters, delegate(WADLParam param) { param.ParameterInfo = parameter; param.Validate(); if (param.Type == SchemaTypes.none) { schemas = Parse(param); } list.Add(param); onParameterAdded(param, schemas); }); }; return list; }).ToArray <WADLParam>()); }
public static XmlSchemaSet Parse(WADLParam attribute) { //used to infer the schematype XmlSchemaSet schemas = new XmlSchemaSet(); WADLParam parameter = CreateWADLParam(attribute.ParameterInfo, ref schemas, attribute.Encoding); attribute.Type = parameter.Type; attribute.Encoding = parameter.Encoding; return(schemas); }
public static WADLParam[] GetWADLParams(MethodInfo method) { var headers = method.ReturnParameter.GetCustomAttributes(typeof(WADLParam), true); return((from header in headers select header).Aggregate <object, List <WADLParam> >(new List <WADLParam>(), delegate(List <WADLParam> list, object parameter) { WADLParam param = ((WADLParam)parameter); if (param.Style == ParamStyles.Header) { param.Validate(); list.Add(param); } else { throw new NotSupportedException(String.Format("invalid WADLParam Style {0}", param.Name)); } return list; }).ToArray <WADLParam>()); }
public static WADLParam CreateWADLParam(ParameterInfo parameter) { // we do not generate representations for primitive types. WADLParam param; System.Type type = parameter.ParameterType; switch (System.Type.GetTypeCode(type)) { case TypeCode.Char: param = new WADLParam(parameter.Name, SchemaTypes.@ushort) { ParameterInfo = parameter }; break; case TypeCode.SByte: param = new WADLParam(parameter.Name, SchemaTypes.@byte) { ParameterInfo = parameter }; break; case TypeCode.UInt16: param = new WADLParam(parameter.Name, SchemaTypes.@ushort) { ParameterInfo = parameter }; break; case TypeCode.UInt32: param = new WADLParam(parameter.Name, SchemaTypes.@uint) { ParameterInfo = parameter }; break; case TypeCode.UInt64: param = new WADLParam(parameter.Name, SchemaTypes.@ulong) { ParameterInfo = parameter }; break; case TypeCode.Boolean: param = new WADLParam(parameter.Name, SchemaTypes.boolean) { ParameterInfo = parameter }; break; case TypeCode.Byte: param = new WADLParam(parameter.Name, SchemaTypes.@ubyte) { ParameterInfo = parameter }; break; case TypeCode.Int16: param = new WADLParam(parameter.Name, SchemaTypes.@short) { ParameterInfo = parameter }; break; case TypeCode.Int32: param = new WADLParam(parameter.Name, SchemaTypes.@int) { ParameterInfo = parameter }; break; case TypeCode.Int64: param = new WADLParam(parameter.Name, SchemaTypes.@long) { ParameterInfo = parameter }; break; case TypeCode.Single: param = new WADLParam(parameter.Name, SchemaTypes.@float) { ParameterInfo = parameter }; break; case TypeCode.Double: param = new WADLParam(parameter.Name, SchemaTypes.@double) { ParameterInfo = parameter }; break; case TypeCode.Decimal: param = new WADLParam(parameter.Name, SchemaTypes.@decimal) { ParameterInfo = parameter }; break; case TypeCode.DateTime: param = new WADLParam(parameter.Name, SchemaTypes.dateTime) { ParameterInfo = parameter }; break; case TypeCode.String: param = new WADLParam(parameter.Name, SchemaTypes.@string) { ParameterInfo = parameter }; break; case TypeCode.Object: //we generate for complex types param = new WADLParam(parameter.Name) { ParameterInfo = parameter }; break; case TypeCode.Empty: case TypeCode.DBNull: default: throw new NotSupportedException("unsupported data type"); } OptionalAttribute[] optional = (OptionalAttribute[])parameter.GetCustomAttributes(typeof(OptionalAttribute), true); DefaultValueAttribute[] defaults = (DefaultValueAttribute[])parameter.GetCustomAttributes(typeof(DefaultValueAttribute), true); return(param); }
public override void Validate() { iff <bool> eval = (delegate(bool candidate, Exception exception) { if (candidate) { throw exception; } }); //must have a name eval(String.IsNullOrEmpty(Name), new NotSupportedException("empty WADLParam name property")); if (this.Style == ParamStyles.Query || this.Style == ParamStyles.Form || this.Style == ParamStyles.Template) { //out params not supported eval(ParameterInfo.IsOut, new NotSupportedException(String.Format("[out] parameters {0}", this.ParameterInfo.Member.ToString()))); //void not supported //eval(ParameterInfo.Name == null && ParameterInfo.ParameterType.Name.ToLower() == "void", new NotSupportedException(String.Format("[void] {0}", this.ParameterInfo.Member.ToString()))); //optional params must have a default value (since possibility of multi attribute test against [this]. eval(!this.IsRequired && String.IsNullOrEmpty(this.Default), new NotSupportedException("optional WADLParam must supply a default value")); //optional params are not supported on Template may bot need the one above? eval(!this.IsRequired && this.Style == ParamStyles.Template, new NotSupportedException("optional WADLParam only supported on ParamStyles.Query")); //type must match parameter except in the case of multiple params Condition IsValidType = (delegate() { switch (System.Type.GetTypeCode(parameterInfo.ParameterType)) { case TypeCode.Char: return(Type == WADL.SchemaTypes.@ushort); case TypeCode.SByte: return(Type == WADL.SchemaTypes.@byte); case TypeCode.UInt16: return(Type == WADL.SchemaTypes.@ushort); case TypeCode.UInt32: return(Type == WADL.SchemaTypes.@uint); case TypeCode.UInt64: return(Type == WADL.SchemaTypes.@ulong); case TypeCode.Boolean: return(Type == WADL.SchemaTypes.boolean); case TypeCode.Byte: return(Type == WADL.SchemaTypes.@ubyte); case TypeCode.Int16: return(Type == WADL.SchemaTypes.@short); case TypeCode.Int32: return(Type == WADL.SchemaTypes.@int); case TypeCode.Int64: return(Type == WADL.SchemaTypes.@long); case TypeCode.Single: return(Type == WADL.SchemaTypes.@float); case TypeCode.Double: return(Type == WADL.SchemaTypes.@double); case TypeCode.Decimal: return(Type == WADL.SchemaTypes.@decimal); case TypeCode.DateTime: return(Type == WADL.SchemaTypes.dateTime); case TypeCode.String: return(true); //can be any type in the event of a mismatch the app must catch it case TypeCode.Object: //check this WADLParam[] parameters = WADLParam.GetWADLParams(parameterInfo); Condition IsValidMutipleParams = (delegate() { MethodInfo methodInfo = ((MethodInfo)parameterInfo.Member); return(parameters.Length >= 1 && (methodInfo.GetParameters().Length == 1 && (parameterInfo.ParameterType.IsInstanceOfType(new object[] { }) || parameterInfo.ParameterType.IsInstanceOfType(new string[] { }) || parameterInfo.ParameterType.IsInstanceOfType(new NameValueCollection())))); }); if (parameters.Length > 1) { eval(!IsValidMutipleParams(), new NotSupportedException(String.Format("multiple WADLParam attributes {0} in {1} only supported on methods with a single value of object[], string[], or NameValueCollection", parameterInfo.Member.ToString(), parameterInfo.Member.DeclaringType.Name))); return(true); } else { return(Type == SchemaTypes.none); } case TypeCode.Empty: case TypeCode.DBNull: default: throw new NotSupportedException(String.Format("datatype {0}", parameterInfo.ParameterType.Name)); } }); eval(!IsValidType(), new InvalidCastException(String.Format("[WADLParam({0})]{1} in {2}", this.Name, this.ParameterInfo.Member.ToString(), ParameterInfo.Member.DeclaringType.Name))); } else { //header params eval(this.type == SchemaTypes.none, new NotSupportedException(String.Format("invalid WADLParam SchemaType {0}", Name))); eval(this.encoding != Encodings.plainText, new NotSupportedException(String.Format("invalid WADLParam Encoding {0}", Name))); } }
/// <summary> /// /// </summary> /// <param name="type"></param> /// <returns></returns> public static WADLMethod[] GetWADLMethods(Type type, WADLParameterAddedHandler onWADLParameterAdded, WADLRepresentationAddedHandler onWADLRepresentationAdded) { State EmptyWADLMethodFlag = State.None; var headers = WADLParam.GetWADLParams(type); return((from method in type.GetMethods(BindingFlags.Instance | BindingFlags.Public) select method).Where <MethodInfo>(delegate(MethodInfo member) { return WADLMethod.GetWADLMethod(member) != null; }).Aggregate <MethodInfo, List <WADLMethod> >(new List <WADLMethod>(), delegate(List <WADLMethod> list, MethodInfo member) { WADLMethod method = WADLMethod.GetWADLMethod(member); method.doc = WADLDoc.GetWADLDoc(member); if (String.IsNullOrEmpty(method.MethodName)) { if ((EmptyWADLMethodFlag & State.EmptyWADLMethod) == State.EmptyWADLMethod) { //throw new NotSupportedException("only one WADLMethod can be defined without a MethodName"); } else { EmptyWADLMethodFlag = State.EmptyWADLMethod | State.EmptyWADLMethodCurrent; } } WADLParam.GetWADLParams(method.MethodInfo, delegate(WADLParam wadlParam, XmlSchemaSet schemas) { if ((EmptyWADLMethodFlag & State.EmptyWADLMethodCurrent) == State.EmptyWADLMethodCurrent && wadlParam.Style == ParamStyles.Template) { EmptyWADLMethodFlag |= State.EmptyWADLMethodCurrentWithWADLPathVariable; } if (method.Parameters.Exists(delegate(WADLParam param) { return param.Name.ToLower() == wadlParam.Name.ToLower() && param.Style == wadlParam.Style; })) { throw new NotSupportedException(String.Format("Duplicate WADLParam {0}", wadlParam.Name)); } else { method.Parameters.Add(wadlParam); if (onWADLParameterAdded != null) { onWADLParameterAdded(wadlParam, schemas); } } }); WADLParam[] methodheaders = WADLParam.GetWADLParams(member); headers.Union <object>(methodheaders.Except <object>(headers, ((IEqualityComparer <object>) new WADLParam())) ).Aggregate <object, List <WADLParam> >(method.Headers, delegate(List <WADLParam> aggregator, object header){ aggregator.Add((WADLParam)header); return aggregator; }); if ((EmptyWADLMethodFlag & State.EmptyWADLMethodCurrent) == State.EmptyWADLMethodCurrent) { if ((EmptyWADLMethodFlag & State.EmptyWADLMethodCurrentWithWADLPathVariable) == State.EmptyWADLMethodCurrentWithWADLPathVariable) { EmptyWADLMethodFlag = EmptyWADLMethodFlag ^ State.EmptyWADLMethodCurrent; } //else //obsolete? //throw new InvalidOperationException("WADLMethods without a MethodName must define at least one WADLParam Template"); } WADLRepresentation.GetWADLRepresentations(member.ReturnParameter, delegate(WADLRepresentation rep, XmlSchemaSet schemas) { method.Representations.Add(rep); onWADLRepresentationAdded(rep, schemas); }); WADLRepresentation.GetWADLRepresentations(member.DeclaringType, delegate(WADLRepresentation rep, XmlSchemaSet schemas) { if (!method.Representations.Contains(rep, (IEqualityComparer <WADLRepresentation>) new WADLRepresentation())) { method.Representations.Add(rep); onWADLRepresentationAdded(rep, schemas); } }); list.Add(method); return list; }).ToArray <WADLMethod>()); }