/// <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>
        /// 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>());
        }
        public override void Validate()
        {
            iff <bool> eval = (delegate(bool candidate, Exception exception)
            {
                if (candidate)
                {
                    throw exception;
                }
            });

            Condition IsSupportedType = (delegate()
            {
                if (ParameterInfo != null)
                {
                    switch (Type.GetTypeCode(ParameterInfo.ParameterType))
                    {
                    case TypeCode.String:
                        break;

                    case TypeCode.Object:
                        if (!type.IsArray || !type.IsInstanceOfType(new byte[] { }))
                        {
                            return(false);    //throw new NotSupportedException(String.Format("WADLRepresentation attribute Type not supported {0}", type.Name));
                        }
                        break;

                    case TypeCode.Empty:
                    case TypeCode.DBNull:
                    default:
                        return(false);       //throw new NotSupportedException(String.Format("WADLRepresentation attribute Type not supported {0}", type.Name));
                    }
                }
                return(true);
            });


            Condition IsValidPathHref = (delegate()
            {
                if (Path != SchemaTypes.none || !String.IsNullOrEmpty(Href))
                {
                    eval(ParameterInfo == null, new NotSupportedException("WADLRepresentation attribute with Path and Href only supported on return types"));
                    eval(!IsSupportedType(), new NotSupportedException(String.Format("WADLRepresentation attribute Type not supported {0}", ParameterInfo.ParameterType.Name)));
                    eval(Type != null, new NotSupportedException("WADLRepresentation attributes that define a Path and Href can't define a Type"));
                    eval(((Path != SchemaTypes.none && Path != SchemaTypes.anyType) && String.IsNullOrEmpty(Href)), new NotSupportedException("WADLRepresentation that define a Path must also define an Href"));
                    eval((!String.IsNullOrEmpty(Href) && (Path == SchemaTypes.none || Path == SchemaTypes.anyType)), new NotSupportedException("WADLRepresentation that define an Href must also define a Path"));
                    return(true);
                }
                else
                {
                    return(false);
                }
            });

            Condition IsValidType = (delegate()
            {
                if (Type != null)
                {
                    eval(!IsSupportedType(), new NotSupportedException(String.Format("WADLRepresentation attribute Type not supported {0}", ParameterInfo.ParameterType.Name)));
                    eval((!String.IsNullOrEmpty(Path) && !String.IsNullOrEmpty(Href)), new NotSupportedException("WADLRepresentation that define a Type must not define an Href or Path"));
                    return(true);
                }
                else
                {
                    return(false);
                }
            });


            eval(String.IsNullOrEmpty(ContentType), new NotSupportedException("WADLRepresentation attributes must define a ContentType"));

            if (!IsValidPathHref())
            {
                IsValidType();
            }
        }
Esempio n. 4
0
        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)));
            }
        }