Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / XmlMemberMapping.cs
1 // 
2 // System.Xml.Serialization.XmlMemberMapping
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //   Lluis Sanchez Gual (lluis@ximian.com)
7 //
8 // Copyright (C) Tim Coleman, 2002
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Xml.Schema;
33
34 namespace System.Xml.Serialization 
35 {
36         public class XmlMemberMapping {
37
38                 XmlTypeMapMember _mapMember;
39                 string _elementName;
40                 string _memberName;
41                 string _namespace;
42                 string _typeNamespace;
43                 XmlSchemaForm _form;
44
45                 internal XmlMemberMapping (string memberName, string defaultNamespace, XmlTypeMapMember mapMem, bool encodedFormat)
46                 {
47                         _mapMember = mapMem;
48                         _memberName = memberName;
49
50                         if (mapMem is XmlTypeMapMemberAnyElement)
51                         {
52                                 XmlTypeMapMemberAnyElement anyelem = (XmlTypeMapMemberAnyElement) mapMem;
53                                 XmlTypeMapElementInfo info = (XmlTypeMapElementInfo) anyelem.ElementInfo[anyelem.ElementInfo.Count-1];
54                                 _elementName = info.ElementName;
55                                 _namespace = info.Namespace;
56                                 if (info.MappedType != null) _typeNamespace = info.MappedType.Namespace;
57                                 else _typeNamespace = "";
58                         }
59                         else if (mapMem is XmlTypeMapMemberElement)
60                         {
61                                 XmlTypeMapElementInfo info = (XmlTypeMapElementInfo) ((XmlTypeMapMemberElement)mapMem).ElementInfo[0];
62                                 _elementName = info.ElementName;
63                                 if (encodedFormat)
64                                 {
65                                         _namespace = defaultNamespace;
66                                         if (info.MappedType != null) _typeNamespace = "";
67                                         else _typeNamespace = info.DataTypeNamespace;
68                                 }
69                                 else
70                                 {
71                                         _namespace = info.Namespace;
72                                         if (info.MappedType != null) _typeNamespace = info.MappedType.Namespace;
73                                         else _typeNamespace = "";
74                                         _form = info.Form;
75                                 }
76                         }
77                         else
78                         {
79                                 _elementName = _memberName;
80                                 _namespace = "";
81                         }
82                         
83                         if (_form == XmlSchemaForm.None) 
84                                 _form = XmlSchemaForm.Qualified;
85                 }
86
87                 #region Properties
88
89                 public bool Any {
90                         get { return _mapMember is XmlTypeMapMemberAnyElement; }
91                 }
92
93                 public string ElementName {     
94                         get { return _elementName; }
95                 }
96
97                 public string MemberName {      
98                         get { return _memberName; }
99                 }
100
101                 public string Namespace {
102                         get { return _namespace; }
103                 }
104
105                 public string TypeFullName {
106                         get { return _mapMember.TypeData.FullTypeName; }
107                 }
108
109                 public string TypeName {
110                         get { return _mapMember.TypeData.XmlType; }
111                 }
112
113                 public string TypeNamespace {
114                         get { return _typeNamespace; }
115                 }
116
117                 internal XmlTypeMapMember TypeMapMember {
118                         get { return _mapMember; }
119                 }
120                 
121                 internal XmlSchemaForm Form {
122                         get { return _form; }
123                 }
124                 
125                 public string XsdElementName
126                 {
127                         get { return _mapMember.Name; }
128                 }
129 #if !TARGET_JVM && !NET_2_1
130                 public string GenerateTypeName (System.CodeDom.Compiler.CodeDomProvider codeProvider)
131                 {
132                         string ret = codeProvider.CreateValidIdentifier (_mapMember.TypeData.FullTypeName);
133                         return _mapMember.TypeData.IsValueType && _mapMember.TypeData.IsNullable ? 
134                                 "System.Nullable`1[" + ret + "]" : ret;
135                 }
136 #endif
137
138                 public bool CheckSpecified
139                 {
140                         get { return _mapMember.IsOptionalValueType; }
141                 }
142                 #endregion // Properties
143         }
144 }