private PrimitiveMapping ImportPrimitiveMapping(PrimitiveModel model, string dataType) { PrimitiveMapping mapping = new PrimitiveMapping(); mapping.IsSoap = true; if (dataType.Length > 0) { mapping.TypeDesc = _typeScope.GetTypeDesc(dataType, XmlSchema.Namespace); if (mapping.TypeDesc == null) { // try it as a non-Xsd type mapping.TypeDesc = _typeScope.GetTypeDesc(dataType, UrtTypes.Namespace); if (mapping.TypeDesc == null) { throw new InvalidOperationException(string.Format(ResXml.XmlUdeclaredXsdType, dataType)); } } } else { mapping.TypeDesc = model.TypeDesc; } mapping.TypeName = mapping.TypeDesc.DataType.Name; mapping.Namespace = mapping.TypeDesc.IsXsdType ? XmlSchema.Namespace : UrtTypes.Namespace; return(mapping); }
private PrimitiveMapping ImportPrimitiveDataType(XmlSchemaSimpleType dataType) { TypeDesc sourceTypeDesc = GetDataTypeSource(dataType); PrimitiveMapping mapping = new PrimitiveMapping(); mapping.TypeDesc = sourceTypeDesc; mapping.TypeName = sourceTypeDesc.DataType.Name; return(mapping); }
private XmlQualifiedName ExportNonXsdPrimitiveMapping(PrimitiveMapping mapping, string ns) { XmlSchemaType type = mapping.TypeDesc.DataType; if (!SchemaContainsItem(type, UrtTypes.Namespace)) { AddSchemaItem(type, UrtTypes.Namespace, ns); } else { AddSchemaImport(UrtTypes.Namespace, ns); } return(new XmlQualifiedName(mapping.TypeDesc.DataType.Name, UrtTypes.Namespace)); }
private PrimitiveMapping ImportNonXsdPrimitiveDataType(XmlSchemaSimpleType dataType, string ns) { PrimitiveMapping mapping = null; TypeDesc typeDesc = null; if (dataType.Name != null && dataType.Name.Length != 0) { typeDesc = Scope.GetTypeDesc(dataType.Name, ns); if (typeDesc != null) { mapping = new PrimitiveMapping(); mapping.TypeDesc = typeDesc; mapping.TypeName = typeDesc.DataType.Name; } } return(mapping); }
private TypeMapping ImportDataType(XmlSchemaSimpleType dataType, string typeNs, string identifier, bool isList) { TypeMapping mapping = ImportNonXsdPrimitiveDataType(dataType, typeNs); if (mapping != null) { return(mapping); } if (dataType.Content is XmlSchemaSimpleTypeRestriction) { XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)dataType.Content; foreach (object o in restriction.Facets) { if (o is XmlSchemaEnumerationFacet) { return(ImportEnumeratedDataType(dataType, typeNs, identifier, isList)); } } } else if (dataType.Content is XmlSchemaSimpleTypeList || dataType.Content is XmlSchemaSimpleTypeUnion) { if (dataType.Content is XmlSchemaSimpleTypeList) { // check if we have enumeration list XmlSchemaSimpleTypeList list = (XmlSchemaSimpleTypeList)dataType.Content; if (list.ItemType != null) { mapping = ImportDataType(list.ItemType, typeNs, identifier, true); if (mapping != null) { return(mapping); } } } mapping = new PrimitiveMapping(); mapping.TypeDesc = Scope.GetTypeDesc(typeof(string)); mapping.TypeName = mapping.TypeDesc.DataType.Name; return(mapping); } return(ImportPrimitiveDataType(dataType)); }
private XmlQualifiedName ExportTypeMapping(TypeMapping mapping, string ns) { if (mapping is ArrayMapping) { return(ExportArrayMapping((ArrayMapping)mapping, ns)); } else if (mapping is EnumMapping) { return(ExportEnumMapping((EnumMapping)mapping, ns)); } else if (mapping is PrimitiveMapping) { PrimitiveMapping pm = (PrimitiveMapping)mapping; if (pm.TypeDesc.IsXsdType) { return(ExportPrimitiveMapping(pm)); } else { return(ExportNonXsdPrimitiveMapping(pm, ns)); } } else if (mapping is StructMapping) { return(ExportStructMapping((StructMapping)mapping, ns)); } else if (mapping is NullableMapping) { return(ExportTypeMapping(((NullableMapping)mapping).BaseMapping, ns)); } else if (mapping is MembersMapping) { return(ExportMembersMapping((MembersMapping)mapping, ns)); } else { throw new ArgumentException(ResXml.XmlInternalError, "mapping"); } }
private TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, string typeNs, string identifier, bool isList) { TypeMapping mapping = (TypeMapping)ImportedMappings[dataType]; if (mapping != null) { return(mapping); } XmlSchemaSimpleType sourceDataType = FindDataType(dataType.DerivedFrom); TypeDesc sourceTypeDesc = Scope.GetTypeDesc(sourceDataType); if (sourceTypeDesc != null && sourceTypeDesc != Scope.GetTypeDesc(typeof(string))) { return(ImportPrimitiveDataType(dataType)); } identifier = Accessor.UnescapeName(identifier); string typeName = GenerateUniqueTypeName(identifier); EnumMapping enumMapping = new EnumMapping(); enumMapping.IsReference = Schemas.IsReference(dataType); enumMapping.TypeDesc = new TypeDesc(typeName, typeName, TypeKind.Enum, null, 0); enumMapping.TypeName = identifier; enumMapping.Namespace = typeNs; enumMapping.IsFlags = isList; CodeIdentifiers constants = new CodeIdentifiers(); if (!(dataType.Content is XmlSchemaSimpleTypeRestriction)) { throw new InvalidOperationException(string.Format(ResXml.XmlInvalidEnumContent, dataType.Content.GetType().Name, identifier)); } XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)dataType.Content; for (int i = 0; i < restriction.Facets.Count; i++) { object facet = restriction.Facets[i]; if (!(facet is XmlSchemaEnumerationFacet)) { continue; } XmlSchemaEnumerationFacet enumeration = (XmlSchemaEnumerationFacet)facet; ConstantMapping constant = new ConstantMapping(); string constantName = CodeIdentifier.MakeValid(enumeration.Value); constant.Name = constants.AddUnique(constantName, constant); constant.XmlName = enumeration.Value; constant.Value = i; } enumMapping.Constants = (ConstantMapping[])constants.ToArray(typeof(ConstantMapping)); if (isList && enumMapping.Constants.Length > 63) { // if we have 64+ flag constants we cannot map the type to long enum, we will use string mapping instead. mapping = new PrimitiveMapping(); mapping.TypeDesc = Scope.GetTypeDesc(typeof(string)); mapping.TypeName = mapping.TypeDesc.DataType.Name; ImportedMappings.Add(dataType, mapping); return(mapping); } ImportedMappings.Add(dataType, enumMapping); Scope.AddTypeMapping(enumMapping); return(enumMapping); }
private ArrayMapping ImportArrayMapping(XmlSchemaType type, string ns) { ArrayMapping arrayMapping; if (type.Name == Soap.Array && ns == Soap.Encoding) { arrayMapping = new ArrayMapping(); TypeMapping mapping = GetRootMapping(); ElementAccessor itemAccessor = new ElementAccessor(); itemAccessor.IsSoap = true; itemAccessor.Name = Soap.UrType; itemAccessor.Namespace = ns; itemAccessor.Mapping = mapping; itemAccessor.IsNullable = true; itemAccessor.Form = XmlSchemaForm.None; arrayMapping.Elements = new ElementAccessor[] { itemAccessor }; arrayMapping.TypeDesc = itemAccessor.Mapping.TypeDesc.CreateArrayTypeDesc(); arrayMapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(itemAccessor.Mapping.TypeName); return(arrayMapping); } if (!(type.DerivedFrom.Name == Soap.Array && type.DerivedFrom.Namespace == Soap.Encoding)) { return(null); } // the type should be a XmlSchemaComplexType XmlSchemaContentModel model = ((XmlSchemaComplexType)type).ContentModel; // the Content should be an restriction if (!(model.Content is XmlSchemaComplexContentRestriction)) { return(null); } arrayMapping = new ArrayMapping(); XmlSchemaComplexContentRestriction restriction = (XmlSchemaComplexContentRestriction)model.Content; for (int i = 0; i < restriction.Attributes.Count; i++) { XmlSchemaAttribute attribute = restriction.Attributes[i] as XmlSchemaAttribute; if (attribute != null && attribute.RefName.Name == Soap.ArrayType && attribute.RefName.Namespace == Soap.Encoding) { // read the value of the wsdl:arrayType attribute string arrayType = null; if (attribute.UnhandledAttributes != null) { foreach (XmlAttribute a in attribute.UnhandledAttributes) { if (a.LocalName == Wsdl.ArrayType && a.NamespaceURI == Wsdl.Namespace) { arrayType = a.Value; break; } } } if (arrayType != null) { string dims; XmlQualifiedName typeName = TypeScope.ParseWsdlArrayType(arrayType, out dims, attribute); TypeMapping mapping; TypeDesc td = Scope.GetTypeDesc(typeName.Name, typeName.Namespace); if (td != null && td.IsPrimitive) { mapping = new PrimitiveMapping(); mapping.TypeDesc = td; mapping.TypeName = td.DataType.Name; } else { mapping = ImportType(typeName, false); } ElementAccessor itemAccessor = new ElementAccessor(); itemAccessor.IsSoap = true; itemAccessor.Name = typeName.Name; itemAccessor.Namespace = ns; itemAccessor.Mapping = mapping; itemAccessor.IsNullable = true; itemAccessor.Form = XmlSchemaForm.None; arrayMapping.Elements = new ElementAccessor[] { itemAccessor }; arrayMapping.TypeDesc = itemAccessor.Mapping.TypeDesc.CreateArrayTypeDesc(); arrayMapping.TypeName = "ArrayOf" + CodeIdentifier.MakePascal(itemAccessor.Mapping.TypeName); return(arrayMapping); } } } XmlSchemaParticle particle = restriction.Particle; if (particle is XmlSchemaAll || particle is XmlSchemaSequence) { XmlSchemaGroupBase group = (XmlSchemaGroupBase)particle; if (group.Items.Count != 1 || !(group.Items[0] is XmlSchemaElement)) { return(null); } XmlSchemaElement itemElement = (XmlSchemaElement)group.Items[0]; if (!itemElement.IsMultipleOccurrence) { return(null); } ElementAccessor itemAccessor = ImportElement(itemElement, ns); arrayMapping.Elements = new ElementAccessor[] { itemAccessor }; arrayMapping.TypeDesc = ((TypeMapping)itemAccessor.Mapping).TypeDesc.CreateArrayTypeDesc(); } else { return(null); } return(arrayMapping); }
private void AddDefaultValueAttribute(CodeMemberField field, CodeAttributeDeclarationCollection metadata, object defaultValue, TypeMapping mapping, CodeCommentStatementCollection comments, TypeDesc memberTypeDesc, Accessor accessor, CodeConstructor ctor) { string attributeName = accessor.IsFixed ? "fixed" : "default"; if (!memberTypeDesc.HasDefaultSupport) { if (comments != null && defaultValue is string) { DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName); // do not generate intializers for the user prefered types if they do not have default capability AddWarningComment(comments, string.Format(ResXml.XmlDropAttributeValue, attributeName, mapping.TypeName, defaultValue.ToString())); } return; } if (memberTypeDesc.IsArrayLike && accessor is ElementAccessor) { if (comments != null && defaultValue is string) { DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName); // do not generate intializers for array-like types AddWarningComment(comments, string.Format(ResXml.XmlDropArrayAttributeValue, attributeName, defaultValue.ToString(), ((ElementAccessor)accessor).Name)); } return; } if (mapping.TypeDesc.IsMappedType && field != null && defaultValue is string) { SchemaImporterExtension extension = mapping.TypeDesc.ExtendedType.Extension; CodeExpression init = extension.ImportDefaultValue((string)defaultValue, mapping.TypeDesc.FullName); if (init != null) { if (ctor != null) { AddInitializationStatement(ctor, field, init); } else { field.InitExpression = extension.ImportDefaultValue((string)defaultValue, mapping.TypeDesc.FullName); } } if (comments != null) { DropDefaultAttribute(accessor, comments, mapping.TypeDesc.FullName); if (init == null) { AddWarningComment(comments, string.Format(ResXml.XmlNotKnownDefaultValue, extension.GetType().FullName, attributeName, (string)defaultValue, mapping.TypeName, mapping.Namespace)); } } return; } object value = null; if (defaultValue is string || defaultValue == null) { value = ImportDefault(mapping, (string)defaultValue); } if (value == null) { return; } if (!(mapping is PrimitiveMapping)) { if (comments != null) { DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName); AddWarningComment(comments, string.Format(ResXml.XmlDropNonPrimitiveAttributeValue, attributeName, defaultValue.ToString())); } return; } PrimitiveMapping pm = (PrimitiveMapping)mapping; if (comments != null && !pm.TypeDesc.HasDefaultSupport && pm.TypeDesc.IsMappedType) { // do not generate intializers for the user prefered types if they do not have default capability DropDefaultAttribute(accessor, comments, pm.TypeDesc.FullName); return; } if (value == DBNull.Value) { if (comments != null) { AddWarningComment(comments, string.Format(ResXml.XmlDropAttributeValue, attributeName, pm.TypeName, defaultValue.ToString())); } return; } CodeAttributeArgument[] arguments = null; CodeExpression initExpression = null; if (pm.IsList) { #if DEBUG // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (value.GetType() != typeof(object[])) { throw new InvalidOperationException(string.Format(ResXml.XmlInternalErrorDetails, "Default value for list should be object[], not " + value.GetType().Name)); } #endif object[] vals = (object[])value; CodeExpression[] initializers = new CodeExpression[vals.Length]; for (int i = 0; i < vals.Length; i++) { GetDefaultValueArguments(pm, vals[i], out initializers[i]); } initExpression = new CodeArrayCreateExpression(field.Type, initializers); } else { arguments = GetDefaultValueArguments(pm, value, out initExpression); } if (field != null) { if (ctor != null) { AddInitializationStatement(ctor, field, initExpression); } else { field.InitExpression = initExpression; } } if (arguments != null && pm.TypeDesc.HasDefaultSupport && accessor.IsOptional && !accessor.IsFixed) { // Add [DefaultValueAttribute] CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(DefaultValueAttribute).FullName, arguments); metadata.Add(attribute); } else if (comments != null) { DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName); } }
private object ImportDefaultValue(TypeMapping mapping, string defaultValue) { if (defaultValue == null) { return(null); } if (!(mapping is PrimitiveMapping)) { return(DBNull.Value); } if (mapping is EnumMapping) { EnumMapping em = (EnumMapping)mapping; ConstantMapping[] c = em.Constants; if (em.IsFlags) { Hashtable values = new Hashtable(); string[] names = new string[c.Length]; long[] ids = new long[c.Length]; for (int i = 0; i < c.Length; i++) { ids[i] = em.IsFlags ? 1L << i : (long)i; names[i] = c[i].Name; values.Add(c[i].Name, ids[i]); } // this validates the values long val = XmlCustomFormatter.ToEnum(defaultValue, values, em.TypeName, true); return(XmlCustomFormatter.FromEnum(val, names, ids, em.TypeDesc.FullName)); } else { for (int i = 0; i < c.Length; i++) { if (c[i].XmlName == defaultValue) { return(c[i].Name); } } } throw new InvalidOperationException(string.Format(ResXml.XmlInvalidDefaultValue, defaultValue, em.TypeDesc.FullName)); } // Primitive mapping PrimitiveMapping pm = (PrimitiveMapping)mapping; if (!pm.TypeDesc.HasCustomFormatter) { if (pm.TypeDesc.FormatterName == "String") { return(defaultValue); } if (pm.TypeDesc.FormatterName == "DateTime") { return(XmlCustomFormatter.ToDateTime(defaultValue)); } Type formatter = typeof(XmlConvert); MethodInfo format = formatter.GetMethod("To" + pm.TypeDesc.FormatterName, new Type[] { typeof(string) }); if (format != null) { return(format.Invoke(formatter, new Object[] { defaultValue })); } } else { if (pm.TypeDesc.HasDefaultSupport) { return(XmlCustomFormatter.ToDefaultValue(defaultValue, pm.TypeDesc.FormatterName)); } } return(DBNull.Value); }
private CodeAttributeArgument[] GetDefaultValueArguments(PrimitiveMapping mapping, object value, out CodeExpression initExpression) { initExpression = null; if (value == null) { return(null); } CodeExpression valueExpression = null; CodeExpression typeofValue = null; Type type = value.GetType(); CodeAttributeArgument[] arguments = null; if (mapping is EnumMapping) { #if DEBUG // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (value.GetType() != typeof(string)) { throw new InvalidOperationException(string.Format(ResXml.XmlInternalErrorDetails, "Invalid enumeration type " + value.GetType().Name)); } #endif if (((EnumMapping)mapping).IsFlags) { string[] values = ((string)value).Split(null); for (int i = 0; i < values.Length; i++) { if (values[i].Length == 0) { continue; } CodeExpression enumRef = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), values[i]); if (valueExpression != null) { valueExpression = new CodeBinaryOperatorExpression(valueExpression, CodeBinaryOperatorType.BitwiseOr, enumRef); } else { valueExpression = enumRef; } } } else { valueExpression = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(mapping.TypeDesc.FullName), (string)value); } initExpression = valueExpression; arguments = new CodeAttributeArgument[] { new CodeAttributeArgument(valueExpression) }; } else if (type == typeof(bool) || type == typeof(Int32) || type == typeof(string) || type == typeof(double)) { initExpression = valueExpression = new CodePrimitiveExpression(value); arguments = new CodeAttributeArgument[] { new CodeAttributeArgument(valueExpression) }; } else if (type == typeof(Int16) || type == typeof(Int64) || type == typeof(float) || type == typeof(byte) || type == typeof(decimal)) { valueExpression = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo)); typeofValue = new CodeTypeOfExpression(type.FullName); arguments = new CodeAttributeArgument[] { new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression) }; initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value)); } else if (type == typeof(sbyte) || type == typeof(UInt16) || type == typeof(UInt32) || type == typeof(UInt64)) { // need to promote the non-CLS complient types value = PromoteType(type, value); valueExpression = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo)); typeofValue = new CodeTypeOfExpression(type.FullName); arguments = new CodeAttributeArgument[] { new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression) }; initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value)); } else if (type == typeof(DateTime)) { DateTime dt = (DateTime)value; string dtString; long ticks; if (mapping.TypeDesc.FormatterName == "Date") { dtString = XmlCustomFormatter.FromDate(dt); ticks = (new DateTime(dt.Year, dt.Month, dt.Day)).Ticks; } else if (mapping.TypeDesc.FormatterName == "Time") { dtString = XmlCustomFormatter.FromDateTime(dt); ticks = dt.Ticks; } else { dtString = XmlCustomFormatter.FromDateTime(dt); ticks = dt.Ticks; } valueExpression = new CodePrimitiveExpression(dtString); typeofValue = new CodeTypeOfExpression(type.FullName); arguments = new CodeAttributeArgument[] { new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression) }; initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(DateTime)), new CodeExpression[] { new CodePrimitiveExpression(ticks) }); } else if (type == typeof(Guid)) { valueExpression = new CodePrimitiveExpression(Convert.ToString(value, NumberFormatInfo.InvariantInfo)); typeofValue = new CodeTypeOfExpression(type.FullName); arguments = new CodeAttributeArgument[] { new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression) }; initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(Guid)), new CodeExpression[] { valueExpression }); } if (mapping.TypeDesc.FullName != type.ToString() && !(mapping is EnumMapping)) { // generate cast initExpression = new CodeCastExpression(mapping.TypeDesc.FullName, initExpression); } return(arguments); }
private XmlQualifiedName ExportPrimitiveMapping(PrimitiveMapping mapping) { return(new XmlQualifiedName(mapping.TypeDesc.DataType.Name, XmlSchema.Namespace)); }