The Labs \ Source Viewer \ SSCLI \ System.Xml.Serialization \ XmlMapping

  1. //------------------------------------------------------------------------------
  2. // <copyright file="XmlMapping.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. // <owner current="true" primary="true">ElenaK</owner>
  15. //------------------------------------------------------------------------------
  16. namespace System.Xml.Serialization
  17. {
  18.     using System;
  19.     using System.ComponentModel;
  20.     using System.Globalization;
  21.    
  22.     [Flags()]
  23.     public enum XmlMappingAccess
  24.     {
  25.         None = 0,
  26.         Read = 1,
  27.         Write = 2
  28.     }
  29.    
  30.     /// <include file='doc\XmlMapping.uex' path='docs/doc[@for="XmlMapping"]/*' />
  31.     ///<internalonly/>
  32.     /// <devdoc>
  33.     /// <para>[To be supplied.]</para>
  34.     /// </devdoc>
  35.     public abstract class XmlMapping
  36.     {
  37.         TypeScope scope;
  38.         bool generateSerializer = false;
  39.         bool isSoap;
  40.         ElementAccessor accessor;
  41.         string key;
  42.         bool shallow = false;
  43.         XmlMappingAccess access;
  44.        
  45.         internal XmlMapping(TypeScope scope, ElementAccessor accessor) : this(scope, accessor, XmlMappingAccess.Read | XmlMappingAccess.Write)
  46.         {
  47.         }
  48.        
  49.         internal XmlMapping(TypeScope scope, ElementAccessor accessor, XmlMappingAccess access)
  50.         {
  51.             this.scope = scope;
  52.             this.accessor = accessor;
  53.             this.access = access;
  54.             this.shallow = scope == null;
  55.         }
  56.        
  57.         internal ElementAccessor Accessor {
  58.             get { return accessor; }
  59.         }
  60.        
  61.         internal TypeScope Scope {
  62.             get { return scope; }
  63.         }
  64.        
  65.         /// <include file='doc\XmlMapping.uex' path='docs/doc[@for="XmlMapping.ElementName"]/*' />
  66.         /// <devdoc>
  67.         /// <para>[To be supplied.]</para>
  68.         /// </devdoc>
  69.         public string ElementName {
  70.             get { return System.Xml.Serialization.Accessor.UnescapeName(Accessor.Name); }
  71.         }
  72.        
  73.         /// <include file='doc\XmlMapping.uex' path='docs/doc[@for="XmlMapping.XsdElementName"]/*' />
  74.         /// <devdoc>
  75.         /// <para>[To be supplied.]</para>
  76.         /// </devdoc>
  77.         public string XsdElementName {
  78.             get { return Accessor.Name; }
  79.         }
  80.        
  81.         /// <include file='doc\XmlMapping.uex' path='docs/doc[@for="XmlMapping.Namespace"]/*' />
  82.         /// <devdoc>
  83.         /// <para>[To be supplied.]</para>
  84.         /// </devdoc>
  85.         public string Namespace {
  86.             get { return accessor.Namespace; }
  87.         }
  88.        
  89.         internal bool GenerateSerializer {
  90.             get { return generateSerializer; }
  91.             set { generateSerializer = value; }
  92.         }
  93.        
  94.         internal bool IsReadable {
  95.             get { return ((access & XmlMappingAccess.Read) != 0); }
  96.         }
  97.        
  98.         internal bool IsWriteable {
  99.             get { return ((access & XmlMappingAccess.Write) != 0); }
  100.         }
  101.        
  102.         internal bool IsSoap {
  103.             get { return isSoap; }
  104.             set { isSoap = value; }
  105.         }
  106.        
  107.         /// <include file='doc\XmlMapping.uex' path='docs/doc[@for="XmlMapping.SetKey"]/*' />
  108.         ///<internalonly/>
  109.         public void SetKey(string key)
  110.         {
  111.             SetKeyInternal(key);
  112.         }
  113.        
  114.         /// <include file='doc\XmlMapping.uex' path='docs/doc[@for="XmlMapping.SetKeyInternal"]/*' />
  115.         ///<internalonly/>
  116.         internal void SetKeyInternal(string key)
  117.         {
  118.             this.key = key;
  119.         }
  120.        
  121.         static internal string GenerateKey(Type type, XmlRootAttribute root, string ns)
  122.         {
  123.             if (root == null) {
  124.                 root = (XmlRootAttribute)XmlAttributes.GetAttr(type, typeof(XmlRootAttribute));
  125.             }
  126.             return type.FullName + ":" + (root == null ? String.Empty : root.Key) + ":" + (ns == null ? String.Empty : ns);
  127.         }
  128.        
  129.         internal string Key {
  130.             get { return key; }
  131.         }
  132.         internal void CheckShallow()
  133.         {
  134.             if (shallow) {
  135.                 throw new InvalidOperationException(Res.GetString(Res.XmlMelformMapping));
  136.             }
  137.         }
  138.         static internal bool IsShallow(XmlMapping[] mappings)
  139.         {
  140.             for (int i = 0; i < mappings.Length; i++) {
  141.                 if (mappings[i] == null || mappings[i].shallow)
  142.                     return true;
  143.             }
  144.             return false;
  145.         }
  146.     }
  147. }

Developer Fusion