Example #1
0
        private void ImplementISoapableWriteTo(WsdlType t, CodeFile f)
        {
            var overrideKeyword = t.BaseType == null ? "virtual" : "override";

            using (var methodScope = f.CreateMethodScope($"public {overrideKeyword}", "void", "WriteTo", "XElement xE"))
            {
                if (t.BaseType != null)
                {
                    f.WriteLine("base.WriteTo(xE);");
                    f.WriteLine($"XmlUtility.SetXsiType(xE, \"{t.XName.Namespace}\", \"{t.XName.Name}\");");
                }
                if (t.Properties.Count > 0)
                {
                    f.WriteLine("XElement xItem = null;");
                }
                foreach (var p in t.Properties)
                {
                    bool checkNull = p.AllowMultiple || p.AllowNull;
                    if (checkNull)
                    {
                        f.WriteLine($"if ({p.CodeName} != null)");
                        f.Ocb();
                    }
                    string valueExpr = null;
                    if (p.AllowMultiple)
                    {
                        valueExpr = StringUtil.ToCamelCaseName(p.CodeName + "Item");
                        f.WriteLine($"foreach (var {valueExpr} in {p.CodeName})");
                        f.Ocb();
                    }
                    else
                    {
                        valueExpr = p.CodeName;
                        if (p.AllowNull && p.DataType.IsPrimitiveType && p.DataType.CodeName != "string" && p.DataType.CodeName != "byte[]")
                        {
                            valueExpr += ".Value";
                        }
                    }
                    f.WriteLine($"xItem = new XElement(XName.Get(\"{p.XName.Name}\", \"{p.XName.Namespace}\"));");
                    if (p.DataType.IsPrimitiveType)
                    {
                        f.WriteLine($"xItem.Add({GetSinglePrimitiveTypeToXmlValueExpression(p, valueExpr)});");
                    }
                    else
                    {
                        f.WriteLine($"{valueExpr}.WriteTo(xItem);");
                    }
                    f.WriteLine("xE.Add(xItem);");

                    if (p.AllowMultiple)
                    {
                        f.Ccb();                                      // close foreach
                    }
                    if (checkNull)
                    {
                        f.Ccb();                                // close check if null
                    }
                }
            }
        }
Example #2
0
        private void CodeBinding(WsdlBinding binding, string directory, string namespaceName)
        {
            using (var file = new CodeFile(directory, $"{binding.CodeName}.cs"))
            {
                file.WriteLine("using Microsoft.Extensions.Logging;");
                file.WriteLine("using System;");
                file.WriteLine("using System.Threading.Tasks;");
                file.WriteLine("using System.Xml.Linq;");
                file.WriteLine("");
                using (var nsScope = file.CreateNamespaceScope(namespaceName))
                {
                    using (var classScope = file.CreateClassScope("internal", binding.CodeName, "BaseSoapBinding"))
                    {
                        // constructor
                        file.WriteLine($"public {binding.CodeName}(string soapLocation, string accessToken, int timeout, bool enableGzipCompression, INetUtility net, ILogger logger)");
                        file.Indent();
                        file.WriteLine(": base(soapLocation, accessToken, timeout, enableGzipCompression, net, logger)");
                        file.Unindent();
                        file.Ocb();
                        file.Ccb();

                        foreach (var op in binding.Operations)
                        {
                            var portTypeOperation = binding.PortType.Operations.First(x => x.Name == op.Name);
                            var inputHeaderType   = op.InputSoapHeader.CodeName;
                            var outputHeaderType  = op.OutputSoapHeader.CodeName;
                            var inputType         = portTypeOperation.InputElement.CodeName;
                            var outputType        = portTypeOperation.OutputElement.CodeName;
                            file.WriteLine($"public async Task<SoapData<{outputHeaderType}, {outputType}>> {StringUtil.ToPascalCaseName(op.Name)}Async(SoapData<{inputHeaderType}, {inputType}> inData)");
                            file.Ocb();
                            file.WriteLine($"var xHeaderData = new XElement(XName.Get(\"{op.InputSoapHeader.XName.Name}\", \"{op.InputSoapHeader.XName.Namespace}\"));");
                            file.WriteLine("inData.Header.WriteTo(xHeaderData);");
                            file.WriteLine($"var xBodyData = new XElement(XName.Get(\"{portTypeOperation.InputElement.XName.Name}\", \"{portTypeOperation.InputElement.XName.Namespace}\"));");
                            file.WriteLine("inData.Body.WriteTo(xBodyData);");
                            file.WriteLine($"var outData = new SoapData<{outputHeaderType}, {outputType}>();");
                            file.WriteLine($"outData.Header = new {outputHeaderType}();");
                            file.WriteLine($"outData.Body = new {outputType}();");
                            file.WriteLine($"var faultData = new {portTypeOperation.FaultElement.CodeName}();");
                            file.WriteLine($"var isSuccessful = await GetSoapResultAsync(\"{op.SoapAction}\", xHeaderData, xBodyData, outData, faultData).ConfigureAwait(false);");

                            using (var ifScope = file.CreateScope("if (!isSuccessful)"))
                            {
                                file.WriteLine("if (faultData.Errors.Count == 1)");
                                file.Ocb();
                                file.WriteLine("throw faultData.Errors[0];");
                                file.Ccb();
                                file.WriteLine("else");
                                file.Ocb();
                                file.WriteLine("throw new AggregateException(faultData.Errors);");
                                file.Ccb();
                            }

                            file.WriteLine("return outData;");
                            file.Ccb();
                        }
                    }
                }
            }
        }
Example #3
0
 private void CodePortType(WsdlPortType portType, string directory, string namespaceName)
 {
     using (var file = new CodeFile(directory, $"{portType.CodeName}.cs"))
     {
         file.WriteLine("using System.Collections.Generic;");
         file.WriteLine("using System.Threading.Tasks;");
         file.WriteLine("");
         using (var nsScope = file.CreateNamespaceScope(namespaceName))
         {
             file.Comment(portType.Documentation);
             file.WriteLine($"public interface {portType.CodeName}");
             file.Ocb();
             foreach (var op in portType.Operations)
             {
                 var    outputType = GetPropertyTypeCode(op.OutputElement.Properties[0], true);
                 string inputs     = null;
                 if (op.InputElement.Properties.Count > 0)
                 {
                     string inputParamName = EscapeKeyword(StringUtil.ToCamelCaseName(op.InputElement.Properties[0].CodeName));
                     inputs = GetPropertyTypeCode(op.InputElement.Properties[0], true) + " " + inputParamName;
                 }
                 file.Comment(op.Documentation);
                 file.WriteLine($"Task<{outputType}> {op.CodeName}Async({inputs});");
             }
             file.Ccb();
         }
     }
 }
Example #4
0
 private void CodeInstanceCreator(WsdlStructure wsdl, string directory, string namespaceName)
 {
     using (var file = new CodeFile(directory, "InstanceCreator.cs"))
     {
         file.WriteLine("using System;");
         file.WriteLine("using System.Xml.Linq;");
         file.WriteLine("");
         using (var nsScope = file.CreateNamespaceScope(namespaceName))
         {
             var allParentTypes = wsdl.Types.Where(x => x.IsAbstract || x.Properties.Any(y => y.IsTypeProperty)).OrderBy(x => x.CodeName).ToList();
             using (var classScope = file.CreateClassScope("internal static", "InstanceCreator"))
             {
                 foreach (var parent in allParentTypes)
                 {
                     using (var methodScope = file.CreateMethodScope("public static", parent.CodeName, $"Create{parent.CodeName}", "XElement xElement"))
                     {
                         file.WriteLine("var type = XmlUtility.GetXmlTypeLocalName(xElement);");
                         bool isFirst = true;
                         foreach (var concreteType in wsdl.GetConcreteChildTypes(parent).OrderBy(x => x.CodeName))
                         {
                             var elseKeyword = isFirst ? "" : "else ";
                             file.WriteLine($"{elseKeyword}if (type == \"{concreteType.XName.Name}\")");
                             file.Ocb();
                             file.WriteLine($"return new {concreteType.CodeName}();");
                             file.Ccb();
                             isFirst = false;
                         }
                         if (parent.IsAbstract)
                         {
                             file.WriteLine("throw new ArgumentException($\"Unknown type {type}\", \"xElement\");");
                         }
                         else
                         {
                             file.WriteLine("else");
                             file.Ocb();
                             file.WriteLine($"return new {parent.CodeName}();");
                             file.Ccb();
                         }
                     }
                 }
             }
         }
     }
 }
Example #5
0
 private void CodeEnumExtensions(WsdlStructure wsdl, string directory, string namespaceName)
 {
     using (var file = new CodeFile(directory, "EnumExtensions.cs"))
     {
         file.WriteLine("using System;");
         file.WriteLine("");
         using (var nsScope = file.CreateNamespaceScope(namespaceName))
         {
             foreach (var et in wsdl.Types.OfType <WsdlEnumType>().OrderBy(x => x.CodeName))
             {
                 using (var classScope = file.CreateClassScope("public static", $"{et.CodeName}Extensions"))
                 {
                     using (var methodScope = file.CreateMethodScope("public static", "string", "ToXmlValue", $"this {et.CodeName} enumValue"))
                     {
                         file.WriteLine("switch (enumValue)");
                         file.Ocb();
                         foreach (var item in et.Items)
                         {
                             file.WriteLine($"case {et.CodeName}.{item.CodeName}: return \"{item.Name}\";");
                         }
                         file.WriteLine("default: return null;");
                         file.Ccb();
                     }
                     using (var methodScope = file.CreateMethodScope("public static", et.CodeName, "Parse", "string xmlValue"))
                     {
                         file.WriteLine("switch (xmlValue)");
                         file.Ocb();
                         foreach (var item in et.Items)
                         {
                             file.WriteLine($"case \"{item.Name}\": return {et.CodeName}.{item.CodeName};");
                         }
                         file.WriteLine($"default: throw new ArgumentException($\"Unknown value \\\"{{xmlValue}}\\\" for type {et.CodeName}.\", nameof(xmlValue));");
                         file.Ccb();
                     }
                 }
             }
         }
     }
 }
Example #6
0
        public void CodeSelectorEnums(List <string> wsdlFiles, string directory, string namespaceName)
        {
            var regex      = new Regex(@"This field can be selected using the value ""([^""]+)""");
            var enumValues = new Dictionary <string, HashSet <string> >();

            foreach (var file in wsdlFiles)
            {
                var hashSet = new HashSet <string>();
                var content = File.ReadAllText(file);
                foreach (Match match in regex.Matches(content))
                {
                    hashSet.Add(match.Groups[1].Value);
                }
                enumValues.Add(Path.GetFileNameWithoutExtension(file), hashSet);
            }
            using (var file = new CodeFile(directory, "SelectorEnums.cs"))
            {
                using (var nsScope = file.CreateNamespaceScope(namespaceName))
                {
                    foreach (var kvp in enumValues.OrderBy(x => x.Key))
                    {
                        if (kvp.Value.Count == 0)
                        {
                            continue;
                        }

                        file.WriteLine($"public enum {kvp.Key}Field");
                        file.Ocb();
                        var values    = kvp.Value.OrderBy(x => x).ToList();
                        var lastValue = values.Last();
                        foreach (var v in values)
                        {
                            file.WriteLine(v + (v == lastValue ? "" : ","));
                        }
                        file.Ccb();
                        file.WriteLine("");
                    }
                }
            }
        }
Example #7
0
 private void CodeEnums(WsdlStructure wsdl, string directory, string namespaceName)
 {
     using (var file = new CodeFile(directory, "Enums.cs"))
     {
         using (var nsScope = file.CreateNamespaceScope(namespaceName))
         {
             foreach (var t in wsdl.Types.OfType <WsdlEnumType>().OrderBy(x => x.CodeName))
             {
                 file.Comment(t.Documentation);
                 file.WriteLine($"public enum {t.CodeName}");
                 file.Ocb();
                 var lastItem = t.Items.Last();
                 foreach (var enumItem in t.Items)
                 {
                     file.Comment(enumItem.Documentation);
                     file.WriteLine(enumItem.CodeName + (enumItem == lastItem ? "" : ","));
                 }
                 file.Ccb();
                 file.WriteLine("");
             }
         }
     }
 }
Example #8
0
        private void ImplementISoapableReadFrom(WsdlType t, CodeFile f)
        {
            var overrideKeyword = t.BaseType == null ? "virtual" : "override";

            using (var methodScope = f.CreateMethodScope($"public {overrideKeyword}", "void", "ReadFrom", "XElement xE"))
            {
                if (t.BaseType != null)
                {
                    f.WriteLine("base.ReadFrom(xE);");
                }
                foreach (var p in t.Properties)
                {
                    if (p.AllowMultiple || p.AllowNull)
                    {
                        f.WriteLine($"{p.CodeName} = null;");
                    }
                }
                f.WriteLine("foreach (var xItem in xE.Elements())");
                f.Ocb();
                f.WriteLine("var localName = xItem.Name.LocalName;");
                bool isFirst = true;
                foreach (var p in t.Properties)
                {
                    f.WriteLine((isFirst ? "" : "else ") + $"if (localName == \"{p.XName.Name}\")");
                    f.Ocb();
                    if (p.AllowMultiple)
                    {
                        f.WriteLine($"if ({p.CodeName} == null) {p.CodeName} = new {GetPropertyTypeCode(p, false)}();");
                        if (p.DataType.IsPrimitiveType)
                        {
                            f.WriteLine($"{p.CodeName}.Add({GetSinglePrimitiveTypeConversionExpression(p, "xItem.Value")});");
                        }
                        else
                        {
                            string ItemVarName = StringUtil.ToCamelCaseName(p.CodeName + "Item");
                            if (p.DataType.IsAbstract || p.DataType.Properties.Any(x => x.IsTypeProperty))
                            {
                                f.WriteLine($"var {ItemVarName} = InstanceCreator.Create{p.DataType.CodeName}(xItem);");
                            }
                            else
                            {
                                f.WriteLine($"var {ItemVarName} = new {p.DataType.CodeName}();");
                            }
                            f.WriteLine($"{ItemVarName}.ReadFrom(xItem);");
                            f.WriteLine($"{p.CodeName}.Add({ItemVarName});");
                        }
                    }
                    else
                    {
                        if (p.DataType.IsPrimitiveType)
                        {
                            f.WriteLine($"{p.CodeName} = {GetSinglePrimitiveTypeConversionExpression(p, "xItem.Value")};");
                        }
                        else
                        {
                            if (p.DataType.IsAbstract || p.DataType.Properties.Any(x => x.IsTypeProperty))
                            {
                                f.WriteLine($"{p.CodeName} = InstanceCreator.Create{p.DataType.CodeName}(xItem);");
                            }
                            else
                            {
                                f.WriteLine($"{p.CodeName} = new {p.DataType.CodeName}();");
                            }
                            f.WriteLine($"{p.CodeName}.ReadFrom(xItem);");
                        }
                    }
                    f.Ccb();
                    isFirst = false;
                }
                f.Ccb();                 // close foreach
            }
        }
Example #9
0
        private void CodeService(WsdlService s, string directory, string namespaceName)
        {
            using (var file = new CodeFile(directory, $"{s.CodeName}.cs"))
            {
                file.WriteLine("using Microsoft.Extensions.Logging;");
                file.WriteLine("using System.Collections.Generic;");
                file.WriteLine("using System.Threading.Tasks;");
                file.WriteLine("");
                using (var nsScope = file.CreateNamespaceScope(namespaceName))
                {
                    using (var classScope = file.CreateClassScope("public", s.CodeName, s.Ports.Select(x => x.Binding.PortType.CodeName).ToArray()))
                    {
                        file.WriteLine("private readonly AdWordsApiConfig _config;");
                        file.WriteLine("private readonly INetUtility _netUtil;");
                        file.WriteLine("private readonly ILogger _logger;");

                        // constructor
                        file.WriteLine($"public {s.CodeName}(AdWordsApiConfig config, INetUtility netUtil, ILoggerFactory loggerFactory)");
                        file.Ocb();
                        file.WriteLine("_config = config;");
                        file.WriteLine("_netUtil = netUtil;");
                        file.WriteLine($"_logger = loggerFactory?.CreateLogger<{s.CodeName}>();");
                        file.Ccb();

                        foreach (var p in s.Ports)
                        {
                            var headerElements = new List <WsdlType>();
                            foreach (var opBinding in p.Binding.Operations)
                            {
                                var pt = p.Binding.PortType;
                                var op = pt.Operations.First(x => x.Name == opBinding.Name);

                                var    outputType     = GetPropertyTypeCode(op.OutputElement.Properties[0], true);
                                string inputs         = null;
                                string inputParamName = null;
                                if (op.InputElement.Properties.Count > 0)
                                {
                                    var prop = op.InputElement.Properties[0];
                                    inputParamName = EscapeKeyword(StringUtil.ToCamelCaseName(prop.CodeName));
                                    inputs         = GetPropertyTypeCode(prop, true) + " " + inputParamName;
                                }
                                file.Comment(op.Documentation);
                                file.WriteLine($"public async Task<{outputType}> {op.CodeName}Async({inputs})");
                                file.Ocb();
                                file.WriteLine($"var binding = new {p.Binding.CodeName}(\"{p.SoapLocation}\", _config.AccessToken, _config.Timeout, _config.EnableGzipCompression, _netUtil, _logger);");
                                file.WriteLine($"var inData = new SoapData<{opBinding.InputSoapHeader.CodeName}, {op.InputElement.CodeName}>();");
                                file.WriteLine($"inData.Header = new {opBinding.InputSoapHeader.CodeName}();");
                                file.WriteLine("AssignHeaderValues(inData.Header);");
                                if (!headerElements.Contains(opBinding.InputSoapHeader))
                                {
                                    headerElements.Add(opBinding.InputSoapHeader);
                                }
                                file.WriteLine($"inData.Body = new {op.InputElement.CodeName}();");
                                if (inputParamName != null)
                                {
                                    var prop = op.InputElement.Properties[0];
                                    if (prop.AllowMultiple)
                                    {
                                        file.WriteLine($"inData.Body.{prop.CodeName} = new {GetPropertyTypeCode(prop, false)}({inputParamName});");
                                    }
                                    else
                                    {
                                        file.WriteLine($"inData.Body.{prop.CodeName} = {inputParamName};");
                                    }
                                }
                                file.WriteLine($"var outData = await binding.{op.CodeName}Async(inData).ConfigureAwait(false);");
                                file.WriteLine($"return outData.Body.{op.OutputElement.Properties[0].CodeName};");
                                file.Ccb();
                            }
                            foreach (var hEle in headerElements)
                            {
                                file.WriteLine($"private void AssignHeaderValues({hEle.CodeName} header)");
                                file.Ocb();
                                file.WriteLine("header.ClientCustomerId = _config.ClientCustomerId;");
                                file.WriteLine("header.DeveloperToken = _config.DeveloperToken;");
                                file.WriteLine("header.PartialFailure = _config.PartialFailure;");
                                file.WriteLine("header.UserAgent = _config.UserAgent;");
                                file.WriteLine("header.ValidateOnly = _config.ValidateOnly;");
                                file.Ccb();
                            }
                        }
                    }
                }
            }
        }
Example #10
0
 public Scope(CodeFile file, string line)
 {
     _file = file;
     _file.WriteLine(line);
     _file.Ocb();
 }