Ejemplo n.º 1
0
        private void AssignComplexTypeValues(WsdlType t, XmlSchemaComplexType type)
        {
            t.XName           = type.QualifiedName;
            t.IsPrimitiveType = false;
            t.Documentation   = GetDocumentation(type.Annotation);
            t.IsAbstract      = type.IsAbstract;
            XmlSchemaObjectCollection propertyItems = null;

            if (type.ContentModel == null)
            {
                var typeSequence = type.Particle as XmlSchemaSequence;
                if (typeSequence != null)
                {
                    propertyItems = typeSequence.Items;
                }
                else
                {
                    var typeChoice = type.Particle as XmlSchemaChoice;
                    propertyItems = typeChoice.Items;
                }
            }
            else
            {
                var content    = type.ContentModel as XmlSchemaComplexContent;
                var contentExt = content.Content as XmlSchemaComplexContentExtension;
                t.BaseTypeXName = contentExt.BaseTypeName;
                var typeSequence = contentExt.Particle as XmlSchemaSequence;
                propertyItems = typeSequence.Items;
            }

            foreach (XmlSchemaElement item in propertyItems)
            {
                t.Properties.Add(CreateDataProperty(item as XmlSchemaElement));
            }
        }
Ejemplo n.º 2
0
        private WsdlType CreateSimpleType(XmlSchemaSimpleType type)
        {
            WsdlType t = null;

            if (type.TypeCode == XmlTypeCode.String)
            {
                var restriction = type.Content as XmlSchemaSimpleTypeRestriction;
                if (restriction != null)
                {
                    var enumType = new WsdlEnumType();
                    t = enumType;
                    foreach (XmlSchemaFacet facet in restriction.Facets.Cast <XmlSchemaFacet>())
                    {
                        var enumFacet = facet as XmlSchemaEnumerationFacet;
                        if (enumFacet == null)
                        {
                            continue;
                        }
                        var item = new WsdlEnumItem();
                        item.Name          = enumFacet.Value;
                        item.Documentation = GetDocumentation(enumFacet.Annotation);
                        enumType.Items.Add(item);
                    }
                }
            }
            if (t == null)
            {
                throw new Exception($"Unhandled SOAP simple type {type.QualifiedName}");
            }
            t.XName           = type.QualifiedName;
            t.IsPrimitiveType = true;
            t.Documentation   = GetDocumentation(type.Annotation);
            return(t);
        }
Ejemplo n.º 3
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
                    }
                }
            }
        }
Ejemplo n.º 4
0
 private void CodeComplexType(WsdlType t, bool isPublic, string directory, string namespaceName)
 {
     using (var file = new CodeFile(directory, $"{t.CodeName}.cs"))
     {
         file.WriteLine("using System;");
         file.WriteLine("using System.Collections.Generic;");
         file.WriteLine("using System.Xml.Linq;");
         file.WriteLine("");
         using (var nsScope = file.CreateNamespaceScope(namespaceName))
         {
             file.Comment(t.Documentation);
             string modifier = isPublic ? "public" : "internal";
             if (t.IsAbstract)
             {
                 modifier += " abstract";
             }
             var inherits = new List <string>();
             if (t.BaseType != null)
             {
                 inherits.Add(t.BaseType.CodeName);
             }
             inherits.Add("ISoapable");
             using (var classScope = file.CreateClassScope(modifier, t.CodeName, inherits.ToArray()))
             {
                 foreach (var p in t.Properties)
                 {
                     file.Comment(p.Documentation);
                     file.WriteLine($"public {GetPropertyTypeCode(p, false)} {p.CodeName} {{ get; set; }}");
                 }
                 ImplementISoapableReadFrom(t, file);
                 ImplementISoapableWriteTo(t, file);
             }
         }
     }
 }
Ejemplo n.º 5
0
 private void CodeApiError(WsdlType t, string directory, string namespaceName)
 {
     using (var file = new CodeFile(directory, $"{t.CodeName}.cs"))
     {
         file.WriteLine("using System;");
         file.WriteLine("using System.Xml.Linq;");
         file.WriteLine("");
         using (var nsScope = file.CreateNamespaceScope(namespaceName))
         {
             file.Comment(t.Documentation);
             var abstractKeyword = t.IsAbstract ? " abstract" : "";
             using (var classScope = file.CreateClassScope($"public{abstractKeyword}", t.CodeName, "Exception", "ISoapable"))
             {
                 file.WriteLine("public override string Message { get { return ErrorString; } }");
                 foreach (var p in t.Properties)
                 {
                     file.Comment(p.Documentation);
                     file.WriteLine($"public {GetPropertyTypeCode(p, false)} {p.CodeName} {{ get; set; }}");
                 }
                 ImplementISoapableReadFrom(t, file);
                 ImplementISoapableWriteTo(t, file);
             }
         }
     }
 }
Ejemplo n.º 6
0
        private void SetPrimitiveType(WsdlType dataType)
        {
            dataType.IsPrimitiveType = true;
            dataType.CodeName        = dataType.XName.Name;
            switch (dataType.CodeName)
            {
            case "boolean": dataType.CodeName = "bool"; break;

            case "base64Binary": dataType.CodeName = "byte[]"; break;
            }
        }
Ejemplo n.º 7
0
        public void AddServiceDescription(ServiceDescription wsdl)
        {
            var elementLookup = new Dictionary <string, WsdlElementType>();

            foreach (XmlSchema schema in wsdl.Types.Schemas)
            {
                foreach (XmlQualifiedName xname in schema.SchemaTypes.Names)
                {
                    var schemaObj = schema.SchemaTypes[xname] as XmlSchemaType;
                    if (_typeLookup.ContainsKey(schemaObj.QualifiedName.Name))
                    {
                        continue;
                    }
                    WsdlType t;
                    if (schemaObj is XmlSchemaSimpleType)
                    {
                        t = CreateSimpleType(schemaObj as XmlSchemaSimpleType);
                    }
                    else
                    {
                        t = new WsdlType();
                        AssignComplexTypeValues(t, schemaObj as XmlSchemaComplexType);
                    }
                    _typeLookup.Add(t.XName.Name, t);
                }
                foreach (XmlQualifiedName xname in schema.Elements.Names)
                {
                    var element = schema.Elements[xname] as XmlSchemaElement;
                    var e       = CreateElement(wsdl, element);
                    elementLookup.Add(e.XName.Name, e);
                }
            }
            _elementLookup.Add(wsdl, elementLookup);

            foreach (PortType portType in wsdl.PortTypes)
            {
                var pt = CreatePortType(wsdl, portType);
                _portTypeLookup.Add(pt.XName.Name, pt);
            }
            foreach (Binding binding in wsdl.Bindings)
            {
                var b = CreateBinding(wsdl, binding);
                _bindingLookup.Add(b.XName.Name, b);
            }
            foreach (Service service in wsdl.Services)
            {
                var s = CreateService(wsdl, service);
                _serviceLookup.Add(s.XName.Name, s);
            }
        }
Ejemplo n.º 8
0
        public IEnumerable <WsdlType> GetConcreteChildTypes(WsdlType parentType)
        {
            var subclasses = _typeLookup.Values.Where(x => x.BaseType == parentType);

            foreach (var sc in subclasses)
            {
                if (sc.IsAbstract)
                {
                    foreach (var secondSubclass in GetConcreteChildTypes(sc))
                    {
                        yield return(secondSubclass);
                    }
                }
                else
                {
                    yield return(sc);
                }
            }
        }
Ejemplo n.º 9
0
        public void AddXmlSchema(XmlSchema xsd)
        {
            foreach (XmlQualifiedName xname in xsd.SchemaTypes.Names)
            {
                var schemaObj = xsd.SchemaTypes[xname] as XmlSchemaType;
                if (_typeLookup.ContainsKey(schemaObj.QualifiedName.Name))
                {
                    continue;
                }
                WsdlType t;
                if (schemaObj is XmlSchemaSimpleType)
                {
                    t = CreateSimpleType(schemaObj as XmlSchemaSimpleType);
                }
                else
                {
                    t = new WsdlType();
                    AssignComplexTypeValues(t, schemaObj as XmlSchemaComplexType);
                }
                _typeLookup.Add(t.XName.Name, t);
            }
            Dictionary <string, WsdlElementType> elementLookup = null;

            if (_elementLookup.ContainsKey(DummyWsdl))
            {
                elementLookup = _elementLookup[DummyWsdl];
            }
            else
            {
                elementLookup = new Dictionary <string, WsdlElementType>();
                _elementLookup.Add(DummyWsdl, elementLookup);
            }
            foreach (XmlQualifiedName xname in xsd.Elements.Names)
            {
                var element = xsd.Elements[xname] as XmlSchemaElement;
                var e       = CreateElement(DummyWsdl, element);
                elementLookup.Add(e.XName.Name, e);
            }
        }
Ejemplo n.º 10
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
            }
        }