internal XmlMapping(TypeScope scope, ElementAccessor accessor, XmlMappingAccess access) { this.scope = scope; this.accessor = accessor; this.access = access; this.shallow = scope == null; }
private void AddScopeElements(INameScope scope, ElementAccessor[] elements, ref bool duplicateElements, bool allowDuplicates) { for (int i = 0; i < elements.Length; i++) { this.AddScopeElement(scope, elements[i], ref duplicateElements, allowDuplicates); } }
internal XmlMembersMapping(TypeScope scope, ElementAccessor accessor) : base(scope) { this.accessor = accessor; MembersMapping mapping = (MembersMapping)accessor.Mapping; mappings = new XmlMemberMapping[mapping.Members.Length]; for (int i = 0; i < mappings.Length; i++) mappings[i] = new XmlMemberMapping(scope, mapping.Members[i]); }
internal XmlMapping(TypeScope scope, ElementAccessor accessor, XmlMappingAccess access) { _scope = scope; _accessor = accessor; _access = access; _shallow = scope == null; }
private static bool IsNeedNullableMember(ElementAccessor element) { if (element.Mapping is ArrayMapping) { ArrayMapping mapping = (ArrayMapping) element.Mapping; return (((mapping.Elements != null) && (mapping.Elements.Length == 1)) && IsNeedNullableMember(mapping.Elements[0])); } return (element.IsNullable && element.Mapping.TypeDesc.IsValueType); }
private string FindChoiceEnumValue(ElementAccessor element, EnumMapping choiceMapping, bool useReflection) { string ident = null; for (int i = 0; i < choiceMapping.Constants.Length; i++) { string xmlName = choiceMapping.Constants[i].XmlName; if (element.Any && (element.Name.Length == 0)) { if (!(xmlName == "##any:")) { continue; } if (useReflection) { ident = choiceMapping.Constants[i].Value.ToString(CultureInfo.InvariantCulture); } else { ident = choiceMapping.Constants[i].Name; } break; } int length = xmlName.LastIndexOf(':'); string str3 = (length < 0) ? choiceMapping.Namespace : xmlName.Substring(0, length); string str4 = (length < 0) ? xmlName : xmlName.Substring(length + 1); if ((element.Name == str4) && (((element.Form == XmlSchemaForm.Unqualified) && string.IsNullOrEmpty(str3)) || (element.Namespace == str3))) { if (useReflection) { ident = choiceMapping.Constants[i].Value.ToString(CultureInfo.InvariantCulture); } else { ident = choiceMapping.Constants[i].Name; } break; } } if ((ident == null) || (ident.Length == 0)) { if (element.Any && (element.Name.Length == 0)) { throw new InvalidOperationException(Res.GetString("XmlChoiceMissingAnyValue", new object[] { choiceMapping.TypeDesc.FullName })); } throw new InvalidOperationException(Res.GetString("XmlChoiceMissingValue", new object[] { choiceMapping.TypeDesc.FullName, element.Namespace + ":" + element.Name, element.Name, element.Namespace })); } if (!useReflection) { CodeIdentifier.CheckValidIdentifier(ident); } return ident; }
internal XmlMembersMapping(TypeScope scope, ElementAccessor accessor, XmlMappingAccess access) : base(scope, accessor, access) { MembersMapping mapping = (MembersMapping)accessor.Mapping; StringBuilder key = new StringBuilder(); key.Append(":"); mappings = new XmlMemberMapping[mapping.Members.Length]; for (int i = 0; i < mappings.Length; i++) { if (mapping.Members[i].TypeDesc.Type != null) { key.Append(GenerateKey(mapping.Members[i].TypeDesc.Type, null, null)); key.Append(":"); } mappings[i] = new XmlMemberMapping(mapping.Members[i]); } SetKeyInternal(key.ToString()); }
private static bool IsNeedNullableMember(ElementAccessor element) { if (element.Mapping is ArrayMapping) { ArrayMapping arrayMapping = (ArrayMapping)element.Mapping; if (arrayMapping.Elements != null && arrayMapping.Elements.Length == 1) { return(IsNeedNullableMember(arrayMapping.Elements[0])); } return(false); } else { return(element.IsNullable && element.Mapping.TypeDesc.IsValueType); } }
internal ElementAccessor Clone() { ElementAccessor newAccessor = new ElementAccessor(); newAccessor._nullable = _nullable; newAccessor.IsTopLevelInSchema = this.IsTopLevelInSchema; newAccessor.Form = this.Form; newAccessor._isSoap = _isSoap; newAccessor.Name = this.Name; newAccessor.Default = this.Default; newAccessor.Namespace = this.Namespace; newAccessor.Mapping = this.Mapping; newAccessor.Any = this.Any; return(newAccessor); }
public XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate, XmlMappingAccess access) { ElementAccessor accessor = new ElementAccessor { IsSoap = true, Name = ((elementName == null) || (elementName.Length == 0)) ? elementName : XmlConvert.EncodeLocalName(elementName), Mapping = this.ImportMembersMapping(members, ns, hasWrapperElement, writeAccessors, validate, new RecursionLimiter()) }; accessor.Mapping.TypeName = elementName; accessor.Namespace = (accessor.Mapping.Namespace == null) ? ns : accessor.Mapping.Namespace; accessor.Form = XmlSchemaForm.Qualified; return(new XmlMembersMapping(this.typeScope, accessor, access) { IsSoap = true, GenerateSerializer = true }); }
public XmlTypeMapping ImportTypeMapping(Type type, string defaultNamespace) { ElementAccessor element = new ElementAccessor(); element.IsSoap = true; element.Mapping = ImportTypeMapping(_modelScope.GetTypeModel(type), new RecursionLimiter()); element.Name = element.Mapping.DefaultElementName; element.Namespace = element.Mapping.Namespace == null ? defaultNamespace : element.Mapping.Namespace; element.Form = XmlSchemaForm.Qualified; XmlTypeMapping xmlMapping = new XmlTypeMapping(_typeScope, element); xmlMapping.SetKeyInternal(XmlMapping.GenerateKey(type, null, defaultNamespace)); xmlMapping.IsSoap = true; xmlMapping.GenerateSerializer = true; return(xmlMapping); }
object WriteNullableMethod(NullableMapping nullableMapping, bool checkType, string defaultNamespace) { object o = Activator.CreateInstance(nullableMapping.TypeDesc.Type); if (!ReadNull()) { ElementAccessor element = new ElementAccessor(); element.Mapping = nullableMapping.BaseMapping; element.Any = false; element.IsNullable = nullableMapping.BaseMapping.TypeDesc.IsNullable; WriteElement(ref o, null, element, null, false, false, false, defaultNamespace); } return(o); }
void ExportType(TypeMapping mapping, string name, string ns, ElementAccessor rootElement) { if (mapping is StructMapping && ((StructMapping)mapping).ReferencedByTopLevelElement && rootElement == null) { return; } CodeTypeDeclaration codeClass = null; if (exportedMappings[mapping] == null) { exportedMappings.Add(mapping, mapping); if (mapping is EnumMapping) { codeClass = ExportEnum((EnumMapping)mapping, name, ns); } else if (mapping is StructMapping) { codeClass = ExportStruct((StructMapping)mapping, name, ns); } else if (mapping is ArrayMapping) { if (rootElement != null && rootElement.IsTopLevelInSchema && ((ArrayMapping)mapping).TopLevelMapping != null) { codeClass = ExportStruct(((ArrayMapping)mapping).TopLevelMapping, name, ns); } else { EnsureTypesExported(((ArrayMapping)mapping).Elements, ns); } } if (codeClass != null) { exportedClasses.Add(mapping, codeClass); } } else { codeClass = (CodeTypeDeclaration)exportedClasses[mapping]; } if (codeClass != null && rootElement != null) { AddRootMetadata(codeClass.CustomAttributes, mapping, name, ns, rootElement); } }
/// <include file='doc\SoapSchemaImporter.uex' path='docs/doc[@for="SoapSchemaImporter.ImportDerivedTypeMapping"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public XmlTypeMapping ImportDerivedTypeMapping(XmlQualifiedName name, Type baseType, bool baseTypeCanBeIndirect) { TypeMapping mapping = ImportType(name, false); if (mapping is StructMapping) { MakeDerived((StructMapping)mapping, baseType, baseTypeCanBeIndirect); } else if (baseType != null) throw new InvalidOperationException(Res.GetString(Res.XmlPrimitiveBaseType, name.Name, name.Namespace, baseType.FullName)); ElementAccessor accessor = new ElementAccessor(); accessor.IsSoap = true; accessor.Name = Accessor.EscapeName(name.Name, false); accessor.Namespace = name.Namespace; accessor.Mapping = mapping; accessor.IsNullable = true; accessor.Form = XmlSchemaForm.Qualified; return new XmlTypeMapping(scope, accessor); }
/// <include file='doc\SoapReflectionImporter.uex' path='docs/doc[@for="SoapReflectionImporter.ImportMembersMapping2"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate) { ElementAccessor element = new ElementAccessor(); element.IsSoap = true; element.Name = Accessor.EscapeName(elementName, false); element.Mapping = ImportMembersMapping(members, ns, hasWrapperElement, writeAccessors, validate); element.Mapping.TypeName = elementName; element.Namespace = element.Mapping.Namespace == null ? ns : element.Mapping.Namespace; element.Form = XmlSchemaForm.Qualified; XmlMembersMapping xmlMapping = new XmlMembersMapping(typeScope, element); xmlMapping.IsSoap = true; xmlMapping.GenerateSerializer = true; return(xmlMapping); }
/// <include file='doc\SoapReflectionImporter.uex' path='docs/doc[@for="SoapReflectionImporter.ImportMembersMapping3"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate, XmlMappingAccess access) { ElementAccessor element = new ElementAccessor(); element.IsSoap = true; element.Name = elementName == null || elementName.Length == 0 ? elementName : XmlConvert.EncodeLocalName(elementName); element.Mapping = ImportMembersMapping(members, ns, hasWrapperElement, writeAccessors, validate, new RecursionLimiter()); element.Mapping.TypeName = elementName; element.Namespace = element.Mapping.Namespace == null ? ns : element.Mapping.Namespace; element.Form = XmlSchemaForm.Qualified; XmlMembersMapping xmlMapping = new XmlMembersMapping(_typeScope, element, access); xmlMapping.IsSoap = true; xmlMapping.GenerateSerializer = true; return(xmlMapping); }
/// <include file='doc\SoapSchemaImporter.uex' path='docs/doc[@for="SoapSchemaImporter.ImportMembersMapping3"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public XmlMembersMapping ImportMembersMapping(string name, string ns, SoapSchemaMember[] members, bool hasWrapperElement, Type baseType, bool baseTypeCanBeIndirect) { XmlSchemaComplexType type = new XmlSchemaComplexType(); XmlSchemaSequence seq = new XmlSchemaSequence(); type.Particle = seq; foreach (SoapSchemaMember member in members) { XmlSchemaElement element = new XmlSchemaElement(); element.Name = member.MemberName; element.SchemaTypeName = member.MemberType; seq.Items.Add(element); } CodeIdentifiers identifiers = new CodeIdentifiers(); identifiers.UseCamelCasing = true; MembersMapping mapping = new MembersMapping(); mapping.TypeDesc = Scope.GetTypeDesc(typeof(object[])); mapping.Members = ImportTypeMembers(type, ns, identifiers); mapping.HasWrapperElement = hasWrapperElement; if (baseType != null) { for (int i = 0; i < mapping.Members.Length; i++) { MemberMapping member = mapping.Members[i]; if (member.Accessor.Mapping is StructMapping) { MakeDerived((StructMapping)member.Accessor.Mapping, baseType, baseTypeCanBeIndirect); } } } ElementAccessor accessor = new ElementAccessor(); accessor.IsSoap = true; accessor.Name = name; accessor.Namespace = ns; accessor.Mapping = mapping; accessor.IsNullable = false; accessor.Form = XmlSchemaForm.Qualified; return(new XmlMembersMapping(Scope, accessor, XmlMappingAccess.Read | XmlMappingAccess.Write)); }
public XmlTypeMapping ImportTypeMapping(Type type, string defaultNamespace) { ElementAccessor accessor; accessor = new ElementAccessor { IsSoap = true, Mapping = this.ImportTypeMapping(this.modelScope.GetTypeModel(type), new RecursionLimiter()), Name = accessor.Mapping.DefaultElementName, Namespace = (accessor.Mapping.Namespace == null) ? defaultNamespace : accessor.Mapping.Namespace, Form = XmlSchemaForm.Qualified }; XmlTypeMapping mapping = new XmlTypeMapping(this.typeScope, accessor); mapping.SetKeyInternal(XmlMapping.GenerateKey(type, null, defaultNamespace)); mapping.IsSoap = true; mapping.GenerateSerializer = true; return(mapping); }
internal XmlMembersMapping(TypeScope scope, ElementAccessor accessor, XmlMappingAccess access) : base(scope, accessor, access) { MembersMapping mapping = (MembersMapping)accessor.Mapping; StringBuilder builder = new StringBuilder(); builder.Append(":"); this.mappings = new XmlMemberMapping[mapping.Members.Length]; for (int i = 0; i < this.mappings.Length; i++) { if (mapping.Members[i].TypeDesc.Type != null) { builder.Append(XmlMapping.GenerateKey(mapping.Members[i].TypeDesc.Type, null, null)); builder.Append(":"); } this.mappings[i] = new XmlMemberMapping(mapping.Members[i]); } base.SetKeyInternal(builder.ToString()); }
static XmlTypeMapping GetKnownMapping(Type type, string ns) { if (ns != null && ns != string.Empty) { return(null); } TypeDesc typeDesc = (TypeDesc)TypeScope.PrimtiveTypes[type]; if (typeDesc == null) { return(null); } ElementAccessor element = new ElementAccessor(); element.Name = typeDesc.DataType.Name; XmlTypeMapping mapping = new XmlTypeMapping(null, element); mapping.SetKeyInternal(XmlMapping.GenerateKey(type, null, null)); return(mapping); }
private static XmlTypeMapping GetKnownMapping(Type type, string ns) { if ((ns != null) && (ns != string.Empty)) { return(null); } TypeDesc desc = (TypeDesc)TypeScope.PrimtiveTypes[type]; if (desc == null) { return(null); } ElementAccessor accessor = new ElementAccessor { Name = desc.DataType.Name }; XmlTypeMapping mapping = new XmlTypeMapping(null, accessor); mapping.SetKeyInternal(XmlMapping.GenerateKey(type, null, null)); return(mapping); }
void ExportArrayElements(CodeAttributeDeclarationCollection metadata, ArrayMapping array, string ns, TypeDesc elementTypeDesc, int nestingLevel) { for (int i = 0; i < array.Elements.Length; i++) { ElementAccessor arrayElement = array.Elements[i]; TypeMapping elementMapping = arrayElement.Mapping; string elementName = Accessor.UnescapeName(arrayElement.Name); bool sameName = elementName == arrayElement.Mapping.TypeName; bool sameElementType = elementMapping.TypeDesc == elementTypeDesc; bool sameElementNs = arrayElement.Namespace == ns; bool sameNullable = arrayElement.IsNullable == elementMapping.TypeDesc.IsNullable; if (!sameName || !sameElementType || !sameElementNs || !sameNullable || arrayElement.Form != XmlSchemaForm.Qualified || nestingLevel > 0) { ExportArrayItem(metadata, sameName ? null : elementName, sameElementNs ? null : arrayElement.Namespace, sameElementType ? null : elementMapping.TypeDesc, elementMapping.TypeDesc, arrayElement.IsNullable, arrayElement.Form, nestingLevel); } if (elementMapping is ArrayMapping) { ExportArrayElements(metadata, (ArrayMapping)elementMapping, ns, elementTypeDesc.ArrayElementTypeDesc, nestingLevel + 1); } } }
private void GenerateTypeElement(object o, XmlTypeMapping xmlMapping) { ElementAccessor element = xmlMapping.Accessor; TypeMapping mapping = element.Mapping; WriteStartDocument(); if (o == null) { if (element.IsNullable) { if (mapping.IsSoap) { throw new PlatformNotSupportedException(); } else { WriteNullTagLiteral(element.Name, (element.Form == XmlSchemaForm.Qualified ? element.Namespace : "")); } } else { WriteEmptyTag(element.Name, (element.Form == XmlSchemaForm.Qualified ? element.Namespace : "")); } return; } if (!mapping.TypeDesc.IsValueType && !mapping.TypeDesc.Type.GetTypeInfo().IsPrimitive) { TopLevelElement(); } WriteMember(o, null, new ElementAccessor[] { element }, null, null, mapping.TypeDesc, !element.IsSoap, xmlMapping); if (mapping.IsSoap) { WriteReferencedElements(); } }
private void ExportArrayElements(CodeAttributeDeclarationCollection metadata, ArrayMapping array, string ns, TypeDesc elementTypeDesc, int nestingLevel) { for (int i = 0; i < array.Elements.Length; i++) { ElementAccessor accessor = array.Elements[i]; TypeMapping mapping = accessor.Mapping; string str = Accessor.UnescapeName(accessor.Name); bool flag = !accessor.Mapping.TypeDesc.IsArray && (str == accessor.Mapping.TypeName); bool flag2 = mapping.TypeDesc == elementTypeDesc; bool flag3 = (accessor.Form == XmlSchemaForm.Unqualified) || (accessor.Namespace == ns); bool flag4 = accessor.IsNullable == mapping.TypeDesc.IsNullable; bool flag5 = accessor.Form != XmlSchemaForm.Unqualified; if (((!flag || !flag2) || (!flag3 || !flag4)) || (!flag5 || (nestingLevel > 0))) { this.ExportArrayItem(metadata, flag ? null : str, flag3 ? null : accessor.Namespace, flag2 ? null : mapping.TypeDesc, mapping.TypeDesc, accessor.IsNullable, flag5 ? XmlSchemaForm.None : accessor.Form, nestingLevel); } if (mapping is ArrayMapping) { this.ExportArrayElements(metadata, (ArrayMapping)mapping, ns, elementTypeDesc.ArrayElementTypeDesc, nestingLevel + 1); } } }
/// <include file='doc\SoapSchemaImporter.uex' path='docs/doc[@for="SoapSchemaImporter.ImportDerivedTypeMapping"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public XmlTypeMapping ImportDerivedTypeMapping(XmlQualifiedName name, Type baseType, bool baseTypeCanBeIndirect) { TypeMapping mapping = ImportType(name, false); if (mapping is StructMapping) { MakeDerived((StructMapping)mapping, baseType, baseTypeCanBeIndirect); } else if (baseType != null) { throw new InvalidOperationException(SR.Format(SR.XmlPrimitiveBaseType, name.Name, name.Namespace, baseType.FullName)); } ElementAccessor accessor = new ElementAccessor(); accessor.IsSoap = true; accessor.Name = name.Name; accessor.Namespace = name.Namespace; accessor.Mapping = mapping; accessor.IsNullable = true; accessor.Form = XmlSchemaForm.Qualified; return(new XmlTypeMapping(Scope, accessor)); }
private object GenerateTypeElement(XmlTypeMapping xmlTypeMapping) { ElementAccessor element = xmlTypeMapping.Accessor; TypeMapping mapping = element.Mapping; Reader.MoveToContent(); MemberMapping member = new MemberMapping(); member.TypeDesc = mapping.TypeDesc; member.Elements = new ElementAccessor[] { element }; UnknownNodeAction elementElseAction = UnknownNodeAction.CreateUnknownNodeException; UnknownNodeAction elseAction = UnknownNodeAction.ReadUnknownNode; object o = null; Member tempMember = null; var currentMember = new Member(member); WriteMemberElements(ref o, null /*collectionMember*/, out tempMember, new Member[] { currentMember }, elementElseAction, elseAction, element.Any ? currentMember : null, null, xmlTypeMapping); return(o); }
internal static bool ElementsMatch(ElementAccessor[] a, ElementAccessor[] b) { if (a == null) { return (b == null); } if (b == null) { return false; } if (a.Length != b.Length) { return false; } for (int i = 0; i < a.Length; i++) { if (((a[i].Name != b[i].Name) || (a[i].Namespace != b[i].Namespace)) || ((a[i].Form != b[i].Form) || (a[i].IsNullable != b[i].IsNullable))) { return false; } } return true; }
private void AddScopeElement(INameScope scope, ElementAccessor element, ref bool duplicateElements, bool allowDuplicates) { if (scope != null) { ElementAccessor accessor = (ElementAccessor) scope[element.Name, element.Namespace]; if (accessor != null) { if (!allowDuplicates) { throw new InvalidOperationException(Res.GetString("XmlDuplicateElementInScope", new object[] { element.Name, element.Namespace })); } if (accessor.Mapping.TypeDesc != element.Mapping.TypeDesc) { throw new InvalidOperationException(Res.GetString("XmlDuplicateElementInScope1", new object[] { element.Name, element.Namespace })); } duplicateElements = true; } else { scope[element.Name, element.Namespace] = element; } } }
ElementAccessor ImportElement(XmlSchemaElement element, string ns) { if (!element.RefName.IsEmpty) { throw new InvalidOperationException(Res.GetString(Res.RefSyntaxNotSupportedForElements0)); } if (element.Name.Length == 0) { throw new InvalidOperationException(Res.GetString(Res.XmlElementHasNoName)); } TypeMapping mapping = ImportElementType(element, ns); ElementAccessor accessor = new ElementAccessor(); accessor.IsSoap = true; accessor.Name = element.Name; accessor.Namespace = ns; accessor.Mapping = mapping; accessor.IsNullable = false; accessor.Form = XmlSchemaForm.None; return(accessor); }
private ElementAccessor ImportElement(XmlSchemaElement element, string ns) { if (!element.RefName.IsEmpty) { throw new InvalidOperationException(SR.Format(SR.RefSyntaxNotSupportedForElements0, element.RefName.Name, element.RefName.Namespace)); } if (element.Name.Length == 0) { XmlQualifiedName parentType = XmlSchemas.GetParentName(element); throw new InvalidOperationException(SR.Format(SR.XmlElementHasNoName, parentType.Name, parentType.Namespace)); } TypeMapping mapping = ImportElementType(element, ns); ElementAccessor accessor = new ElementAccessor(); accessor.IsSoap = true; accessor.Name = element.Name; accessor.Namespace = ns; accessor.Mapping = mapping; accessor.IsNullable = element.IsNillable; accessor.Form = XmlSchemaForm.None; return(accessor); }
ElementAccessor ImportElement(XmlSchemaElement element, string ns) { if (!element.RefName.IsEmpty) { throw new InvalidOperationException(Res.GetString(Res.RefSyntaxNotSupportedForElements0, element.RefName.Name, element.RefName.Namespace)); } if (element.Name.Length == 0) { XmlQualifiedName parentType = XmlSchemas.GetParentName(element); throw new InvalidOperationException(Res.GetString(Res.XmlElementHasNoName, parentType.Name, parentType.Namespace)); } TypeMapping mapping = ImportElementType(element, ns); ElementAccessor accessor = new ElementAccessor(); accessor.IsSoap = true; accessor.Name = element.Name; accessor.Namespace = ns; accessor.Mapping = mapping; accessor.IsNullable = element.IsNillable; accessor.Form = XmlSchemaForm.None; return accessor; }
internal XmlTypeMapping(TypeScope scope, ElementAccessor accessor) : base(scope, accessor) { }
void WriteArrayItems(ElementAccessor[] elements, TextAccessor text, ChoiceIdentifierAccessor choice, TypeDesc arrayTypeDesc, string arrayName, string choiceName) { TypeDesc arrayElementTypeDesc = arrayTypeDesc.ArrayElementTypeDesc; if (arrayTypeDesc.IsEnumerable) { Writer.Write(typeof(IEnumerator).FullName); Writer.Write(" e = "); if (arrayTypeDesc.IsPrivateImplementation) { Writer.Write("(("); Writer.Write(typeof(IEnumerable).FullName); Writer.Write(")"); Writer.Write(arrayName); Writer.WriteLine(").GetEnumerator();"); } else if(arrayTypeDesc.IsGenericInterface) { if (arrayTypeDesc.UseReflection) { // we use wildcard method name for generic GetEnumerator method, so we cannot use GetStringForMethodInvoke call here Writer.Write("("); Writer.Write(typeof(IEnumerator).FullName); Writer.Write(")"); Writer.Write(RaCodeGen.GetReflectionVariable(arrayTypeDesc.CSharpName, "System.Collections.Generic.IEnumerable*")); Writer.Write(".Invoke("); Writer.Write(arrayName); Writer.WriteLine(", new object[0]);"); } else { Writer.Write("((System.Collections.Generic.IEnumerable<"); Writer.Write(arrayElementTypeDesc.CSharpName); Writer.Write(">)"); Writer.Write(arrayName); Writer.WriteLine(").GetEnumerator();"); } } else { if (arrayTypeDesc.UseReflection) { Writer.Write("("); Writer.Write(typeof(IEnumerator).FullName); Writer.Write(")"); } Writer.Write(RaCodeGen.GetStringForMethodInvoke(arrayName, arrayTypeDesc.CSharpName, "GetEnumerator", arrayTypeDesc.UseReflection)); Writer.WriteLine(";"); } Writer.WriteLine("if (e != null)"); Writer.WriteLine("while (e.MoveNext()) {"); Writer.Indent++; string arrayTypeFullName = arrayElementTypeDesc.CSharpName; WriteLocalDecl(arrayTypeFullName, arrayName+"i", "e.Current", arrayElementTypeDesc.UseReflection); WriteElements(arrayName + "i", choiceName + "i", elements, text, choice, arrayName + "a", true, true); } else { Writer.Write("for (int i"); Writer.Write(arrayName); Writer.Write(" = 0; i"); Writer.Write(arrayName); Writer.Write(" < "); if (arrayTypeDesc.IsArray) { Writer.Write(arrayName); Writer.Write(".Length"); } else { Writer.Write("(("); Writer.Write(typeof(ICollection).FullName); Writer.Write(")"); Writer.Write(arrayName); Writer.Write(").Count"); } Writer.Write("; i"); Writer.Write(arrayName); Writer.WriteLine("++) {"); Writer.Indent++; int count = elements.Length + (text == null ? 0 : 1); if (count > 1) { string arrayTypeFullName = arrayElementTypeDesc.CSharpName; WriteLocalDecl(arrayTypeFullName, arrayName+"i", RaCodeGen.GetStringForArrayMember(arrayName, "i"+arrayName, arrayTypeDesc), arrayElementTypeDesc.UseReflection); if (choice != null) { string choiceFullName = choice.Mapping.TypeDesc.CSharpName; WriteLocalDecl(choiceFullName, choiceName+"i", RaCodeGen.GetStringForArrayMember(choiceName, "i"+arrayName, choice.Mapping.TypeDesc), choice.Mapping.TypeDesc.UseReflection); } WriteElements(arrayName + "i", choiceName + "i", elements, text, choice, arrayName + "a", true, arrayElementTypeDesc.IsNullable); } else { WriteElements(RaCodeGen.GetStringForArrayMember(arrayName , "i" + arrayName, arrayTypeDesc), elements, text, choice, arrayName + "a", true, arrayElementTypeDesc.IsNullable); } } Writer.Indent--; Writer.WriteLine("}"); }
void WriteElements(string source, string enumSource, ElementAccessor[] elements, TextAccessor text, ChoiceIdentifierAccessor choice, string arrayName, bool writeAccessors, bool isNullable) { if (elements.Length == 0 && text == null) return; if (elements.Length == 1 && text == null) { TypeDesc td = elements[0].IsUnbounded ? elements[0].Mapping.TypeDesc.CreateArrayTypeDesc() : elements[0].Mapping.TypeDesc; if (!elements[0].Any && !elements[0].Mapping.TypeDesc.UseReflection && !elements[0].Mapping.TypeDesc.IsOptionalValue) source = "(("+td.CSharpName+")"+ source+")"; WriteElement(source, elements[0], arrayName, writeAccessors); } else { if (isNullable && choice == null) { Writer.Write("if ((object)("); Writer.Write(source); Writer.Write(") != null)"); } Writer.WriteLine("{"); Writer.Indent++; int anyCount = 0; ArrayList namedAnys = new ArrayList(); ElementAccessor unnamedAny = null; // can only have one bool wroteFirstIf = false; string enumTypeName = choice == null ? null : choice.Mapping.TypeDesc.FullName; for (int i = 0; i < elements.Length; i++) { ElementAccessor element = elements[i]; if (element.Any) { anyCount++; if (element.Name != null && element.Name.Length > 0) namedAnys.Add(element); else if (unnamedAny == null) unnamedAny = element; } else if (choice != null) { bool useReflection = element.Mapping.TypeDesc.UseReflection; string fullTypeName = element.Mapping.TypeDesc.CSharpName; bool enumUseReflection = choice.Mapping.TypeDesc.UseReflection; string enumFullName = (enumUseReflection?"":enumTypeName + ".@") + FindChoiceEnumValue(element, (EnumMapping)choice.Mapping, enumUseReflection); if (wroteFirstIf) Writer.Write("else "); else wroteFirstIf = true; Writer.Write("if ("); Writer.Write(enumUseReflection?RaCodeGen.GetStringForEnumLongValue(enumSource, enumUseReflection):enumSource); Writer.Write(" == "); Writer.Write(enumFullName); if (isNullable && !element.IsNullable) { Writer.Write(" && ((object)("); Writer.Write(source); Writer.Write(") != null)"); } Writer.WriteLine(") {"); Writer.Indent++; WriteChoiceTypeCheck(source, fullTypeName, useReflection, choice, enumFullName, element.Mapping.TypeDesc); string castedSource = source; if (!useReflection) castedSource = "(("+fullTypeName+")"+ source+")"; WriteElement(element.Any ? source : castedSource, element, arrayName, writeAccessors); Writer.Indent--; Writer.WriteLine("}"); } else { bool useReflection = element.Mapping.TypeDesc.UseReflection; TypeDesc td = element.IsUnbounded ? element.Mapping.TypeDesc.CreateArrayTypeDesc() : element.Mapping.TypeDesc; string fullTypeName = td.CSharpName; if (wroteFirstIf) Writer.Write("else "); else wroteFirstIf = true; Writer.Write("if ("); WriteInstanceOf(source, fullTypeName, useReflection); Writer.WriteLine(") {"); Writer.Indent++; string castedSource = source; if (!useReflection) castedSource = "(("+fullTypeName+")"+ source+")"; WriteElement(element.Any ? source : castedSource, element, arrayName, writeAccessors); Writer.Indent--; Writer.WriteLine("}"); } } if (anyCount > 0) { if (elements.Length - anyCount > 0) Writer.Write("else "); string fullTypeName = typeof(XmlElement).FullName; Writer.Write("if ("); Writer.Write(source); Writer.Write(" is "); Writer.Write(fullTypeName); Writer.WriteLine(") {"); Writer.Indent++; Writer.Write(fullTypeName); Writer.Write(" elem = ("); Writer.Write(fullTypeName); Writer.Write(")"); Writer.Write(source); Writer.WriteLine(";"); int c = 0; foreach (ElementAccessor element in namedAnys) { if (c++ > 0) Writer.Write("else "); string enumFullName = null; bool useReflection = element.Mapping.TypeDesc.UseReflection; if (choice != null) { bool enumUseReflection = choice.Mapping.TypeDesc.UseReflection; enumFullName = (enumUseReflection?"":enumTypeName + ".@") + FindChoiceEnumValue(element, (EnumMapping)choice.Mapping, enumUseReflection); Writer.Write("if ("); Writer.Write(enumUseReflection?RaCodeGen.GetStringForEnumLongValue(enumSource, enumUseReflection):enumSource); Writer.Write(" == "); Writer.Write(enumFullName); if (isNullable && !element.IsNullable) { Writer.Write(" && ((object)("); Writer.Write(source); Writer.Write(") != null)"); } Writer.WriteLine(") {"); Writer.Indent++; } Writer.Write("if (elem.Name == "); WriteQuotedCSharpString(element.Name); Writer.Write(" && elem.NamespaceURI == "); WriteQuotedCSharpString(element.Namespace); Writer.WriteLine(") {"); Writer.Indent++; WriteElement("elem", element, arrayName, writeAccessors); if (choice != null) { Writer.Indent--; Writer.WriteLine("}"); Writer.WriteLine("else {"); Writer.Indent++; Writer.WriteLine("// throw Value '{0}' of the choice identifier '{1}' does not match element '{2}' from namespace '{3}'."); Writer.Write("throw CreateChoiceIdentifierValueException("); WriteQuotedCSharpString(enumFullName); Writer.Write(", "); WriteQuotedCSharpString(choice.MemberName); Writer.WriteLine(", elem.Name, elem.NamespaceURI);"); Writer.Indent--; Writer.WriteLine("}"); } Writer.Indent--; Writer.WriteLine("}"); } if (c > 0) { Writer.WriteLine("else {"); Writer.Indent++; } if (unnamedAny != null) { WriteElement("elem", unnamedAny, arrayName, writeAccessors); } else { Writer.WriteLine("throw CreateUnknownAnyElementException(elem.Name, elem.NamespaceURI);"); } if (c > 0) { Writer.Indent--; Writer.WriteLine("}"); } Writer.Indent--; Writer.WriteLine("}"); } if (text != null) { bool useReflection = text.Mapping.TypeDesc.UseReflection; string fullTypeName = text.Mapping.TypeDesc.CSharpName; if (elements.Length > 0) { Writer.Write("else "); Writer.Write("if ("); WriteInstanceOf(source, fullTypeName, useReflection); Writer.WriteLine(") {"); Writer.Indent++; string castedSource = source; if (!useReflection) castedSource = "(("+fullTypeName+")"+ source+")"; WriteText(castedSource, text); Writer.Indent--; Writer.WriteLine("}"); } else { string castedSource = source; if (!useReflection) castedSource = "(("+fullTypeName+")"+ source+")"; WriteText(castedSource, text); } } if (elements.Length > 0) { Writer.Write("else "); if (isNullable) { Writer.Write(" if ((object)("); Writer.Write(source); Writer.Write(") != null)"); } Writer.WriteLine("{"); Writer.Indent++; Writer.Write("throw CreateUnknownTypeException("); Writer.Write(source); Writer.WriteLine(");"); Writer.Indent--; Writer.WriteLine("}"); } Writer.Indent--; Writer.WriteLine("}"); } }
void ImportAccessorMapping(MemberMapping accessor, FieldModel model, SoapAttributes a, string ns, XmlSchemaForm form) { Type accessorType = model.FieldType; string accessorName = model.Name; accessor.TypeDesc = typeScope.GetTypeDesc(accessorType); if (accessor.TypeDesc.IsVoid) { throw new InvalidOperationException(Res.GetString(Res.XmlInvalidVoid)); } SoapAttributeFlags flags = a.SoapFlags; if ((flags & SoapAttributeFlags.Attribute) == SoapAttributeFlags.Attribute) { if (!accessor.TypeDesc.IsPrimitive && !accessor.TypeDesc.IsEnum) throw new InvalidOperationException(Res.GetString(Res.XmlIllegalSoapAttribute, accessorName, accessor.TypeDesc.FullName)); if ((flags & SoapAttributeFlags.Attribute) != flags) throw new InvalidOperationException(Res.GetString(Res.XmlInvalidElementAttribute)); AttributeAccessor attribute = new AttributeAccessor(); attribute.Name = Accessor.EscapeQName(a.SoapAttribute == null || a.SoapAttribute.AttributeName.Length == 0 ? accessorName : a.SoapAttribute.AttributeName); attribute.Namespace = a.SoapAttribute == null || a.SoapAttribute.Namespace == null ? ns : a.SoapAttribute.Namespace; attribute.Form = XmlSchemaForm.Qualified; // attributes are always qualified since they're only used for encoded soap headers attribute.Mapping = ImportTypeMapping(modelScope.GetTypeModel(accessorType), (a.SoapAttribute == null ? String.Empty : a.SoapAttribute.DataType)); attribute.Default = GetDefaultValue(model.FieldTypeDesc, a); accessor.Attribute = attribute; accessor.Elements = new ElementAccessor[0]; } else { if ((flags & SoapAttributeFlags.Element) != flags) throw new InvalidOperationException(Res.GetString(Res.XmlInvalidElementAttribute)); ElementAccessor element = new ElementAccessor(); element.IsSoap = true; element.Name = XmlConvert.EncodeLocalName(a.SoapElement == null || a.SoapElement.ElementName.Length == 0 ? accessorName : a.SoapElement.ElementName); element.Namespace = ns; element.Form = form; element.Mapping = ImportTypeMapping(modelScope.GetTypeModel(accessorType), (a.SoapElement == null ? String.Empty : a.SoapElement.DataType)); if (a.SoapElement != null) element.IsNullable = a.SoapElement.IsNullable; accessor.Elements = new ElementAccessor[] { element }; } }
void WriteMember(string source, string choiceSource, ElementAccessor[] elements, TextAccessor text, ChoiceIdentifierAccessor choice, TypeDesc memberTypeDesc, bool writeAccessors) { if (memberTypeDesc.IsArrayLike && !(elements.Length == 1 && elements[0].Mapping is ArrayMapping)) WriteArray(source, choiceSource, elements, text, choice, memberTypeDesc); else WriteElements(source, choiceSource, elements, text, choice, "a", writeAccessors, memberTypeDesc.IsNullable); }
void WriteUnknownNode(string func, string node, ElementAccessor e, bool anyIfs) { if (anyIfs) { Writer.WriteLine("else {"); Writer.Indent++; } Writer.Write(func); Writer.Write("("); Writer.Write(node); if (e != null) { Writer.Write(", "); string expectedElement = e.Form == XmlSchemaForm.Qualified ? e.Namespace : ""; expectedElement += ":"; expectedElement += e.Name; ReflectionAwareCodeGen.WriteQuotedCSharpString(Writer, expectedElement); } Writer.WriteLine(");"); if (anyIfs) { Writer.Indent--; Writer.WriteLine("}"); } }
/// <include file='doc\SoapReflectionImporter.uex' path='docs/doc[@for="XmlReflectionImporter.ImportTypeMapping1"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public XmlTypeMapping ImportTypeMapping(Type type, string defaultNamespace) { ElementAccessor element = new ElementAccessor(); element.IsSoap = true; element.Mapping = ImportTypeMapping(modelScope.GetTypeModel(type)); element.Name = element.Mapping.DefaultElementName; element.Namespace = element.Mapping.Namespace == null ? defaultNamespace : element.Mapping.Namespace; element.Form = XmlSchemaForm.Qualified; XmlTypeMapping xmlMapping = new XmlTypeMapping(typeScope, element); xmlMapping.SetKeyInternal(XmlMapping.GenerateKey(type, null, defaultNamespace)); xmlMapping.IsSoap = true; xmlMapping.GenerateSerializer = true; return xmlMapping; }
private void WriteElement(ref object o, CollectionMember collectionMember, ElementAccessor element, ChoiceIdentifierAccessor choice, bool checkSpecified, bool checkForNull, bool readOnly, string defaultNamespace, object masterObject = null) { object value = null; if (element.Mapping is ArrayMapping) { WriteArray(ref value, (ArrayMapping)element.Mapping, readOnly, element.IsNullable, defaultNamespace); } else if (element.Mapping is NullableMapping) { value = WriteNullableMethod((NullableMapping)element.Mapping, true, defaultNamespace); } else if (!element.Mapping.IsSoap && (element.Mapping is PrimitiveMapping)) { if (element.IsNullable && ReadNull()) { if (element.Mapping.TypeDesc.IsValueType) { value = ReflectionCreateObject(element.Mapping.TypeDesc.Type); } else { value = null; } } else if ((element.Default != null && !Globals.IsDBNullValue(element.Default) && element.Mapping.TypeDesc.IsValueType) && (Reader.IsEmptyElement)) { Reader.Skip(); } else { if (element.Mapping.TypeDesc == QnameTypeDesc) { value = ReadElementQualifiedName(); } else { if (element.Mapping.TypeDesc.FormatterName == "ByteArrayBase64") { value = ToByteArrayBase64(false); } else if (element.Mapping.TypeDesc.FormatterName == "ByteArrayHex") { value = ToByteArrayHex(false); } else { Func <string> readFunc = () => Reader.ReadElementContentAsString(); value = WritePrimitive(element.Mapping, readFunc); } } } } else if (element.Mapping is StructMapping || (element.Mapping.IsSoap && element.Mapping is PrimitiveMapping)) { TypeMapping mapping = element.Mapping; if (mapping.IsSoap) { throw new PlatformNotSupportedException(); } else { if (checkForNull && o == null) { Reader.Skip(); } else { value = WriteStructMethod( mapping: (StructMapping)mapping, isNullable: mapping.TypeDesc.IsNullable && element.IsNullable, checkType: true, defaultNamespace: defaultNamespace ); } } } else if (element.Mapping is SpecialMapping) { SpecialMapping special = (SpecialMapping)element.Mapping; switch (special.TypeDesc.Kind) { case TypeKind.Node: bool isDoc = special.TypeDesc.FullName == typeof(XmlDocument).FullName; if (isDoc) { value = ReadXmlDocument(!element.Any); } else { value = ReadXmlNode(!element.Any); } break; case TypeKind.Serializable: SerializableMapping sm = (SerializableMapping)element.Mapping; // check to see if we need to do the derivation bool flag = true; if (sm.DerivedMappings != null) { XmlQualifiedName tser = GetXsiType(); if (tser == null || QNameEqual(tser, sm.XsiType.Name, sm.XsiType.Namespace, defaultNamespace)) { } else { flag = false; } } if (flag) { bool isWrappedAny = !element.Any && IsWildcard(sm); value = ReadSerializable((IXmlSerializable)ReflectionCreateObject(sm.TypeDesc.Type), isWrappedAny); } if (sm.DerivedMappings != null) { // #10587: To Support SpecialMapping Types Having DerivedMappings throw new NotImplementedException("sm.DerivedMappings != null"); //WriteDerivedSerializable(sm, sm, source, isWrappedAny); //WriteUnknownNode("UnknownNode", "null", null, true); } break; default: throw new InvalidOperationException(SR.Format(SR.XmlInternalError)); } } else { throw new InvalidOperationException(SR.Format(SR.XmlInternalError)); } if (choice != null && masterObject != null) { foreach (var name in choice.MemberIds) { if (name == element.Name) { object choiceValue = Enum.Parse(choice.Mapping.TypeDesc.Type, name); SetOrAddValueToMember(masterObject, choiceValue, choice.MemberInfo); break; } } } if (collectionMember != null) { collectionMember.Add(value); } else { o = value; } }
private void WriteMemberElementsIf(ref object o, CollectionMember collectionMember, out Member member, Member[] expectedMembers, Member anyElementMember, UnknownNodeAction elementElseAction, object masterObject = null) { bool isSequence = IsSequence(expectedMembers); if (isSequence) { // #10586: Currently the reflection based method treat this kind of type as normal types. // But potentially we can do some optimization for types that have ordered properties. } ElementAccessor e = null; member = null; bool foundElement = false; foreach (var m in expectedMembers) { if (m.Mapping.Xmlns != null) { continue; } if (m.Mapping.Ignore) { continue; } if (isSequence && (m.Mapping.IsText || m.Mapping.IsAttribute)) { continue; } foreach (var ele in m.Mapping.Elements) { if (ele.Name == Reader.LocalName && ele.Namespace == Reader.NamespaceURI) { e = ele; member = m; foundElement = true; break; } } if (foundElement) { break; } } if (foundElement) { ChoiceIdentifierAccessor choice = member.Mapping.ChoiceIdentifier; string ns = e.Form == XmlSchemaForm.Qualified ? e.Namespace : ""; bool isList = member.Mapping.TypeDesc.IsArrayLike && !member.Mapping.TypeDesc.IsArray; WriteElement(ref o, collectionMember, e, choice, member.Mapping.CheckSpecified == SpecifiedAccessor.ReadWrite, isList && member.Mapping.TypeDesc.IsNullable, member.Mapping.ReadOnly, ns, masterObject); } else { if (anyElementMember != null && anyElementMember.Mapping != null) { var anyElement = anyElementMember.Mapping; member = anyElementMember; ChoiceIdentifierAccessor choice = member.Mapping.ChoiceIdentifier; ElementAccessor[] elements = anyElement.Elements; for (int i = 0; i < elements.Length; i++) { ElementAccessor element = elements[i]; if (element.Any && element.Name.Length == 0) { string ns = element.Form == XmlSchemaForm.Qualified ? element.Namespace : ""; WriteElement(ref o, collectionMember, element, choice, anyElement.CheckSpecified == SpecifiedAccessor.ReadWrite, false, false, ns, masterObject: masterObject); break; } } } else { member = null; ProcessUnknownNode(elementElseAction); } } }
/// <include file='doc\SoapSchemaImporter.uex' path='docs/doc[@for="SoapSchemaImporter.ImportMembersMapping"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public XmlMembersMapping ImportMembersMapping(string name, string ns, SoapSchemaMember member) { TypeMapping typeMapping = ImportType(member.MemberType, true); if (!(typeMapping is StructMapping)) return ImportMembersMapping(name, ns, new SoapSchemaMember[] { member }); MembersMapping mapping = new MembersMapping(); mapping.TypeDesc = Scope.GetTypeDesc(typeof(object[])); mapping.Members = ((StructMapping)typeMapping).Members; mapping.HasWrapperElement = true; ElementAccessor accessor = new ElementAccessor(); accessor.IsSoap = true; accessor.Name = name; accessor.Namespace = typeMapping.Namespace != null ? typeMapping.Namespace : ns; accessor.Mapping = mapping; accessor.IsNullable = false; accessor.Form = XmlSchemaForm.Qualified; return new XmlMembersMapping(Scope, accessor, XmlMappingAccess.Read | XmlMappingAccess.Write); }
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; }
ElementAccessor ImportArray(XmlSchemaElement element, string ns) { if (element.SchemaType == null) return null; if (!element.IsMultipleOccurrence) return null; XmlSchemaType type = element.SchemaType; ArrayMapping arrayMapping = ImportArrayMapping(type, ns); if (arrayMapping == null) return null; ElementAccessor arrayAccessor = new ElementAccessor(); arrayAccessor.IsSoap = true; arrayAccessor.Name = element.Name; arrayAccessor.Namespace = ns; arrayAccessor.Mapping = arrayMapping; arrayAccessor.IsNullable = false; arrayAccessor.Form = XmlSchemaForm.None; return arrayAccessor; }
private void WriteElements(object o, object enumSource, ElementAccessor[] elements, TextAccessor text, ChoiceIdentifierAccessor choice, string arrayName, bool writeAccessors, bool isNullable, XmlMapping parentMapping = null) { if (elements.Length == 0 && text == null) { return; } if (elements.Length == 1 && text == null) { WriteElement(o, elements[0], arrayName, writeAccessors, parentMapping); } else { if (isNullable && choice == null && o == null) { return; } int anyCount = 0; var namedAnys = new List <ElementAccessor>(); ElementAccessor unnamedAny = null; // can only have one string enumTypeName = choice == null ? null : choice.Mapping.TypeDesc.FullName; for (int i = 0; i < elements.Length; i++) { ElementAccessor element = elements[i]; if (element.Any) { anyCount++; if (element.Name != null && element.Name.Length > 0) { namedAnys.Add(element); } else if (unnamedAny == null) { unnamedAny = element; } } else if (choice != null) { if (o != null && o.GetType() == element.Mapping.TypeDesc.Type) { WriteElement(o, element, arrayName, writeAccessors); return; } } else { TypeDesc td = element.IsUnbounded ? element.Mapping.TypeDesc.CreateArrayTypeDesc() : element.Mapping.TypeDesc; if (o.GetType() == td.Type) { WriteElement(o, element, arrayName, writeAccessors); return; } } } if (anyCount > 0) { var elem = o as XmlElement; if (elem != null) { foreach (ElementAccessor element in namedAnys) { if (element.Name == elem.Name && element.Namespace == elem.NamespaceURI) { WriteElement(elem, element, arrayName, writeAccessors); return; } } if (choice != null) { throw CreateChoiceIdentifierValueException(choice.Mapping.TypeDesc.FullName, choice.MemberName, elem.Name, elem.NamespaceURI); } if (unnamedAny != null) { WriteElement(elem, unnamedAny, arrayName, writeAccessors); return; } throw CreateUnknownAnyElementException(elem.Name, elem.NamespaceURI); } } if (text != null) { bool useReflection = text.Mapping.TypeDesc.UseReflection; string fullTypeName = text.Mapping.TypeDesc.CSharpName; WriteText(o, text); return; } if (elements.Length > 0 && o != null) { throw CreateUnknownTypeException(o); } } }
void WriteNullableMethod(NullableMapping nullableMapping) { string methodName = (string)MethodNames[nullableMapping]; bool useReflection = nullableMapping.BaseMapping.TypeDesc.UseReflection; string typeName = useReflection ? "object" : nullableMapping.TypeDesc.CSharpName; Writer.WriteLine(); Writer.Write(typeName); Writer.Write(" "); Writer.Write(methodName); Writer.WriteLine("(bool checkType) {"); Writer.Indent++; Writer.Write(typeName); Writer.Write(" o = "); if (useReflection) { Writer.Write("null"); } else { Writer.Write("default("); Writer.Write(typeName); Writer.Write(")"); } Writer.WriteLine(";"); Writer.WriteLine("if (ReadNull())"); Writer.Indent++; Writer.WriteLine("return o;"); Writer.Indent--; ElementAccessor element = new ElementAccessor(); element.Mapping = nullableMapping.BaseMapping; element.Any = false; element.IsNullable = nullableMapping.BaseMapping.TypeDesc.IsNullable; WriteElement("o", null, null, element, null, null, false, false, -1, -1); Writer.WriteLine("return o;"); Writer.Indent--; Writer.WriteLine("}"); }
void WriteElement(string source, string arrayName, string choiceSource, ElementAccessor element, ChoiceIdentifierAccessor choice, string checkSpecified, bool checkForNull, bool readOnly, int fixupIndex, int elementIndex) { if (checkSpecified != null && checkSpecified.Length > 0) { Writer.Write(checkSpecified); Writer.WriteLine(" = true;"); } if (element.Mapping is ArrayMapping) { WriteArray(source, arrayName, (ArrayMapping)element.Mapping, readOnly, element.IsNullable, fixupIndex); } else if (element.Mapping is NullableMapping) { string methodName = ReferenceMapping(element.Mapping); #if DEBUG // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (methodName == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorMethod, element.Mapping.TypeDesc.Name)); #endif WriteSourceBegin(source); Writer.Write(methodName); Writer.Write("(true)"); WriteSourceEnd(source); Writer.WriteLine(";"); } else if (!element.Mapping.IsSoap && (element.Mapping is PrimitiveMapping)) { if (element.IsNullable) { Writer.WriteLine("if (ReadNull()) {"); Writer.Indent++; WriteSourceBegin(source); if (element.Mapping.TypeDesc.IsValueType) { Writer.Write(RaCodeGen.GetStringForCreateInstance(element.Mapping.TypeDesc.CSharpName, element.Mapping.TypeDesc.UseReflection, false, false)); } else { Writer.Write("null"); } WriteSourceEnd(source); Writer.WriteLine(";"); Writer.Indent--; Writer.WriteLine("}"); Writer.Write("else "); } if (element.Default != null && element.Default != DBNull.Value && element.Mapping.TypeDesc.IsValueType) { Writer.WriteLine("if (Reader.IsEmptyElement) {"); Writer.Indent++; Writer.WriteLine("Reader.Skip();"); Writer.Indent--; Writer.WriteLine("}"); Writer.WriteLine("else {"); } else { Writer.WriteLine("{"); } Writer.Indent++; WriteSourceBegin(source); if (element.Mapping.TypeDesc == QnameTypeDesc) Writer.Write("ReadElementQualifiedName()"); else { string readFunc; switch (element.Mapping.TypeDesc.FormatterName) { case "ByteArrayBase64": case "ByteArrayHex": readFunc = "false"; break; default: readFunc = "Reader.ReadElementString()"; break; } WritePrimitive(element.Mapping, readFunc); } WriteSourceEnd(source); Writer.WriteLine(";"); Writer.Indent--; Writer.WriteLine("}"); } else if (element.Mapping is StructMapping || (element.Mapping.IsSoap && element.Mapping is PrimitiveMapping)) { TypeMapping mapping = element.Mapping; if (mapping.IsSoap) { Writer.Write("object rre = "); Writer.Write(fixupIndex >= 0 ? "ReadReferencingElement" : "ReadReferencedElement"); Writer.Write("("); WriteID(mapping.TypeName); Writer.Write(", "); WriteID(mapping.Namespace); if (fixupIndex >= 0) { Writer.Write(", out fixup.Ids["); Writer.Write((fixupIndex).ToString(CultureInfo.InvariantCulture)); Writer.Write("]"); } Writer.Write(")"); WriteSourceEnd(source); Writer.WriteLine(";"); if (mapping.TypeDesc.IsValueType) { Writer.WriteLine("if (rre != null) {"); Writer.Indent++; } Writer.WriteLine("try {"); Writer.Indent++; WriteSourceBeginTyped(source, mapping.TypeDesc); Writer.Write("rre"); WriteSourceEnd(source); Writer.WriteLine(";"); WriteCatchCastException(mapping.TypeDesc, "rre", null); Writer.Write("Referenced("); Writer.Write(source); Writer.WriteLine(");"); if (mapping.TypeDesc.IsValueType) { Writer.Indent--; Writer.WriteLine("}"); } } else { string methodName = ReferenceMapping(mapping); #if DEBUG // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (methodName == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorMethod, mapping.TypeDesc.Name)); #endif if (checkForNull) { Writer.Write("if ((object)("); Writer.Write(arrayName); Writer.Write(") == null) Reader.Skip(); else "); } WriteSourceBegin(source); Writer.Write(methodName); Writer.Write("("); if (mapping.TypeDesc.IsNullable) { WriteBooleanValue(element.IsNullable); Writer.Write(", "); } Writer.Write("true"); Writer.Write(")"); WriteSourceEnd(source); Writer.WriteLine(";"); } } else if (element.Mapping is SpecialMapping) { SpecialMapping special = (SpecialMapping)element.Mapping; switch (special.TypeDesc.Kind) { case TypeKind.Node: bool isDoc = special.TypeDesc.FullName == typeof(XmlDocument).FullName; WriteSourceBeginTyped(source, special.TypeDesc); Writer.Write(isDoc ? "ReadXmlDocument(" : "ReadXmlNode("); Writer.Write(element.Any ? "false" : "true"); Writer.Write(")"); WriteSourceEnd(source); Writer.WriteLine(";"); break; case TypeKind.Serializable: SerializableMapping sm = (SerializableMapping)element.Mapping; // check to see if we need to do the derivation if (sm.DerivedMappings != null) { Writer.Write(typeof(XmlQualifiedName).FullName); Writer.WriteLine(" tser = GetXsiType();"); Writer.Write("if (tser == null"); Writer.Write(" || "); WriteQNameEqual("tser", sm.XsiType.Name, sm.XsiType.Namespace); Writer.WriteLine(") {"); Writer.Indent++; } WriteSourceBeginTyped(source, sm.TypeDesc); Writer.Write("ReadSerializable(( "); Writer.Write(typeof(IXmlSerializable).FullName); Writer.Write(")"); Writer.Write(RaCodeGen.GetStringForCreateInstance(sm.TypeDesc.CSharpName, sm.TypeDesc.UseReflection, sm.TypeDesc.CannotNew, false)); bool isWrappedAny = !element.Any && IsWildcard(sm); if (isWrappedAny) { Writer.WriteLine(", true"); } Writer.Write(")"); WriteSourceEnd(source); Writer.WriteLine(";"); if (sm.DerivedMappings != null) { Writer.Indent--; Writer.WriteLine("}"); WriteDerivedSerializable(sm, sm, source, isWrappedAny); WriteUnknownNode("UnknownNode", "null", null, true); } break; default: throw new InvalidOperationException(Res.GetString(Res.XmlInternalError)); } } else { throw new InvalidOperationException(Res.GetString(Res.XmlInternalError)); } if (choice != null) { #if DEBUG // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (choiceSource == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "need parent for the " + source)); #endif string enumTypeName = choice.Mapping.TypeDesc.CSharpName; Writer.Write(choiceSource); Writer.Write(" = "); CodeIdentifier.CheckValidIdentifier(choice.MemberIds[elementIndex]); Writer.Write(RaCodeGen.GetStringForEnumMember(enumTypeName, choice.MemberIds[elementIndex], choice.Mapping.TypeDesc.UseReflection)); Writer.WriteLine(";"); } }
/// <include file='doc\SoapReflectionImporter.uex' path='docs/doc[@for="SoapReflectionImporter.ImportMembersMapping3"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate, XmlMappingAccess access) { ElementAccessor element = new ElementAccessor(); element.IsSoap = true; element.Name = elementName == null || elementName.Length == 0 ? elementName : XmlConvert.EncodeLocalName(elementName); element.Mapping = ImportMembersMapping(members, ns, hasWrapperElement, writeAccessors, validate); element.Mapping.TypeName = elementName; element.Namespace = element.Mapping.Namespace == null ? ns : element.Mapping.Namespace; element.Form = XmlSchemaForm.Qualified; XmlMembersMapping xmlMapping = new XmlMembersMapping(typeScope, element, access); xmlMapping.IsSoap = true; xmlMapping.GenerateSerializer = true; return xmlMapping; }
void ExportElement(ElementAccessor element) { ExportType(element.Mapping, Accessor.UnescapeName(element.Name), element.Namespace, element); }
static ElementAccessor CreateElementAccessor(TypeMapping mapping, string ns) { ElementAccessor element = new ElementAccessor(); element.IsSoap = true; element.Name = mapping.TypeName; //XmlConvert.EncodeLocalName(name == null || name.Length == 0 ? mapping.TypeName : name); element.Namespace = ns; element.Mapping = mapping; return element; }
void AddRootMetadata(CodeAttributeDeclarationCollection metadata, TypeMapping typeMapping, string name, string ns, ElementAccessor rootElement) { string rootAttrName = typeof(XmlRootAttribute).FullName; // check that we haven't already added a root attribute since we can only add one foreach (CodeAttributeDeclaration attr in metadata) { if (attr.Name == rootAttrName) { return; } } CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(rootAttrName); if (typeMapping.TypeDesc.Name != name) { attribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(name))); } if (ns != null) { attribute.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(ns))); } if (typeMapping.TypeDesc != null && typeMapping.TypeDesc.IsAmbiguousDataType) { attribute.Arguments.Add(new CodeAttributeArgument("DataType", new CodePrimitiveExpression(typeMapping.TypeDesc.DataType.Name))); } if ((object)(rootElement.IsNullable) != null) { attribute.Arguments.Add(new CodeAttributeArgument("IsNullable", new CodePrimitiveExpression((bool)rootElement.IsNullable))); } metadata.Add(attribute); }
void WriteArray(string source, string choiceSource, ElementAccessor[] elements, TextAccessor text, ChoiceIdentifierAccessor choice, TypeDesc arrayTypeDesc) { if (elements.Length == 0 && text == null) return; Writer.WriteLine("{"); Writer.Indent++; string arrayTypeName = arrayTypeDesc.CSharpName; WriteArrayLocalDecl(arrayTypeName, "a", source, arrayTypeDesc); if (arrayTypeDesc.IsNullable) { Writer.WriteLine("if (a != null) {"); Writer.Indent++; } if (choice != null) { bool choiceUseReflection = choice.Mapping.TypeDesc.UseReflection; string choiceFullName = choice.Mapping.TypeDesc.CSharpName; WriteArrayLocalDecl(choiceFullName+"[]", "c", choiceSource, choice.Mapping.TypeDesc); // write check for the choice identifier array Writer.WriteLine("if (c == null || c.Length < a.Length) {"); Writer.Indent++; Writer.Write("throw CreateInvalidChoiceIdentifierValueException("); WriteQuotedCSharpString(choice.Mapping.TypeDesc.FullName); Writer.Write(", "); WriteQuotedCSharpString(choice.MemberName); Writer.Write(");"); Writer.Indent--; Writer.WriteLine("}"); } WriteArrayItems(elements, text, choice, arrayTypeDesc, "a", "c"); if (arrayTypeDesc.IsNullable) { Writer.Indent--; Writer.WriteLine("}"); } Writer.Indent--; Writer.WriteLine("}"); }
void AddMemberMetadata(CodeMemberField field, CodeAttributeDeclarationCollection metadata, MemberMapping member, string ns, bool forceUseMemberName) { if (member.Xmlns != null) { CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(XmlNamespaceDeclarationsAttribute).FullName); metadata.Add(attribute); } else if (member.Attribute != null) { AttributeAccessor attribute = member.Attribute; if (attribute.Any) { ExportAnyAttribute(metadata); } else { TypeMapping mapping = (TypeMapping)attribute.Mapping; string attrName = Accessor.UnescapeName(attribute.Name); bool sameType = mapping.TypeDesc == member.TypeDesc || (member.TypeDesc.IsArrayLike && mapping.TypeDesc == member.TypeDesc.ArrayElementTypeDesc); bool sameName = attrName == member.Name && !forceUseMemberName; bool sameNs = attribute.Namespace == ns; bool defaultForm = attribute.Form != XmlSchemaForm.Qualified; ExportAttribute(metadata, sameName ? null : attrName, sameNs ? null : attribute.Namespace, sameType ? null : mapping.TypeDesc, mapping.TypeDesc, defaultForm ? XmlSchemaForm.None : attribute.Form); if (attribute.HasDefault) { AddDefaultValueAttribute(field, metadata, attribute.Default, mapping); } } } else { if (member.Text != null) { TypeMapping mapping = (TypeMapping)member.Text.Mapping; bool sameType = mapping.TypeDesc == member.TypeDesc || (member.TypeDesc.IsArrayLike && mapping.TypeDesc == member.TypeDesc.ArrayElementTypeDesc); ExportText(metadata, sameType ? null : mapping.TypeDesc, mapping.TypeDesc.IsAmbiguousDataType ? mapping.TypeDesc.DataType.Name : null); } if (member.Elements.Length == 1) { ElementAccessor element = member.Elements[0]; TypeMapping mapping = (TypeMapping)element.Mapping; string elemName = Accessor.UnescapeName(element.Name); bool sameName = ((elemName == member.Name) && !forceUseMemberName); bool isArray = mapping is ArrayMapping; bool sameNs = element.Namespace == ns; bool defaultForm = element.Form != XmlSchemaForm.Unqualified; if (element.Any) { ExportAnyElement(metadata, elemName, sameNs ? null : element.Namespace); } else if (isArray) { bool sameType = mapping.TypeDesc == member.TypeDesc; ArrayMapping array = (ArrayMapping)mapping; if (!sameName || !sameNs || element.IsNullable || !defaultForm) { ExportArray(metadata, sameName ? null : elemName, sameNs ? null : element.Namespace, element.IsNullable, defaultForm ? XmlSchemaForm.None : element.Form); } ExportArrayElements(metadata, array, ns, member.TypeDesc.ArrayElementTypeDesc, 0); } else { bool sameType = mapping.TypeDesc == member.TypeDesc || (member.TypeDesc.IsArrayLike && mapping.TypeDesc == member.TypeDesc.ArrayElementTypeDesc); if (member.TypeDesc.IsArrayLike) { sameName = false; } if (member.TypeDesc.IsAmbiguousDataType || member.TypeDesc.IsArrayLike || !sameName || !sameType || !sameNs || element.IsNullable || !defaultForm) { ExportElement(metadata, sameName ? null : elemName, sameNs ? null : element.Namespace, sameType ? null : mapping.TypeDesc, mapping.TypeDesc, element.IsNullable, defaultForm ? XmlSchemaForm.None : element.Form); } } if (element.HasDefault) { AddDefaultValueAttribute(field, metadata, element.Default, mapping); } } else { for (int i = 0; i < member.Elements.Length; i++) { ElementAccessor element = member.Elements[i]; string elemName = Accessor.UnescapeName(element.Name); bool sameNs = element.Namespace == ns; if (element.Any) { ExportAnyElement(metadata, elemName, sameNs ? null : element.Namespace); } else { bool defaultForm = element.Form != XmlSchemaForm.Unqualified; ExportElement(metadata, elemName, sameNs ? null : element.Namespace, ((TypeMapping)element.Mapping).TypeDesc, ((TypeMapping)element.Mapping).TypeDesc, element.IsNullable, defaultForm ? XmlSchemaForm.None : element.Form); } } } if (member.ChoiceIdentifier != null) { CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(XmlChoiceIdentifierAttribute).FullName); attribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(member.ChoiceIdentifier.MemberName))); metadata.Add(attribute); } if (member.Ignore) { CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(XmlIgnoreAttribute).FullName); metadata.Add(attribute); } } }
void WriteElements(string source, ElementAccessor[] elements, TextAccessor text, ChoiceIdentifierAccessor choice, string arrayName, bool writeAccessors, bool isNullable) { WriteElements(source, null, elements, text, choice, arrayName, writeAccessors, isNullable); }
private void WriteElement(object o, ElementAccessor element, string arrayName, bool writeAccessor, XmlMapping parentMapping = null) { string name = writeAccessor ? element.Name : element.Mapping.TypeName; string ns = element.Any && element.Name.Length == 0 ? null : (element.Form == XmlSchemaForm.Qualified ? (writeAccessor ? element.Namespace : element.Mapping.Namespace) : ""); if (element.Mapping is NullableMapping) { if (o != null) { ElementAccessor e = element.Clone(); e.Mapping = ((NullableMapping)element.Mapping).BaseMapping; WriteElement(o, e, arrayName, writeAccessor); } else if (element.IsNullable) { WriteNullTagLiteral(element.Name, ns); } } else if (element.Mapping is ArrayMapping) { var mapping = (ArrayMapping)element.Mapping; if (mapping.IsSoap) { throw new PlatformNotSupportedException(); } if (element.IsNullable && o == null) { WriteNullTagLiteral(element.Name, element.Form == XmlSchemaForm.Qualified ? element.Namespace : ""); } else { if (element.IsUnbounded) { TypeDesc arrayTypeDesc = mapping.TypeDesc.CreateArrayTypeDesc(); var enumerable = (IEnumerable)o; foreach (var e in enumerable) { element.IsUnbounded = false; WriteElement(e, element, arrayName, writeAccessor); element.IsUnbounded = true; } } else { if (o != null) { WriteStartElement(name, ns, false); WriteArrayItems(mapping.ElementsSortedByDerivation, null, null, mapping.TypeDesc, o); WriteEndElement(); } } } } else if (element.Mapping is EnumMapping) { if (element.Mapping.IsSoap) { Writer.WriteStartElement(name, ns); WriteEnumMethod((EnumMapping)element.Mapping, o); WriteEndElement(); } else { WritePrimitive(WritePrimitiveMethodRequirement.WriteElementString, name, ns, element.Default, o, element.Mapping, false, true, element.IsNullable); } } else if (element.Mapping is PrimitiveMapping) { PrimitiveMapping mapping = (PrimitiveMapping)element.Mapping; if (mapping.TypeDesc == QnameTypeDesc) { WriteQualifiedNameElement(name, ns, element.Default, (XmlQualifiedName)o, element.IsNullable, mapping.IsSoap, mapping); } else { WritePrimitiveMethodRequirement suffixNullable = mapping.IsSoap ? WritePrimitiveMethodRequirement.Encoded : WritePrimitiveMethodRequirement.None; WritePrimitiveMethodRequirement suffixRaw = mapping.TypeDesc.XmlEncodingNotRequired ? WritePrimitiveMethodRequirement.Raw : WritePrimitiveMethodRequirement.None; WritePrimitive(element.IsNullable ? WritePrimitiveMethodRequirement.WriteNullableStringLiteral | suffixNullable | suffixRaw : WritePrimitiveMethodRequirement.WriteElementString | suffixRaw, name, ns, element.Default, o, mapping, mapping.IsSoap, true, element.IsNullable); } } else if (element.Mapping is StructMapping) { var mapping = (StructMapping)element.Mapping; if (mapping.IsSoap) { EnsureXmlSerializationWriteCallbackForMapping(mapping, name, ns, element.IsNullable, needType: false, parentMapping: parentMapping); WritePotentiallyReferencingElement(name, ns, o, !writeAccessor ? mapping.TypeDesc.Type : null, !writeAccessor, element.IsNullable); } else { WriteStructMethod(mapping, name, ns, o, element.IsNullable, needType: false, parentMapping: parentMapping); } } else if (element.Mapping is SpecialMapping) { if (element.Mapping is SerializableMapping) { WriteSerializable((IXmlSerializable)o, name, ns, element.IsNullable, !element.Any); } else { // XmlNode, XmlElement var node = o as XmlNode; if (node != null) { WriteElementLiteral(node, name, ns, element.IsNullable, element.Any); } else { throw CreateInvalidAnyTypeException(o); } } } else { throw new InvalidOperationException(SR.XmlInternalError); } }
void WriteElement(string source, ElementAccessor element, string arrayName, bool writeAccessor) { string name = writeAccessor ? element.Name : element.Mapping.TypeName; string ns = element.Any && element.Name.Length == 0 ? null : (element.Form == XmlSchemaForm.Qualified ? (writeAccessor ? element.Namespace : element.Mapping.Namespace) : ""); if (element.Mapping is NullableMapping) { Writer.Write("if ("); Writer.Write(source); Writer.WriteLine(" != null) {"); Writer.Indent++; string fullTypeName = element.Mapping.TypeDesc.BaseTypeDesc.CSharpName; string castedSource = source; if (!element.Mapping.TypeDesc.BaseTypeDesc.UseReflection) castedSource = "(("+fullTypeName+")"+ source+")"; ElementAccessor e = element.Clone(); e.Mapping = ((NullableMapping)element.Mapping).BaseMapping; WriteElement(e.Any ? source : castedSource, e, arrayName, writeAccessor); Writer.Indent--; Writer.WriteLine("}"); if (element.IsNullable) { Writer.WriteLine("else {"); Writer.Indent++; WriteLiteralNullTag(element.Name, element.Form == XmlSchemaForm.Qualified ? element.Namespace : ""); Writer.Indent--; Writer.WriteLine("}"); } } else if (element.Mapping is ArrayMapping) { ArrayMapping mapping = (ArrayMapping)element.Mapping; if (mapping.IsSoap) { Writer.Write("WritePotentiallyReferencingElement("); WriteQuotedCSharpString(name); Writer.Write(", "); WriteQuotedCSharpString(ns); Writer.Write(", "); Writer.Write(source); if (!writeAccessor) { Writer.Write(", "); Writer.Write(RaCodeGen.GetStringForTypeof(mapping.TypeDesc.CSharpName, mapping.TypeDesc.UseReflection)); Writer.Write(", true, "); } else { Writer.Write(", null, false, "); } WriteValue(element.IsNullable); Writer.WriteLine(");"); } else if (element.IsUnbounded) { TypeDesc td = mapping.TypeDesc.CreateArrayTypeDesc(); string fullTypeName = td.CSharpName; string elementArrayName = "el" + arrayName; string arrayIndex = "c" + elementArrayName; Writer.WriteLine("{"); Writer.Indent++; WriteArrayLocalDecl(fullTypeName, elementArrayName, source, mapping.TypeDesc); if (element.IsNullable) { WriteNullCheckBegin(elementArrayName, element); } else { if (mapping.TypeDesc.IsNullable) { Writer.Write("if ("); Writer.Write(elementArrayName); Writer.Write(" != null)"); } Writer.WriteLine("{"); Writer.Indent++; } Writer.Write("for (int "); Writer.Write(arrayIndex); Writer.Write(" = 0; "); Writer.Write(arrayIndex); Writer.Write(" < "); if (td.IsArray) { Writer.Write(elementArrayName); Writer.Write(".Length"); } else { Writer.Write("(("); Writer.Write(typeof(ICollection).FullName); Writer.Write(")"); Writer.Write(elementArrayName); Writer.Write(").Count"); } Writer.Write("; "); Writer.Write(arrayIndex); Writer.WriteLine("++) {"); Writer.Indent++; element.IsUnbounded = false; WriteElement(elementArrayName + "[" + arrayIndex + "]", element, arrayName, writeAccessor); element.IsUnbounded = true; Writer.Indent--; Writer.WriteLine("}"); Writer.Indent--; Writer.WriteLine("}"); Writer.Indent--; Writer.WriteLine("}"); } else { string fullTypeName = mapping.TypeDesc.CSharpName; Writer.WriteLine("{"); Writer.Indent++; WriteArrayLocalDecl(fullTypeName, arrayName, source, mapping.TypeDesc); if (element.IsNullable) { WriteNullCheckBegin(arrayName, element); } else { if (mapping.TypeDesc.IsNullable) { Writer.Write("if ("); Writer.Write(arrayName); Writer.Write(" != null)"); } Writer.WriteLine("{"); Writer.Indent++; } WriteStartElement(name, ns, false); WriteArrayItems(mapping.ElementsSortedByDerivation, null, null, mapping.TypeDesc, arrayName, null); WriteEndElement(); Writer.Indent--; Writer.WriteLine("}"); Writer.Indent--; Writer.WriteLine("}"); } } else if (element.Mapping is EnumMapping) { if (element.Mapping.IsSoap) { string methodName = (string)MethodNames[element.Mapping]; Writer.Write("Writer.WriteStartElement("); WriteQuotedCSharpString(name); Writer.Write(", "); WriteQuotedCSharpString(ns); Writer.WriteLine(");"); Writer.Write(methodName); Writer.Write("("); Writer.Write(source); Writer.WriteLine(");"); WriteEndElement(); } else { WritePrimitive("WriteElementString", name, ns, element.Default, source, element.Mapping, false, true, element.IsNullable); } } else if (element.Mapping is PrimitiveMapping) { PrimitiveMapping mapping = (PrimitiveMapping)element.Mapping; if (mapping.TypeDesc == QnameTypeDesc) WriteQualifiedNameElement(name, ns, element.Default, source, element.IsNullable, mapping.IsSoap, mapping); else { string suffixNullable = mapping.IsSoap ? "Encoded" : "Literal"; string suffixRaw = mapping.TypeDesc.XmlEncodingNotRequired?"Raw":""; WritePrimitive(element.IsNullable ? ("WriteNullableString" + suffixNullable + suffixRaw) : ("WriteElementString" + suffixRaw), name, ns, element.Default, source, mapping, mapping.IsSoap, true, element.IsNullable); } } else if (element.Mapping is StructMapping) { StructMapping mapping = (StructMapping)element.Mapping; if (mapping.IsSoap) { Writer.Write("WritePotentiallyReferencingElement("); WriteQuotedCSharpString(name); Writer.Write(", "); WriteQuotedCSharpString(ns); Writer.Write(", "); Writer.Write(source); if (!writeAccessor) { Writer.Write(", "); Writer.Write(RaCodeGen.GetStringForTypeof(mapping.TypeDesc.CSharpName, mapping.TypeDesc.UseReflection)); Writer.Write(", true, "); } else { Writer.Write(", null, false, "); } WriteValue(element.IsNullable); } else { string methodName = ReferenceMapping(mapping); #if DEBUG // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (methodName == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorMethod, mapping.TypeDesc.Name) + Environment.StackTrace); #endif Writer.Write(methodName); Writer.Write("("); WriteQuotedCSharpString(name); Writer.Write(", "); if (ns == null) Writer.Write("null"); else { WriteQuotedCSharpString(ns); } Writer.Write(", "); Writer.Write(source); if (mapping.TypeDesc.IsNullable) { Writer.Write(", "); WriteValue(element.IsNullable); } Writer.Write(", false"); } Writer.WriteLine(");"); } else if (element.Mapping is SpecialMapping) { SpecialMapping mapping = (SpecialMapping)element.Mapping; bool useReflection = mapping.TypeDesc.UseReflection; TypeDesc td = mapping.TypeDesc; string fullTypeName = td.CSharpName; if (element.Mapping is SerializableMapping) { WriteElementCall("WriteSerializable", typeof(IXmlSerializable), source, name, ns, element.IsNullable, !element.Any); } else { // XmlNode, XmlElement Writer.Write("if (("); Writer.Write(source); Writer.Write(") is "); Writer.Write(typeof(XmlNode).FullName); Writer.Write(" || "); Writer.Write(source); Writer.Write(" == null"); Writer.WriteLine(") {"); Writer.Indent++; WriteElementCall("WriteElementLiteral", typeof(XmlNode), source, name, ns, element.IsNullable, element.Any); Writer.Indent--; Writer.WriteLine("}"); Writer.WriteLine("else {"); Writer.Indent++; Writer.Write("throw CreateInvalidAnyTypeException("); Writer.Write(source); Writer.WriteLine(");"); Writer.Indent--; Writer.WriteLine("}"); } } else { throw new InvalidOperationException(Res.GetString(Res.XmlInternalError)); } }
void WriteNullCheckBegin(string source, ElementAccessor element) { Writer.Write("if ((object)("); Writer.Write(source); Writer.WriteLine(") == null) {"); Writer.Indent++; WriteLiteralNullTag(element.Name, element.Form == XmlSchemaForm.Qualified ? element.Namespace : ""); Writer.Indent--; Writer.WriteLine("}"); Writer.WriteLine("else {"); Writer.Indent++; }
string FindChoiceEnumValue(ElementAccessor element, EnumMapping choiceMapping, bool useReflection) { string enumValue = null; for (int i = 0; i < choiceMapping.Constants.Length; i++) { string xmlName = choiceMapping.Constants[i].XmlName; if (element.Any && element.Name.Length == 0) { if (xmlName == "##any:") { if (useReflection) enumValue = choiceMapping.Constants[i].Value.ToString(CultureInfo.InvariantCulture); else enumValue = choiceMapping.Constants[i].Name; break; } continue; } int colon = xmlName.LastIndexOf(':'); string choiceNs = colon < 0 ? choiceMapping.Namespace : xmlName.Substring(0, colon); string choiceName = colon < 0 ? xmlName : xmlName.Substring(colon+1); if (element.Name == choiceName) { if ((element.Form == XmlSchemaForm.Unqualified && string.IsNullOrEmpty(choiceNs)) || element.Namespace == choiceNs) { if (useReflection) enumValue = choiceMapping.Constants[i].Value.ToString(CultureInfo.InvariantCulture); else enumValue = choiceMapping.Constants[i].Name; break; } } } if (enumValue == null || enumValue.Length == 0) { if (element.Any && element.Name.Length == 0) { // Type {0} is missing enumeration value '##any' for XmlAnyElementAttribute. throw new InvalidOperationException(Res.GetString(Res.XmlChoiceMissingAnyValue, choiceMapping.TypeDesc.FullName)); } // Type {0} is missing value for '{1}'. throw new InvalidOperationException(Res.GetString(Res.XmlChoiceMissingValue, choiceMapping.TypeDesc.FullName, element.Namespace + ":" + element.Name, element.Name, element.Namespace)); } if(!useReflection) CodeIdentifier.CheckValidIdentifier(enumValue); return enumValue; }
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); }