1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
|
//
// System.Xml.Serialization.SoapReflectionImporter
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2002
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections;
using System.Globalization;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;
namespace System.Xml.Serialization {
public class SoapReflectionImporter {
SoapAttributeOverrides attributeOverrides;
string initialDefaultNamespace;
ArrayList includedTypes;
ArrayList relatedMaps = new ArrayList ();
ReflectionHelper helper = new ReflectionHelper();
#region Constructors
public SoapReflectionImporter (): this (null, null)
{
}
public SoapReflectionImporter (SoapAttributeOverrides attributeOverrides): this (attributeOverrides, null)
{
}
public SoapReflectionImporter (string defaultNamespace): this (null, defaultNamespace)
{
}
public SoapReflectionImporter (SoapAttributeOverrides attributeOverrides, string defaultNamespace)
{
if (defaultNamespace == null) initialDefaultNamespace = String.Empty;
else initialDefaultNamespace = defaultNamespace;
if (attributeOverrides == null) this.attributeOverrides = new SoapAttributeOverrides();
else this.attributeOverrides = attributeOverrides;
}
#endregion // Constructors
#region Methods
public XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members)
{
return ImportMembersMapping (elementName, ns, members, true, true, false);
}
public XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors)
{
return ImportMembersMapping (elementName, ns, members, hasWrapperElement, writeAccessors, false);
}
public XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate)
{
return ImportMembersMapping (elementName, ns, members, hasWrapperElement, writeAccessors, validate, XmlMappingAccess.Read | XmlMappingAccess.Write);
}
#if NET_2_0
[MonoTODO]
public
#endif
XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate, XmlMappingAccess access)
{
elementName = XmlConvert.EncodeLocalName (elementName);
XmlMemberMapping[] mapping = new XmlMemberMapping[members.Length];
for (int n=0; n<members.Length; n++)
{
XmlTypeMapMember mapMem = CreateMapMember (members[n], ns);
mapping[n] = new XmlMemberMapping (XmlConvert.EncodeLocalName (members[n].MemberName), ns, mapMem, true);
}
XmlMembersMapping mps = new XmlMembersMapping (elementName, ns, hasWrapperElement, writeAccessors, mapping);
mps.RelatedMaps = relatedMaps;
mps.Format = SerializationFormat.Encoded;
Type[] extraTypes = includedTypes != null ? (Type[])includedTypes.ToArray(typeof(Type)) : null;
mps.Source = new MembersSerializationSource (elementName, hasWrapperElement, members, writeAccessors, false, null, extraTypes);
return mps;
}
public XmlTypeMapping ImportTypeMapping (Type type)
{
return ImportTypeMapping (type, null);
}
public XmlTypeMapping ImportTypeMapping (Type type, string defaultNamespace)
{
if (type == null)
throw new ArgumentNullException ("type");
if (type == typeof (void))
throw new InvalidOperationException ("Type " + type.Name + " may not be serialized.");
return ImportTypeMapping (TypeTranslator.GetTypeData (type),
defaultNamespace);
}
internal XmlTypeMapping ImportTypeMapping (TypeData typeData, string defaultNamespace)
{
if (typeData == null)
throw new ArgumentNullException ("typeData");
if (typeData.Type == null)
throw new ArgumentException ("Specified TypeData instance does not have Type set.");
string oldNs = initialDefaultNamespace;
if (defaultNamespace == null) defaultNamespace = initialDefaultNamespace;
if (defaultNamespace == null) defaultNamespace = string.Empty;
initialDefaultNamespace = defaultNamespace;
XmlTypeMapping map;
switch (typeData.SchemaType) {
case SchemaTypes.Class: map = ImportClassMapping (typeData, defaultNamespace); break;
case SchemaTypes.Array: map = ImportListMapping (typeData, defaultNamespace); break;
case SchemaTypes.XmlNode: throw CreateTypeException (typeData.Type);
case SchemaTypes.Primitive: map = ImportPrimitiveMapping (typeData, defaultNamespace); break;
case SchemaTypes.Enum: map = ImportEnumMapping (typeData, defaultNamespace); break;
case SchemaTypes.XmlSerializable:
default: throw new NotSupportedException ("Type " + typeData.Type.FullName + " not supported for XML serialization");
}
map.RelatedMaps = relatedMaps;
map.Format = SerializationFormat.Encoded;
Type[] extraTypes = includedTypes != null ? (Type[])includedTypes.ToArray(typeof(Type)) : null;
map.Source = new SoapTypeSerializationSource (typeData.Type, attributeOverrides, defaultNamespace, extraTypes);
initialDefaultNamespace = oldNs;
return map;
}
XmlTypeMapping CreateTypeMapping (TypeData typeData, string defaultXmlType, string defaultNamespace)
{
string membersNamespace = defaultNamespace;
bool includeInSchema = true;
SoapAttributes atts = null;
if (defaultXmlType == null) defaultXmlType = typeData.XmlType;
if (!typeData.IsListType)
{
if (attributeOverrides != null)
atts = attributeOverrides[typeData.Type];
if (atts != null && typeData.SchemaType == SchemaTypes.Primitive)
throw new InvalidOperationException ("SoapType attribute may not be specified for the type " + typeData.FullTypeName);
}
if (atts == null)
atts = new SoapAttributes (typeData.Type);
if (atts.SoapType != null)
{
if (atts.SoapType.Namespace != null && atts.SoapType.Namespace != string.Empty)
membersNamespace = atts.SoapType.Namespace;
if (atts.SoapType.TypeName != null && atts.SoapType.TypeName != string.Empty)
defaultXmlType = XmlConvert.EncodeLocalName (atts.SoapType.TypeName);
includeInSchema = atts.SoapType.IncludeInSchema;
}
if (membersNamespace == null) membersNamespace = "";
XmlTypeMapping map = new XmlTypeMapping (defaultXmlType, membersNamespace, typeData, defaultXmlType, membersNamespace);
map.IncludeInSchema = includeInSchema;
relatedMaps.Add (map);
return map;
}
XmlTypeMapping ImportClassMapping (Type type, string defaultNamespace)
{
TypeData typeData = TypeTranslator.GetTypeData (type);
return ImportClassMapping (typeData, defaultNamespace);
}
XmlTypeMapping ImportClassMapping (TypeData typeData, string defaultNamespace)
{
Type type = typeData.Type;
if (type.IsValueType) throw CreateStructException (type);
if (type == typeof (object)) defaultNamespace = XmlSchema.Namespace;
ReflectionHelper.CheckSerializableType (type, false);
XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, defaultNamespace));
if (map != null) return map;
map = CreateTypeMapping (typeData, null, defaultNamespace);
helper.RegisterClrType (map, type, map.Namespace);
map.MultiReferenceType = true;
ClassMap classMap = new ClassMap ();
map.ObjectMap = classMap;
// Import members
ICollection members = GetReflectionMembers (type);
foreach (XmlReflectionMember rmember in members) {
if (rmember.SoapAttributes.SoapIgnore) continue;
classMap.AddMember (CreateMapMember (rmember, defaultNamespace));
}
// Import included classes
SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes (typeof (SoapIncludeAttribute), false);
for (int n=0; n<includes.Length; n++)
{
Type includedType = includes[n].Type;
ImportTypeMapping (includedType);
}
if (type == typeof (object) && includedTypes != null)
{
foreach (Type intype in includedTypes)
map.DerivedTypes.Add (ImportTypeMapping (intype));
}
// Register inheritance relations
if (type.BaseType != null)
{
XmlTypeMapping bmap = ImportClassMapping (type.BaseType, defaultNamespace);
if (type.BaseType != typeof (object))
map.BaseMap = bmap;
// At this point, derived classes of this map must be already registered
RegisterDerivedMap (bmap, map);
}
return map;
}
void RegisterDerivedMap (XmlTypeMapping map, XmlTypeMapping derivedMap)
{
map.DerivedTypes.Add (derivedMap);
map.DerivedTypes.AddRange (derivedMap.DerivedTypes);
if (map.BaseMap != null)
RegisterDerivedMap (map.BaseMap, derivedMap);
else {
XmlTypeMapping obmap = ImportTypeMapping (typeof(object));
if (obmap != map)
obmap.DerivedTypes.Add (derivedMap);
}
}
string GetTypeNamespace (TypeData typeData, string defaultNamespace)
{
string membersNamespace = defaultNamespace;
SoapAttributes atts = null;
if (!typeData.IsListType)
{
if (attributeOverrides != null)
atts = attributeOverrides[typeData.Type];
}
if (atts == null)
atts = new SoapAttributes (typeData.Type);
if (atts.SoapType != null)
{
if (atts.SoapType.Namespace != null && atts.SoapType.Namespace != string.Empty)
membersNamespace = atts.SoapType.Namespace;
}
if (membersNamespace == null) return "";
else return membersNamespace;
}
XmlTypeMapping ImportListMapping (TypeData typeData, string defaultNamespace)
{
Type type = typeData.Type;
XmlTypeMapping map = helper.GetRegisteredClrType (type, XmlSerializer.EncodingNamespace);
if (map != null) return map;
ListMap obmap = new ListMap ();
TypeData itemTypeData = typeData.ListItemTypeData;
map = CreateTypeMapping (typeData, "Array", XmlSerializer.EncodingNamespace);
helper.RegisterClrType (map, type, XmlSerializer.EncodingNamespace);
map.MultiReferenceType = true;
map.ObjectMap = obmap;
XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, itemTypeData);
if (elem.TypeData.IsComplexType) {
elem.MappedType = ImportTypeMapping (typeData.ListItemType, defaultNamespace);
elem.TypeData = elem.MappedType.TypeData;
}
elem.ElementName = "Item";
elem.Namespace = string.Empty;
elem.IsNullable = true; // By default, items are nullable
XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
list.Add (elem);
obmap.ItemInfo = list;
XmlTypeMapping objMap = ImportTypeMapping (typeof(object), defaultNamespace);
objMap.DerivedTypes.Add (map);
// Register any of the including types as a derived class of object
SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes (typeof (SoapIncludeAttribute), false);
for (int i = 0; i < includes.Length; i++)
{
Type includedType = includes[i].Type;
objMap.DerivedTypes.Add(ImportTypeMapping (includedType, defaultNamespace));
}
return map;
}
XmlTypeMapping ImportPrimitiveMapping (TypeData typeData, string defaultNamespace)
{
if (typeData.SchemaType == SchemaTypes.Primitive)
defaultNamespace = typeData.IsXsdType ? XmlSchema.Namespace : XmlSerializer.WsdlTypesNamespace;
Type type = typeData.Type;
XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, defaultNamespace));
if (map != null) return map;
map = CreateTypeMapping (typeData, null, defaultNamespace);
helper.RegisterClrType (map, type, map.Namespace);
return map;
}
XmlTypeMapping ImportEnumMapping (TypeData typeData, string defaultNamespace)
{
Type type = typeData.Type;
XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, defaultNamespace));
if (map != null) return map;
ReflectionHelper.CheckSerializableType (type, false);
map = CreateTypeMapping (typeData, null, defaultNamespace);
helper.RegisterClrType (map, type, map.Namespace);
map.MultiReferenceType = true;
string [] names = Enum.GetNames (type);
EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember[names.Length];
for (int n=0; n<names.Length; n++)
{
FieldInfo field = type.GetField (names[n]);
string xmlName = names[n];
object[] atts = field.GetCustomAttributes (typeof(SoapEnumAttribute), false);
if (atts.Length > 0) xmlName = ((SoapEnumAttribute)atts[0]).Name;
long value = ((IConvertible) field.GetValue (null)).ToInt64 (CultureInfo.InvariantCulture);
members[n] = new EnumMap.EnumMapMember (XmlConvert.EncodeLocalName (xmlName), names[n], value);
}
bool isFlags = type.IsDefined (typeof (FlagsAttribute), false);
map.ObjectMap = new EnumMap (members, isFlags);
ImportTypeMapping (typeof(object), defaultNamespace).DerivedTypes.Add (map);
return map;
}
ICollection GetReflectionMembers (Type type)
{
ArrayList members = new ArrayList();
PropertyInfo[] properties = type.GetProperties (BindingFlags.Instance | BindingFlags.Public);
foreach (PropertyInfo prop in properties)
{
if (!prop.CanRead) continue;
if (!prop.CanWrite && (TypeTranslator.GetTypeData (prop.PropertyType).SchemaType != SchemaTypes.Array || prop.PropertyType.IsArray))
continue;
SoapAttributes atts = attributeOverrides[type, prop.Name];
if (atts == null) atts = new SoapAttributes (prop);
if (atts.SoapIgnore) continue;
XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
members.Add (member);
}
FieldInfo[] fields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
foreach (FieldInfo field in fields)
{
SoapAttributes atts = attributeOverrides[type, field.Name];
if (atts == null) atts = new SoapAttributes (field);
if (atts.SoapIgnore) continue;
XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
members.Add (member);
}
return members;
}
private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
{
XmlTypeMapMember mapMember;
SoapAttributes atts = rmember.SoapAttributes;
TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
if (atts.SoapAttribute != null)
{
// An attribute
if (typeData.SchemaType != SchemaTypes.Enum && typeData.SchemaType != SchemaTypes.Primitive) {
throw new InvalidOperationException (string.Format (CultureInfo.InvariantCulture,
"Cannot serialize member '{0}' of type {1}. " +
"SoapAttribute cannot be used to encode complex types.",
rmember.MemberName, typeData.FullTypeName));
}
if (atts.SoapElement != null)
throw new Exception ("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member");
XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
if (atts.SoapAttribute.AttributeName.Length == 0)
mapAttribute.AttributeName = XmlConvert.EncodeLocalName (rmember.MemberName);
else
mapAttribute.AttributeName = XmlConvert.EncodeLocalName (atts.SoapAttribute.AttributeName);
mapAttribute.Namespace = (atts.SoapAttribute.Namespace != null) ? atts.SoapAttribute.Namespace : "";
if (typeData.IsComplexType)
mapAttribute.MappedType = ImportTypeMapping (typeData.Type, defaultNamespace);
typeData = TypeTranslator.GetTypeData (rmember.MemberType, atts.SoapAttribute.DataType);
mapMember = mapAttribute;
mapMember.DefaultValue = GetDefaultValue (typeData, atts.SoapDefaultValue);
}
else
{
if (typeData.SchemaType == SchemaTypes.Array) mapMember = new XmlTypeMapMemberList ();
else mapMember = new XmlTypeMapMemberElement ();
if (atts.SoapElement != null && atts.SoapElement.DataType.Length != 0)
typeData = TypeTranslator.GetTypeData (rmember.MemberType, atts.SoapElement.DataType);
// Creates an ElementInfo that identifies the element
XmlTypeMapElementInfoList infoList = new XmlTypeMapElementInfoList();
XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (mapMember, typeData);
elem.ElementName = XmlConvert.EncodeLocalName ((atts.SoapElement != null && atts.SoapElement.ElementName.Length != 0) ? atts.SoapElement.ElementName : rmember.MemberName);
elem.Namespace = string.Empty;
elem.IsNullable = (atts.SoapElement != null) ? atts.SoapElement.IsNullable : false;
if (typeData.IsComplexType)
elem.MappedType = ImportTypeMapping (typeData.Type, defaultNamespace);
infoList.Add (elem);
((XmlTypeMapMemberElement)mapMember).ElementInfo = infoList;
}
mapMember.TypeData = typeData;
mapMember.Name = rmember.MemberName;
mapMember.IsReturnValue = rmember.IsReturnValue;
return mapMember;
}
public void IncludeType (Type type)
{
if (type == null)
throw new ArgumentNullException ("type");
if (includedTypes == null) includedTypes = new ArrayList ();
if (!includedTypes.Contains (type))
includedTypes.Add (type);
}
public void IncludeTypes (ICustomAttributeProvider provider)
{
object[] ats = provider.GetCustomAttributes (typeof(SoapIncludeAttribute), true);
foreach (SoapIncludeAttribute at in ats)
IncludeType (at.Type);
}
Exception CreateTypeException (Type type)
{
return new NotSupportedException ("The type " + type.FullName + " may not be serialized with SOAP-encoded messages. Set the Use for your message to Literal");
}
Exception CreateStructException (Type type)
{
return new NotSupportedException ("Cannot serialize " + type.FullName + ". Nested structs are not supported with encoded SOAP");
}
private object GetDefaultValue (TypeData typeData, object defaultValue)
{
if (defaultValue == DBNull.Value || typeData.SchemaType != SchemaTypes.Enum)
return defaultValue;
if (typeData.Type != defaultValue.GetType ()) {
string msg = string.Format (CultureInfo.InvariantCulture,
"Enum {0} cannot be converted to {1}.",
defaultValue.GetType ().FullName, typeData.FullTypeName);
throw new InvalidOperationException (msg);
}
// get string representation of enum value
string namedValue = Enum.Format (typeData.Type, defaultValue, "g");
// get decimal representation of enum value
string decimalValue = Enum.Format (typeData.Type, defaultValue, "d");
// if decimal representation matches string representation, then
// the value is not defined in the enum type (as the "g" format
// will return the decimal equivalent of the value if the value
// is not equal to a combination of named enumerated constants
if (namedValue == decimalValue) {
string msg = string.Format (CultureInfo.InvariantCulture,
"Value '{0}' cannot be converted to {1}.", defaultValue,
defaultValue.GetType ().FullName);
throw new InvalidOperationException (msg);
}
// XmlSerializer expects integral enum value
//return namedValue.Replace (',', ' ');
return defaultValue;
}
#endregion // Methods
}
}
|