private void WriteVerifyAction(CodeMonkey monkey, OfflineAction action) { if (!action.IsOptional) { monkey.WriteLine(@"if (!controller.Actions.ContainsKey (""{0}"")) throw new UpnpDeserializationException (string.Format (""The service {{0}} claims to be of type {1} but it does not have the required action {0}."", controller.Description.Id));", action.Name, Context.Type); } else { monkey.StartWriteBlock("if (Can{0})", action.Name); } foreach (Argument argument in action.InArguments.Values) { monkey.WriteLine(@"if (!controller.Actions[""{0}""].InArguments.ContainersKey (""{1}"")) throw new UpnpDeserializationException (string.Format (""The service {{0}} claims to be of type {2} but it does not have the required argument {1} in action {0}."", controller.Description.Id));", action.Name, argument.Name, Context.Type); } foreach (Argument argument in action.OutArguments.Values) { monkey.WriteLine(@"if (!controller.Actions[""{0}""].OutArguments.ContainersKey (""{1}"")) throw new UpnpDeserializationException (string.Format (""The service {{0}} claims to be of type {2} but it does not have the required argument {1} in action {0}."", controller.Description.Id));", action.Name, argument.Name, Context.Type); } if (action.ReturnArgument != null) { monkey.WriteLine(@"if (controller.Actions[""{0}""].ReturnArgument == null || controller.Actions[""{0}""].ReturnArgument.Name != ""{1}"") throw new UpnpDeserializationException (string.Format (""The service {{0}} claims to be of type {2} but it does not have the required return argument {1} in action {0}."", controller.Description.Id));", action.Name, action.ReturnArgument.Name, Context.Type); } if (action.IsOptional) { monkey.EndWriteBlock(); } }
private void WriteVerifyAction(CodeMonkey monkey, OfflineAction action) { if (!action.IsOptional) { monkey.WriteLine (@"if (!controller.Actions.ContainsKey (""{0}"")) throw new UpnpDeserializationException (string.Format (""The service {{0}} claims to be of type {1} but it does not have the required action {0}."", controller.Description.Id));", action.Name, Context.Type); } else { monkey.StartWriteBlock ("if (Can{0})", action.Name); } foreach (Argument argument in action.InArguments.Values) { monkey.WriteLine (@"if (!controller.Actions[""{0}""].InArguments.ContainersKey (""{1}"")) throw new UpnpDeserializationException (string.Format (""The service {{0}} claims to be of type {2} but it does not have the required argument {1} in action {0}."", controller.Description.Id));", action.Name, argument.Name, Context.Type); } foreach (Argument argument in action.OutArguments.Values) { monkey.WriteLine (@"if (!controller.Actions[""{0}""].OutArguments.ContainersKey (""{1}"")) throw new UpnpDeserializationException (string.Format (""The service {{0}} claims to be of type {2} but it does not have the required argument {1} in action {0}."", controller.Description.Id));", action.Name, argument.Name, Context.Type); } if (action.ReturnArgument != null) { monkey.WriteLine (@"if (controller.Actions[""{0}""].ReturnArgument == null || controller.Actions[""{0}""].ReturnArgument.Name != ""{1}"") throw new UpnpDeserializationException (string.Format (""The service {{0}} claims to be of type {2} but it does not have the required return argument {1} in action {0}."", controller.Description.Id));", action.Name, action.ReturnArgument.Name, Context.Type); } if (action.IsOptional) { monkey.EndWriteBlock (); } }
private void WriteMethod(CodeMonkey monkey, OfflineAction action) { if (action.IsOptional) { monkey.WriteLine(@"public bool Can{0} {{ get {{ return controller.Actions.ContainsKey(""{0}""); }} }}", action.Name); } bool return_single_out_arg = action.ReturnArgument == null && action.OutArguments.Count == 1; string return_type = action.ReturnArgument != null || return_single_out_arg ? "string" : "void"; monkey.WriteLine("public {0} {1} (", return_type, action.Name); bool first = true; List <Argument> enums = new List <Argument>(); foreach (Argument argument in action.InArguments.Values) { if (first) { first = false; } else { monkey.Write(", "); } // TODO proper typing WriteArgumentParameterDefinition(monkey, argument); if (argument.RelatedStateVariable.AllowedValues != null) { enums.Add(argument); } } if (action.OutArguments.Count > 0 && !return_single_out_arg) { foreach (Argument argument in action.OutArguments.Values) { if (first) { first = false; } else { monkey.Write(", "); } monkey.Write("out string {0}", ToCamelCase(argument.Name)); } } monkey.Write(")"); monkey.StartWriteBlock(); if (action.IsOptional) { monkey.WriteLine("if (!Can{0}) throw new NotImplementedException ();", action.Name); } foreach (Argument argument in enums) { IList <string> values = argument.RelatedStateVariable.AllowedValues; monkey.WriteLine(@"if ({0} < {1}.{2} || {1}.{3} < {0}) throw new ArgumentOutOfRangeException (""{0}"");", ToCamelCase(argument.Name), EnumerationNames[argument.RelatedStateVariable], values[0], values[values.Count - 1]); } if (action.InArguments.Count > 0) { monkey.WriteLine("Dictionary<string, string> in_arguments = new Dictionary<string, string> ({0});", action.InArguments.Count); foreach (Argument argument in action.InArguments.Values) { monkey.WriteLine(@"in_arguments.Add (""{0}"", {1});", argument.Name, InArgumentAssignment(argument)); } monkey.WriteLine(@"ActionResult action_result = controller.Actions[""{0}""].Invoke (in_arguments);", action.Name); } else { monkey.WriteLine(@"ActionResult action_result = controller.Actions[""{0}""].Invoke ();", action.Name); } if (!return_single_out_arg) { foreach (Argument argument in action.OutArguments.Values) { monkey.WriteLine(@"{0} = action_result.OutValues[""{1}""];", ToCamelCase(argument.Name), argument.Name); } } if (action.ReturnArgument != null) { monkey.WriteLine("return action_result.ReturnValue;"); } else if (return_single_out_arg) { Argument out_argument = null; foreach (Argument argument in action.OutArguments.Values) { out_argument = argument; break; } monkey.WriteLine(@"return action_result.OutValues[""{0}""];", out_argument.Name); } monkey.EndWriteBlock(); monkey.WriteLine(); }
private void WriteMethod(CodeMonkey monkey, OfflineAction action) { if (action.IsOptional) { monkey.WriteLine (@"public bool Can{0} {{ get {{ return controller.Actions.ContainsKey(""{0}""); }} }}", action.Name); } bool return_single_out_arg = action.ReturnArgument == null && action.OutArguments.Count == 1; string return_type = action.ReturnArgument != null || return_single_out_arg ? "string" : "void"; monkey.WriteLine ("public {0} {1} (", return_type, action.Name); bool first = true; List<Argument> enums = new List<Argument>(); foreach (Argument argument in action.InArguments.Values) { if (first) { first = false; } else { monkey.Write (", "); } // TODO proper typing WriteArgumentParameterDefinition (monkey, argument); if(argument.RelatedStateVariable.AllowedValues != null) { enums.Add(argument); } } if (action.OutArguments.Count > 0 && !return_single_out_arg) { foreach (Argument argument in action.OutArguments.Values) { if (first) { first = false; } else { monkey.Write (", "); } monkey.Write ("out string {0}", ToCamelCase (argument.Name)); } } monkey.Write (")"); monkey.StartWriteBlock (); if (action.IsOptional) { monkey.WriteLine ("if (!Can{0}) throw new NotImplementedException ();", action.Name); } foreach (Argument argument in enums) { IList<string> values = argument.RelatedStateVariable.AllowedValues; monkey.WriteLine (@"if ({0} < {1}.{2} || {1}.{3} < {0}) throw new ArgumentOutOfRangeException (""{0}"");", ToCamelCase(argument.Name), EnumerationNames[argument.RelatedStateVariable], values[0], values[values.Count - 1]); } if (action.InArguments.Count > 0) { monkey.WriteLine ("Dictionary<string, string> in_arguments = new Dictionary<string, string> ({0});", action.InArguments.Count); foreach (Argument argument in action.InArguments.Values) { monkey.WriteLine (@"in_arguments.Add (""{0}"", {1});", argument.Name, InArgumentAssignment (argument)); } monkey.WriteLine (@"ActionResult action_result = controller.Actions[""{0}""].Invoke (in_arguments);", action.Name); } else { monkey.WriteLine (@"ActionResult action_result = controller.Actions[""{0}""].Invoke ();", action.Name); } if (!return_single_out_arg) { foreach (Argument argument in action.OutArguments.Values) { monkey.WriteLine (@"{0} = action_result.OutValues[""{1}""];", ToCamelCase (argument.Name), argument.Name); } } if (action.ReturnArgument != null) { monkey.WriteLine ("return action_result.ReturnValue;"); } else if (return_single_out_arg) { Argument out_argument = null; foreach (Argument argument in action.OutArguments.Values) { out_argument = argument; break; } monkey.WriteLine (@"return action_result.OutValues[""{0}""];", out_argument.Name); } monkey.EndWriteBlock (); monkey.WriteLine (); }