public ClassModel(GeneratorConfiguration configuration) : base(configuration) { Properties = new List<PropertyModel>(); Interfaces = new List<InterfaceModel>(); DerivedTypes = new List<ClassModel>(); }
private static bool? IsDataTypeAttributeAllowed(XmlTypeCode typeCode, GeneratorConfiguration configuration) { bool? result; switch (typeCode) { case XmlTypeCode.AnyAtomicType: // union result = false; break; case XmlTypeCode.Integer: case XmlTypeCode.NegativeInteger: case XmlTypeCode.NonNegativeInteger: case XmlTypeCode.NonPositiveInteger: case XmlTypeCode.PositiveInteger: if (configuration.IntegerDataType != null && configuration.IntegerDataType != typeof(string)) result = false; else result = null; break; case XmlTypeCode.Base64Binary: case XmlTypeCode.HexBinary: result = true; break; default: result = null; break; } return result; }
public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set) { _configuration = configuration; _set = set; DocumentationModel.DisableComments = _configuration.DisableComments; var objectModel = new SimpleModel(_configuration) { Name = "AnyType", Namespace = CreateNamespaceModel(new Uri(XmlSchema.Namespace), AnyType), XmlSchemaName = AnyType, XmlSchemaType = null, ValueType = typeof(object), UseDataTypeAttribute = false }; var key = BuildKey(new XmlSchemaComplexType(), AnyType); Types[key] = objectModel; AttributeGroups = set.Schemas().Cast <XmlSchema>().SelectMany(s => s.AttributeGroups.Values.Cast <XmlSchemaAttributeGroup>()) .DistinctBy(g => g.QualifiedName.ToString()) .ToDictionary(g => g.QualifiedName); Groups = set.Schemas().Cast <XmlSchema>().SelectMany(s => s.Groups.Values.Cast <XmlSchemaGroup>()) .DistinctBy(g => g.QualifiedName.ToString()) .ToDictionary(g => g.QualifiedName); var dependencyOrder = new List <XmlSchema>(); var seenSchemas = new HashSet <XmlSchema>(); foreach (var schema in set.Schemas().Cast <XmlSchema>()) { ResolveDependencies(schema, dependencyOrder, seenSchemas); } foreach (var schema in dependencyOrder) { var types = set.GlobalTypes.Values.Cast <XmlSchemaType>().Where(s => s.GetSchema() == schema); CreateTypes(types); var elements = set.GlobalElements.Values.Cast <XmlSchemaElement>().Where(s => s.GetSchema() == schema); CreateElements(elements); } if (configuration.GenerateInterfaces) { RenameInterfacePropertiesIfRenamedInDerivedClasses(); RemoveDuplicateInterfaceProperties(); } AddXmlRootAttributeToAmbiguousTypes(); }
private static Type GetIntegerDerivedType(XmlSchemaDatatype type, GeneratorConfiguration configuration, IEnumerable <RestrictionModel> restrictions) { if (configuration.IntegerDataType != null && !configuration.UseIntegerDataTypeAsFallback) { return(configuration.IntegerDataType); } var xmlTypeCode = type.TypeCode; Type result = null; var maxInclusive = restrictions.OfType <MaxInclusiveRestrictionModel>().SingleOrDefault(); var minInclusive = restrictions.OfType <MinInclusiveRestrictionModel>().SingleOrDefault(); decimal?maxInclusiveValue = null; if (maxInclusive is null && xmlTypeCode == XmlTypeCode.NegativeInteger) { maxInclusiveValue = -1; }
public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set) { _configuration = configuration; _set = set; DocumentationModel.DisableComments = _configuration.DisableComments; var objectModel = new SimpleModel(_configuration) { Name = "AnyType", Namespace = CreateNamespaceModel(new Uri(XmlSchema.Namespace), AnyType), XmlSchemaName = AnyType, XmlSchemaType = null, ValueType = typeof(object), UseDataTypeAttribute = false }; Types[AnyType] = objectModel; AttributeGroups = set.Schemas().Cast <XmlSchema>().SelectMany(s => s.AttributeGroups.Values.Cast <XmlSchemaAttributeGroup>()) .DistinctBy(g => g.QualifiedName.ToString()) .ToDictionary(g => g.QualifiedName); Groups = set.Schemas().Cast <XmlSchema>().SelectMany(s => s.Groups.Values.Cast <XmlSchemaGroup>()) .DistinctBy(g => g.QualifiedName.ToString()) .ToDictionary(g => g.QualifiedName); List <XmlSchema> dependencyOrder = new List <XmlSchema>(); foreach (var schema in set.Schemas().Cast <XmlSchema>()) { ResolveDependencies(schema, dependencyOrder); } foreach (var schema in dependencyOrder) { var types = set.GlobalTypes.Values.Cast <XmlSchemaType>().Where(s => s.GetSchema() == schema); CreateTypes(types); var elements = set.GlobalElements.Values.Cast <XmlSchemaElement>().Where(s => s.GetSchema() == schema); CreateElements(elements); } }
private static Type GetEffectiveType(XmlTypeCode typeCode, XmlSchemaDatatypeVariety variety, GeneratorConfiguration configuration) { Type result; switch (typeCode) { case XmlTypeCode.AnyAtomicType: // union result = typeof(string); break; case XmlTypeCode.AnyUri: case XmlTypeCode.Duration: case XmlTypeCode.GDay: case XmlTypeCode.GMonth: case XmlTypeCode.GMonthDay: case XmlTypeCode.GYear: case XmlTypeCode.GYearMonth: case XmlTypeCode.Time: result = variety == XmlSchemaDatatypeVariety.List ? typeof(string[]) : typeof(string); break; case XmlTypeCode.Integer: case XmlTypeCode.NegativeInteger: case XmlTypeCode.NonNegativeInteger: case XmlTypeCode.NonPositiveInteger: case XmlTypeCode.PositiveInteger: if (configuration.IntegerDataType == null || configuration.IntegerDataType == typeof(string)) result = typeof(string); else { result = configuration.IntegerDataType; } break; default: result = null; break; } return result; }
private static bool?IsDataTypeAttributeAllowed(XmlTypeCode typeCode, GeneratorConfiguration configuration) { bool?result; switch (typeCode) { case XmlTypeCode.AnyAtomicType: // union result = false; break; case XmlTypeCode.Integer: case XmlTypeCode.NegativeInteger: case XmlTypeCode.NonNegativeInteger: case XmlTypeCode.NonPositiveInteger: case XmlTypeCode.PositiveInteger: if (configuration.IntegerDataType != null && configuration.IntegerDataType != typeof(string)) { result = false; } else { result = null; } break; case XmlTypeCode.Base64Binary: case XmlTypeCode.HexBinary: result = true; break; default: result = null; break; } return(result); }
protected TypeModel(GeneratorConfiguration configuration) { Configuration = configuration; Documentation = new List<DocumentationModel>(); }
public static string GetCollectionImplementationName(string typeName, GeneratorConfiguration configuration) { var typeRef = new CodeTypeReference(configuration.CollectionImplementationType ?? configuration.CollectionType); if (configuration.CollectionType.IsGenericTypeDefinition) typeRef.TypeArguments.Add(typeName); var typeOfExpr = new CodeTypeOfExpression(typeRef); var writer = new System.IO.StringWriter(); CSharpProvider.GenerateCodeFromExpression(typeOfExpr, writer, new CodeGeneratorOptions()); var fullTypeName = writer.ToString(); Debug.Assert(fullTypeName.StartsWith("typeof(") && fullTypeName.EndsWith(")")); fullTypeName = fullTypeName.Substring(7, fullTypeName.Length - 8); return fullTypeName; }
public SimpleModel(GeneratorConfiguration configuration) : base(configuration) { Restrictions = new List<RestrictionModel>(); UseDataTypeAttribute = true; }
public MinMaxLengthRestrictionModel(GeneratorConfiguration configuration) : base(configuration) { }
public static Type GetEffectiveType(this XmlSchemaDatatype type, GeneratorConfiguration configuration, IEnumerable <RestrictionModel> restrictions, bool attribute = false) { Type resultType; switch (type.TypeCode) { case XmlTypeCode.AnyAtomicType: // union resultType = typeof(string); break; case XmlTypeCode.AnyUri: case XmlTypeCode.Duration: case XmlTypeCode.GDay: case XmlTypeCode.GMonth: case XmlTypeCode.GMonthDay: case XmlTypeCode.GYear: case XmlTypeCode.GYearMonth: resultType = typeof(string); break; case XmlTypeCode.Time: resultType = typeof(DateTime); break; case XmlTypeCode.Idref: resultType = typeof(string); break; case XmlTypeCode.Integer: case XmlTypeCode.NegativeInteger: case XmlTypeCode.NonNegativeInteger: case XmlTypeCode.NonPositiveInteger: case XmlTypeCode.PositiveInteger: resultType = GetIntegerDerivedType(type, configuration, restrictions); break; default: resultType = type.ValueType; break; } if (type.Variety == XmlSchemaDatatypeVariety.List) { if (resultType.IsArray) { resultType = resultType.GetElementType(); } // XmlSerializer doesn't support xsd:list for elements, only for attributes: // https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/t84dzyst(v%3dvs.100) // Also, de/serialization fails when the XML schema type is ambiguous // DateTime -> date, datetime, or time // byte[] -> hexBinary or base64Binary if (!attribute || resultType == typeof(DateTime) || resultType == typeof(byte[])) { resultType = typeof(string); } } return(resultType); }
private static Type GetEffectiveType(XmlTypeCode typeCode, XmlSchemaDatatypeVariety variety, GeneratorConfiguration configuration) { Type result; switch (typeCode) { case XmlTypeCode.AnyAtomicType: // union result = typeof(string); break; case XmlTypeCode.AnyUri: case XmlTypeCode.Duration: case XmlTypeCode.GDay: case XmlTypeCode.GMonth: case XmlTypeCode.GMonthDay: case XmlTypeCode.GYear: case XmlTypeCode.GYearMonth: result = variety == XmlSchemaDatatypeVariety.List ? typeof(string[]) : typeof(string); break; case XmlTypeCode.Time: result = typeof(DateTime); break; case XmlTypeCode.Idref: result = typeof(string); break; case XmlTypeCode.Integer: case XmlTypeCode.NegativeInteger: case XmlTypeCode.NonNegativeInteger: case XmlTypeCode.NonPositiveInteger: case XmlTypeCode.PositiveInteger: if (configuration.IntegerDataType == null || configuration.IntegerDataType == typeof(string)) { result = typeof(string); } else { result = configuration.IntegerDataType; } break; default: result = null; break; } return(result); }
public PropertyModel(GeneratorConfiguration configuration) { Configuration = configuration; Documentation = new List<DocumentationModel>(); }
public InterfaceModel(GeneratorConfiguration configuration) : base(configuration) { Properties = new List<PropertyModel>(); Interfaces = new List<InterfaceModel>(); }
private static Type GetIntegerDerivedType(XmlSchemaDatatype type, GeneratorConfiguration configuration, IEnumerable <RestrictionModel> restrictions) { if (configuration.IntegerDataType != null) { return(configuration.IntegerDataType); } var xmlTypeCode = type.TypeCode; Type result = null; if (!(restrictions.SingleOrDefault(r => r is TotalDigitsRestrictionModel) is TotalDigitsRestrictionModel totalDigits) || ((xmlTypeCode == XmlTypeCode.PositiveInteger || xmlTypeCode == XmlTypeCode.NonNegativeInteger) && totalDigits.Value >= 30) || ((xmlTypeCode == XmlTypeCode.Integer || xmlTypeCode == XmlTypeCode.NegativeInteger || xmlTypeCode == XmlTypeCode.NonPositiveInteger) && totalDigits.Value >= 29)) { return(typeof(string)); } switch (xmlTypeCode) { case XmlTypeCode.PositiveInteger: case XmlTypeCode.NonNegativeInteger: switch (totalDigits.Value) { case int n when(n < 3): result = typeof(byte); break; case int n when(n < 5): result = typeof(ushort); break; case int n when(n < 10): result = typeof(uint); break; case int n when(n < 20): result = typeof(ulong); break; case int n when(n < 30): result = typeof(decimal); break; } break; case XmlTypeCode.Integer: case XmlTypeCode.NegativeInteger: case XmlTypeCode.NonPositiveInteger: switch (totalDigits.Value) { case int n when(n < 3): result = typeof(sbyte); break; case int n when(n < 5): result = typeof(short); break; case int n when(n < 10): result = typeof(int); break; case int n when(n < 19): result = typeof(long); break; case int n when(n < 29): result = typeof(decimal); break; } break; } return(result); }
public static bool? IsDataTypeAttributeAllowed(this XmlSchemaType type, GeneratorConfiguration configuration) { return IsDataTypeAttributeAllowed(type.TypeCode, configuration); }
public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set) { _configuration = configuration; _set = set; DocumentationModel.DisableComments = _configuration.DisableComments; var objectModel = new SimpleModel(_configuration) { Name = "AnyType", Namespace = CreateNamespaceModel(new Uri(XmlSchema.Namespace), AnyType), XmlSchemaName = AnyType, XmlSchemaType = null, ValueType = typeof(object), UseDataTypeAttribute = false }; Types[AnyType] = objectModel; AttributeGroups = set.Schemas().Cast <XmlSchema>().SelectMany(s => s.AttributeGroups.Values.Cast <XmlSchemaAttributeGroup>()) .DistinctBy(g => g.QualifiedName.ToString()) .ToDictionary(g => g.QualifiedName); Groups = set.Schemas().Cast <XmlSchema>().SelectMany(s => s.Groups.Values.Cast <XmlSchemaGroup>()) .DistinctBy(g => g.QualifiedName.ToString()) .ToDictionary(g => g.QualifiedName); foreach (var globalType in set.GlobalTypes.Values.Cast <XmlSchemaType>()) { var schema = globalType.GetSchema(); var source = string.IsNullOrEmpty(schema?.SourceUri) ? null : new Uri(schema.SourceUri); var type = CreateTypeModel(source, globalType, globalType.QualifiedName); } foreach (var rootElement in set.GlobalElements.Values.Cast <XmlSchemaElement>()) { var source = new Uri(rootElement.GetSchema().SourceUri); var qualifiedName = rootElement.ElementSchemaType.QualifiedName; if (qualifiedName.IsEmpty) { qualifiedName = rootElement.QualifiedName; } var type = CreateTypeModel(source, rootElement.ElementSchemaType, qualifiedName); if (type.RootElementName != null) { if (type is ClassModel) { // There is already another global element with this type. // Need to create an empty derived class. var derivedClassModel = new ClassModel(_configuration) { Name = ToTitleCase(rootElement.QualifiedName.Name), Namespace = CreateNamespaceModel(source, rootElement.QualifiedName) }; derivedClassModel.Documentation.AddRange(GetDocumentation(rootElement)); if (derivedClassModel.Namespace != null) { derivedClassModel.Name = derivedClassModel.Namespace.GetUniqueTypeName(derivedClassModel.Name); derivedClassModel.Namespace.Types[derivedClassModel.Name] = derivedClassModel; } Types[rootElement.QualifiedName] = derivedClassModel; derivedClassModel.BaseClass = (ClassModel)type; ((ClassModel)derivedClassModel.BaseClass).DerivedTypes.Add(derivedClassModel); derivedClassModel.RootElementName = rootElement.QualifiedName; } else { Types[rootElement.QualifiedName] = type; } } else { if (type is ClassModel classModel) { classModel.Documentation.AddRange(GetDocumentation(rootElement)); } type.RootElementName = rootElement.QualifiedName; } } }
public static Type GetEffectiveType(this XmlSchemaType type, GeneratorConfiguration configuration) { return GetEffectiveType(type.TypeCode, type.Datatype.Variety, configuration) ?? type.Datatype.ValueType; }
public EnumModel(GeneratorConfiguration configuration) : base(configuration) { Values = new List<EnumValueModel>(); }
protected RestrictionModel(GeneratorConfiguration configuration) { Configuration = configuration; }
public NamespaceModel(NamespaceKey key, GeneratorConfiguration configuration) { Configuration = configuration; Key = key; Types = new Dictionary<string, TypeModel>(); }
public FractionDigitsRestrictionModel(GeneratorConfiguration configuration) : base(configuration) { }
public static Type GetEffectiveType(this XmlSchemaType type, GeneratorConfiguration configuration) { return(GetEffectiveType(type.TypeCode, type.Datatype.Variety, configuration) ?? type.Datatype.ValueType); }
public PatternRestrictionModel(GeneratorConfiguration configuration) : base(configuration) { }
public static bool?IsDataTypeAttributeAllowed(this XmlSchemaType type, GeneratorConfiguration configuration) { return(IsDataTypeAttributeAllowed(type.TypeCode, configuration)); }
public MaxExclusiveRestrictionModel(GeneratorConfiguration configuration) : base(configuration) { }
protected ValueTypeRestrictionModel(GeneratorConfiguration configuration) : base(configuration) { }
public AtLeastOneRestrictionModel(GeneratorConfiguration configuration) : base(configuration) { }