internal static ClrSimpleTypeInfo CreateSimpleTypeInfo(XmlSchemaType type) { ClrSimpleTypeInfo typeInfo = null; Debug.Assert(type.Datatype != null); switch (type.Datatype.Variety) { case XmlSchemaDatatypeVariety.Atomic: { typeInfo = new AtomicSimpleTypeInfo(type); break; } case XmlSchemaDatatypeVariety.List: { typeInfo = new ListSimpleTypeInfo(type); break; } case XmlSchemaDatatypeVariety.Union: { typeInfo = new UnionSimpleTypeInfo(type); break; } } return(typeInfo); }
public static void GetCreateValueExpression(object value, ClrSimpleTypeInfo typeDef, CodeExpressionCollection collection) { if (value == null) { collection.Add(new CodePrimitiveExpression(value)); return; } switch (typeDef.Variety) { case XmlSchemaDatatypeVariety.List: string str = ListSimpleTypeValidator.ToString(value); collection.Add(new CodePrimitiveExpression(str)); break; case XmlSchemaDatatypeVariety.Atomic: if (value is string) { collection.Add(new CodePrimitiveExpression(value)); } else { collection.Add(CreateTypedValueExpression(typeDef.InnerType.Datatype, value)); } break; case XmlSchemaDatatypeVariety.Union: GetCreateUnionValueExpression(value, typeDef as UnionSimpleTypeInfo, collection); break; default: break; } }
public static void GetCreateUnionValueExpression(object value, UnionSimpleTypeInfo unionDef, CodeExpressionCollection collection) { Debug.Assert(unionDef != null); //Use reflection to get real value and type from "value", which is an XsdSimpleValue object typedValue = value.GetType().InvokeMember("TypedValue", BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance, null, value, null, CultureInfo.InvariantCulture); CodeExpressionCollection dummy = new CodeExpressionCollection(); ClrSimpleTypeInfo matchingType = null; foreach (ClrSimpleTypeInfo type in unionDef.MemberTypes) { try { GetCreateValueExpression(typedValue, type, dummy); matchingType = type; break; } catch (Exception) { continue; } } Debug.Assert(matchingType != null); GetCreateValueExpression(typedValue, matchingType, collection); }
public IEnumerable <CodeNamespace> GenerateTypes(ClrMappingInfo binding) { if (binding == null) { throw new ArgumentException("binding"); } nameMappings = binding.NameMappings; Debug.Assert(nameMappings != null); foreach (ClrTypeInfo type in binding.Types) { if (type.IsWrapper) { if (wrapperRootElements == null) { wrapperRootElements = new List <ClrWrapperTypeInfo>(); } wrapperRootElements.Add(type as ClrWrapperTypeInfo); } else { codeNamespace = GetCodeNamespace(type.clrtypeNs); ClrSimpleTypeInfo stInfo = type as ClrSimpleTypeInfo; if (stInfo != null) { if (stInfo is EnumSimpleTypeInfo enumTypeInfo) { codeNamespace.Types.Add(TypeBuilder.CreateEnumType(enumTypeInfo, settings)); } codeNamespace.Types.Add(TypeBuilder.CreateSimpleType(stInfo, nameMappings, settings)); } else { CodeTypeDeclaration decl = ProcessType(type as ClrContentTypeInfo, null, true); //Sets current codeNamespace codeNamespace.Types.Add(decl); if (type.IsRootElement) { List <CodeTypeDeclaration> types; if (!xroots.TryGetValue(codeNamespace, out types)) { types = new List <CodeTypeDeclaration>(); xroots.Add(codeNamespace, types); } types.Add(decl); } } } } ProcessWrapperTypes(); CreateTypeManager(); CreateXRoots(); return(codeNamespacesTable.Values); }
internal static CodeExpression MaterializeSimpleTypeDef( ClrSimpleTypeInfo typeInfo, Dictionary <XmlSchemaObject, string> nameMappings, LinqToXsdSettings settings) { CodeObjectCreateExpression simpleTypeCreate = null; CodeExpressionCollection expressions = null; switch (typeInfo.Variety) { case XmlSchemaDatatypeVariety.Atomic: simpleTypeCreate = new CodeObjectCreateExpression( Constants.AtomicSimpleTypeValidator); expressions = simpleTypeCreate.Parameters; expressions.Add(CreateGetBuiltInSimpleType(typeInfo.TypeCode)); expressions.Add(CreateFacets(typeInfo)); break; case XmlSchemaDatatypeVariety.List: simpleTypeCreate = new CodeObjectCreateExpression( Constants.ListSimpleTypeValidator); expressions = simpleTypeCreate.Parameters; expressions.Add(CreateGetBuiltInSimpleType(typeInfo.TypeCode)); expressions.Add(CreateFacets(typeInfo)); ListSimpleTypeInfo listType = typeInfo as ListSimpleTypeInfo; ClrSimpleTypeInfo itemType = listType.ItemType; expressions.Add(CreateSimpleTypeDef( itemType, nameMappings, settings, true)); break; case XmlSchemaDatatypeVariety.Union: simpleTypeCreate = new CodeObjectCreateExpression( Constants.UnionSimpleTypeValidator); expressions = simpleTypeCreate.Parameters; expressions.Add(CreateGetBuiltInSimpleType(typeInfo.TypeCode)); expressions.Add(CreateFacets(typeInfo)); UnionSimpleTypeInfo unionType = typeInfo as UnionSimpleTypeInfo; CodeArrayCreateExpression memberTypeCreate = new CodeArrayCreateExpression(); memberTypeCreate.CreateType = new CodeTypeReference( Constants.SimpleTypeValidator); foreach (ClrSimpleTypeInfo st in unionType.MemberTypes) { memberTypeCreate.Initializers.Add(CreateSimpleTypeDef( st, nameMappings, settings, true)); } expressions.Add(memberTypeCreate); break; } return(simpleTypeCreate); }
internal static CodeExpression CreateSimpleTypeDef(ClrSimpleTypeInfo typeInfo, Dictionary<XmlSchemaObject, string> nameMappings, LinqToXsdSettings settings, bool memberOrItemType) { //If the enclosed member type or item type is a global named type, reuse the definition if (memberOrItemType && typeInfo.IsGlobal) { typeInfo.UpdateClrTypeName(nameMappings, settings); return CodeDomHelper.CreateFieldReference(typeInfo.clrtypeName, Constants.SimpleTypeDefInnerType); } else { return MaterializeSimpleTypeDef(typeInfo, nameMappings, settings); } }
internal static CodeExpression MaterializeSimpleTypeDef( ClrSimpleTypeInfo typeInfo, Dictionary<XmlSchemaObject, string> nameMappings, LinqToXsdSettings settings) { CodeObjectCreateExpression simpleTypeCreate = null; CodeExpressionCollection expressions = null; switch(typeInfo.Variety) { case XmlSchemaDatatypeVariety.Atomic: simpleTypeCreate = new CodeObjectCreateExpression( Constants.AtomicSimpleTypeValidator); expressions = simpleTypeCreate.Parameters; expressions.Add(CreateGetBuiltInSimpleType(typeInfo.TypeCode)); expressions.Add(CreateFacets(typeInfo)); break; case XmlSchemaDatatypeVariety.List: simpleTypeCreate = new CodeObjectCreateExpression( Constants.ListSimpleTypeValidator); expressions = simpleTypeCreate.Parameters; expressions.Add(CreateGetBuiltInSimpleType(typeInfo.TypeCode)); expressions.Add(CreateFacets(typeInfo)); ListSimpleTypeInfo listType = typeInfo as ListSimpleTypeInfo; ClrSimpleTypeInfo itemType = listType.ItemType; expressions.Add(CreateSimpleTypeDef( itemType, nameMappings, settings, true)); break; case XmlSchemaDatatypeVariety.Union: simpleTypeCreate = new CodeObjectCreateExpression( Constants.UnionSimpleTypeValidator); expressions = simpleTypeCreate.Parameters; expressions.Add(CreateGetBuiltInSimpleType(typeInfo.TypeCode)); expressions.Add(CreateFacets(typeInfo)); UnionSimpleTypeInfo unionType = typeInfo as UnionSimpleTypeInfo; CodeArrayCreateExpression memberTypeCreate = new CodeArrayCreateExpression(); memberTypeCreate.CreateType = new CodeTypeReference( Constants.SimpleTypeValidator); foreach (ClrSimpleTypeInfo st in unionType.MemberTypes) { memberTypeCreate.Initializers.Add(CreateSimpleTypeDef( st, nameMappings, settings, true)); } expressions.Add(memberTypeCreate); break; } return simpleTypeCreate; }
internal static CodeExpression MaterializeSimpleTypeDef(ClrSimpleTypeInfo typeInfo, Dictionary <XmlSchemaObject, string> nameMappings, LinqToXsdSettings settings) { CodeObjectCreateExpression simpleTypeCreate = null; CodeExpressionCollection expressions = null; switch (typeInfo.Variety) { case XmlSchemaDatatypeVariety.Atomic: { simpleTypeCreate = new CodeObjectCreateExpression("Xml.Schema.Linq.AtomicSimpleTypeValidator", new CodeExpression[0]); expressions = simpleTypeCreate.Parameters; expressions.Add(SimpleTypeCodeDomHelper.CreateGetBuiltInSimpleType(typeInfo.TypeCode)); expressions.Add(SimpleTypeCodeDomHelper.CreateFacets(typeInfo)); break; } case XmlSchemaDatatypeVariety.List: { simpleTypeCreate = new CodeObjectCreateExpression("Xml.Schema.Linq.ListSimpleTypeValidator", new CodeExpression[0]); expressions = simpleTypeCreate.Parameters; expressions.Add(SimpleTypeCodeDomHelper.CreateGetBuiltInSimpleType(typeInfo.TypeCode)); expressions.Add(SimpleTypeCodeDomHelper.CreateFacets(typeInfo)); ClrSimpleTypeInfo itemType = (typeInfo as ListSimpleTypeInfo).ItemType; expressions.Add(SimpleTypeCodeDomHelper.CreateSimpleTypeDef(itemType, nameMappings, settings, true)); break; } case XmlSchemaDatatypeVariety.Union: { simpleTypeCreate = new CodeObjectCreateExpression("Xml.Schema.Linq.UnionSimpleTypeValidator", new CodeExpression[0]); expressions = simpleTypeCreate.Parameters; expressions.Add(SimpleTypeCodeDomHelper.CreateGetBuiltInSimpleType(typeInfo.TypeCode)); expressions.Add(SimpleTypeCodeDomHelper.CreateFacets(typeInfo)); UnionSimpleTypeInfo unionType = typeInfo as UnionSimpleTypeInfo; CodeArrayCreateExpression memberTypeCreate = new CodeArrayCreateExpression() { CreateType = new CodeTypeReference("Xml.Schema.Linq.SimpleTypeValidator") }; ClrSimpleTypeInfo[] memberTypes = unionType.MemberTypes; for (int i = 0; i < (int)memberTypes.Length; i++) { ClrSimpleTypeInfo st = memberTypes[i]; memberTypeCreate.Initializers.Add(SimpleTypeCodeDomHelper.CreateSimpleTypeDef(st, nameMappings, settings, true)); } expressions.Add(memberTypeCreate); break; } } return(simpleTypeCreate); }
public IEnumerable <CodeNamespace> GenerateTypes(ClrMappingInfo binding) { List <CodeTypeDeclaration> types; if (binding == null) { throw new ArgumentException("binding"); } this.nameMappings = binding.NameMappings; Debug.Assert(this.nameMappings != null); foreach (ClrTypeInfo type in binding.Types) { if (!type.IsWrapper) { this.codeNamespace = this.GetCodeNamespace(type.clrtypeNs); ClrSimpleTypeInfo stInfo = type as ClrSimpleTypeInfo; if (stInfo == null) { CodeTypeDeclaration decl = this.ProcessType(type as ClrContentTypeInfo, null, true); this.codeNamespace.Types.Add(decl); if (type.IsRootElement) { if (!this.xroots.TryGetValue(this.codeNamespace, out types)) { types = new List <CodeTypeDeclaration>(); this.xroots.Add(this.codeNamespace, types); } types.Add(decl); } } else { this.codeNamespace.Types.Add(TypeBuilder.CreateSimpleType(stInfo, this.nameMappings, this.settings)); } } else { if (this.wrapperRootElements == null) { this.wrapperRootElements = new List <ClrWrapperTypeInfo>(); } this.wrapperRootElements.Add(type as ClrWrapperTypeInfo); } } this.ProcessWrapperTypes(); this.CreateTypeManager(); this.CreateXRoots(); return(this.codeNamespacesTable.Values); }
internal void TypesToTypes() { foreach (XmlSchemaType st in schemas.GlobalTypes.Values) { XmlSchemaSimpleType simpleType = st as XmlSchemaSimpleType; if (simpleType != null) { SymbolEntry symbol = symbolTable.AddType(simpleType); string xsdNamespace = simpleType.QualifiedName.Namespace; ClrSimpleTypeInfo typeInfo = ClrSimpleTypeInfo .CreateSimpleTypeInfo(simpleType); //Create corresponding simple type info objects typeInfo.IsAbstract = false; typeInfo.clrtypeName = symbol.identifierName; typeInfo.clrtypeNs = symbol.clrNamespace; typeInfo.schemaName = symbol.symbolName; typeInfo.schemaNs = xsdNamespace; typeInfo.typeOrigin = SchemaOrigin.Fragment; BuildAnnotationInformation(typeInfo, st); binding.Types.Add(typeInfo); } else { XmlSchemaComplexType ct = st as XmlSchemaComplexType; if (ct != null && ct.TypeCode != XmlTypeCode.Item) { SymbolEntry symbol = symbolTable.AddType(ct); string xsdNamespace = ct.QualifiedName.Namespace; localSymbolTable.Init(symbol.identifierName); ClrContentTypeInfo typeInfo = new ClrContentTypeInfo(); typeInfo.IsAbstract = ct.IsAbstract; typeInfo.IsSealed = ct.IsFinal(); typeInfo.clrtypeName = symbol.identifierName; typeInfo.clrtypeNs = symbol.clrNamespace; typeInfo.schemaName = symbol.symbolName; typeInfo.schemaNs = xsdNamespace; typeInfo.typeOrigin = SchemaOrigin.Fragment; typeInfo.baseType = BaseType(ct); BuildProperties(null, ct, typeInfo); BuildNestedTypes(typeInfo); BuildAnnotationInformation(typeInfo, ct); binding.Types.Add(typeInfo); } } } }
internal void AddSimpleType(XmlQualifiedName name, XmlSchemaSimpleType simpleType) { SymbolEntry symbol = this.symbolTable.AddType(name, simpleType); string xsdNamespace = simpleType.QualifiedName.Namespace; ClrSimpleTypeInfo typeInfo = ClrSimpleTypeInfo.CreateSimpleTypeInfo(simpleType); typeInfo.IsAbstract = false; typeInfo.clrtypeName = symbol.identifierName; typeInfo.clrtypeNs = symbol.clrNamespace; typeInfo.schemaName = symbol.symbolName; typeInfo.schemaNs = xsdNamespace; typeInfo.typeOrigin = SchemaOrigin.Fragment; this.BuildAnnotationInformation(typeInfo, simpleType); this.binding.Types.Add(typeInfo); }
internal static CodeExpression CreateSimpleTypeDef(ClrSimpleTypeInfo typeInfo, Dictionary <XmlSchemaObject, string> nameMappings, LinqToXsdSettings settings, bool memberOrItemType) { //If the enclosed member type or item type is a global named type, reuse the definition if (memberOrItemType && typeInfo.IsGlobal) { typeInfo.UpdateClrTypeName(nameMappings, settings); return(CodeDomHelper.CreateFieldReference(typeInfo.clrtypeName, Constants.SimpleTypeDefInnerType)); } else { return(MaterializeSimpleTypeDef(typeInfo, nameMappings, settings)); } }
internal static CodeExpression CreateSimpleTypeDef(ClrSimpleTypeInfo typeInfo, Dictionary <XmlSchemaObject, string> nameMappings, LinqToXsdSettings settings, bool memberOrItemType) { CodeExpression codeExpression; if ((!memberOrItemType ? true : !typeInfo.IsGlobal)) { codeExpression = SimpleTypeCodeDomHelper.MaterializeSimpleTypeDef(typeInfo, nameMappings, settings); } else { typeInfo.UpdateClrTypeName(nameMappings, settings); codeExpression = CodeDomHelper.CreateFieldReference(typeInfo.clrtypeName, "TypeDefinition"); } return(codeExpression); }
internal static ClrSimpleTypeInfo CreateSimpleTypeInfo(XmlSchemaType type) { ClrSimpleTypeInfo typeInfo = null; Debug.Assert(type.Datatype != null); switch (type.Datatype.Variety) { case XmlSchemaDatatypeVariety.Atomic: if (type is XmlSchemaSimpleType simpleType && simpleType.IsEnum()) { typeInfo = new EnumSimpleTypeInfo(simpleType); } else { typeInfo = new AtomicSimpleTypeInfo(type); } break;
private void ProcessNestedTypes(List <ClrTypeInfo> anonymousTypes, CodeTypeDeclaration parentTypeDecl, string parentIdentifier) { foreach (ClrTypeInfo nestedType in anonymousTypes) { ClrSimpleTypeInfo stInfo = nestedType as ClrSimpleTypeInfo; CodeTypeDeclaration decl = null; if (stInfo != null) { decl = TypeBuilder.CreateSimpleType(stInfo, nameMappings, settings); decl.TypeAttributes = System.Reflection.TypeAttributes.NestedPrivate; //Anonymous simple types are private within the scope of the parent class } else { decl = ProcessType(nestedType as ClrContentTypeInfo, parentIdentifier, false); } parentTypeDecl.Members.Add(decl); } }
private void ProcessNestedTypes(List <ClrTypeInfo> anonymousTypes, CodeTypeDeclaration parentTypeDecl, string parentIdentifier) { foreach (ClrTypeInfo nestedType in anonymousTypes) { ClrSimpleTypeInfo stInfo = nestedType as ClrSimpleTypeInfo; CodeTypeDeclaration decl = null; if (stInfo == null) { decl = this.ProcessType(nestedType as ClrContentTypeInfo, parentIdentifier, false); } else { decl = TypeBuilder.CreateSimpleType(stInfo, this.nameMappings, this.settings); decl.TypeAttributes = TypeAttributes.NestedPrivate; } parentTypeDecl.Members.Add(decl); } }
internal static CodeTypeDeclaration CreateSimpleType(ClrSimpleTypeInfo typeInfo, Dictionary <XmlSchemaObject, string> nameMappings, LinqToXsdSettings settings) { CodeTypeDeclaration simpleTypeDecl = new CodeTypeDeclaration(typeInfo.clrtypeName) { TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed }; CodeConstructor privateConst = new CodeConstructor() { Attributes = MemberAttributes.Private }; simpleTypeDecl.Members.Add(privateConst); CodeMemberField typeField = CodeDomHelper.CreateMemberField("TypeDefinition", "Xml.Schema.Linq.SimpleTypeValidator", MemberAttributes.Abstract | MemberAttributes.Final | MemberAttributes.Static | MemberAttributes.FamilyAndAssembly | MemberAttributes.FamilyOrAssembly | MemberAttributes.Public, false); typeField.InitExpression = SimpleTypeCodeDomHelper.CreateSimpleTypeDef(typeInfo, nameMappings, settings, false); simpleTypeDecl.Members.Add(typeField); TypeBuilder.ApplyAnnotations(simpleTypeDecl, typeInfo); return(simpleTypeDecl); }
public static void GetCreateValueExpression(object value, ClrSimpleTypeInfo typeDef, CodeExpressionCollection collection) { if (value != null) { switch (typeDef.Variety) { case XmlSchemaDatatypeVariety.Atomic: { if (!(value is string)) { collection.Add(SimpleTypeCodeDomHelper.CreateTypedValueExpression(typeDef.InnerType.Datatype, value)); } else { collection.Add(new CodePrimitiveExpression(value)); } break; } case XmlSchemaDatatypeVariety.List: { string str = ListSimpleTypeValidator.ToString(value); collection.Add(new CodePrimitiveExpression(str)); break; } case XmlSchemaDatatypeVariety.Union: { SimpleTypeCodeDomHelper.GetCreateUnionValueExpression(value, typeDef as UnionSimpleTypeInfo, collection); break; } } } else { collection.Add(new CodePrimitiveExpression(value)); } }
public static void GetCreateUnionValueExpression(object value, UnionSimpleTypeInfo unionDef, CodeExpressionCollection collection) { Debug.Assert(unionDef != null); object typedValue = value.GetType().InvokeMember("TypedValue", BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty, null, value, null, CultureInfo.InvariantCulture); CodeExpressionCollection dummy = new CodeExpressionCollection(); ClrSimpleTypeInfo matchingType = null; ClrSimpleTypeInfo[] memberTypes = unionDef.MemberTypes; for (int i = 0; i < (int)memberTypes.Length; i++) { ClrSimpleTypeInfo type = memberTypes[i]; try { SimpleTypeCodeDomHelper.GetCreateValueExpression(typedValue, type, dummy); matchingType = type; break; } catch (Exception exception) { } } Debug.Assert(matchingType != null); SimpleTypeCodeDomHelper.GetCreateValueExpression(typedValue, matchingType, collection); }
public static CodeExpression CreateFacets(ClrSimpleTypeInfo type) { CompiledFacets facets = type.RestrictionFacets; CodeObjectCreateExpression createFacets = new CodeObjectCreateExpression(); createFacets.CreateType = new CodeTypeReference(Constants.RestrictionFacets); RestrictionFlags flags = facets.Flags; if (flags == 0) { return(new CodePrimitiveExpression(null)); } else { CodeCastExpression cast = new CodeCastExpression(new CodeTypeReference(Constants.RestrictionFlags), new CodePrimitiveExpression( System.Convert.ToInt32(flags, CultureInfo.InvariantCulture.NumberFormat))); createFacets.Parameters.Add(cast); } if ((flags & RestrictionFlags.Enumeration) != 0) { CodeArrayCreateExpression enums = new CodeArrayCreateExpression(); enums.CreateType = new CodeTypeReference("System.Object"); foreach (object o in facets.Enumeration) { GetCreateValueExpression(o, type, enums.Initializers); } createFacets.Parameters.Add(enums); } else { createFacets.Parameters.Add(new CodePrimitiveExpression(null)); } int fractionDigits = default(int); if ((flags & RestrictionFlags.FractionDigits) != 0) { fractionDigits = facets.FractionDigits; } createFacets.Parameters.Add(new CodePrimitiveExpression(fractionDigits)); int length = default(int); if ((flags & RestrictionFlags.Length) != 0) { length = facets.Length; } createFacets.Parameters.Add(new CodePrimitiveExpression(length)); object maxExclusive = default(object); if ((flags & RestrictionFlags.MaxExclusive) != 0) { maxExclusive = facets.MaxExclusive; } GetCreateValueExpression(maxExclusive, type, createFacets.Parameters); object maxInclusive = default(object); if ((flags & RestrictionFlags.MaxInclusive) != 0) { maxInclusive = facets.MaxInclusive; } GetCreateValueExpression(maxInclusive, type, createFacets.Parameters); int maxLength = default(int); if ((flags & RestrictionFlags.MaxLength) != 0) { maxLength = facets.MaxLength; } createFacets.Parameters.Add(new CodePrimitiveExpression(maxLength)); object minExclusive = default(object); if ((flags & RestrictionFlags.MinExclusive) != 0) { minExclusive = facets.MinExclusive; } GetCreateValueExpression(minExclusive, type, createFacets.Parameters); object minInclusive = default(object); if ((flags & RestrictionFlags.MinInclusive) != 0) { minInclusive = facets.MinInclusive; } GetCreateValueExpression(minInclusive, type, createFacets.Parameters); int minLength = default(int); if ((flags & RestrictionFlags.MinLength) != 0) { minLength = facets.MinLength; } createFacets.Parameters.Add(new CodePrimitiveExpression(minLength)); if ((flags & RestrictionFlags.Pattern) != 0) { CodeArrayCreateExpression patternStrs = new CodeArrayCreateExpression(); patternStrs.CreateType = new CodeTypeReference(XTypedServices.typeOfString); foreach (object o in facets.Patterns) { string str = o.ToString(); patternStrs.Initializers.Add(new CodePrimitiveExpression(str)); } createFacets.Parameters.Add(patternStrs); } else { createFacets.Parameters.Add(new CodePrimitiveExpression(null)); } int totalDigits = default(int); if ((flags & RestrictionFlags.TotalDigits) != 0) { totalDigits = facets.TotalDigits; } createFacets.Parameters.Add(new CodePrimitiveExpression(totalDigits)); XmlSchemaWhiteSpace ws = facets.WhiteSpace; createFacets.Parameters.Add( CodeDomHelper.CreateFieldReference(Constants.XmlSchemaWhiteSpace, ws.ToString())); return(createFacets); }
public static CodeExpression CreateFacets(ClrSimpleTypeInfo type) { //object o = null; CodeExpression codePrimitiveExpression; CompiledFacets facets = type.RestrictionFacets; CodeObjectCreateExpression createFacets = new CodeObjectCreateExpression() { CreateType = new CodeTypeReference("Xml.Schema.Linq.RestrictionFacets") }; Xml.Schema.Linq.RestrictionFlags flags = facets.Flags; if ((int)flags != 0) { CodeCastExpression cast = new CodeCastExpression(new CodeTypeReference("Xml.Schema.Linq.RestrictionFlags"), new CodePrimitiveExpression((object)Convert.ToInt32(flags, CultureInfo.InvariantCulture.NumberFormat))); createFacets.Parameters.Add(cast); if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.Enumeration) == 0) { createFacets.Parameters.Add(new CodePrimitiveExpression(null)); } else { CodeArrayCreateExpression enums = new CodeArrayCreateExpression() { CreateType = new CodeTypeReference("System.Object") }; foreach (object o in facets.Enumeration) { SimpleTypeCodeDomHelper.GetCreateValueExpression(o, type, enums.Initializers); } createFacets.Parameters.Add(enums); } int fractionDigits = 0; if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.FractionDigits) != 0) { fractionDigits = facets.FractionDigits; } createFacets.Parameters.Add(new CodePrimitiveExpression((object)fractionDigits)); int length = 0; if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.Length) != 0) { length = facets.Length; } createFacets.Parameters.Add(new CodePrimitiveExpression((object)length)); object maxExclusive = null; if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.MaxExclusive) != 0) { maxExclusive = facets.MaxExclusive; } SimpleTypeCodeDomHelper.GetCreateValueExpression(maxExclusive, type, createFacets.Parameters); object maxInclusive = null; if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.MaxInclusive) != 0) { maxInclusive = facets.MaxInclusive; } SimpleTypeCodeDomHelper.GetCreateValueExpression(maxInclusive, type, createFacets.Parameters); int maxLength = 0; if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.MaxLength) != 0) { maxLength = facets.MaxLength; } createFacets.Parameters.Add(new CodePrimitiveExpression((object)maxLength)); object minExclusive = null; if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.MinExclusive) != 0) { minExclusive = facets.MinExclusive; } SimpleTypeCodeDomHelper.GetCreateValueExpression(minExclusive, type, createFacets.Parameters); object minInclusive = null; if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.MinInclusive) != 0) { minInclusive = facets.MinInclusive; } SimpleTypeCodeDomHelper.GetCreateValueExpression(minInclusive, type, createFacets.Parameters); int minLength = 0; if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.MinLength) != 0) { minLength = facets.MinLength; } createFacets.Parameters.Add(new CodePrimitiveExpression((object)minLength)); if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.Pattern) == 0) { createFacets.Parameters.Add(new CodePrimitiveExpression(null)); } else { CodeArrayCreateExpression patternStrs = new CodeArrayCreateExpression() { CreateType = new CodeTypeReference(XTypedServices.typeOfString) }; foreach (object pattern in facets.Patterns) { string str = pattern.ToString(); patternStrs.Initializers.Add(new CodePrimitiveExpression(str)); } createFacets.Parameters.Add(patternStrs); } int totalDigits = 0; if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.TotalDigits) != 0) { totalDigits = facets.TotalDigits; } createFacets.Parameters.Add(new CodePrimitiveExpression((object)totalDigits)); Xml.Schema.Linq.XmlSchemaWhiteSpace ws = facets.WhiteSpace; createFacets.Parameters.Add(CodeDomHelper.CreateFieldReference("XmlSchemaWhiteSpace", ws.ToString())); codePrimitiveExpression = createFacets; } else { codePrimitiveExpression = new CodePrimitiveExpression(null); } return(codePrimitiveExpression); }
public IEnumerable <CodeNamespace> GenerateTypes(ClrMappingInfo binding) { if (binding == null) { throw new ArgumentException("binding"); } nameMappings = binding.NameMappings; Debug.Assert(nameMappings != null); foreach (ClrTypeInfo type in binding.Types) { if (type.IsWrapper) { if (wrapperRootElements == null) { wrapperRootElements = new List <ClrWrapperTypeInfo>(); } wrapperRootElements.Add(type as ClrWrapperTypeInfo); } else { codeNamespace = GetCodeNamespace(type.clrtypeNs); ClrSimpleTypeInfo stInfo = type as ClrSimpleTypeInfo; if (stInfo != null) { if (stInfo is EnumSimpleTypeInfo enumTypeInfo) { var enumType = TypeBuilder.CreateEnumType(enumTypeInfo, settings, stInfo); codeNamespace.Types.Add(enumType); var enumsInOtherTypes = codeNamespace.DescendentTypeScopedEnumDeclarations(); // if an enum is defined in another type, remove it, if it is the same as the global (namespace scoped type) if (enumsInOtherTypes.EqualEnumDeclarationExists(enumType)) { var typeWithDuplicateEnum = codeNamespace.TypeWithEnumDeclaration(enumType); var duplicateEnum = typeWithDuplicateEnum.Members.OfType <CodeTypeDeclaration>() .First(c => c.IsEqualEnumDeclaration(enumType)); typeWithDuplicateEnum.Members.Remove(duplicateEnum); } } codeNamespace.Types.Add(TypeBuilder.CreateSimpleType(stInfo, nameMappings, settings)); } else { CodeTypeDeclaration decl = ProcessType(type as ClrContentTypeInfo, null, true); //Sets current codeNamespace codeNamespace.Types.Add(decl); if (type.IsRootElement) { List <CodeTypeDeclaration> types; if (!xroots.TryGetValue(codeNamespace, out types)) { types = new List <CodeTypeDeclaration>(); xroots.Add(codeNamespace, types); } types.Add(decl); } } } } ProcessWrapperTypes(); CreateTypeManager(); CreateXRoots(); return(codeNamespacesTable.Values); }
private void BuildNestedTypes(ClrContentTypeInfo typeInfo) { ClrSimpleTypeInfo clrSimpleTypeInfo; foreach (AnonymousType at in this.localSymbolTable.GetAnonymousTypes()) { XmlQualifiedName qname = null; XmlSchemaComplexType complexType = null; XmlSchemaElement elem = at.parentElement; if (elem != null) { qname = elem.QualifiedName; complexType = elem.ElementSchemaType as XmlSchemaComplexType; } else { complexType = at.wrappingType; qname = complexType.QualifiedName; } if (complexType != null) { if ((complexType.GetContentType() != XmlSchemaContentType.TextOnly ? true : !complexType.IsDerivedByRestriction())) { ClrContentTypeInfo clrContentTypeInfo = new ClrContentTypeInfo(); this.localSymbolTable.Init(at.identifier); clrContentTypeInfo.clrtypeName = at.identifier; clrContentTypeInfo.clrtypeNs = this.configSettings.GetClrNamespace(qname.Namespace); clrContentTypeInfo.schemaName = qname.Name; clrContentTypeInfo.schemaNs = qname.Namespace; clrContentTypeInfo.typeOrigin = SchemaOrigin.Fragment; clrContentTypeInfo.IsNested = true; clrContentTypeInfo.baseType = this.BaseType(complexType); this.BuildProperties(elem, complexType, clrContentTypeInfo); this.BuildNestedTypes(clrContentTypeInfo); this.BuildAnnotationInformation(clrContentTypeInfo, complexType); typeInfo.NestedTypes.Add(clrContentTypeInfo); } else { clrSimpleTypeInfo = ClrSimpleTypeInfo.CreateSimpleTypeInfo(complexType); clrSimpleTypeInfo.clrtypeName = at.identifier; clrSimpleTypeInfo.clrtypeNs = this.configSettings.GetClrNamespace(qname.Namespace); clrSimpleTypeInfo.schemaName = qname.Name; clrSimpleTypeInfo.schemaNs = qname.Namespace; clrSimpleTypeInfo.typeOrigin = SchemaOrigin.Fragment; clrSimpleTypeInfo.IsNested = true; this.BuildAnnotationInformation(clrSimpleTypeInfo, complexType); typeInfo.NestedTypes.Add(clrSimpleTypeInfo); } } XmlSchemaSimpleType simpleType = null; if (elem != null) { simpleType = elem.ElementSchemaType as XmlSchemaSimpleType; } if (simpleType != null) { clrSimpleTypeInfo = ClrSimpleTypeInfo.CreateSimpleTypeInfo(simpleType); clrSimpleTypeInfo.clrtypeName = at.identifier; clrSimpleTypeInfo.clrtypeNs = this.configSettings.GetClrNamespace(qname.Namespace); clrSimpleTypeInfo.schemaName = qname.Name; clrSimpleTypeInfo.schemaNs = qname.Namespace; clrSimpleTypeInfo.typeOrigin = SchemaOrigin.Fragment; clrSimpleTypeInfo.IsNested = true; this.BuildAnnotationInformation(clrSimpleTypeInfo, simpleType); typeInfo.NestedTypes.Add(clrSimpleTypeInfo); } } }
internal static CodeTypeDeclaration CreateSimpleType(ClrSimpleTypeInfo typeInfo, Dictionary<XmlSchemaObject, string> nameMappings, LinqToXsdSettings settings) { string typeName = typeInfo.clrtypeName; CodeTypeDeclaration simpleTypeDecl = new CodeTypeDeclaration(typeName); simpleTypeDecl.TypeAttributes = TypeAttributes.Sealed | TypeAttributes.Public; //Add private constructor so it cannot be instantiated CodeConstructor privateConst = new CodeConstructor(); privateConst.Attributes = MemberAttributes.Private; simpleTypeDecl.Members.Add(privateConst); //Create a static field for the XTypedSchemaSimpleType CodeMemberField typeField = CodeDomHelper.CreateMemberField(Constants.SimpleTypeDefInnerType, Constants.SimpleTypeValidator, MemberAttributes.Public | MemberAttributes.Static, false); typeField.InitExpression = SimpleTypeCodeDomHelper.CreateSimpleTypeDef(typeInfo, nameMappings, settings, false); simpleTypeDecl.Members.Add(typeField); // inconsistency w/ the wasy ApplyAnnotations are us ApplyAnnotations(simpleTypeDecl, typeInfo); return simpleTypeDecl; }
public static CodeExpression CreateFacets(ClrSimpleTypeInfo type) { CompiledFacets facets = type.RestrictionFacets; CodeObjectCreateExpression createFacets = new CodeObjectCreateExpression(); createFacets.CreateType = new CodeTypeReference( Constants.RestrictionFacets); RestrictionFlags flags = facets.Flags; if (flags == 0) { return new CodePrimitiveExpression(null); } else { CodeCastExpression cast = new CodeCastExpression( new CodeTypeReference(Constants.RestrictionFlags), new CodePrimitiveExpression( System.Convert.ToInt32( flags, CultureInfo.InvariantCulture.NumberFormat))); createFacets.Parameters.Add(cast); } if ((flags & RestrictionFlags.Enumeration) != 0) { CodeArrayCreateExpression enums = new CodeArrayCreateExpression(); enums.CreateType = new CodeTypeReference("System.Object"); foreach (object o in facets.Enumeration) { GetCreateValueExpression(o, type, enums.Initializers); } createFacets.Parameters.Add(enums); } else { createFacets.Parameters.Add(new CodePrimitiveExpression(null)); } int fractionDigits = default(int); if ((flags & RestrictionFlags.FractionDigits) != 0) { fractionDigits = facets.FractionDigits; } createFacets.Parameters.Add(new CodePrimitiveExpression(fractionDigits)); int length = default(int); if ((flags & RestrictionFlags.Length) != 0) { length = facets.Length; } createFacets.Parameters.Add(new CodePrimitiveExpression(length)); object maxExclusive = default(object); if ((flags & RestrictionFlags.MaxExclusive) != 0) { maxExclusive = facets.MaxExclusive; } GetCreateValueExpression(maxExclusive, type, createFacets.Parameters); object maxInclusive = default(object); if ((flags & RestrictionFlags.MaxInclusive) != 0) { maxInclusive = facets.MaxInclusive; } GetCreateValueExpression(maxInclusive, type, createFacets.Parameters); int maxLength = default(int); if ((flags & RestrictionFlags.MaxLength) != 0) { maxLength = facets.MaxLength; } createFacets.Parameters.Add(new CodePrimitiveExpression(maxLength)); object minExclusive = default(object); if ((flags & RestrictionFlags.MinExclusive) != 0) { minExclusive = facets.MinExclusive; } GetCreateValueExpression(minExclusive, type, createFacets.Parameters); object minInclusive = default(object); if ((flags & RestrictionFlags.MinInclusive) != 0) { minInclusive = facets.MinInclusive; } GetCreateValueExpression(minInclusive, type, createFacets.Parameters); int minLength = default(int); if ((flags & RestrictionFlags.MinLength) != 0) { minLength = facets.MinLength; } createFacets.Parameters.Add(new CodePrimitiveExpression(minLength)); if ((flags & RestrictionFlags.Pattern) != 0) { CodeArrayCreateExpression patternStrs = new CodeArrayCreateExpression(); patternStrs.CreateType = new CodeTypeReference( XTypedServices.typeOfString); foreach (object o in facets.Patterns) { string str = o.ToString(); patternStrs.Initializers.Add(new CodePrimitiveExpression(str)); } createFacets.Parameters.Add(patternStrs); } else { createFacets.Parameters.Add(new CodePrimitiveExpression(null)); } int totalDigits = default(int); if ((flags & RestrictionFlags.TotalDigits) != 0) { totalDigits = facets.TotalDigits; } createFacets.Parameters.Add(new CodePrimitiveExpression(totalDigits)); XmlSchemaWhiteSpace ws = facets.WhiteSpace; createFacets.Parameters.Add( CodeDomHelper.CreateFieldReference( Constants.XmlSchemaWhiteSpace, ws.ToString())); return createFacets; }
public static void GetCreateValueExpression( object value, ClrSimpleTypeInfo typeDef, CodeExpressionCollection collection) { if (value == null) { collection.Add(new CodePrimitiveExpression(value)); return; } switch(typeDef.Variety) { case XmlSchemaDatatypeVariety.List: string str = ListSimpleTypeValidator.ToString(value); collection.Add(new CodePrimitiveExpression(str)); break; case XmlSchemaDatatypeVariety.Atomic: if (value is string) { collection.Add(new CodePrimitiveExpression(value)); } else { collection.Add(CreateTypedValueExpression( typeDef.InnerType.Datatype, value)); } break; case XmlSchemaDatatypeVariety.Union: GetCreateUnionValueExpression( value, typeDef as UnionSimpleTypeInfo, collection); break; default: break; } }
private void BuildNestedTypes(ClrContentTypeInfo typeInfo) { List <AnonymousType> anonymousTypes = localSymbolTable.GetAnonymousTypes(); foreach (AnonymousType at in anonymousTypes) { XmlQualifiedName qname = null; XmlSchemaComplexType complexType = null; XmlSchemaElement elem = at.parentElement; if (elem == null) { //case 1: "dummy" type for text content in a complex type, with restrictions complexType = at.wrappingType; qname = complexType.QualifiedName; } else { //case 2: anonymous type can also be nested under an element qname = elem.QualifiedName; complexType = elem.ElementSchemaType as XmlSchemaComplexType; } if (complexType != null) { if (complexType.GetContentType() == XmlSchemaContentType.TextOnly && complexType.IsDerivedByRestriction()) { //In this case, we take care of the content/text part only. No nesting types exist. ClrSimpleTypeInfo nestedTypeInfo = ClrSimpleTypeInfo .CreateSimpleTypeInfo( complexType); //Generate its "simple type" version to save restrictions nestedTypeInfo.clrtypeName = at.identifier; nestedTypeInfo.clrtypeNs = configSettings.GetClrNamespace(qname.Namespace); nestedTypeInfo.schemaName = qname.Name; nestedTypeInfo.schemaNs = qname.Namespace; nestedTypeInfo.typeOrigin = SchemaOrigin.Fragment; nestedTypeInfo.IsNested = true; BuildAnnotationInformation(nestedTypeInfo, complexType); typeInfo.NestedTypes.Add(nestedTypeInfo); } else { ClrContentTypeInfo nestedTypeInfo = new ClrContentTypeInfo() { Parent = typeInfo }; localSymbolTable.Init(at.identifier); nestedTypeInfo.clrtypeName = at.identifier; nestedTypeInfo.clrtypeNs = configSettings.GetClrNamespace(qname.Namespace); nestedTypeInfo.schemaName = qname.Name; nestedTypeInfo.schemaNs = qname.Namespace; nestedTypeInfo.typeOrigin = SchemaOrigin.Fragment; nestedTypeInfo.IsNested = true; nestedTypeInfo.baseType = BaseType(complexType); BuildProperties(elem, complexType, nestedTypeInfo); BuildNestedTypes(nestedTypeInfo); BuildAnnotationInformation(nestedTypeInfo, complexType); typeInfo.NestedTypes.Add(nestedTypeInfo); } } //Also handle simple types XmlSchemaSimpleType simpleType = null; if (elem != null) { simpleType = elem.ElementSchemaType as XmlSchemaSimpleType; } if (simpleType != null) { ClrSimpleTypeInfo nestedTypeInfo = ClrSimpleTypeInfo.CreateSimpleTypeInfo(simpleType); nestedTypeInfo.clrtypeName = at.identifier; nestedTypeInfo.clrtypeNs = configSettings.GetClrNamespace(qname.Namespace); nestedTypeInfo.schemaName = qname.Name; nestedTypeInfo.schemaNs = qname.Namespace; nestedTypeInfo.typeOrigin = SchemaOrigin.Fragment; nestedTypeInfo.IsNested = true; BuildAnnotationInformation(nestedTypeInfo, simpleType); typeInfo.NestedTypes.Add(nestedTypeInfo); } } }