/// <summary> /// Get WADLRepresentation from a class /// </summary> /// <param name="type"></param> /// <param name="onRepresentationAdded"></param> /// <returns></returns> public static WADLRepresentation[] GetWADLRepresentations(System.Type type, WADLRepresentationAddedHandler onRepresentationAdded) { iff <bool> eval = (delegate(bool candidate, Exception exception) { if (candidate) { throw exception; } }); object[] attributes = type.GetCustomAttributes(typeof(WADLRepresentation), true); WADLRepresentation[] representations = null; if (attributes.Length == 0) { representations = new WADLRepresentation[] {}; } else { representations = (WADLRepresentation[])attributes; } return((from representation in representations select representation).Aggregate <WADLRepresentation, List <WADLRepresentation> >(new List <WADLRepresentation>(), delegate(List <WADLRepresentation> list, WADLRepresentation rep) { Condition IsValidClassRepresentation = delegate() { //only status codes other than 200 allowed at class level eval(rep.Status == HttpStatusCode.OK, new NotSupportedException("only WADLReprentations with a status other than 200 allowed in class definition")); if (rep.Type != null) { //only type that inherit from System.Exception allowed in class eval(rep.Type.IsSubclassOf(typeof(System.Exception)), new NotSupportedException("only classes that inherit from System.Exception allowed as Type attribute of WADLRepresentations in class definition")); } return true; }; if (IsValidClassRepresentation()) { rep.Validate(); } XmlSchemaSet schemas = Parse(rep); onRepresentationAdded(rep, schemas); list.Add(rep); return list; }).ToArray <WADLRepresentation>()); }
/// <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>(); }
/// <summary> /// Get WADLRepresentation from a return parameter /// </summary> /// <param name="parameter"></param> /// <param name="onRepresentationAdded"></param> /// <returns></returns> public static WADLRepresentation[] GetWADLRepresentations(ParameterInfo parameter, WADLRepresentationAddedHandler onRepresentationAdded) { iff <bool> eval = (delegate(bool candidate, Exception exception) { if (candidate) { throw exception; } }); object[] attributes = parameter.GetCustomAttributes(typeof(WADLRepresentation), true); WADLRepresentation[] representations = null; if (attributes.Length == 0) { XmlSchemaSet schemas; WADLRepresentation representation = CreateWADLRepresentation(parameter, out schemas); onRepresentationAdded(representation, schemas); return(new WADLRepresentation[] { representation }); } else { representations = (WADLRepresentation[])attributes; } bool OKSpecified = false; return((from representation in representations select representation).Aggregate <WADLRepresentation, List <WADLRepresentation> >(new List <WADLRepresentation>(), delegate(List <WADLRepresentation> list, WADLRepresentation wadlRepresentation) { wadlRepresentation.ParameterInfo = parameter; wadlRepresentation.Validate(); XmlSchemaSet schemas = Parse(wadlRepresentation); onRepresentationAdded(wadlRepresentation, schemas); if (wadlRepresentation.Status == HttpStatusCode.OK) { OKSpecified = true; } list.Add(wadlRepresentation); //atleast 1 must be Status 200 eval(list.Count == representations.Length && !OKSpecified, new NotSupportedException("No Staus 200 WADLRepresentation defined")); return list; }).ToArray <WADLRepresentation>()); }
/// <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>()); }